home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Behaviors / Actions / Jump Menu Go.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  8.0 KB  |  276 lines

  1.          
  2. // Copyright 1999 Macromedia, Inc. All rights reserved.
  3.  
  4. //*****************GLOBAL VARIABLES*************************
  5.   
  6. var helpDoc = MM.HELP_behJumpMenuGo;
  7.  
  8. var GarrJumpMenus;   //global array storing jump menu object and names             
  9. var GselJumpMenus;   //select widget listing jump menus
  10. var GbValidContext=true;  //can the action be applied in the current context
  11.  
  12.  
  13. //******************BEHAVIOR FUNCTION(S)********************
  14.  
  15. //behavior function: MM_jumpMenuGo
  16. //description: attached to a button that is associated with a jump menu
  17. //             changes the document or frame URL based on jump menu selection
  18. //arguments: selName - simple name of select
  19. //           targ - target, either a frame reference or 'parent'
  20. //           retore - boolean - controls whether the first option selection is restored
  21.  
  22.  
  23. function MM_jumpMenuGo(selName,targ,restore){ //v3.0
  24.   var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore);
  25. }
  26.                   
  27.  
  28. //************************API FUNCTIONS************************
  29.  
  30. //accept the behavior no matter what.
  31.  
  32. function canAcceptBehavior(){
  33.   var selArr = dw.getDocumentDOM().getElementsByTagName("SELECT");
  34.   for (var i=0;i<selArr.length;i++){
  35.     curAtr = selArr[i].getAttribute('onChange');
  36.     if ( curAtr && (curAtr.indexOf("MM_jumpMenu") != -1) ) {
  37.       return 'onClick,(onClick)';
  38.   } }
  39.   return false;
  40. }
  41.  
  42. //returns the name of the function that will be inserted into the document head
  43.  
  44. function behaviorFunction(){
  45.    return "MM_findObj,MM_jumpMenuGo";
  46. }
  47.  
  48.  
  49. //function: applyBehavior
  50. //description: returns the function call attached to the event call
  51.  
  52. function applyBehavior(){
  53.  
  54.    // if context isn't valid, don't bother with the rest
  55.    // returning an emptry string from this function means the dialog
  56.    // closes if the user clicks the OK button
  57.    // the dialog has already been populated with a friendly error message
  58.    // in the initialize UI function
  59.    if ( !GbValidContext )
  60.       return "";
  61.  
  62.    //If the menu is set to an empty option, tell the user they need to
  63.    //select a jump menu
  64.    if ( GselJumpMenus.options[GselJumpMenus.selectedIndex].text == ""){
  65.       return (MSG_No_Item_Selected);
  66.    }
  67.  
  68.    //The GarrJumpMenus global array holds a jump menu obj in [i][0],
  69.    //(and the associated jump menu name in [i][1].)
  70.    var menuObj = GarrJumpMenus[GselJumpMenus.selectedIndex][0];
  71.    var menuName = GarrJumpMenus[GselJumpMenus.selectedIndex][1];
  72.    
  73.    
  74.    //The MM_jumpMenuGo function call needs to have the same target
  75.    //and restore options as the MM_jumpMenu function call that it is
  76.    //associated with.
  77.    //Find the function call from the outerHTML of the tag
  78.    //It is probably on the onChange handler, but using regular expressions
  79.    //is more robust and will find the function call on any handler.
  80.    var pattern = /MM_jumpMenu(\()([^\)]*)(\))/;
  81.    var fnStr = pattern.exec(menuObj.outerHTML); 
  82.    
  83.    
  84.    //next, extract the arguments from the function call and get the target
  85.    //and restore arguments
  86.    var fnArgs = extractArgs(fnStr).slice(1); //args now equal target,"this",restore
  87.    var target = escQuotes(fnArgs[0]);
  88.    var bRestore = fnArgs[2];
  89.    
  90.    
  91.    //create argument list
  92.    return "MM_jumpMenuGo('" + menuName + "','" + target + "',"+ bRestore  + ")";
  93. }
  94.  
  95.  
  96. //function: inspectBehavior
  97. //description: populate the behavior dialog based on the function arguments
  98.  
  99. function inspectBehavior(behFnCallStr){
  100.  
  101.   //select correct item in the Jump Menu list
  102.   var fnArgs = extractArgs(behFnCallStr).slice(1);
  103.   if (fnArgs.length > 2) {
  104.     var menuName = fnArgs[0];
  105.     var nMenus = GarrJumpMenus.length,i;
  106.      
  107.     for (i=0;i<nMenus;i++){
  108.       if(menuName == GarrJumpMenus[i][1]){
  109.           GselJumpMenus.selectedIndex = i;
  110.         break;
  111.       } }
  112.    
  113.     //if we can't find the menu associated with this button, tell the
  114.     //user it can't be found, and tell them to select another one.
  115.     if (i == nMenus) {
  116.       alert (MSG_Menu_Not_Found + "\n" + MSG_Why_Not_Found + "\n");
  117.     }
  118.   }
  119. }
  120.  
  121.  
  122. //function: identifyBehaviorArguments
  123. //description: identify certain types of behavior arguments
  124.  
  125. function identifyBehaviorArguments(fnCallStr){
  126.   var argArray, retVal = "";
  127.  
  128.   argArray = extractArgs(fnCallStr);
  129.   if (argArray.length == 4) retVal = "objName,other,other";
  130.   return retVal;
  131. }
  132.  
  133. //************************LOCAL FUNCTIONS***************************
  134.  
  135.  
  136.  
  137. //function: getAllJumpMenus
  138. //description: returns a multi-dimensional array of i items
  139. //representing all of the jump menus in the current document or frame
  140. //i[0] is the name of the select menu
  141. //i[1] is the object reference to it
  142.  
  143. function getAllJumpMenus(){
  144.    var jumpMenuArr = new Array();
  145.    var doc = dreamweaver.getDocumentDOM();
  146.    var selArr = doc.getElementsByTagName("SELECT");
  147.    var nSelects = selArr.length,i,currSel;
  148.    var counter = 0;
  149.    
  150.    for (i=0;i<nSelects;i++){
  151.       currSelObj = selArr[i];
  152.       if (  currSelObj.outerHTML.indexOf("MM_jumpMenu")!= -1  ){
  153.          jumpMenuArr[counter] = new Array(2);
  154.          jumpMenuArr[counter][0] = currSelObj;
  155.          jumpMenuArr[counter++][1] = currSelObj.name;
  156.       }
  157.    }
  158.    return jumpMenuArr;
  159. }
  160.  
  161.  
  162.  
  163. //function: initializeUI
  164. //description: initialize the user interface, attached to body onload
  165. //in this case, make sure that behavior is valid in this context, and
  166. //populate the Jump Menu list
  167. function initializeUI(){
  168.  
  169.     GselJumpMenus = findObject("JumpMenuList");
  170.    
  171.    //populate the GarrJumpMenus global array
  172.    GarrJumpMenus = getAllJumpMenus();
  173.    
  174.    //check validity  of using behavior and give user 
  175.    //informative message if not valid
  176.    if ( !checkValidity() ){
  177.       GbValidContext = false;
  178.       return;
  179.    }
  180.    
  181.    //make main layer visible, previously hidden to prevent flashing
  182.    //if dialog text changes because document is invalid
  183.    findObject("mainLayer").visibility = "visible";
  184.    
  185.    //populate list and select a likely candidate
  186.    populateJumpMenuOptions(); 
  187.    selectJumpMenuOption();
  188. }
  189.  
  190.  
  191.  
  192. //function: selectJumpMenuOption
  193. //description: selects a likely jump menu choice, based on cursor location
  194. //if cursor in a form, selects a menu that is in the same form as the cursor
  195. //exists. Barring that, selects the first item.
  196. //In a perfect world, would determined which menu was closest to cursor
  197. //if determining that more than one jump menu was in the form
  198. function selectJumpMenuOption(){
  199.    
  200.    //default selection
  201.    GselJumpMenus.selectedIndex = 0;
  202.   
  203.    if (  GarrJumpMenus.length == 1  )
  204.       return;
  205.    else {
  206.       //check if it is possible to select a menu in the 
  207.       //same form as the current selection
  208.       var selObj = getSelectedObj();
  209.       var currFormObj = getFormObj(selObj);
  210.       var nMenus = GselJumpMenus.length;
  211.    
  212.       for (i=0;i<nMenus;i++){
  213.          if (  GarrJumpMenus[i].form == currFormObj  ){
  214.             GselJumpMenus.selectedIndex = i;
  215.             break;
  216.          }   
  217.       }
  218.    }
  219. }
  220.  
  221.  
  222.  
  223. //function: getFormObj
  224. //description: given the currently selected object,
  225. //return the parent form, if one exists, otherwise return ""
  226.  
  227. function getFormObj(selObj){
  228.    var currObj = selObj;
  229.    var formObj = "";
  230.    
  231.    while (currObj){
  232.       if (currObj.tagName && currObj.tagName == "FORM"){
  233.          formObj = currObj;
  234.          break;
  235.       } 
  236.       currObj = (currObj.parentNode)?currObj.parentNode:"";   
  237.    }
  238.    return formObj;
  239. }
  240.  
  241.  
  242.  
  243. //function: populateJumpMenuOptions
  244. //description: populate the UI based on the global array
  245.  
  246. function populateJumpMenuOptions(){
  247.    var nMenus = GarrJumpMenus.length,i;
  248.    
  249.    for (i = 0;i<nMenus;i++){
  250.       GselJumpMenus.options[i] = new Option(GarrJumpMenus[i][1]);
  251.    }
  252. }
  253.  
  254.  
  255. //function: checkValidity
  256. //description: check the validity of using this behavior action
  257. //if not valid, display an informative error message
  258.  
  259. function checkValidity(){
  260.    var isValid = true,behFile,bodyNode;
  261.    
  262.    if ( GarrJumpMenus.length < 1){
  263.       isValid = false;
  264.       
  265.       behFile = dreamweaver.getConfigurationPath() +
  266.       "/Behaviors/Actions/" + FILE_Name;
  267.    
  268.        bodyNode = dreamweaver.getDocumentDOM(behFile).body;
  269.                       
  270.        bodyNode.innerHTML = "<p> </p>" +
  271.                           "<p>" + MSG_Invalid_Document + "</p>" +
  272.                           "<p>" + MSG_Correcting_Document + "</p>";    
  273.    }
  274.    return isValid;
  275. }
  276.