home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 86 / PIWD86.iso / pc / contents / dreamweaver / software / dwmx2004.exe / Disk1 / data1.cab / Configuration_En / Commands / InsertConditionalContent.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  10.3 KB  |  360 lines

  1. // Copyright 2001, 2002, 2003 Macromedia, Inc. All rights reserved.
  2.  
  3. //---------------   GLOBAL VARIABLES   ---------------
  4.  
  5. var helpDoc = MM.HELP_insertOptionalContent;
  6. var targetDom = null;
  7. var abortCommand = false; //Set to true if the user cancels out of the 'save as template' dialog
  8.  
  9. var selectedOptionalNode = null;
  10. var currentExpression = "";
  11. var addEditable = false; 
  12.  
  13. var T = null; //Tab Control
  14.  
  15. var paramController = null;
  16.  
  17. //---------------     API FUNCTIONS    ---------------
  18.  
  19. function commandButtons()
  20.     {
  21.       return new Array(
  22.                       MM.BTN_OK,"okClicked()",
  23.                       MM.BTN_Cancel,"window.close()",
  24.                       MM.BTN_Help,"displayHelp()");
  25.     }
  26.  
  27.  
  28. function isDomRequired() {
  29.     return true;
  30. }
  31.  
  32. function receiveArguments()
  33.     {    
  34.     targetDom = arguments[0];
  35.     if (targetDom == null)
  36.         targetDom = dw.getDocumentDOM();
  37.         
  38.     abortCommand = false;
  39.     
  40.     if (!CheckWarnNoTemplate(targetDom))
  41.         abortCommand = true;
  42.     
  43.     var result = new Object();
  44.     
  45.     if (!canMakeTemplateContent("optional", targetDom, result))
  46.         {
  47.         abortCommand = true;
  48.         
  49.         if (result.status == "contained in DW4 edit")
  50.             alert(MM.MSG_SaveDW4First);
  51.         else if (result.status == "locked")
  52.             alert(MM.TEMPLATE_UTILS_CantInsertOptionalHere); 
  53.         else
  54.             alert(MM.MSG_AlreadyEdit);
  55.         }
  56.  
  57.    var curSelNode = null;
  58.    if (arguments.length > 2 && arguments[2] != null)
  59.            curSelNode = arguments[2];
  60.        else
  61.            curSelNode = getUnlockedSelNode(targetDom, false);
  62.  
  63.    if (curSelNode != null && curSelNode.tagName == "MMTEMPLATE:IF" && arguments.length > 1 && arguments[1] == "useSelectedNode")
  64.         selectedOptionalNode = curSelNode;
  65.     
  66.     if (arguments.length > 3 && arguments[3] == "addEditable")
  67.         addEditable = true; 
  68.     else
  69.         addEditable = false; 
  70.                 
  71.     } //receiveArguments
  72.  
  73. function canAcceptCommand()
  74.     {
  75.     targetDom = arguments[0];
  76.     if (targetDom == null)
  77.         targetDom = dw.getDocumentDOM();
  78.         
  79.     return (targetDom != null && dw.getFocus() != 'browser' && dw.getDocumentDOM().getParseMode() == 'html');
  80.     } //canAcceptCommand
  81.  
  82.     
  83. function okClicked()
  84.     {    
  85.     var curDOM = (targetDom == null) ? dw.getDocumentDOM('document') : targetDom;
  86.     
  87.     if (T.getCurPageNum() == 1)
  88.         {
  89.         var newCondName = findObject("condName").value;
  90.         var showByDefault = findObject("showByDefault").checked;
  91.         
  92.         if (stringIsAllDigits(newCondName))
  93.             {
  94.             //parsed as a number - this is not allowed.
  95.             alert(MSG_numberNotAllowed);
  96.             return;
  97.             }
  98.                         
  99.         if (selectedOptionalNode != null)
  100.             {
  101.             curDOM.disableLocking();
  102.             
  103.             if (!checkLegalTemplateName(newCondName))
  104.                 return; //make them enter the name, or cancel the main dialog.
  105.  
  106.             //selectedOptionalNode.cond = dwscripts.minEntityNameEncode(encodeTemplateParam(newCondName, true));
  107.             selectedOptionalNode.cond = encodeTemplateParam(newCondName, true);
  108.  
  109.             //Insert or update the param, if this isn't an expression. Don't encode.
  110.             if (!isExpressionString(newCondName, true))
  111.                 {
  112.                 if (checkInsertTemplateParam(targetDom))
  113.                     insertTemplateParam(newCondName, "boolean", showByDefault ? "true" : "false", targetDom);
  114.                 }
  115.                 
  116.             curDOM.enableLocking();
  117.             window.close();
  118.             }
  119.         else
  120.             {
  121.             var oldParamNode = getTemplateParamTag(newCondName, targetDom);
  122.             if (oldParamNode != null && !RunDSConfirmDialog_YesNo(dwscripts.sprintf(MM.Optional_MSG_ParamExists, newCondName), "showCreateParamWarning", true))
  123.                 return;
  124.  
  125.             if (!doInsertConditional(newCondName,showByDefault, null, targetDom, true, addEditable))
  126.                 return;
  127.                 
  128.             window.close();
  129.             }
  130.         }
  131.     else
  132.         {        
  133.         var isExpression = findObject("expressionRadio").checked;
  134.         var exprString = findObject("expression").value;
  135.  
  136.         //Strip linefeeds from the expression, replace with spaces. For some reason 
  137.         //the regexp code only replaces the first instance, so loop until there aren't any more. 
  138.         var oldMulti = RegExp.multiline;
  139.         RegExp.multiline = true;
  140.         var pat1 = /\r/g;
  141.         exprString = exprString.replace(pat1, " ");
  142.         var pat2 = /\n/g; 
  143.         exprString = exprString.replace(pat2, " ");
  144.         RegExp.multiline = oldMulti; 
  145.             
  146.         
  147.         var isExistingParam = findObject("existingParamRadio").checked;
  148.         var existingParamToUse = paramController.getValue();
  149.         
  150.         curDOM.synchronizeDocument();
  151.         curDOM.disableLocking();
  152.         var result = false;
  153.                                             
  154.         if (selectedOptionalNode != null)
  155.             {
  156.             if (isExpression && exprString.length > 0)
  157.                 {
  158.                 //selectedOptionalNode.cond = dwscripts.minEntityNameEncode(exprString);
  159.                 selectedOptionalNode.cond = exprString;
  160.                                 
  161.                 result = true;
  162.                 }
  163.             else if (isExistingParam && existingParamToUse != MM.Optional_MSG_None)
  164.                 {
  165.                 //selectedOptionalNode.cond = dwscripts.minEntityNameEncode(encodeTemplateParam(existingParamToUse, true));
  166.                 selectedOptionalNode.cond = encodeTemplateParam(existingParamToUse, true);
  167.                 result = true;
  168.                 }
  169.             else
  170.                 alert(MM.Optional_MSG_MustPick);
  171.             }
  172.         else
  173.             {
  174.             if (isExpression && exprString.length > 0)
  175.                 result = doInsertConditional("",true, exprString, targetDom, false, addEditable);
  176.             else if (isExistingParam && existingParamToUse != MM.Optional_MSG_None)
  177.                 result = doInsertConditional("",true, encodeTemplateParam(existingParamToUse, true), targetDom, true, addEditable);
  178.             else
  179.                 alert(MM.Optional_MSG_MustPick);
  180.             }
  181.         
  182.         curDOM.enableLocking();
  183.     
  184.         if (result)
  185.             {
  186.             curDOM.synchronizeDocument();
  187.             window.close();
  188.             }
  189.         }
  190.     } //okClicked
  191.     
  192.  
  193.     
  194. //---------------    LOCAL FUNCTIONS   ---------------
  195.  
  196.  
  197.  
  198. //Update the selection state on the controls, based on one of them changing.
  199. function updateControls(whichControl)
  200.     {    
  201.     var expressionRadio = findObject("expressionRadio");
  202.     var existingParamRadio = findObject("existingParamRadio");
  203.     
  204.     switch (whichControl)
  205.         {
  206.         case 'expression':
  207.             {
  208.               expressionRadio.checked = true;
  209.               existingParamRadio.checked = false;
  210.               break;
  211.             }
  212.             
  213.         case 'existingParams':
  214.             {
  215.               expressionRadio.checked = false;
  216.               existingParamRadio.checked = true;
  217.               break;
  218.             }
  219.             
  220.         case 'expressionRadio':
  221.             {
  222.               expressionRadio.checked = true;
  223.               existingParamRadio.checked = false;
  224.               break;
  225.             }
  226.             
  227.         case 'existingParamRadio':
  228.             {
  229.               expressionRadio.checked = false;
  230.               existingParamRadio.checked = true;
  231.               break;
  232.             }
  233.         }
  234.     } //updateControls
  235.  
  236.  
  237.  
  238.  
  239. //This is an example of a page class to be used with the TabControl.
  240. //Uncomment the alert() calls to display the various events as they occur.
  241.  
  242. function Pg1(theTabLabel) {
  243.   this.tabLabel = theTabLabel;
  244. }
  245. Pg1.prototype.getTabLabel = Pg1_getTabLabel;
  246.  
  247.  
  248. function Pg1_getTabLabel() {
  249.   return this.tabLabel;
  250. }
  251.  
  252.  
  253. function initializeUI()
  254.     {    
  255.     if (abortCommand)
  256.         {
  257.         window.close();
  258.         return;
  259.         }
  260.  
  261.   var tab0 = findObject("Tab0");
  262.   var tab1 = findObject("Tab1");
  263.  
  264.   //Use appropriate background & tabs for Mac OS X.
  265.   if (dw.isOSX()) {
  266.     findObject("tabBgWin").src = "../Shared/MM/Images/tabBgOSX335x290.gif";    
  267.     var oldMulti = RegExp.multiline;
  268.     RegExp.multiline = true;
  269.     var pat1 = /tabBg\.gif/;
  270.     tab0.innerHTML = tab0.innerHTML.replace(pat1, "tabBgOSX.gif");
  271.       tab1.innerHTML = tab1.innerHTML.replace(pat1, "tabBgOSX.gif");
  272.     var pat2 = /tabBgSel\.gif/;
  273.     tab0.innerHTML = tab0.innerHTML.replace(pat2, "tabBgSelOSX.gif");
  274.     tab1.innerHTML = tab1.innerHTML.replace(pat2, "tabBgSelOSX.gif");
  275.       RegExp.multiline = oldMulti;
  276.   // Use appropriate background & tabs for WinXP with themes  
  277.   } else if (dw.isXPThemed()) {
  278.     findObject("tabBgWin").src = "../Shared/MM/Images/tabBgWinXP335x290.gif";
  279.     var oldMulti = RegExp.multiline;
  280.     RegExp.multiline = true;
  281.     var pat1 = /tabBg\.gif/;
  282.     tab0.innerHTML = tab0.innerHTML.replace(pat1, "tabBgXP.gif");
  283.       tab1.innerHTML = tab1.innerHTML.replace(pat1, "tabBgXP.gif");
  284.     var pat2 = /tabBgSel\.gif/;
  285.     tab0.innerHTML = tab0.innerHTML.replace(pat2, "tabBgSelXP.gif");
  286.     tab1.innerHTML = tab1.innerHTML.replace(pat2, "tabBgSelXP.gif");
  287.     RegExp.multiline = oldMulti;
  288.   // Use standard background  
  289.   } else {    
  290.     findObject("tabBgWin").src = "../Shared/MM/Images/tabBgWin.gif";
  291.   }
  292.  
  293.    T = new TabControl('Tab');
  294.    T.addPage('basic', new Pg1(MM.Optional_LABEL_Basic));
  295.    T.addPage('advanced', new Pg1(MM.Optional_LABEL_Advanced));
  296.   
  297.    var startPage = 'basic';
  298.    currentExpression = "";
  299.           
  300.    if (selectedOptionalNode != null)
  301.            currentExpression = decodeTemplateParam(dwscripts.minEntityNameDecode(selectedOptionalNode.cond));
  302.    else
  303.        {
  304.        //auto-name the optional area. 
  305.        //Count the number of optional areas, add one. 
  306.        currentExpression = getUniqueRegionName(MM.OptionalAutonamePreamble, "MMTemplate:If",  dw.getDocumentDOM('document'));
  307.        }
  308.            
  309.    var isExpression = (selectedOptionalNode != null && isExpressionString(currentExpression, true));
  310.   
  311.    if (isExpression)
  312.       startPage = 'advanced';
  313.        
  314.    T.start(startPage);
  315.  
  316.   
  317.    if (currentExpression != "")
  318.        {
  319.         findObject("condName").value = currentExpression;
  320.         findObject("expression").value = currentExpression;
  321.         }
  322.         
  323.     if (startPage == 'basic')
  324.         {
  325.           findObject("condName").focus(); //set focus on textbox
  326.           findObject("condName").select(); //set insertion point into textbox
  327.           }
  328.           
  329.       findObject("expressionRadio").checked = isExpression;
  330.       findObject("existingParamRadio").checked = !isExpression;
  331.  
  332.       //Setup the popup of existing params.
  333.       paramController = new ListControl("existingParams");
  334.       var boolParams =  getTemplateParams("boolean", null);
  335.       
  336.       var paramNames = new Array();
  337.       paramNames.push(MM.Optional_MSG_None);
  338.       for (i=0;i<boolParams.length;i++)
  339.           paramNames.push(boolParams[i].name);
  340.           
  341.       paramController.setAll(paramNames, paramNames);      
  342.     if (!isExpression && selectedOptionalNode != null && currentExpression.length > 0)
  343.         paramController.pickValue(currentExpression);
  344.     if (selectedOptionalNode != null)
  345.         {
  346.         var curDOM = (targetDom == null) ? dw.getDocumentDOM('document') : targetDom;
  347.         var paramTag = getTemplateParamTag(currentExpression, curDOM);
  348.  
  349.         if (paramTag != null)
  350.             {
  351.             findObject("showByDefault").checked = paramTag.value == "true";
  352.               findObject("existingParamRadio").checked = true;
  353.               findObject("expressionRadio").checked = false;
  354.               paramController.pickValue(paramTag.name);
  355.             }
  356.         }
  357.     } //initializeUI
  358.  
  359.  
  360.