home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / comm.jar / content / editor / EdAdvancedEdit.js < prev    next >
Encoding:
Text File  |  2001-08-17  |  9.8 KB  |  323 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. /**************       NAMESPACES       ***************/
  25. const XUL_NS    = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  26.  
  27. /**************         GLOBALS         **************/
  28. var gElement    = null; // handle to actual element edited
  29.  
  30. var HTMLAttrs   = [];   // html attributes
  31. var CSSAttrs    = [];   // css attributes
  32. var JSEAttrs    = [];   // js events
  33.  
  34. var HTMLRAttrs  = [];   // removed html attributes
  35. var JSERAttrs   = [];   // removed js events
  36.  
  37. /* Set false to allow changing selection in tree
  38.    without doing "onselect" handler actions
  39. */
  40. var gDoOnSelectTree = true;
  41. var gUpdateTreeValue = true;
  42.  
  43. var dialog;
  44.  
  45. /************** INITIALISATION && SETUP **************/
  46.  
  47. /**
  48.  * function   : void Startup();
  49.  * parameters : none
  50.  * returns    : none
  51.  * desc.      : startup and initialisation, prepares dialog.
  52.  **/
  53. function Startup()
  54. {
  55.   // This is the return value for the parent,
  56.   // who only needs to know if OK was clicked
  57.   window.opener.AdvancedEditOK = false;
  58.  
  59.   if (!InitEditorShell())
  60.     return;
  61.  
  62.   // initialise the ok and cancel buttons
  63.   doSetOKCancel(onOK, onCancel);
  64.  
  65.   // Element to edit is passed in
  66.   if (!window.arguments[1])
  67.   {
  68.     dump("Advanced Edit: Element to edit not supplied\n");
  69.     window.close();
  70.     return;
  71.   }
  72.   // The actual element edited (not a copy!)
  73.   gElement = window.arguments[1];
  74.  
  75.   // place the tag name in the header
  76.   var tagLabel = document.getElementById("tagLabel");
  77.   tagLabel.setAttribute("value", ("<" + gElement.localName + ">"));
  78.  
  79.   // Create dialog object to store controls for easy access
  80.   dialog                            = {};
  81.   dialog.AddHTMLAttributeNameInput  = document.getElementById("AddHTMLAttributeNameInput");
  82.  
  83.   // We use a <deck> to switch between editable menulist and textbox
  84.   dialog.AddHTMLAttributeValueDeck     = document.getElementById("AddHTMLAttributeValueDeck");
  85.   dialog.AddHTMLAttributeValueMenulist = document.getElementById("AddHTMLAttributeValueMenulist");
  86.   dialog.AddHTMLAttributeValueTextbox  = document.getElementById("AddHTMLAttributeValueTextbox");
  87.   dialog.AddHTMLAttributeValueInput    = dialog.AddHTMLAttributeValueTextbox;
  88.  
  89.   dialog.AddHTMLAttributeTree          = document.getElementById("HTMLATree");
  90.   dialog.AddCSSAttributeNameInput      = document.getElementById("AddCSSAttributeNameInput");
  91.   dialog.AddCSSAttributeValueInput     = document.getElementById("AddCSSAttributeValueInput");
  92.   dialog.AddCSSAttributeTree           = document.getElementById("CSSATree");
  93.   dialog.AddJSEAttributeNameList       = document.getElementById("AddJSEAttributeNameList");
  94.   dialog.AddJSEAttributeValueInput     = document.getElementById("AddJSEAttributeValueInput");
  95.   dialog.AddJSEAttributeTree           = document.getElementById("JSEATree");
  96.   dialog.okButton                      = document.getElementById("ok");
  97.  
  98.   // build the attribute trees
  99.   BuildHTMLAttributeTable();
  100.   BuildCSSAttributeTable();
  101.   BuildJSEAttributeTable();
  102.   
  103.   // Build attribute name arrays for menulists
  104.   BuildJSEAttributeNameList();
  105.  
  106.   // No menulists for CSS panel (yet), so just clear input fields
  107.   ClearCSSInputWidgets();
  108.  
  109.   // Do this last -- sets focus to HTML Name menulist
  110.   BuildHTMLAttributeNameList();
  111.  
  112.   // size the dialog properly
  113.   window.sizeToContent();
  114.  
  115.   SetWindowLocation();
  116. }
  117.  
  118. /**
  119.  * function   : bool onOK ( void );
  120.  * parameters : none
  121.  * returns    : boolean true to close the window
  122.  * desc.      : event handler for ok button
  123.  **/
  124. function onOK()
  125. {
  126.   // Update our gElement attributes
  127.   UpdateHTMLAttributes();
  128.   UpdateCSSAttributes();
  129.   UpdateJSEAttributes();
  130.  
  131.   window.opener.AdvancedEditOK = true;
  132.   SaveWindowLocation();
  133.  
  134.   return true; // do close the window
  135. }
  136.  
  137. /**
  138.  * function   : bool CheckAttributeNameSimilarity ( string attName, array attArray );
  139.  * parameters : attribute to look for, array of current attributes
  140.  * returns    : true if attribute already exists, false if it does not
  141.  * desc.      : checks to see if any other attributes by the same name as the arg supplied
  142.  *              already exist.
  143.  **/
  144. function CheckAttributeNameSimilarity(attName, attArray)
  145. {
  146.   for (var i = 0; i < attArray.length; i++)
  147.   {
  148.     if (attName.toLowerCase() == attArray[i].toLowerCase())
  149.       return true;
  150.   }
  151.   return false;
  152. }
  153.  
  154. /**
  155.  * function   : bool UpdateExistingAttribute ( string attName, string attValue, string treeChildrenId );
  156.  * parameters : attribute to look for, new value, ID of <treeChildren> node in XUL tree
  157.  * returns    : true if attribute already exists in tree, false if it does not
  158.  * desc.      : checks to see if any other attributes by the same name as the arg supplied
  159.  *              already exist while setting the associated value if different from current value
  160.  **/
  161. function UpdateExistingAttribute( attName, attValue, treeChildrenId )
  162. {
  163.   var treeChildren = document.getElementById(treeChildrenId);
  164.   if (!treeChildren)
  165.     return false;
  166.  
  167.   var name;
  168.   var i;
  169.   attName = TrimString(attName).toLowerCase();
  170.   attValue = TrimString(attValue);
  171.  
  172.   for (i = 0; i < treeChildren.childNodes.length; i++)
  173.   {
  174.     var item = treeChildren.childNodes[i];
  175.     name = GetTreeItemAttributeStr(item);
  176.     if (name.toLowerCase() == attName)
  177.     {
  178.       // Set the text in the "value' column treecell
  179.       SetTreeItemValueStr(item, attValue);
  180.  
  181.       // Select item just changed, 
  182.       //  but don't trigger the tree's onSelect handler
  183.       gDoOnSelectTree = false;
  184.       try {
  185.         treeChildren.parentNode.selectItem(item);
  186.       } catch (e) {}
  187.       gDoOnSelectTree = true;
  188.  
  189.       return true;
  190.     }
  191.   }
  192.   return false;
  193. }
  194.  
  195. /**
  196.  * function   : string GetAndSelectExistingAttributeValue ( string attName, string treeChildrenId );
  197.  * parameters : attribute to look for, ID of <treeChildren> node in XUL tree
  198.  * returns    : value in from the tree or empty string if name not found
  199.  **/
  200. function GetAndSelectExistingAttributeValue( attName, treeChildrenId )
  201. {
  202.   if (!attName)
  203.     return "";
  204.  
  205.   var treeChildren = document.getElementById(treeChildrenId);
  206.   var name;
  207.   var i;
  208.  
  209.   for (i = 0; i < treeChildren.childNodes.length; i++)
  210.   {
  211.     var item = treeChildren.childNodes[i];
  212.     name = GetTreeItemAttributeStr(item);
  213.     if (name.toLowerCase() == attName.toLowerCase())
  214.     {
  215.       // Select item in the tree
  216.       //  but don't trigger the tree's onSelect handler
  217.       gDoOnSelectTree = false;
  218.       try {
  219.         treeChildren.parentNode.selectItem(item);
  220.       } catch (e) {}
  221.       gDoOnSelectTree = true;
  222.  
  223.       // Get the text in the "value' column treecell
  224.       return GetTreeItemValueStr(item);
  225.     }
  226.   }
  227.  
  228.   // Attribute doesn't exist in tree, so remove selection
  229.   gDoOnSelectTree = false;
  230.   try {
  231.     treeChildren.parentNode.clearItemSelection();
  232.   } catch (e) {}
  233.   gDoOnSelectTree = true;
  234.  
  235.   return "";
  236. }
  237.  
  238. /* Tree structure: 
  239.   <treeItem>
  240.     <treeRow>
  241.       <treeCell> // Name Cell
  242.       <treeCell  // Value Cell
  243. */
  244. function GetTreeItemAttributeStr(treeItem)
  245. {
  246.   if (treeItem)
  247.     return TrimString(treeItem.firstChild.firstChild.getAttribute("label"));
  248.  
  249.   return "";
  250. }
  251.  
  252. function GetTreeItemValueStr(treeItem)
  253. {
  254.   if (treeItem)
  255.     return TrimString(treeItem.firstChild.lastChild.getAttribute("label"));
  256.  
  257.   return "";
  258. }
  259.  
  260. function SetTreeItemValueStr(treeItem, value)
  261. {
  262.   if (treeItem && GetTreeItemValueStr(treeItem) != value)
  263.     treeItem.firstChild.lastChild.setAttribute("label", value);
  264. }
  265.  
  266. function IsNotTreeHeader(treeCell)
  267. {
  268.   if (treeCell)
  269.     return (treeCell.parentNode.parentNode.nodeName != "treehead");
  270.  
  271.   return false;
  272. }
  273.  
  274. function RemoveNameFromAttArray(attName, attArray)
  275. {
  276.   for (var i=0; i < attArray.length; i++)
  277.   {
  278.     if (attName.toLowerCase() == attArray[i].toLowerCase())
  279.     {
  280.       // Remove 1 array item
  281.       attArray.splice(i,1);
  282.       break;
  283.     }
  284.   }
  285. }
  286.  
  287. // adds a generalised treeitem.
  288. function AddTreeItem ( name, value, treeChildrenId, attArray )
  289. {
  290.   attArray[attArray.length] = name;
  291.   var treeChildren    = document.getElementById ( treeChildrenId );
  292.   var treeitem    = document.createElementNS ( XUL_NS, "treeitem" );
  293.   var treerow     = document.createElementNS ( XUL_NS, "treerow" );
  294.  
  295.   var attrCell    = document.createElementNS ( XUL_NS, "treecell" );
  296.   attrCell.setAttribute( "class", "propertylist" );
  297.   attrCell.setAttribute( "label", name );
  298.  
  299.   var valueCell    = document.createElementNS ( XUL_NS, "treecell" );
  300.   valueCell.setAttribute( "class", "propertylist" );
  301.   valueCell.setAttribute( "label", value );
  302.  
  303.   treerow.appendChild ( attrCell );
  304.   treerow.appendChild ( valueCell );
  305.   treeitem.appendChild ( treerow );
  306.   treeChildren.appendChild ( treeitem );
  307.  
  308.   // Select item just added,
  309.   //  but suppress calling the onSelect handler
  310.   gDoOnSelectTree = false;
  311.   try {
  312.     treeChildren.parentNode.selectItem(treeitem);
  313.   } catch (e) {}
  314.   gDoOnSelectTree = true;
  315.  
  316.   return treeitem;
  317. }
  318.  
  319. function doHelpButton()
  320. {
  321.   openHelp("chrome://help/content/help.xul?advanced_property_editor");
  322. }
  323.