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