home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Behaviors / Actions / Show-Hide Layers.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  7.7 KB  |  204 lines

  1. // Copyright 1998 Macromedia, Inc. All rights reserved.
  2.  
  3. //*************** GLOBAL VARS  *****************
  4.  
  5. var helpDoc = MM.HELP_behShowHideLayers;
  6.  
  7. //******************* BEHAVIOR FUNCTION **********************
  8.  
  9. //Shows and/or hides a number of layers at the same time.
  10. //Accepts a variable number of args, in triplets as follows:
  11. //  objName  - simple name of object, such as "Layer1". Older calls accepted: "document.layers['Layer1']"
  12. //  x        - ignored (for backward compatibility)
  13. //  visStr   - hide, show, or inherit, the Netscape visibility value. This values are
  14. //             converted based on the current browser. IE uses visible, hidden, inherit.
  15. //
  16. //This function can make a whole set of layers visible while hiding others.
  17. //Uses MM_findObj() to resolve browser differences.
  18. //For Netscape, essentially calls
  19. //   document.layers['myLayer'].visibility = 'show'
  20. //For IE, essentially call
  21. //   document.all['myLayer'].style.visibility = 'visible'
  22. //Fails gracefully on older browsers by ensuring the layers and the particular layer exists.
  23.  
  24. function MM_showHideLayers() { //v3.0
  25.   var i,p,v,obj,args=MM_showHideLayers.arguments;
  26.   for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
  27.     if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
  28.     obj.visibility=v; }
  29. }
  30.  
  31. document.VERSION_MM_showHideLayers = 3.0; //define latest version number for behavior inspector
  32.  
  33. //******************* API **********************
  34.  
  35.  
  36. //Checks for the existence of layers.
  37. //If none exist, returns false so this Action is grayed out.
  38.  
  39. function canAcceptBehavior(){
  40.   var nameArray = getAllObjectRefs("NS 4.0","LAYER");  //get layer names (includes CSS)
  41.   return (nameArray.length > 0);
  42. }
  43.  
  44.  
  45.  
  46. //Returns a Javascript function to be inserted in HTML head with script tags.
  47.  
  48. function behaviorFunction(){
  49.   return "MM_findObj,MM_showHideLayers";
  50. }
  51.  
  52.  
  53.  
  54. //Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
  55. //Gets list of layers from doc property. With each layer, it gets the parallel
  56. //frame name from select 'menu'. Each theVis & layerObj refs are embedded as arguments.
  57.  
  58. function applyBehavior() {
  59.   var visArray,i,theVis,layerObjNS,argList = "", retVal="";
  60.  
  61.   visArray = document.MM_visArray;           //get global list of visibilities
  62.   for (i=0; i<visArray.length; i++){    //with each visibility
  63.     theVis = visArray[i];
  64.     if (theVis) {      //if not empty
  65.       theVis = (theVis == LABEL_DEFAULT)? "inherit" : ((theVis == LABEL_SHOW) ? "show" : "hide");
  66.       layerObjNS = document.MM_NS_REFS[i]; //get layer name from list
  67.       if (argList) argList += ",";    //if stuff already in list, add comma
  68.       if (layerObjNS.indexOf(REF_UNNAMED) != 0) { //if real layer
  69.         layerObjNS = getNameFromRef(layerObjNS);
  70.         argList += "'"+layerObjNS+"','','"+theVis+"'";
  71.       }
  72.       else return MSG_UnnamedLayer;
  73.     }
  74.   }
  75.   if (argList) {
  76.     updateBehaviorFns("MM_findObj","MM_showHideLayers");
  77.     retVal = "MM_showHideLayers("+argList+")";  //return fn call with args
  78.   } else {
  79.     retVal = MSG_NoLayersSet;
  80.   }
  81.   return retVal;
  82. }
  83.  
  84.  
  85.  
  86. //Returns a dummy function call to inform Dreamweaver the type of certain behavior
  87. //call arguments. This information is used by DW to fixup behavior args when the
  88. //document is moved or changed.
  89. //
  90. //It is passed an actual function call string generated by applyBehavior(), which
  91. //may have a variable list of arguments, and this should return a matching mask.
  92. //
  93. //The return values are:
  94. //  URL     : argument could be a file path, which DW will update during Save As...
  95. //  NS4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  96. //  IE4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  97. //  other...: argument is ignored
  98.  
  99. function identifyBehaviorArguments(fnCallStr) {
  100.   var argList, argArray, numArgGroups, i;
  101.  
  102.   argList = "";
  103.   argArray = extractArgs(fnCallStr);
  104.   numArgGroups = (argArray.length - 1) / 3; //args come in triplets
  105.   for (i=0; i<numArgGroups; i++) {          //with NSobj,IEobj,vis triplet
  106.     if (argList) argList += ",";
  107.     argList += (argArray[3*i+1].indexOf(".")==-1)? "objName,other,other" : "NS4.0ref,IE4.0ref,other";
  108.   }
  109.   return argList;
  110. }
  111.  
  112.  
  113.  
  114. //Given the original function call, this parses out the args and updates
  115. //the UI. First it gets new layer,vis pairs.
  116. //If layer already present in menu, stuff vis value in visArray. If layer
  117. //doesn't exist, add to menu, and extend visArray.
  118.  
  119. function inspectBehavior(upStr){
  120.   var argArray,visArray,found,numLayers,i,j,theLayerNS,theVis,layerNum;
  121.  
  122.   argArray = extractArgs(upStr);//get new list of layer,vis pairs
  123.   visArray = document.MM_visArray; //get the prior list of layers
  124.   numLayers = document.MM_NS_REFS.length;
  125.   for (i=1; i<(argArray.length-2); i+=3){ //with each layerNS,layerIE,vis triplet (skip fn name)
  126.     theLayerNS=argArray[i];
  127.     theVis=argArray[i+2];
  128.     theVis = (theVis == "inherit")? LABEL_DEFAULT : ((theVis == "show") ? LABEL_SHOW : LABEL_HIDE);
  129.     found = false;
  130.     for (j=0; j<numLayers; j++){  //check if layer is in menu
  131.       if (theLayerNS == document.MM_NS_REFS[j] || theLayerNS == getNameFromRef(document.MM_NS_REFS[j])) { //if layer there
  132.         visArray[j] = theVis;               //store vis at that pos
  133.         addValueToMenuItem(document.theForm.menu,j,theVis);
  134.         found = true;
  135.         break;
  136.       }
  137.     }
  138.     if (!found) alert(errMsg(MSG_LayerNotFound,theLayerNS,theVis));
  139.   }
  140.   document.MM_visArray = visArray; //save updated layer list
  141. }
  142.  
  143.  
  144.  
  145. //***************** LOCAL FUNCTIONS  ******************
  146.  
  147.  
  148. //Load the select menu with layer names.
  149. //Also sets the global property MM_visArray to the right num of items.
  150.  
  151. function initializeUI(){
  152.   var niceNameSrcArray, nameArray,visArray,i;
  153.  
  154.   document.MM_NS_REFS = getAllObjectRefs("NS 4.0","LAYER"); //store parallel NS refs
  155.   document.MM_IE_REFS = getAllObjectRefs("IE 4.0","LAYER"); //store parallel IE refs
  156.   niceNameSrcArray = document.MM_NS_REFS;
  157.  
  158.   //Search for unreferenceable objects. <DIV id="foo"> is IE only, <LAYER> is NS only.
  159.   //if REF_CANNOT found, return empty string, and use IE refs for nice namelist.
  160.   for (i=0; i<document.MM_NS_REFS.length; i++) {
  161.     if (document.MM_IE_REFS[i].indexOf(REF_CANNOT) == 0) {
  162.       document.MM_IE_REFS[i] = ""; //blank it out
  163.     }
  164.     if (document.MM_NS_REFS[i].indexOf(REF_CANNOT) == 0) {
  165.       document.MM_NS_REFS[i] = ""; //blank it out
  166.       niceNameSrcArray = document.MM_IE_REFS; //use the IE list
  167.     }
  168.   }
  169.   nameArray = niceNames(niceNameSrcArray,TYPE_Layer);  //get layer names (includes CSS)
  170.  
  171.   visArray = new Array();     //start global list
  172.   for (i=0; i<nameArray.length; i++) {
  173.     document.theForm.menu.options[i]=new Option(nameArray[i]); //load menu
  174.     visArray[i] = "";
  175.   }
  176.   document.MM_visArray = visArray; //set global
  177. }
  178.  
  179.  
  180.  
  181. //Given radio selection, looks up the menu's selection number, and stores the
  182. //new visiblity at that position in the global document property "MM_visArray".
  183.  
  184. function storeVis(newVis){
  185.   var visArray, curLayerNum, oldVis;
  186.  
  187.   visArray = document.MM_visArray; //get the prior list of vis
  188.   if (visArray){  // if anything to store on
  189.     curLayerNum = document.theForm.menu.selectedIndex; //get index to swap
  190.     oldVis = visArray[curLayerNum];
  191.     if (oldVis != newVis) {
  192.       visArray[curLayerNum] = newVis;   //swap
  193.       document.MM_visArray = visArray;   //rewrite list
  194.       addValueToMenuItem(document.theForm.menu, curLayerNum, newVis);
  195.       document.theForm.menu.selectedIndex = curLayerNum; //reset selection index
  196.     } else {  //same value, so toggle it (remove value)
  197.       visArray[curLayerNum] = "";   //clear
  198.       document.MM_visArray = visArray;   //rewrite list
  199.       addValueToMenuItem(document.theForm.menu, curLayerNum, ""); //clear value from menu item
  200.       document.theForm.menu.selectedIndex = curLayerNum; //reset selection index
  201.     }
  202.   }
  203. }
  204.