home *** CD-ROM | disk | FTP | other *** search
/ internet.au CDrom 42 / NETCD42.iso / web / w95 / dream2.exe / data1.cab / Program_Files / Configuration / Behaviors / Actions / Show-Hide Layers.js < prev    next >
Encoding:
JavaScript  |  1998-11-30  |  8.3 KB  |  217 lines

  1. // Copyright 1998 Macromedia, Inc. All rights reserved.
  2.  
  3. //*************** GLOBAL VARS  *****************
  4.  
  5. //******************* BEHAVIOR FUNCTION **********************
  6.  
  7. //Shows and/or hides a number of layers at the same time.
  8. //Accepts a variable number of args, in triplets as follows:
  9. //  objStrNS - Javascript layer object ref for Netscape (ex: document.layers['foo'])
  10. //  objStrIE - JScript layer object ref for Internet Explorer (ex: document.all['foo'])
  11. //  visStr   - hide, show, or inherit. A Netscape visibility value. This values are
  12. //             converted based on the current browser. IE uses visible, hidden, inherit.
  13. //
  14. //This function can make a whole set of layers visible while hiding others. The main
  15. //work is to convert for the differences between the two browsers.
  16. //For Netscape, use the first arg for the object reference, and set the visibility property:
  17. //   document.layers['myLayer'].visibility = 'show'
  18. //For IE, use the second arg for the object ref, set style.visibility, and the values differ:
  19. //   document.all['myLayer'].style.visibility = 'visible'
  20. //Fails gracefully on older browsers by ensuring the layers and the particular layer exists.
  21.  
  22. function MM_showHideLayers() { //v2.0
  23.   var i, visStr, args, theObj;
  24.   args = MM_showHideLayers.arguments;
  25.   for (i=0; i<(args.length-2); i+=3) { //with arg triples (objNS,objIE,visStr)
  26.     visStr   = args[i+2];
  27.     if (navigator.appName == 'Netscape' && document.layers != null) {
  28.       theObj = eval(args[i]);
  29.       if (theObj) theObj.visibility = visStr;
  30.     } else if (document.all != null) { //IE
  31.       if (visStr == 'show') visStr = 'visible'; //convert vals
  32.       if (visStr == 'hide') visStr = 'hidden';
  33.       theObj = eval(args[i+1]);
  34.       if (theObj) theObj.style.visibility = visStr;
  35.   } }
  36. }
  37.  
  38.  
  39. //******************* API **********************
  40.  
  41.  
  42. //Checks for the existence of layers.
  43. //If none exist, returns false so this Action is grayed out.
  44.  
  45. function canAcceptBehavior(){
  46.   var nameArray = getAllObjectRefs("NS 4.0","LAYER");  //get layer names (includes CSS)
  47.   return (nameArray.length > 0);
  48. }
  49.  
  50.  
  51.  
  52. //Returns a Javascript function to be inserted in HTML head with script tags.
  53.  
  54. function behaviorFunction(){
  55.   return "MM_showHideLayers";
  56. }
  57.  
  58.  
  59.  
  60. //Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
  61. //Gets list of layers from doc property. With each layer, it gets the parallel
  62. //frame name from select 'menu'. Each theVis & layerObj refs are embedded as arguments.
  63.  
  64. function applyBehavior() {
  65.   var visArray,i,theVis,layerObjNS,layerObjIE,argList = "";
  66.  
  67.   visArray = document.MM_visArray;           //get global list of visibilities
  68.   for (i=0; i<visArray.length; i++){    //with each visibility
  69.     theVis = visArray[i];
  70.     if (theVis) {      //if not empty
  71.       theVis = (theVis == "default")? "inherit" : theVis;
  72.       layerObjNS = document.MM_NS_REFS[i]; //get layer name from list
  73.       if (argList) argList += ",";    //if stuff already in list, add comma
  74.       if (layerObjNS.indexOf(REF_UNNAMED) != 0) { //if real layer
  75.         layerObjIE = document.MM_IE_REFS[i];
  76.         argList += "'"+escQuotes(layerObjNS)+"','"+escQuotes(layerObjIE)+"','"+theVis+"'";
  77.       }
  78.       else return MSG_UnnamedLayer;
  79.     }
  80.   }
  81.   if (argList) return "MM_showHideLayers("+argList+")";  //return fn call with args
  82.   else         return MSG_NoLayersSet;
  83. }
  84.  
  85.  
  86.  
  87. //Returns a dummy function call to inform Dreamweaver the type of certain behavior
  88. //call arguments. This information is used by DW to fixup behavior args when the
  89. //document is moved or changed.
  90. //
  91. //It is passed an actual function call string generated by applyBehavior(), which
  92. //may have a variable list of arguments, and this should return a matching mask.
  93. //
  94. //The return values are:
  95. //  URL     : argument could be a file path, which DW will update during Save As...
  96. //  NS4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  97. //  IE4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  98. //  other...: argument is ignored
  99.  
  100. function identifyBehaviorArguments(fnCallStr) {
  101.   var argList, argArray, numArgGroups, i;
  102.  
  103.   argList = "";
  104.   argArray = extractArgs(fnCallStr);
  105.   numArgGroups = (argArray.length - 1) / 3; //args come in triplets
  106.   for (i=0; i<numArgGroups; i++) {          //with NSobj,IEobj,vis triplet
  107.     argList += ((argList)?",":"")+"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")? "default" : theVis;
  129.     found = false;
  130.     for (j=0; j<numLayers; j++){  //check if layer is in menu
  131.       if (document.MM_NS_REFS[j] == theLayerNS) { //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.  
  205.  
  206.  
  207. //**************** GENERIC FUNCTIONS ****************
  208.  
  209. //function extractArgs(behFnCallStr){
  210. //function stripValue(theStr) {
  211. //function addValueToMenuItem(theSelect,menuIndex,value) {
  212. //function escQuotes(theStr){
  213. //function unescQuotes(theStr){
  214. //function niceNames(objRefArray,objTypeStr) {
  215. //function nameReduce (objName) {
  216. //function errMsg() {
  217.