home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / comm.jar / content / editor / EdHLineProps.js < prev    next >
Encoding:
JavaScript  |  2001-08-10  |  5.9 KB  |  206 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.  */
  22.  
  23. var tagName = "hr";
  24. var hLineElement;
  25. var width;
  26. var height;
  27. var align;
  28. var shading;
  29. var dialog;
  30.  
  31. // dialog initialization code
  32. function Startup()
  33. {
  34.   if (!InitEditorShell())
  35.     return;
  36.  
  37.   doSetOKCancel(onOK, onCancel);
  38.  
  39.   // Get the selected horizontal line
  40.   hLineElement = editorShell.GetSelectedElement(tagName);
  41.  
  42.   if (!hLineElement) {
  43.     // We should never be here if not editing an existing HLine
  44.     window.close();
  45.     return;
  46.   }
  47.   // Create dialog object to store controls for easy access
  48.   dialog = new Object;
  49.   dialog.heightInput = document.getElementById("height");
  50.   dialog.widthInput = document.getElementById("width");
  51.   dialog.leftAlign = document.getElementById("leftAlign");
  52.   dialog.centerAlign = document.getElementById("centerAlign");
  53.   dialog.rightAlign = document.getElementById("rightAlign");
  54.   dialog.shading = document.getElementById("3dShading");
  55.   dialog.pixelOrPercentMenulist = document.getElementById("pixelOrPercentMenulist");
  56.  
  57.   // Make a copy to use for AdvancedEdit and onSaveDefault
  58.   globalElement = hLineElement.cloneNode(false);
  59.  
  60.   // Initialize control values based on existing attributes
  61.   InitDialog()
  62.  
  63.   // SET FOCUS TO FIRST CONTROL
  64.   SetTextboxFocus(dialog.widthInput);
  65.  
  66.   // Resize window
  67.   window.sizeToContent();
  68.  
  69.   SetWindowLocation();
  70. }
  71.  
  72. // Set dialog widgets with attribute data
  73. // We get them from globalElement copy so this can be used
  74. //   by AdvancedEdit(), which is shared by all property dialogs
  75. function InitDialog()
  76. {
  77.   // Just to be confusing, "size" is used instead of height
  78.   var height = globalElement.getAttribute("size");
  79.   if(!height) {
  80.     height = 2; //Default value
  81.   }
  82.  
  83.   // We will use "height" here and in UI
  84.   dialog.heightInput.value = height;
  85.  
  86.   // Get the width attribute of the element, stripping out "%"
  87.   // This sets contents of menulist (adds pixel and percent menuitems elements)
  88.   dialog.widthInput.value = InitPixelOrPercentMenulist(globalElement, hLineElement, "width","pixelOrPercentMenulist");
  89.  
  90.   align = globalElement.getAttribute("align").toLowerCase();
  91.  
  92.   dialog.centerAlign.checked = (align == "center" || !align);
  93.   dialog.rightAlign.checked  = (align == "right");
  94.   dialog.leftAlign.checked   = (align == "left");
  95.  
  96.   // This is tricky! Since the "noshade" attribute doesn't always have a value,
  97.   //  we can't use getAttribute to figure out if it's set!
  98.   // This gets the attribute NODE from the attributes NamedNodeMap
  99.   if (globalElement.attributes.getNamedItem("noshade"))
  100.     dialog.shading.checked = false;
  101.   else
  102.     dialog.shading.checked = true;
  103.  
  104. }
  105.  
  106. function onSaveDefault()
  107. {
  108.   // "false" means set attributes on the globalElement,
  109.   //   not the real element being edited
  110.   if (ValidateData()) {
  111.     var prefs = GetPrefs();
  112.     if (prefs) {
  113.  
  114.       var alignInt;
  115.       if (align == "left") {
  116.         alignInt = 0;
  117.       } else if (align == "right") {
  118.         alignInt = 2;
  119.       } else {
  120.         alignInt = 1;
  121.       }
  122.       prefs.SetIntPref("editor.hrule.align", alignInt);
  123.  
  124.       var percentIndex = width.search(/%/);
  125.       var percent;
  126.       var widthInt;
  127.       var heightInt;
  128.  
  129.       if (width)
  130.       {
  131.         if (percentIndex > 0) {
  132.           percent = true;
  133.           widthInt = Number(width.substr(0, percentIndex));
  134.         } else {
  135.           percent = false;
  136.           widthInt = Number(width);
  137.         }
  138.       }
  139.       else
  140.       {
  141.         percent = true;
  142.         widthInt = Number(100);
  143.       }
  144.  
  145.       heightInt = height ? Number(height) : 2;
  146.  
  147.       prefs.SetIntPref("editor.hrule.width", widthInt);
  148.       prefs.SetBoolPref("editor.hrule.width_percent", percent);
  149.       prefs.SetIntPref("editor.hrule.height", heightInt);
  150.       prefs.SetBoolPref("editor.hrule.shading", shading);
  151.  
  152.       // Write the prefs out NOW!
  153.       prefs.savePrefFile(null);
  154.     }
  155.     }
  156. }
  157.  
  158. // Get and validate data from widgets.
  159. // Set attributes on globalElement so they can be accessed by AdvancedEdit()
  160. function ValidateData()
  161. {
  162.   // Height is always pixels
  163.   height = ValidateNumber(dialog.heightInput, null, 1, maxPixels,
  164.                           globalElement, "size", false);
  165.   if (gValidationError)
  166.     return false;
  167.  
  168.   width = ValidateNumber(dialog.widthInput, dialog.pixelOrPercentMenulist, 1, maxPixels, 
  169.                          globalElement, "width", false);
  170.   if (gValidationError)
  171.     return false;
  172.  
  173.   align = "left";
  174.   if (dialog.centerAlign.checked) {
  175.     // Don't write out default attribute
  176.     align = "";
  177.   } else if (dialog.rightAlign.checked) {
  178.     align = "right";
  179.   }
  180.   if (align)
  181.     globalElement.setAttribute("align", align);
  182.   else
  183.     globalElement.removeAttribute("align");
  184.  
  185.   if (dialog.shading.checked) {
  186.     shading = true;
  187.     globalElement.removeAttribute("noshade");
  188.   } else {
  189.     shading = false;
  190.     globalElement.setAttribute("noshade", "noshade");
  191.   }
  192.   return true;
  193. }
  194.  
  195. function onOK()
  196. {
  197.   if (ValidateData())
  198.   {
  199.     // Copy attributes from the globalElement to the document element
  200.     editorShell.CloneAttributes(hLineElement, globalElement);
  201.     SaveWindowLocation();
  202.     return true;
  203.   }
  204.   return false;
  205. }
  206.