home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Menus / MM / Context_QTE_Dynamic.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  4.3 KB  |  139 lines

  1. // Copyright 1999 Macromedia, Inc. All rights reserved.
  2.  
  3.    // This is an array of tags we don't want to allow people to remove.
  4.    var noStripTags = new Array("body", "head", "html", "tr", "td", "table");
  5.  
  6.    // getSelectionParentTag() returns the tag or empty tag that's the
  7.    // closest ancestor of the current selection.
  8.    function getSelectionParentTag()
  9.    {
  10.        var dom = dw.getDocumentDOM();
  11.        var selArray = dom.getSelection(true);
  12.        var obj = dom.offsetsToNode(selArray[0], selArray[1]);
  13.        while (obj && !obj.tagName)
  14.            obj = obj.parentNode;
  15.  
  16.        // Special case: If more than one table cell is selected,
  17.        // return the parent table.
  18.        if (selArray.length > 2 && obj.tagName.toLowerCase() == "td")
  19.        {
  20.            var tableObj = obj.parentNode;
  21.            while (tableObj && 
  22.                   (tableObj.nodeType != Node.ELEMENT_NODE ||
  23.                    tableObj.tagName.toLowerCase() != "table"))
  24.            {
  25.                tableObj = tableObj.parentNode;
  26.            }
  27.            if (tableObj)
  28.                obj = tableObj;
  29.        }
  30.  
  31.        return obj;
  32.    }
  33.  
  34.    function receiveArguments()
  35.    {
  36.           var obj = getSelectionParentTag();
  37.        var itemID = arguments[0];
  38.  
  39.        if (itemID == "DW:QTE:Insert")
  40.            dw.showQuickTagEditor("selection", "insert");
  41.        else if (itemID == "DW:QTE:Edit")
  42.        {
  43.               // If we showed the user a specific tag to edit, then
  44.            // select it first, to make sure that's the tag the
  45.            // QTE will let us edit.
  46.            if (obj)
  47.            {
  48.                dw.getDocumentDOM().setSelectedNode(obj);
  49.            }
  50.               dw.showQuickTagEditor("selection", "edit");
  51.        }
  52.        else if (itemID == "DW:QTE:Wrap")
  53.            dw.showQuickTagEditor("selection", "wrap");
  54.        else if (itemID == "DW:QTE:Remove")
  55.        {
  56.               // Get the parent tag of the current selection and remove it.
  57.            if (obj)
  58.            {
  59.                var selection = dw.nodeToOffsets(obj);
  60.                dw.setSelection(selection[0], selection[1]);
  61.                      dw.getDocumentDOM().stripTag();
  62.            }
  63.        }
  64.        else if (itemID == "DW:QTE")
  65.            dw.showQuickTagEditor();
  66.    }
  67.  
  68.    function canAcceptCommand()
  69.    {
  70.        // If we put it in the menu, it must be reasonable in the
  71.        // current context.
  72.        return true;
  73.    }
  74.  
  75.    function getDynamicContent()
  76.    {
  77.        var result = new Array;
  78.  
  79.        // Depending on the current selection, add entries for
  80.        // specific Quick Tag Editor modes.
  81.           var resultIndex = 0;
  82.        var selection = dw.getSelection();
  83.        if (selection[0] == selection[1])
  84.        {
  85.            // We have an IP.  Add "Insert HTML..."
  86.            result[resultIndex++] = MENU_strInsert + ";id='DW:QTE:Insert'";
  87.        }
  88.        else
  89.        {
  90.               // "Wrap" is available if the current selection is balanced.
  91.            // A selection is balanced only if both ends of the selection
  92.            // have a common parent.  But "wrap" isn't useful when the
  93.            // selection is an IP.
  94.            var startNode = dw.offsetsToNode(selection[0], selection[0]);
  95.            var endNode = dw.offsetsToNode(selection[1], selection[1]);
  96.            if (startNode != endNode)
  97.            {
  98.                // If we're looking at a text node, we can go up one level
  99.                // to the parent tag.
  100.                if (startNode.nodeType == Node.TEXT_NODE)
  101.                    startNode = startNode.parentNode;
  102.                if (endNode.nodeType == Node.TEXT_NODE)
  103.                    endNode = endNode.parentNode;
  104.            }
  105.            if (startNode == endNode)
  106.            {
  107.                // We can probably wrap a tag around the selection.
  108.                result[resultIndex++] = MENU_strWrap + ";id='DW:QTE:Wrap'";
  109.            }
  110.        }
  111.  
  112.        // Okay, see if we're inside a tag, and if so, add "Edit".
  113.        var obj = getSelectionParentTag();
  114.        if (obj)
  115.        {
  116.            // "Edit" is available for container tags.
  117.            result[resultIndex++] = MENU_strEdit + " <" + obj.tagName.toLowerCase() + ">...;id='DW:QTE:Edit'";
  118.  
  119.            // "Remove" is available on a tag surrounding the selection.
  120.            // However, we block certain "dangerous" tags from being stripped.
  121.               var tagName = obj.tagName.toLowerCase();
  122.               var found = false;
  123.               var i;
  124.               for (i = 0; i < noStripTags.length; i++)
  125.               {
  126.                   if (noStripTags[i] == tagName)
  127.                   {
  128.                       found = true;
  129.                       break;
  130.                   }
  131.               }
  132.     
  133.               if (!found)
  134.                result[resultIndex++] = MENU_strRemove + " <" + obj.tagName.toLowerCase() + ">;id='DW:QTE:Remove'";
  135.        }
  136.  
  137.        return result;
  138.    }
  139.