home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / comm.jar / content / editor / EdAEJSEAttributes.js < prev    next >
Encoding:
JavaScript  |  2001-08-17  |  6.8 KB  |  233 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.   ClearMenulist(dialog.AddJSEAttributeNameList);
  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.         AppendStringToMenulist(dialog.AddJSEAttributeNameList, attNames[i]);
  52.  
  53.       popup = dialog.AddJSEAttributeNameList.firstChild;
  54.       if (popup)
  55.       {
  56.         sep = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "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 = dialog.AddJSEAttributeNameList.firstChild;
  70.  
  71.       sep = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "menuseparator");
  72.  
  73.       if (popup && sep)
  74.         popup.appendChild(sep);
  75.     }
  76.     else
  77.       AppendStringToMenulist(dialog.AddJSEAttributeNameList, gCoreJSEvents[i]);
  78.   }
  79.   
  80.   dialog.AddJSEAttributeNameList.selectedIndex = 0;
  81.  
  82.   // Use current name and value of first tree item if it exists
  83.   onSelectJSETreeItem();
  84.  
  85.   dialog.AddJSEAttributeNameList.focus();
  86. }
  87.  
  88. // build attribute list in tree form from element attributes
  89. function BuildJSEAttributeTable()
  90. {
  91.   var nodeMap = gElement.attributes;
  92.   if (nodeMap.length > 0)
  93.   {
  94.     var added = false;
  95.     for (var i = 0; i < nodeMap.length; i++)
  96.     {
  97.       if( CheckAttributeNameSimilarity( nodeMap[i].nodeName, JSEAttrs ) )
  98.         continue;   // repeated or non-JS handler, ignore this one and go to next
  99.       if( !IsEventHandler( nodeMap[i].nodeName ) )
  100.         continue; // attribute isn't an event handler.
  101.       var name  = nodeMap[i].nodeName.toLowerCase();
  102.       var value = gElement.getAttribute(nodeMap[i].nodeName);
  103.       if (AddTreeItem( name, value, "JSEAList", JSEAttrs )) // add item to tree
  104.         added = true;
  105.     }
  106.  
  107.     // Select first item
  108.     if (added)
  109.       dialog.AddJSEAttributeTree.selectedIndex = 0;
  110.   }
  111. }
  112.  
  113. // check to see if given string is an event handler.
  114. function IsEventHandler( which )
  115. {
  116.   var handlerName = which.toLowerCase();
  117.   var firstTwo = handlerName.substring(0,2);
  118.   if (firstTwo == "on")
  119.     return true;
  120.   else
  121.     return false;
  122. }
  123.  
  124. function onSelectJSEAttribute()
  125. {
  126.   if(!gDoOnSelectTree)
  127.     return;
  128.  
  129.   dialog.AddJSEAttributeValueInput.value = 
  130.       GetAndSelectExistingAttributeValue(dialog.AddJSEAttributeNameList.label, "JSEAList");
  131. }
  132.  
  133. function onSelectJSETreeItem()
  134. {
  135.   var tree = dialog.AddJSEAttributeTree;
  136.   if (tree && tree.selectedItems && tree.selectedItems.length)
  137.   {
  138.     var name = GetTreeItemAttributeStr(tree.selectedItems[0]);
  139.  
  140.     // Select attribute name in list
  141.     if (dialog.AddJSEAttributeNameList.firstChild)
  142.     {
  143.       var arr = dialog.AddJSEAttributeNameList.firstChild.getElementsByAttribute('label', name);
  144.       if (arr && arr.length)
  145.         dialog.AddJSEAttributeNameList.selectedItem = arr[0];
  146.  
  147.       // Set value input to that in tree (no need to update this in the tree)
  148.       gUpdateTreeValue = false;
  149.       dialog.AddJSEAttributeValueInput.value =  GetTreeItemValueStr(tree.selectedItems[0]);
  150.       gUpdateTreeValue = true;
  151.     }
  152.   }
  153. }
  154.  
  155. function onInputJSEAttributeValue()
  156. {
  157.   if (gUpdateTreeValue)
  158.   {
  159.  
  160.     var name = TrimString(dialog.AddJSEAttributeNameList.label);
  161.     var value = TrimString(dialog.AddJSEAttributeValueInput.value);
  162.  
  163.     // Update value in the tree list
  164.     // Since we have a non-editable menulist, 
  165.     //   we MUST automatically add the event attribute if it doesn't exist
  166.     if (!UpdateExistingAttribute( name, value, "JSEAList" ))
  167.       AddTreeItem( name, value, "JSEAList", JSEAttrs );
  168.   }
  169. }
  170.  
  171. function editJSEAttributeValue(targetCell)
  172. {
  173.   if (IsNotTreeHeader(targetCell))
  174.     dialog.AddJSEAttributeValueInput.inputField.select();
  175. }
  176.  
  177. function UpdateJSEAttributes()
  178. {
  179.   var JSEAList = document.getElementById("JSEAList");
  180.   var i;
  181.  
  182.   // remove removed attributes
  183.   for (i = 0; i < JSERAttrs.length; i++)
  184.   {
  185.     name = JSERAttrs[i];
  186.     if (gElement.getAttribute(name))
  187.       gElement.removeAttribute(name);
  188.   }
  189.  
  190.   // Add events
  191.   for (i = 0; i < JSEAList.childNodes.length; i++)
  192.   {
  193.     var item = JSEAList.childNodes[i];
  194.  
  195.     // set the event handler
  196.     gElement.setAttribute( GetTreeItemAttributeStr(item), GetTreeItemValueStr(item) );
  197.   }
  198. }
  199.  
  200. function RemoveJSEAttribute()
  201. {
  202.   var treechildren = dialog.AddJSEAttributeTree.lastChild;
  203.  
  204.   // This differs from HTML and CSS panels: 
  205.   // We reselect after removing, because there is not
  206.   //  editable attribute name input, so we can't clear that
  207.   //  like we do in other panels
  208.   var newIndex = dialog.AddJSEAttributeTree.selectedIndex;
  209.  
  210.   // We only allow 1 selected item
  211.   if (dialog.AddJSEAttributeTree.selectedItems.length)
  212.   {
  213.     var item = dialog.AddJSEAttributeTree.selectedItems[0];
  214.  
  215.     // Name is the text of the treecell
  216.     var attr = GetTreeItemAttributeStr(item);
  217.  
  218.     // remove the item from the attribute array
  219.     if (newIndex >= (JSEAttrs.length-1))
  220.       newIndex--;
  221.  
  222.     // remove the item from the attribute array
  223.     JSERAttrs[JSERAttrs.length] = attr;
  224.     RemoveNameFromAttArray(attr, JSEAttrs);
  225.  
  226.     // Remove the item from the tree
  227.     treechildren.removeChild (item);
  228.  
  229.     // Reselect an item
  230.     dialog.AddJSEAttributeTree.selectedIndex = newIndex;
  231.   }
  232. }
  233.