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 / EdAECSSAttributes.js < prev    next >
Text File  |  2003-06-08  |  5KB  |  181 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.   // we can't trust DOM 2 ElementCSSInlineStyle because gElement can be
  31.   // outside of the document's tree
  32.   var styleAttr = gElement.getAttribute("style");
  33.   var styleRule;
  34.   try {
  35.     var editor = GetCurrentEditor();
  36.     styleRule = editor.parseStyleAttrIntoCSSRule(styleAttr);
  37.   } catch(ex) {}
  38.   
  39.   if (styleRule == undefined)
  40.   {
  41.     dump("Inline styles undefined\n");
  42.     return;
  43.   }
  44.  
  45.   var style = styleRule.style;
  46.   var declLength = style.length;
  47.  
  48.   if (declLength == undefined || declLength == 0)
  49.   {
  50.     if (declLength == undefined) {
  51.       dump("Failed to query the number of inline style declarations\n");
  52.     }
  53.  
  54.     return;
  55.   }
  56.  
  57.   if (declLength > 0)
  58.   {
  59.     var added = false;
  60.     for (i = 0; i < declLength; i++)
  61.     {
  62.       name = style.item(i);
  63.       value = style.getPropertyValue(name);
  64.       AddTreeItem( name, value, "CSSAList", CSSAttrs );
  65.     }
  66.   }
  67.  
  68.   ClearCSSInputWidgets();
  69. }
  70.  
  71. function onChangeCSSAttribute()
  72. {
  73.   var name = TrimString(gDialog.AddCSSAttributeNameInput.value);
  74.   if ( !name )
  75.     return;
  76.  
  77.   var value = TrimString(gDialog.AddCSSAttributeValueInput.value);
  78.  
  79.   // First try to update existing attribute
  80.   // If not found, add new attribute
  81.   if ( !UpdateExistingAttribute( name, value, "CSSAList" ) && value)
  82.     AddTreeItem( name, value, "CSSAList", CSSAttrs );
  83. }
  84.  
  85. function ClearCSSInputWidgets()
  86. {
  87.   gDialog.AddCSSAttributeTree.treeBoxObject.selection.clearSelection();
  88.   gDialog.AddCSSAttributeNameInput.value ="";
  89.   gDialog.AddCSSAttributeValueInput.value = "";
  90.   SetTextboxFocus(gDialog.AddCSSAttributeNameInput);
  91. }
  92.  
  93. function onSelectCSSTreeItem()
  94. {
  95.   if (!gDoOnSelectTree)
  96.     return;
  97.  
  98.   var tree = gDialog.AddCSSAttributeTree;
  99.   if (tree && tree.treeBoxObject.selection.count)
  100.   {
  101.     gDialog.AddCSSAttributeNameInput.value = GetTreeItemAttributeStr(getSelectedItem(tree));
  102.     gDialog.AddCSSAttributeValueInput.value = GetTreeItemValueStr(getSelectedItem(tree));
  103.   }
  104. }
  105.  
  106. function onInputCSSAttributeName()
  107. {
  108.   var attName = TrimString(gDialog.AddCSSAttributeNameInput.value).toLowerCase();
  109.   var newValue = "";
  110.  
  111.   var existingValue = GetAndSelectExistingAttributeValue(attName, "CSSAList");
  112.   if (existingValue)
  113.     newValue = existingValue;
  114.  
  115.   gDialog.AddCSSAttributeValueInput.value = newValue;
  116. }
  117.  
  118. function editCSSAttributeValue(targetCell)
  119. {
  120.   if (IsNotTreeHeader(targetCell))
  121.     gDialog.AddCSSAttributeValueInput.inputField.select();
  122. }
  123.  
  124. function UpdateCSSAttributes()
  125. {
  126.   var CSSAList = document.getElementById("CSSAList");
  127.   var styleString = "";
  128.   for(var i = 0; i < CSSAList.childNodes.length; i++)
  129.   {
  130.     var item = CSSAList.childNodes[i];
  131.     var name = GetTreeItemAttributeStr(item);
  132.     var value = GetTreeItemValueStr(item);
  133.     // this code allows users to be sloppy in typing in values, and enter
  134.     // things like "foo: " and "bar;". This will trim off everything after the
  135.     // respective character.
  136.     if (name.indexOf(":") != -1)
  137.       name = name.substring(0,name.lastIndexOf(":"));
  138.     if (value.indexOf(";") != -1)
  139.       value = value.substring(0,value.lastIndexOf(";"));
  140.     if (i == (CSSAList.childNodes.length - 1))
  141.       styleString += name + ": " + value + ";";   // last property
  142.     else
  143.       styleString += name + ": " + value + "; ";
  144.   }
  145.   if (styleString)
  146.   {
  147.     // Use editor transactions if modifying the element directly in the document
  148.     doRemoveAttribute("style");
  149.     doSetAttribute("style", styleString);  // NOTE BUG 18894!!!
  150.   } 
  151.   else if (gElement.getAttribute("style"))
  152.     doRemoveAttribute("style");
  153. }
  154.  
  155. function RemoveCSSAttribute()
  156. {
  157.   var treechildren = gDialog.AddCSSAttributeTree.lastChild;
  158.  
  159.   // We only allow 1 selected item
  160.   if (gDialog.AddCSSAttributeTree.treeBoxObject.selection.count)
  161.   {
  162.     var item = getSelectedItem(gDialog.AddCSSAttributeTree);
  163.  
  164.     // Remove the item from the tree
  165.     // We always rebuild complete "style" string,
  166.     //  so no list of "removed" items 
  167.     treechildren.removeChild (item);
  168.  
  169.     ClearCSSInputWidgets();
  170.   }
  171. }
  172.  
  173. function SelectCSSTree( index )
  174. {
  175.   gDoOnSelectTree = false;
  176.   try {
  177.     gDialog.AddCSSAttributeTree.selectedIndex = index;
  178.   } catch (e) {}
  179.   gDoOnSelectTree = true;
  180. }
  181.