home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / comm.jar / content / editor / EdAECSSAttributes.js < prev    next >
Encoding:
JavaScript  |  2001-06-12  |  5.0 KB  |  177 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. // build attribute list in tree form from element attributes
  25. function BuildCSSAttributeTable()
  26. {
  27.   // get the CSS declaration from DOM 2 ElementCSSInlineStyle
  28.   var style = gElement.style;
  29.  
  30.   if (style == undefined)
  31.   {
  32.     dump("Inline styles undefined\n");
  33.     return;
  34.   }
  35.  
  36.   var l = style.length;
  37.   if (l == undefined || l == 0)
  38.   {
  39.     if (l == undefined) {
  40.       dump("Failed to query the number of inline style declarations\n");
  41.     }
  42.     return;
  43.   }
  44.  
  45.   if (l > 0)
  46.   {
  47.     var added = false;
  48.     for (i = 0; i < l; i++)
  49.     {
  50.       name = style.item(i);
  51.       value = style.getPropertyValue(name);
  52.       AddTreeItem( name, value, "CSSAList", CSSAttrs );
  53.     }
  54.   }
  55.  
  56.   ClearCSSInputWidgets();
  57. }
  58.  
  59. // add or select attribute in the tree widget
  60. function onChangeCSSAttribute()
  61. {
  62.   var name = TrimString(dialog.AddCSSAttributeNameInput.value);
  63.   if ( !name )
  64.     return;
  65.  
  66.   var value = TrimString(dialog.AddCSSAttributeValueInput.value);
  67.  
  68.   // First try to update existing attribute
  69.   // If not found, add new attribute
  70.   if ( !UpdateExistingAttribute( name, value, "CSSAList" ) )
  71.     AddTreeItem( name, value, "CSSAList", CSSAttrs );
  72. }
  73.  
  74. function ClearCSSInputWidgets()
  75. {
  76.   dialog.AddCSSAttributeTree.clearItemSelection();
  77.   dialog.AddCSSAttributeNameInput.value ="";
  78.   dialog.AddCSSAttributeValueInput.value = "";
  79.   dialog.AddCSSAttributeNameInput.inputField.focus();
  80. }
  81.  
  82. function onSelectCSSTreeItem()
  83. {
  84.   if (!gDoOnSelectTree)
  85.     return;
  86.  
  87.   var tree = dialog.AddCSSAttributeTree;
  88.   if (tree && tree.selectedItems && tree.selectedItems.length)
  89.   {
  90.     dialog.AddCSSAttributeNameInput.value = GetTreeItemAttributeStr(tree.selectedItems[0]);
  91.     dialog.AddCSSAttributeValueInput.value = GetTreeItemValueStr(tree.selectedItems[0]);
  92.   }
  93. }
  94.  
  95. function onInputCSSAttributeName()
  96. {
  97.   var attName = TrimString(dialog.AddCSSAttributeNameInput.value).toLowerCase();
  98.   var newValue = "";
  99.  
  100.   var existingValue = GetAndSelectExistingAttributeValue(attName, "CSSAList");
  101.   if (existingValue)
  102.     newValue = existingValue;
  103.  
  104.   dialog.AddCSSAttributeValueInput.value = newValue;
  105. }
  106.  
  107. function onInputCSSAttributeValue()
  108. {
  109.   // Update value in the tree list
  110.   UpdateExistingAttribute( dialog.AddCSSAttributeNameInput.value,
  111.                            dialog.AddCSSAttributeValueInput.value,
  112.                            "CSSAList" );
  113. }
  114.  
  115. function editCSSAttributeValue(targetCell)
  116. {
  117.   if (IsNotTreeHeader(targetCell))
  118.     dialog.AddCSSAttributeValueInput.inputField.select();
  119. }
  120.  
  121. function UpdateCSSAttributes()
  122. {
  123.   var CSSAList = document.getElementById("CSSAList");
  124.   var styleString = "";
  125.   for(var i = 0; i < CSSAList.childNodes.length; i++)
  126.   {
  127.     var item = CSSAList.childNodes[i];
  128.     var name = GetTreeItemAttributeStr(item);
  129.     var value = GetTreeItemValueStr(item);
  130.     // this code allows users to be sloppy in typing in values, and enter
  131.     // things like "foo: " and "bar;". This will trim off everything after the
  132.     // respective character.
  133.     if (name.indexOf(":") != -1)
  134.       name = name.substring(0,name.lastIndexOf(":"));
  135.     if (value.indexOf(";") != -1)
  136.       value = value.substring(0,value.lastIndexOf(";"));
  137.     if (i == (CSSAList.childNodes.length - 1))
  138.       styleString += name + ": " + value + ";";   // last property
  139.     else
  140.       styleString += name + ": " + value + "; ";
  141.   }
  142.   if (styleString.length > 0)
  143.   {
  144.     gElement.removeAttribute("style");
  145.     gElement.setAttribute("style",styleString);  // NOTE BUG 18894!!!
  146.   } 
  147.   else if (gElement.getAttribute("style"))
  148.     gElement.removeAttribute("style");
  149. }
  150.  
  151. function RemoveCSSAttribute()
  152. {
  153.   var treechildren = dialog.AddCSSAttributeTree.lastChild;
  154.  
  155.   // We only allow 1 selected item
  156.   if (dialog.AddCSSAttributeTree.selectedItems.length)
  157.   {
  158.     var item = dialog.AddCSSAttributeTree.selectedItems[0];
  159.  
  160.     // Remove the item from the tree
  161.     // We always rebuild complete "style" string,
  162.     //  so no list of "removed" items 
  163.     treechildren.removeChild (item);
  164.  
  165.     ClearCSSInputWidgets();
  166.   }
  167. }
  168.  
  169. function SelectCSSTree( index )
  170. {
  171.   gDoOnSelectTree = false;
  172.   try {
  173.     dialog.AddCSSAttributeTree.selectedIndex = index;
  174.   } catch (e) {}
  175.   gDoOnSelectTree = true;
  176. }
  177.