home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2000 January / maximum-cd-2000-01.iso / Dreamweaver2 / data1.cab / Program_Files / Configuration / Behaviors / Actions / _common.js next >
Encoding:
JavaScript  |  1999-02-23  |  10.9 KB  |  311 lines

  1. // Copyright 1998 Macromedia, Inc. All rights reserved.
  2.  
  3. //*************** GLOBALS VARS *****************
  4.  
  5. var REF_UNNAMED = "unnamed <"; //this is what getObjectRefs() returns for unnamed objects;
  6. var REF_CANNOT = "cannot reference <";
  7.  
  8. //**************** GENERIC FUNCTIONS ****************
  9.  
  10. //Given a function call, extracts the args and returns them in array
  11. //Respects ', skips over stuff between quotes, and returns them dequoted.
  12. //IMPORTANT: argArray[0] is the function call!! Actual args start at argArray[1].
  13.  
  14. function extractArgs(behFnCallStr){
  15.   var i, theStr, lastPos, argArray;
  16.  
  17.   argArray = getTokens(behFnCallStr,"(),");
  18.   for (i=0; i<argArray.length; i++) {
  19.     theStr = unescQuotes(argArray[i]);
  20.     lastPos = theStr.length-1;
  21.     if (theStr.charAt(0) == "'" && lastPos > 0 && theStr.charAt(lastPos) == "'")
  22.       argArray[i] = theStr.substring(1,lastPos);
  23.   }
  24.   return argArray
  25. }
  26.  
  27.  
  28.  
  29. //Passed a string, finds special chars '"\ and escapes them with \
  30.  
  31. function escQuotes(theStr){
  32.   var i, theChar, escStr = "";
  33.   for(var i=0; i<theStr.length; i++) {
  34.     theChar = theStr.charAt(i);
  35.     escStr += (theChar=='"' || theChar=="'" || theChar=="\\")?("\\"+theChar):theChar;
  36.   }
  37.   return escStr;
  38. }
  39.  
  40.  
  41.  
  42. //Passed a string, finds any escape chars \ and removes them
  43.  
  44. function unescQuotes(theStr){
  45.   var strLen, i, theChar, unescStr = "";
  46.   strLen = theStr.length;
  47.   for(i=0; i<strLen; i++) {
  48.     theChar = theStr.charAt(i);
  49.     if (theChar == "\\" && i < strLen - 1) //if escape char and not end
  50.       theChar = theStr.charAt(++i); //append next char and skip over
  51.     unescStr += theChar;
  52.   }
  53.   return unescStr;
  54. }
  55.  
  56.  
  57.  
  58. //Invokes dialog to allow user to select filename. Puts value in text input.
  59.  
  60. function browseFile(fieldToStoreURL){
  61.   var fileName = "";
  62.   fileName = browseForFileURL();  //returns a local filename
  63.   if (fileName) fieldToStoreURL.value = fileName;
  64. }
  65.  
  66.  
  67.  
  68. //Given a string "myObject  *" returns "myObject  *".
  69.  
  70. function stripStar(theStr) {
  71.   var endPos;
  72.  
  73.   endPos = theStr.indexOf('  *');
  74.   return ((endPos > 0)? theStr.substring(0,endPos) : theStr);
  75. }
  76.  
  77.  
  78.  
  79. //Given a string "some property (value)" returns "some property".
  80.  
  81. function stripValue(theStr) {
  82.   var endPos = theStr.indexOf(' (');
  83.   return ((endPos > 0)? theStr.substring(0,endPos) : theStr);
  84. }
  85.  
  86.  
  87.  
  88. //Given theSelect obj and an index, it appends a star
  89. //and inserts the new string into the menu at position index.
  90. //If the menu item was "layer[2]" it becomes "layer[2]  *".
  91. //Existing "  *" values get stripped off first.
  92.  
  93. function addStarToMenuItem(theSelect,menuIndex) {
  94.   var newMenuText;
  95.  
  96.   newMenuText = stripStar(theSelect.options[menuIndex].text); //remove if old star
  97.   newMenuText += "  *";  //append "  *"
  98.   theSelect.options[menuIndex]=new Option(newMenuText); //add new line to menu
  99. }
  100.  
  101.  
  102.  
  103. //Given theSelect obj and an index and a value, it appends the value in parens
  104. //and inserts the new string into the menu at position index.
  105. //If the menu item was "layer[2]" and value is "show", it becomes "layer[2] (show)".
  106. //Existing " (value)" values get stripped off first. If value is empty, strips all.
  107.  
  108. function addValueToMenuItem(theSelect,menuIndex,value) {
  109.   var newMenuText = stripValue(theSelect.options[menuIndex].text); //remove old val
  110.   if (value.length > 0) { //if valid value
  111.     newMenuText += " (" + value + ")";  //append " (value)"
  112.   }
  113.   theSelect.options[menuIndex]=new Option(newMenuText); //add new line to menu
  114. }
  115.  
  116.  
  117.  
  118. //Converts an array of JS object references to an array of nice names.
  119. //For example, document.layers['layr1'].document.form1.img1 becomes
  120. //             image "img1" in form "form1" in layer "layr1"
  121. //Assumes all objects are of type objTypeStr, and the last token is the name
  122. //Note: I reverse the array of tokens to simplify nesting.
  123.  
  124. function niceNames(objRefArray,objTypeStr) {
  125.   var i, j, niceRef, tokens;
  126.   var niceNameArray = new Array(objRefArray.length);
  127.  
  128.   for (i in objRefArray) {  //with object reference array
  129.     tokens = getTokens(objRefArray[i],".").reverse();   //split ref into tokens and rev order
  130.     if (tokens.length > 1) {
  131.       niceRef = objTypeStr + ' ' + nameReduce(tokens[0]);  //start building str, ie: image "foo"
  132.       if (tokens.length > 2) {  //reference includes some nesting...
  133.         if (tokens[1] != "document" && tokens[2] == "document") //inside form, add form reference
  134.           niceRef += ' ' + TYPE_Separator + ' ' + TYPE_Form + ' ' + nameReduce(tokens[1]);
  135.         for (j=1; j<tokens.length-1; j++) if (tokens[j].indexOf("layers[") == 0)
  136.             niceRef += ' ' + TYPE_Separator + ' ' + TYPE_Layer + ' ' + nameReduce(tokens[j]);
  137.         if (tokens[j] != "document")  //if top, parent, or window, expect frame
  138.           niceRef += ' ' + TYPE_Separator + ' ' + TYPE_Frame + ' ' + nameReduce(tokens[j-1]);
  139.       }
  140.     } else niceRef = objRefArray[i];
  141.     niceNameArray[i] = niceRef;
  142.   }
  143.   return niceNameArray;
  144. }
  145.  
  146.  
  147.  
  148. //Extracts a name or num from array string and quotes if necessary. So
  149. // myImg         => "myImg"
  150. // layers['foo'] => "foo"
  151. // embeds[0]     => 0
  152. // myImg[2]      => "myImg[2]"
  153.  
  154. function nameReduce (objName) {
  155.   var retVal, arrayTokens;
  156.  
  157.   retVal = '"' + objName + '"';  //default is object wrapped in quotes
  158.   if (objName.indexOf("[") > 0) {  //if it's an array
  159.     arrayTokens = getTokens(objName,"[]\"'");  //break up tokens
  160.     if (arrayTokens.length == 2) {  //if exactly two tokens
  161.       if ("frames layers forms embeds links anchors all".indexOf(arrayTokens[0]) != -1) { //if legal
  162.         if (arrayTokens[1] == ""+parseInt(arrayTokens[1])) //if a number
  163.           retVal = arrayTokens[1];
  164.         else                                               //else it's a string
  165.           retVal = '"' + arrayTokens[1] + '"';
  166.       }
  167.     }
  168.   }
  169.   return retVal;
  170. }
  171.  
  172.  
  173.  
  174. //Emulates printf("blah blah %s blah %s",str1,str2)
  175. //Used for concatenating error message for easier localization.
  176. //Returns assembled string.
  177.  
  178. function errMsg() {
  179. var i,numArgs,errStr="",argNum=0,startPos;
  180.  
  181.   numArgs = errMsg.arguments.length;
  182.   if (numArgs) {
  183.     theStr = errMsg.arguments[argNum++];
  184.     startPos = 0;  endPos = theStr.indexOf("%s",startPos);
  185.     if (endPos == -1) endPos = theStr.length;
  186.     while (startPos < theStr.length) {
  187.       errStr += theStr.substring(startPos,endPos);
  188.       if (argNum < numArgs) errStr += errMsg.arguments[argNum++];
  189.       startPos = endPos+2;  endPos = theStr.indexOf("%s",startPos);
  190.       if (endPos == -1) endPos = theStr.length;
  191.     }
  192.     if (!errStr) errStr = errMsg.arguments[0];
  193.   }
  194.   return errStr;
  195. }
  196.  
  197.  
  198.  
  199. function findObject(objName,  parentObj) {
  200.   var i,tempObj="",found=false,curObj = "";
  201.   var NS = (navigator.appName.indexOf("Netscape") != -1);
  202.   if (!NS && document.all) curObj = document.all[objName]; //IE4
  203.   else {
  204.     parentObj = (parentObj != null)? parentObj.document : document;
  205.     if (parentObj[objName] != null) curObj = parentObj[objName]; //at top level
  206.     else { //if in form
  207.       if (parentObj.forms) for (i=0; i<parentObj.forms.length; i++) {  //search level for form object
  208.         if (parentObj.forms[i][objName]) {
  209.           curObj = parentObj.forms[i][objName];
  210.           found = true; break;
  211.       } }
  212.       if (!found && NS && parentObj.layers && parentObj.layers.length > 0) {
  213.         parentObj = parentObj.layers;
  214.         for (i=0; i<parentObj.length; i++) { //else search for child layers
  215.           tempObj = findObject(objName,parentObj[i]); //recurse
  216.           if (tempObj) { curObj = tempObj; break;} //if found, done
  217.   } } } }
  218.   return curObj;
  219. }
  220.  
  221.  
  222.  
  223. //Custom non-Javascript code to extract tags and get object names.
  224. //Passed HTML tag (ie IMG), gets the current doc source
  225. //HTML and returns an array of names (empty if unnamed).
  226. //This argument is not case sensitive (can be LAYER, Layer, or layer).
  227. //For Example, given <IMG NAME="myPhoto"> <IMG><IMG name="bobsPhoto">
  228. //returns array: myPhoto,,bobsPhoto
  229.  
  230. function getParam(tagStr,param){
  231.   var j,tokenString;
  232.   var theName = "";
  233.   var tokenArray = new Array;
  234.   tokenArray = getTokens(tagStr," =<>");
  235.   for (j=0; j<(tokenArray.length - 1); j++) {
  236.     tokenString = tokenArray[j].toUpperCase(); //force UPPER CASE
  237.     if (tokenString.indexOf(param.toUpperCase()) == 0) {  //found name
  238.       theName = tokenArray[j+1];  //should return single quoted element in array
  239.       firstChar = theName.charAt(0);
  240.       lastChar = theName.charAt(theName.length - 1);
  241.       if ((firstChar == lastChar) && (firstChar == "'" || firstChar == "\""))
  242.         theName = theName.substring(1,theName.length - 1);
  243.       break;
  244.   } }
  245.   return theName;
  246. }
  247.  
  248. //Passed a string, finds removes special chars '"! and space
  249.  
  250. function badChars(theStr){
  251.   var i,theChar,isBad=false;
  252.   var someBadChars = " ~!@#$%^&*()_+|`-=\\{}[]:\";'<>,./?";
  253.   for (i=0; i<theStr.length; i++) {
  254.     theChar = theStr.charAt(i);
  255.     if (someBadChars.indexOf(theChar) != -1) isBad = true;
  256.   }
  257.   return isBad;
  258. }
  259.  
  260. //Passed a browser type and one or more tags, returns an array of object
  261. //references for the tag(s) and browser type. If the document is in a
  262. //frameset, returns references in all of the frames of the parent frameset.
  263.  
  264. function getAllObjectRefs(browserType,tagName){
  265.   var refsArray = new Array(),theFrame,frameName,Tag,i,j;
  266.   var frameList = getObjectRefs("NS 4.0","parent","frame"); //get list of frames
  267.   var numArgs = arguments.length;
  268.   for (i=1;i<numArgs;i++){
  269.     Tag = arguments[i]
  270.     if (frameList && frameList.length>0) { //if frames
  271.       for (j=0; j<frameList.length; j++) {
  272.         if (frameList[j].indexOf(REF_UNNAMED) != -1)
  273.           theFrame = "parent.frames[" + j + "]";
  274.         else {
  275.           //check for duplicately named frames by checking numer of ['s in name
  276.           //for instance,parent.frames['name'][0] would have 2
  277.           if (frameList[j].indexOf("[")!=frameList[j].lastIndexOf("["))
  278.             frameList[j] = frameList[j].substring(0,frameList[j].indexOf("]") + 1)  
  279.           theFrame = frameList[j];
  280.           //alert("after generating new ref, it is " + theFrame);      
  281.         }  
  282.         refsArray = refsArray.concat(getObjectRefs(browserType,theFrame,Tag));
  283.       }
  284.     } else 
  285.       refsArray = refsArray.concat(getObjectRefs(browserType,"document",Tag)); 
  286.   }
  287.   return refsArray;
  288. }
  289.  
  290. //Passed a tagName, returns an array of all tags of tagName. If the document is in a frameset,
  291. //the array includes all of the tags in all of the frames of the document's parent.
  292.  
  293. function getAllObjectTags(tagName){
  294.   var tagsArray = new Array(),theFrame,frameName,i;
  295.   var frameList = getObjectRefs("NS 4.0","parent","frame"); //get list of frames
  296.   if (frameList && frameList.length>0) { //if frames
  297.     for (i=0;i<frameList.length; i++){
  298.       if (frameList[i].indexOf(REF_UNNAMED)!=-1)
  299.         theFrame = "parent.frames["+i+"]";
  300.       else
  301.         theFrame = frameList[i];
  302.     tagsArray = tagsArray.concat(getObjectTags(theFrame,tagName));  
  303.     }  
  304.   } else
  305.    tagsArray = tagsArray.concat(getObjectTags("document",tagName));    
  306.    return tagsArray;
  307. }
  308.   
  309.   
  310.  
  311.