home *** CD-ROM | disk | FTP | other *** search
- // ---------------------------------------------------------------------------
- //
- // Terry Farrell (html_autolayout@hotmail.com)
- // Button Dialog Testing Code
- //
- // Created: December 1998
- // Last Updated: May 4th 1999
- //
- // ---------------------------------------------------------------------------
-
- // ---------------------------------------------------------------------------
- // Global Variables
-
- var usedHotkeys; // Keep track of used hotkeys for the dialog
-
- // ---------------------------------------------------------------------------
- // testItems
- //
- // This cycles through the dialog inserting random text into the controls.
- // This is very useful in checking if the HAL rules have been followed.
- //
- // inputs:
- // doc The document object for the dialog
- // option Testing option (see code for details)
- // debugstr A debug string to use (prepended or appended)
- //
- // result: none
- // ---------------------------------------------------------------------------
- function testItems(doc, option, debugstr){
-
- var len = doc.length;
- var i;
- var elem;
- var elemID;
-
-
- for (i=0; i < len; i++)
- {
- elem = doc(i);
-
- // Handle lists first
- if (elem != null && elem.tagName == "SELECT")
- {
- var l;
- for (l=0; l < elem.length; l++)
- elem[l].text = getRandomString(elem[l].text);;
- }
-
- if (elem != null)
- {
-
- if ((elem.tagName == "BUTTON" || elem.tagName == "LABEL" || elem.tagName == "LEGEND") ||
- ( (elem.tagName == "TD" || elem.tagName == "DIV") &&
- elem.innerText.length != 0 && elem.innerHTML == elem.innerText))
- {
-
- // always switch to <label> if it's present
- if (elem.children.length > 0 && elem.children[0].tagName == "LABEL")
- elem = elem.children[0];
-
- if (option == "1") // uppercase
- elem.innerHTML = elem.innerHTML.toUpperCase();
- else if (option == "2") // lowercase
- elem.innerHTML = elem.innerHTML.toLowerCase();
- else if (option == "4") // append
- elem.innerHTML = elem.innerHTML + debugstr;
- else if (option == "3") // prepend
- elem.innerHTML = debugstr + elem.innerHTML;
- else if (option == "0") // random
- {
- elem.innerHTML = getRandomString(elem.innerHTML);
- }
- }
-
- }
- }
-
-
- }
-
- // ---------------------------------------------------------------------------
- // Create a random string.
- //
- // Inputs:
- // 1. Original string
- // ---------------------------------------------------------------------------
- function getRandomString(str) {
-
- var strlen = str.length;
-
- // extract hotkey if there is one
- var strHotkey = "";
- str = str.toUpperCase();
- var starthk = str.search("<U>");
-
- //if (starthk != -1)
- // strHotkey = str.substr(starthk, 8);
-
-
- // 75% of the time, pick a random length
- if (parseInt(Math.random() * 100) % 3 != 1)
- {
- // pick random growth
- var growth = Math.random();
-
- if (strlen < 10)
- growth = growth * 2;
-
- strlen = parseInt(strlen * growth);
-
- if (strlen < 2) // no zero allowed
- strlen = 5 // good default
- }
-
-
- //alert(elem.innerHTML + " : " + strlen);
- var j;
- var newStr = "";
- for (j = 0; j < strlen; j++)
- {
- if (j==0 && starthk != -1) // stick a hotkey at the start
- {
- newStr = newStr + String.fromCharCode(97 + (Math.random() * 100 % 5));
- newStr = "<U>" + newStr.toUpperCase() + "</U>";
- }
- else
- newStr = newStr + String.fromCharCode(97 + (Math.random() * 100 % 26));
-
- if (j && j % 5 == 0) // add space every 5
- newStr = newStr + " ";
- }
-
- return newStr;
-
- }
-
- // ---------------------------------------------------------------------------
- // showTableBorders
- //
- // Toggle all the table borders in the document on or off.
- // This is very useful for seeing the structure of the dialog, which in
- // turn is useful for seeing of HAL rules have been used.
- //
- // Inputs:
- // doc The document object for the dialog
- // ---------------------------------------------------------------------------
- function showTableBorders(doc){
-
- var i;
- var doc_tables = doc.all.tags("TABLE");
- var colors = new Array("blue", "green", "brown", "cyan", "KHAKI", "lawngreen", "steelblue", "yellow", "palegreen", "mediumpurple");
-
- for (i=0; i<doc_tables.length; i++)
- {
- doc_tables(i).border = 1 - doc_tables(i).border;
- doc_tables(i).style.borderColor = colors[i % 9];
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // Detect and fix duplicate hotkeys
- // ---------------------------------------------------------------------------
- function fixHotkeys(doc) {
- var len = doc.length;
- var i;
- var elem;
- var boolAssignedKey = true;
- var badHotkeys = "gijlpqy";
- var dupHotkeys = new Array(); // list of elements that are dup
- var numDupHotkeys = 0; // no of duplicate hotkeys
- var posUsedHotkeys = new Array(); // store position of used hotkeys
- var numUsedHotkeys = 0;
-
- usedHotkeys = "";
-
- // first pass: find all duplicate hotkeys
- for (i=0; i < len && boolAssignedKey == true; i++)
- {
- elem = doc(i);
-
- if (elem !=null &&
- ((elem.htmlFor != null && elem.htmlFor.length != 0) ||
- (elem.tagName == "BUTTON")))
- {
- // This is most likely a label
- var strHotkey = "";
-
- var hotkeyFor;
-
- // see if it was a FOR item
- if (elem.htmlFor != null && elem.htmlFor.length)
- hotkeyFor = doc(elem.htmlFor);
- else // or a self contained hotkey
- hotkeyFor = elem;
-
- if (elem.innerHTML.length != 0)
- {
- var str = elem.innerHTML;
- var strlen = str.length;
-
- // extract hotkey if there is one
- //str = str.toUpperCase();
- var starthk = str.search("<U>");
- if (starthk != -1)
- {
- strHotkey = str.substr(starthk+3, 1);
- //strHotkey = strHotkey.toUpperCase();
-
- if (usedHotkeys.search(strHotkey.toUpperCase()) != -1)
- {
- // duplicate hotkey found
- //elem.style.color = "red";
- dupHotkeys[numDupHotkeys] = i; // store bad element
- numDupHotkeys = numDupHotkeys + 1;
- }
- else
- {
- usedHotkeys = usedHotkeys + strHotkey.toUpperCase();
- posUsedHotkeys[numUsedHotkeys] = i;
- numUsedHotkeys = numUsedHotkeys + 1;
- }
-
- // check for bogus hotkeys
- //if (badHotkeys.search(strHotkey) != -1)
- // elem.style.background = "white";
-
- str = elem.innerText;
- } // end (if there is a hotkey)
-
- } // end (if the element has text)
-
- } // end (if it's the type of item that normally has hotkeys)
-
- } // end (for loop running through the doc)
-
-
- // Second pass, fix the duplicates - go top to bottom
- var boolAssignedKey = true;
- for (i=0; i < numDupHotkeys; i++)
- {
- elem = doc(dupHotkeys[i]);
- str = elem.innerText;
-
- var hotkeyPos;
- hotkeyPos = findHotkey(str, false);
-
- if (hotkeyPos == -1)
- {
- // try again, but allow "bad" characters
- hotkeyPos = findHotkey(str, true);
- }
-
- if (hotkeyPos == -1) // no hotkey available
- {
- // try going through every character, go to the other
- // string that uses that hotkey and see if it can use a different hotkey
-
- str = str.toUpperCase();
-
- //for (j=0; j<numUsedHotkeys; j++)
- // alert("used hotkeys: " + usedHotkeys.substr(j, 1) + " " + posUsedHotkeys[j] + " " + doc(posUsedHotkeys[j]).innerText);
-
- for (j=0; j < str.length; j++)
- {
- var posOtherElem = usedHotkeys.search(str.substr(j, 1));
- if (posOtherElem != -1)
- {
- //alert("char = " + j + str.substr(j, 1) + " " + doc(posUsedHotkeys[posOtherElem]).innerText);
- }
- }
-
- elem.style.color = "red";
- //alert(usedHotkeys.length + ": " + usedHotkeys)
- }
- else
- {
- var oldText = elem.innerText;
- str = elem.innerText;
-
- elem.innerHTML = oldText.substr(0, hotkeyPos) + "<U>" + oldText.substr(hotkeyPos, 1) + "</U>" + oldText.substr(hotkeyPos+1, oldText.length - hotkeyPos);
- }
- }
-
- }
-
- // ---------------------------------------------------------------------------
- // findHotkey
- // returns: the position of the hotkey, or -1 if not hotkey is found
- // New hotkeys are also appended to the global list of usedHotkeys
- //
- // ---------------------------------------------------------------------------
-
- function findHotkey(str, tryBadKeys)
- {
- var hotkeyPos;
- hotkeyPos = -1;
- var badHotkeys = "gijlpqy";
- var invalidHotkeys = " -:\t";
-
-
- for (j = 0, newAccessKey = ""; j < str.length && newAccessKey == ""; j++)
- {
- var testChar = str.substr(j, 1);
- if (invalidHotkeys.search(testChar) == -1 && // not bogus
- usedHotkeys.search(testChar.toUpperCase()) == -1) // not used
- {
- if ((tryBadKeys == false && badHotkeys.search(testChar) == -1) ||
- tryBadKeys == true )
- {
- hotkeyPos = j;
-
- newAccessKey = testChar.toUpperCase();
- usedHotkeys = usedHotkeys + newAccessKey;
-
- //alert(usedHotkeys + " " + newAccessKey);
- }
- }
- }
-
- return (hotkeyPos);
-
- }
-
- // ---------------------------------------------------------------------------
- // assignhotkeys
- //
- // This function assigns hotkeys to all labels in the document and the controls
- // they apply to.
- // A very simple rule of attempting to use the first available letter is used.
- // Bad hotkeys are avoided.
- //
- // All controls *must* have a <label for=xxx>, except buttons
- //
- // ---------------------------------------------------------------------------
-
- function assignHotkeys(doc){
-
- var len = doc.length;
- var i;
- var elem;
- var boolAssignedKey = true;
-
- usedHotkeys = "";
-
- // first pass, top to bottom
- for (i=0; i < len && boolAssignedKey == true; i++)
- {
- elem = doc(i);
- boolAssignedKey = assignHotkey(elem, doc);
- }
-
- // do we need to try again? bottom up
- if (boolAssignedKey == false) // failed to get a key
- {
- //alert("need second pass");
- usedHotkeys = "";
-
- for (i=len-1; i > 0; i--) // work from bottom to top
- {
- elem = doc(i);
- boolAssignedKey = assignHotkey(elem, doc);
- }
- }
-
-
- }
-
- // ---------------------------------------------------------------------------
- // assignHotkey
- //
- // Try to find an unused hotkey for the item.
- // Underline the character in the document.
- // TODO: set the accessKey attribute.
- //
- // return value: true or false
- // ---------------------------------------------------------------------------
- function assignHotkey(elem, doc)
- {
- var returnVal = true; // assume a good outcome
-
- if (elem !=null &&
- ((elem.htmlFor != null && elem.htmlFor.length != 0) ||
- (elem.tagName == "BUTTON")))
- {
- // This is most likely a label
- var strHotkey = "";
-
- var hotkeyFor;
-
- // see if it was a FOR item
- if (elem.htmlFor != null && elem.htmlFor.length)
- hotkeyFor = doc(elem.htmlFor);
- else // or a self contained hotkey
- hotkeyFor = elem;
-
- if (elem.innerHTML.length != 0)
- {
- var str = elem.innerHTML;
- var strlen = str.length;
-
- // extract hotkey if there is one
- //str = str.toUpperCase();
- //var starthk = str.search("<U>");
- //if (starthk != -1)
- // {
- // strHotkey = str.substr(starthk+3, 1);
- // }
-
- str = elem.innerText;
-
- var hotkeyPos;
- hotkeyPos = findHotkey(str, false);
-
- if (hotkeyPos == -1)
- {
- // try again, but allow "bad" characters
- hotkeyPos = findHotkey(str, true);
- }
-
- if (hotkeyPos == -1)
- { // failed
- elem.innerHTML = "<font color=red>" + elem.innerText + "</font>";
- elem.accessKey = null;
- returnVal = false; // bad outcome
- }
- else
- {
- var oldText = elem.innerText;
- str = elem.innerText;
-
- elem.innerHTML = oldText.substr(0, hotkeyPos) + "<U>" + oldText.substr(hotkeyPos, 1) + "</U>" + oldText.substr(hotkeyPos+1, oldText.length - hotkeyPos);
- elem.accessKey = oldText.substr(hotkeyPos, 1);
- }
- }
-
-
- //alert("Current tag: <" + elem.tagName + "> htmlFor: " +
- //elem.htmlFor + " targetTag: <" + hotkeyFor.tagName +
- //"> targetHK = " + hotkeyFor.accessKey + " Underline on: " + strHotkey);
-
-
- }
-
- return (returnVal);
- }
-
-
- // ---------------------------------------------------------------------------
- // Flip dialog
- // Just set the reading order on the body to rtl
- // Good for testing if the dialog will work for mideast.
- // ---------------------------------------------------------------------------
- function flipDialog(doc){
-
- if (doc.body.dir == null || doc.body.dir == "" || doc.body.dir == "ltr")
- doc.body.dir = "rtl";
- else
- doc.body.dir = "ltr";
- }
-
-