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 / InsertDiv.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  6.7 KB  |  255 lines

  1. // Copyright 2003 Macromedia, Inc. All rights reserved.
  2.  
  3. //---------------   GLOBAL VARIABLES   ---------------
  4. var helpDoc = MM.HELP_objDiv;
  5. var LIST_CLASSES;
  6. var LIST_IDS;
  7.  
  8. //---------------     API FUNCTIONS    ---------------
  9.  
  10. function isDOMRequired()
  11. {
  12.     // TODO: is this kosher? We need to do this to get the list of
  13.     // existing classes/ids.
  14.     return true;
  15. }
  16.  
  17. function commandButtons()
  18. {
  19.    return new Array( MM.BTN_OK,     "doInsertDiv()",
  20.                      MM.BTN_Cancel, "window.close()",
  21.                      MM.BTN_Help,   "displayHelp()");
  22. }
  23.  
  24. //---------------    LOCAL FUNCTIONS   ---------------
  25.  
  26. function initializeUI()
  27. {
  28.     var i;
  29.     var dom = dw.getDocumentDOM();
  30.  
  31.     LIST_CLASSES = new ListControl('divClass');
  32.     LIST_CLASSES.setIndex(-1);
  33.     if (dom)
  34.     {
  35.         var classes = dom.getSelectorsDefinedInStylesheet('class');
  36.         for (i = 0; i < classes.length; i++)
  37.         {
  38.             if (classes[i][0] == '.')
  39.                 classes[i] = classes[i].slice(1);
  40.         }
  41.         LIST_CLASSES.setAll(classes);
  42.     }
  43.  
  44.     LIST_IDS = new ListControl('divID');
  45.     LIST_IDS.setIndex(-1);
  46.     if (dom)
  47.     {
  48.         var ids = dom.getSelectorsDefinedInStylesheet('id');
  49.         var unusedIDs = new Array();
  50.         for (i = 0; i < ids.length; i++)
  51.         {
  52.             if (ids[i][0] == '#')
  53.                 ids[i] = ids[i].slice(1);
  54.             if (!isIDInUse(ids[i]))
  55.                 unusedIDs.push(ids[i]);
  56.         }
  57.         LIST_IDS.setAll(unusedIDs);
  58.     }
  59.  
  60.     document.theForm.divClass.focus();
  61.  
  62.     // If the selection is a range, show the "wrap around selection" text.
  63.     var selection = dw.getSelection();
  64.     if (selection[0] != selection[1])
  65.         document.theForm.divInsertWhere.options[0].innerHTML = dw.loadString("insertbar/div/wraparound");
  66.  
  67.     // If there's no tags with IDs, hide the before/after tag options.
  68.     var idNodeList = dom.getElementsByAttributeName("id");
  69.     if (!idNodeList || idNodeList.length == 0)
  70.     {
  71.         // Should really rewrite this in terms of the ListControl class.
  72.         var beforeOption = null;
  73.         var afterOption = null;
  74.         for (i = 0; i < document.theForm.divInsertWhere.options.length; i++)
  75.         {
  76.             var optionNode = document.theForm.divInsertWhere.options[i];
  77.             if (optionNode.value == 'before')
  78.                 beforeOption = optionNode;
  79.             else if (optionNode.value == 'after')
  80.                 afterOption = optionNode;
  81.         }
  82.         if (beforeOption)
  83.             beforeOption.outerHTML = '';
  84.         if (afterOption)
  85.             afterOption.outerHTML = '';
  86.     }
  87. }
  88.  
  89. function updateInsertDropdown()
  90. {
  91.     var selOption = document.theForm.divInsertWhere.options[document.theForm.divInsertWhere.selectedIndex];
  92.     if (selOption == null || selOption.value == "cursel")
  93.     {
  94.         // "Insert at IP" or "Wrap around selection" is selected.
  95.         document.theForm.divInsertAtTag.setAttribute("disabled", "disabled");
  96.         document.theForm.divInsertAtTag.innerHTML = "";
  97.     }
  98.     else
  99.     {
  100.         // Reset the list of tags in the dropdown based on the selection.
  101.         var tagList = "";
  102.         if (selOption.value == "insideStart" || selOption.value == "insideEnd")
  103.             tagList += '<option value="!body" name="body"><body></option>';
  104.         
  105.         var dom = dw.getDocumentDOM();
  106.         if (dom)
  107.         {
  108.             var nodeList = dom.getElementsByAttributeName("id");
  109.             if (nodeList)
  110.             {
  111.                 for (var i = 0; i < nodeList.length; i++)
  112.                 {
  113.                     tagList += '<option value="' + nodeList[i].id + '" name="' + nodeList[i].id + '"><' + nodeList[i].tagName.toLowerCase() + ' id="' + nodeList[i].id + '"></option>';
  114.                 }
  115.             }
  116.         }
  117.         
  118.         document.theForm.divInsertAtTag.removeAttribute("disabled");
  119.         document.theForm.divInsertAtTag.innerHTML = tagList;
  120.         document.theForm.divInsertAtTag.selectedIndex = 0;
  121.     }
  122.     document.theForm.divInsertWhere.focus();
  123. }
  124.  
  125. function isIDInUse(idStr)
  126. {
  127.     var dom = dw.getDocumentDOM();
  128.     if (dom)
  129.     {    
  130.         var nodeList = dom.getElementsByAttributeName('id');
  131.         if (nodeList)
  132.         {
  133.             for (var i = 0; i < nodeList.length; i++)
  134.             {
  135.                 if (nodeList[i].id.toLowerCase() == idStr.toLowerCase())
  136.                     return true;
  137.             }
  138.         }
  139.     }
  140.     return false;
  141. }
  142.  
  143. function doInsertDiv()
  144. {
  145.     var dom = dw.getDocumentDOM();
  146.     if (dom)
  147.     {
  148.         var newDivID = LIST_IDS.get();
  149.         if (newDivID != '')
  150.         {
  151.             if (isIDInUse(newDivID))
  152.             {
  153.                 if (!confirm(dw.loadString("insertbar/div/dupID")))
  154.                     return;
  155.             }
  156.         }
  157.  
  158.         var newTag = '<div';
  159.         var contentDesc = '';
  160.         if (LIST_CLASSES.get() != '')
  161.         {
  162.             newTag += ' class="' + LIST_CLASSES.get() + '"';
  163.             contentDesc += ' class "' + LIST_CLASSES.get() + '"';
  164.         }
  165.         if (newDivID != '')
  166.         {
  167.             newTag += ' id="' + newDivID + '"';
  168.             contentDesc += ' id "' + newDivID + '"';
  169.         }
  170.         newTag += '>';
  171.  
  172.         var content;
  173.         if (contentDesc == '')
  174.         {
  175.             content = dw.loadString("insertbar/div/divContentNoID");
  176.         }
  177.         else
  178.         {
  179.             content = dw.loadString("insertbar/div/divContent");
  180.             content = content.replace(/%1/, contentDesc);
  181.         }
  182.  
  183.         var closeTag = '</div>';
  184.         var newTagWithContent = newTag + content + closeTag;
  185.  
  186.         var startOffset = newTag.length;
  187.         var endOffset = closeTag.length;
  188.  
  189.         var selOption = document.theForm.divInsertWhere.options[document.theForm.divInsertWhere.selectedIndex];
  190.         if (selOption == null || selOption.value == 'cursel')
  191.         {
  192.             var selection = dw.getSelection();
  193.             if (selection[0] == selection[1])
  194.                 dom.insertHTML(newTagWithContent);
  195.             else
  196.                 dom.wrapTag(newTag + closeTag, true, true);
  197.         }
  198.         else
  199.         {
  200.             var tagNode = null;
  201.             var tagID = document.theForm.divInsertAtTag.options[document.theForm.divInsertAtTag.selectedIndex].value;
  202.             if (tagID == '!body')
  203.                 tagNode = dom.body;
  204.             else
  205.             {
  206.                 var nodeList = dom.getElementsByAttributeName('id');
  207.                 for (var i = 0; i < nodeList.length; i++)
  208.                 {
  209.                     if (nodeList[i].id == tagID)
  210.                     {
  211.                         tagNode = nodeList[i];
  212.                         break;
  213.                     }
  214.                 }
  215.             }
  216.             if (tagNode != null)
  217.             {
  218.                 if (selOption.value == 'before')
  219.                 {
  220.                     endOffset += tagNode.outerHTML.length;
  221.                     tagNode.outerHTML = newTagWithContent + tagNode.outerHTML;
  222.                 }
  223.                 else if (selOption.value == 'after')
  224.                 {
  225.                     startOffset += tagNode.outerHTML.length;
  226.                     tagNode.outerHTML = tagNode.outerHTML + newTagWithContent;
  227.                 }
  228.                 else if (selOption.value == 'insideStart')
  229.                 {
  230.                     endOffset += tagNode.innerHTML.length;
  231.                     tagNode.innerHTML = newTagWithContent + tagNode.innerHTML;
  232.                 }
  233.                 else if (selOption.value == 'insideEnd')
  234.                 {
  235.                     startOffset += tagNode.innerHTML.length;
  236.                     tagNode.innerHTML = tagNode.innerHTML + newTagWithContent;
  237.                 }
  238.             }
  239.         }
  240.  
  241.         // At this point, the <div> should be selected.  Move the
  242.         // selection within the <div>.
  243.         var newSelection = dw.getSelection();
  244.         newSelection[0] += startOffset;
  245.         newSelection[1] -= endOffset;
  246.         if ((newSelection[1] > newSelection[0]) &&
  247.             (!dw.getDocumentDOM().rangeContainsLockedRegion(newSelection[0], newSelection[1])))
  248.         {
  249.             dw.setSelection(newSelection[0], newSelection[1]);
  250.         }
  251.     }
  252.  
  253.     window.close();
  254. }
  255.