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 / EdHLineProps.js < prev    next >
Text File  |  2003-06-08  |  7KB  |  220 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 gHLineElement;
  25. var width;
  26. var height;
  27. var align;
  28. var shading;
  29. const gMaxHRSize = 1000; // This is hard-coded in nsHTMLHRElement::StringToAttribute()
  30.  
  31. // dialog initialization code
  32. function Startup()
  33. {
  34.   var editor = GetCurrentEditor();
  35.   if (!editor)
  36.   {
  37.     window.close();
  38.     return;
  39.   }
  40.   try {
  41.     // Get the selected horizontal line
  42.     gHLineElement = editor.getSelectedElement(tagName);
  43.   } catch (e) {}
  44.  
  45.   if (!gHLineElement) {
  46.     // We should never be here if not editing an existing HLine
  47.     window.close();
  48.     return;
  49.   }
  50.   gDialog.heightInput = document.getElementById("height");
  51.   gDialog.widthInput = document.getElementById("width");
  52.   gDialog.leftAlign = document.getElementById("leftAlign");
  53.   gDialog.centerAlign = document.getElementById("centerAlign");
  54.   gDialog.rightAlign = document.getElementById("rightAlign");
  55.   gDialog.alignGroup = gDialog.rightAlign.radioGroup;
  56.   gDialog.shading = document.getElementById("3dShading");
  57.   gDialog.pixelOrPercentMenulist = document.getElementById("pixelOrPercentMenulist");
  58.  
  59.   // Make a copy to use for AdvancedEdit and onSaveDefault
  60.   globalElement = gHLineElement.cloneNode(false);
  61.  
  62.   // Initialize control values based on existing attributes
  63.   InitDialog()
  64.  
  65.   // SET FOCUS TO FIRST CONTROL
  66.   SetTextboxFocus(gDialog.widthInput);
  67.  
  68.   // Resize window
  69.   window.sizeToContent();
  70.  
  71.   SetWindowLocation();
  72. }
  73.  
  74. // Set dialog widgets with attribute data
  75. // We get them from globalElement copy so this can be used
  76. //   by AdvancedEdit(), which is shared by all property dialogs
  77. function InitDialog()
  78. {
  79.   // Just to be confusing, "size" is used instead of height because it does
  80.   // not accept % values, only pixels
  81.   var height = GetHTMLOrCSSStyleValue(globalElement, "size", "height")
  82.   if (/px/.test(height)) {
  83.     height = RegExp.leftContext;
  84.   }
  85.   if(!height) {
  86.     height = 2; //Default value
  87.   }
  88.  
  89.   // We will use "height" here and in UI
  90.   gDialog.heightInput.value = height;
  91.  
  92.   // Get the width attribute of the element, stripping out "%"
  93.   // This sets contents of menulist (adds pixel and percent menuitems elements)
  94.   gDialog.widthInput.value = InitPixelOrPercentMenulist(globalElement, gHLineElement, "width","pixelOrPercentMenulist");
  95.  
  96.   var marginLeft  = GetHTMLOrCSSStyleValue(globalElement, "align", "margin-left").toLowerCase();
  97.   var marginRight = GetHTMLOrCSSStyleValue(globalElement, "align", "margin-right").toLowerCase();
  98.   align = marginLeft + " " + marginRight;
  99.   gDialog.leftAlign.checked   = (align == "left left"     || align == "0px auto");
  100.   gDialog.centerAlign.checked = (align == "center center" || align == "auto auto" || align == " ");
  101.   gDialog.rightAlign.checked  = (align == "right right"   || align == "auto 0px");
  102.  
  103.   if (gDialog.centerAlign.checked) {
  104.     gDialog.alignGroup.selectedItem = gDialog.centerAlign;
  105.   }
  106.   else if (gDialog.rightAlign.checked) {
  107.     gDialog.alignGroup.selectedItem = gDialog.rightAlign;
  108.   }
  109.   else {
  110.     gDialog.alignGroup.selectedItem = gDialog.leftAlign;
  111.   }
  112.  
  113.   gDialog.shading.checked = !globalElement.hasAttribute("noshade");
  114. }
  115.  
  116. function onSaveDefault()
  117. {
  118.   // "false" means set attributes on the globalElement,
  119.   //   not the real element being edited
  120.   if (ValidateData()) {
  121.     var prefs = GetPrefs();
  122.     if (prefs) {
  123.  
  124.       var alignInt;
  125.       if (align == "left") {
  126.         alignInt = 0;
  127.       } else if (align == "right") {
  128.         alignInt = 2;
  129.       } else {
  130.         alignInt = 1;
  131.       }
  132.       prefs.setIntPref("editor.hrule.align", alignInt);
  133.  
  134.       var percent;
  135.       var widthInt;
  136.       var heightInt;
  137.  
  138.       if (width)
  139.       {
  140.         if (/%/.test(width)) {
  141.           percent = true;
  142.           widthInt = Number(RegExp.leftContext);
  143.         } else {
  144.           percent = false;
  145.           widthInt = Number(width);
  146.         }
  147.       }
  148.       else
  149.       {
  150.         percent = true;
  151.         widthInt = Number(100);
  152.       }
  153.  
  154.       heightInt = height ? Number(height) : 2;
  155.  
  156.       prefs.setIntPref("editor.hrule.width", widthInt);
  157.       prefs.setBoolPref("editor.hrule.width_percent", percent);
  158.       prefs.setIntPref("editor.hrule.height", heightInt);
  159.       prefs.setBoolPref("editor.hrule.shading", shading);
  160.  
  161.       // Write the prefs out NOW!
  162.       var prefService = Components.classes["@mozilla.org/preferences-service;1"]
  163.                                   .getService(Components.interfaces.nsIPrefService);
  164.       prefService.savePrefFile(null);
  165.     }
  166.     }
  167. }
  168.  
  169. // Get and validate data from widgets.
  170. // Set attributes on globalElement so they can be accessed by AdvancedEdit()
  171. function ValidateData()
  172. {
  173.   // Height is always pixels
  174.   height = ValidateNumber(gDialog.heightInput, null, 1, gMaxHRSize,
  175.                           globalElement, "size", false);
  176.   if (gValidationError)
  177.     return false;
  178.  
  179.   width = ValidateNumber(gDialog.widthInput, gDialog.pixelOrPercentMenulist, 1, gMaxPixels, 
  180.                          globalElement, "width", false);
  181.   if (gValidationError)
  182.     return false;
  183.  
  184.   align = "left";
  185.   if (gDialog.centerAlign.selected) {
  186.     // Don't write out default attribute
  187.     align = "";
  188.   } else if (gDialog.rightAlign.selected) {
  189.     align = "right";
  190.   }
  191.   if (align)
  192.     globalElement.setAttribute("align", align);
  193.   else
  194.     try {
  195.       GetCurrentEditor().removeAttributeOrEquivalent(globalElement, "align", true);
  196.     } catch (e) {}
  197.  
  198.   if (gDialog.shading.checked) {
  199.     shading = true;
  200.     globalElement.removeAttribute("noshade");
  201.   } else {
  202.     shading = false;
  203.     globalElement.setAttribute("noshade", "noshade");
  204.   }
  205.   return true;
  206. }
  207.  
  208. function onAccept()
  209. {
  210.   if (ValidateData())
  211.   {
  212.     // Copy attributes from the globalElement to the document element
  213.     try {
  214.       GetCurrentEditor().cloneAttributes(gHLineElement, globalElement);
  215.     } catch (e) {}
  216.     return true;
  217.   }
  218.   return false;
  219. }
  220.