home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / editor / EdAEJSEAttributes.js < prev    next >
Text File  |  2003-06-08  |  6KB  |  225 lines

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Ben "Count XULula" Goodger
  22.  */
  23.  
  24. function BuildJSEAttributeNameList()
  25. {
  26.   gDialog.AddJSEAttributeNameList.removeAllItems();
  27.   
  28.   // Get events specific to current element
  29.   var elementName = gElement.localName.toLowerCase();
  30.   if (elementName in gJSAttr)
  31.   {
  32.     var attNames = gJSAttr[elementName];
  33.     var i;
  34.     var popup;
  35.     var sep;
  36.  
  37.     if (attNames && attNames.length)
  38.     {
  39.       // Since we don't allow user-editable JS events yet (but we will soon)
  40.       //  simply remove the JS tab to not allow adding JS events
  41.       if (attNames[0] == "noJSEvents")
  42.       {
  43.         var tab = document.getElementById("tabJSE");
  44.         if (tab)
  45.           tab.parentNode.removeChild(tab);
  46.  
  47.         return;
  48.       }
  49.  
  50.       for (i = 0; i < attNames.length; i++)
  51.         gDialog.AddJSEAttributeNameList.appendItem(attNames[i], attNames[i]);
  52.  
  53.       popup = gDialog.AddJSEAttributeNameList.firstChild;
  54.       if (popup)
  55.       {
  56.         sep = document.createElementNS(XUL_NS, "menuseparator");
  57.         if (sep)
  58.           popup.appendChild(sep);
  59.       }        
  60.     }
  61.   }
  62.  
  63.   // Always add core JS events unless we aborted above
  64.   for (i = 0; i < gCoreJSEvents.length; i++)
  65.   {
  66.     if (gCoreJSEvents[i] == "-")
  67.     {
  68.       if (!popup)
  69.         popup = gDialog.AddJSEAttributeNameList.firstChild;
  70.  
  71.       sep = document.createElementNS(XUL_NS, "menuseparator");
  72.  
  73.       if (popup && sep)
  74.         popup.appendChild(sep);
  75.     }
  76.     else
  77.       gDialog.AddJSEAttributeNameList.appendItem(gCoreJSEvents[i], gCoreJSEvents[i]);
  78.   }
  79.   
  80.   gDialog.AddJSEAttributeNameList.selectedIndex = 0;
  81.  
  82.   // Use current name and value of first tree item if it exists
  83.   onSelectJSETreeItem();
  84. }
  85.  
  86. // build attribute list in tree form from element attributes
  87. function BuildJSEAttributeTable()
  88. {
  89.   var nodeMap = gElement.attributes;
  90.   if (nodeMap.length > 0)
  91.   {
  92.     var added = false;
  93.     for (var i = 0; i < nodeMap.length; i++)
  94.     {
  95.       if( CheckAttributeNameSimilarity( nodeMap[i].nodeName, JSEAttrs ) )
  96.         continue;   // repeated or non-JS handler, ignore this one and go to next
  97.       if( !IsEventHandler( nodeMap[i].nodeName ) )
  98.         continue; // attribute isn't an event handler.
  99.       var name  = nodeMap[i].nodeName.toLowerCase();
  100.       var value = gElement.getAttribute(nodeMap[i].nodeName);
  101.       if (AddTreeItem( name, value, "JSEAList", JSEAttrs )) // add item to tree
  102.         added = true;
  103.     }
  104.  
  105.     // Select first item
  106.     if (added)
  107.       gDialog.AddJSEAttributeTree.selectedIndex = 0;
  108.   }
  109. }
  110.  
  111. // check to see if given string is an event handler.
  112. function IsEventHandler( which )
  113. {
  114.   var handlerName = which.toLowerCase();
  115.   var firstTwo = handlerName.substring(0,2);
  116.   if (firstTwo == "on")
  117.     return true;
  118.   else
  119.     return false;
  120. }
  121.  
  122. function onSelectJSEAttribute()
  123. {
  124.   if(!gDoOnSelectTree)
  125.     return;
  126.  
  127.   gDialog.AddJSEAttributeValueInput.value = 
  128.       GetAndSelectExistingAttributeValue(gDialog.AddJSEAttributeNameList.label, "JSEAList");
  129. }
  130.  
  131. function onSelectJSETreeItem()
  132. {
  133.   var tree = gDialog.AddJSEAttributeTree;
  134.   if (tree && tree.treeBoxObject.selection.count)
  135.   {
  136.     // Select attribute name in list
  137.     gDialog.AddJSEAttributeNameList.value = GetTreeItemAttributeStr(getSelectedItem(tree));
  138.  
  139.     // Set value input to that in tree (no need to update this in the tree)
  140.     gUpdateTreeValue = false;
  141.     gDialog.AddJSEAttributeValueInput.value =  GetTreeItemValueStr(getSelectedItem(tree));
  142.     gUpdateTreeValue = true;
  143.   }
  144. }
  145.  
  146. function onInputJSEAttributeValue()
  147. {
  148.   if (gUpdateTreeValue)
  149.   {
  150.  
  151.     var name = TrimString(gDialog.AddJSEAttributeNameList.label);
  152.     var value = TrimString(gDialog.AddJSEAttributeValueInput.value);
  153.  
  154.     // Update value in the tree list
  155.     // Since we have a non-editable menulist, 
  156.     //   we MUST automatically add the event attribute if it doesn't exist
  157.     if (!UpdateExistingAttribute( name, value, "JSEAList" ) && value)
  158.       AddTreeItem( name, value, "JSEAList", JSEAttrs );
  159.   }
  160. }
  161.  
  162. function editJSEAttributeValue(targetCell)
  163. {
  164.   if (IsNotTreeHeader(targetCell))
  165.     gDialog.AddJSEAttributeValueInput.inputField.select();
  166. }
  167.  
  168. function UpdateJSEAttributes()
  169. {
  170.   var JSEAList = document.getElementById("JSEAList");
  171.   var i;
  172.  
  173.   // remove removed attributes
  174.   for (i = 0; i < JSERAttrs.length; i++)
  175.   {
  176.     var name = JSERAttrs[i];
  177.  
  178.     if (gElement.hasAttribute(name))
  179.       doRemoveAttribute(name);
  180.   }
  181.  
  182.   // Add events
  183.   for (i = 0; i < JSEAList.childNodes.length; i++)
  184.   {
  185.     var item = JSEAList.childNodes[i];
  186.  
  187.     // set the event handler
  188.     doSetAttribute( GetTreeItemAttributeStr(item), GetTreeItemValueStr(item) );
  189.   }
  190. }
  191.  
  192. function RemoveJSEAttribute()
  193. {
  194.   var treechildren = gDialog.AddJSEAttributeTree.lastChild;
  195.  
  196.   // This differs from HTML and CSS panels: 
  197.   // We reselect after removing, because there is not
  198.   //  editable attribute name input, so we can't clear that
  199.   //  like we do in other panels
  200.   var newIndex = gDialog.AddJSEAttributeTree.selectedIndex;
  201.  
  202.   // We only allow 1 selected item
  203.   if (gDialog.AddJSEAttributeTree.treeBoxObject.selection.count)
  204.   {
  205.     var item = getSelectedItem(gDialog.AddJSEAttributeTree);
  206.  
  207.     // Name is the text of the treecell
  208.     var attr = GetTreeItemAttributeStr(item);
  209.  
  210.     // remove the item from the attribute array
  211.     if (newIndex >= (JSEAttrs.length-1))
  212.       newIndex--;
  213.  
  214.     // remove the item from the attribute array
  215.     JSERAttrs[JSERAttrs.length] = attr;
  216.     RemoveNameFromAttArray(attr, JSEAttrs);
  217.  
  218.     // Remove the item from the tree
  219.     treechildren.removeChild (item);
  220.  
  221.     // Reselect an item
  222.     gDialog.AddJSEAttributeTree.selectedIndex = newIndex;
  223.   }
  224. }
  225.