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 / EdColorProps.js < prev    next >
Text File  |  2003-06-08  |  14KB  |  436 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. /*
  24.  Behavior notes:
  25.  Radio buttons select "UseDefaultColors" vs. "UseCustomColors" modes.
  26.  If any color attribute is set in the body, mode is "Custom Colors",
  27.   even if 1 or more (but not all) are actually null (= "use default")
  28.  When in "Custom Colors" mode, all colors will be set on body tag,
  29.   even if they are just default colors, to assure compatable colors in page.
  30.  User cannot select "use default" for individual colors
  31. */
  32.  
  33. //Cancel() is in EdDialogCommon.js
  34.  
  35. var gBodyElement;
  36. var prefs;
  37. var gBackgroundImage;
  38.  
  39. // Initialize in case we can't get them from prefs???
  40. const defaultTextColor="#000000";
  41. const defaultLinkColor="#000099";
  42. const defaultActiveColor="#000099";
  43. const defaultVisitedColor="#990099";
  44. const defaultBackgroundColor="#FFFFFF";
  45. const styleStr =       "style";
  46. const textStr =        "text";
  47. const linkStr =        "link";
  48. const vlinkStr =       "vlink";
  49. const alinkStr =       "alink";
  50. const bgcolorStr =     "bgcolor";
  51. const backgroundStr =  "background";
  52. const cssColorStr = "color";
  53. const cssBackgroundColorStr = "background-color";
  54. const cssBackgroundImageStr = "background-image";
  55. const colorStyle =     cssColorStr + ": ";
  56. const backColorStyle = cssBackgroundColorStr + ": ";
  57. const backImageStyle = "; " + cssBackgroundImageStr + ": url(";
  58.  
  59. var customTextColor;
  60. var customLinkColor;
  61. var customActiveColor;
  62. var customVisitedColor;
  63. var customBackgroundColor;
  64. var previewBGColor;
  65. var gHaveDocumentUrl = false;
  66.  
  67. // dialog initialization code
  68. function Startup()
  69. {
  70.   var editor = GetCurrentEditor();
  71.   if (!editor)
  72.   {
  73.     window.close();
  74.     return;
  75.   }
  76.  
  77.   gDialog.ColorPreview = document.getElementById("ColorPreview");
  78.   gDialog.NormalText = document.getElementById("NormalText");
  79.   gDialog.LinkText = document.getElementById("LinkText");
  80.   gDialog.ActiveLinkText = document.getElementById("ActiveLinkText");
  81.   gDialog.VisitedLinkText = document.getElementById("VisitedLinkText");
  82.   gDialog.PageColorGroup = document.getElementById("PageColorGroup");
  83.   gDialog.DefaultColorsRadio = document.getElementById("DefaultColorsRadio");
  84.   gDialog.CustomColorsRadio = document.getElementById("CustomColorsRadio");
  85.   gDialog.BackgroundImageInput = document.getElementById("BackgroundImageInput");
  86.  
  87.   try {
  88.     gBodyElement = editor.rootElement;
  89.   } catch (e) {}
  90.  
  91.   if (!gBodyElement)
  92.   {
  93.     dump("Failed to get BODY element!\n");
  94.     window.close();
  95.   }
  96.  
  97.   // Set element we will edit
  98.   globalElement = gBodyElement.cloneNode(false);
  99.  
  100.   // Initialize default colors from browser prefs
  101.   var browserColors = GetDefaultBrowserColors();
  102.   if (browserColors)
  103.   {
  104.     // Use author's browser pref colors passed into dialog
  105.     defaultTextColor = browserColors.TextColor;
  106.     defaultLinkColor = browserColors.LinkColor;
  107.     // Note: Browser doesn't store a value for ActiveLinkColor
  108.     defaultActiveColor = defaultLinkColor;
  109.     defaultVisitedColor =  browserColors.VisitedLinkColor;
  110.     defaultBackgroundColor=  browserColors.BackgroundColor;
  111.   }
  112.  
  113.   // We only need to test for this once per dialog load
  114.   gHaveDocumentUrl = GetDocumentBaseUrl();
  115.  
  116.   InitDialog();
  117.  
  118.   gDialog.PageColorGroup.focus();
  119.  
  120.   SetWindowLocation();
  121. }
  122.  
  123. function InitDialog()
  124. {
  125.   // Get image from document
  126.   gBackgroundImage = GetHTMLOrCSSStyleValue(globalElement, backgroundStr, cssBackgroundImageStr);
  127.   if (/url\((.*)\)/.test( gBackgroundImage ))
  128.     gBackgroundImage = RegExp.$1;
  129.  
  130.   gDialog.BackgroundImageInput.value = gBackgroundImage;
  131.  
  132.   if (gBackgroundImage)
  133.     gDialog.ColorPreview.setAttribute(styleStr, backImageStyle+gBackgroundImage+");");
  134.  
  135.   SetRelativeCheckbox();
  136.  
  137.   customTextColor        = GetHTMLOrCSSStyleValue(globalElement, textStr, cssColorStr);
  138.   customTextColor        = ConvertRGBColorIntoHEXColor(customTextColor);
  139.   customLinkColor        = globalElement.getAttribute(linkStr);
  140.   customActiveColor      = globalElement.getAttribute(alinkStr);
  141.   customVisitedColor     = globalElement.getAttribute(vlinkStr);
  142.   customBackgroundColor  = GetHTMLOrCSSStyleValue(globalElement, bgcolorStr, cssBackgroundColorStr);
  143.   customBackgroundColor  = ConvertRGBColorIntoHEXColor(customBackgroundColor);
  144.  
  145.   var haveCustomColor = 
  146.         customTextColor       ||
  147.         customLinkColor       ||
  148.         customVisitedColor    ||
  149.         customActiveColor     ||
  150.         customBackgroundColor;
  151.  
  152.   // Set default color explicitly for any that are missing
  153.   // PROBLEM: We are using "windowtext" and "window" for the Windows OS
  154.   //   default color values. This works with CSS in preview window,
  155.   //   but we should NOT use these as values for HTML attributes!
  156.  
  157.   if (!customTextColor) customTextColor = defaultTextColor;
  158.   if (!customLinkColor) customLinkColor = defaultLinkColor;
  159.   if (!customActiveColor) customActiveColor = defaultActiveColor;
  160.   if (!customVisitedColor) customVisitedColor = defaultVisitedColor;
  161.   if (!customBackgroundColor) customBackgroundColor = defaultBackgroundColor;
  162.  
  163.   if (haveCustomColor)
  164.   {
  165.     // If any colors are set, then check the "Custom" radio button
  166.     gDialog.PageColorGroup.selectedItem = gDialog.CustomColorsRadio;
  167.     UseCustomColors();
  168.   }
  169.   else 
  170.   {
  171.     gDialog.PageColorGroup.selectedItem = gDialog.DefaultColorsRadio;
  172.     UseDefaultColors();
  173.   }
  174. }
  175.  
  176. function GetColorAndUpdate(ColorWellID)
  177. {
  178.   // Only allow selecting when in custom mode
  179.   if (!gDialog.CustomColorsRadio.selected) return;
  180.  
  181.   var colorWell = document.getElementById(ColorWellID);
  182.   if (!colorWell) return;
  183.  
  184.   // Don't allow a blank color, i.e., using the "default"
  185.   var colorObj = { NoDefault:true, Type:"", TextColor:0, PageColor:0, Cancel:false };
  186.  
  187.   switch( ColorWellID )
  188.   {
  189.     case "textCW":
  190.       colorObj.Type = "Text";
  191.       colorObj.TextColor = customTextColor;
  192.       break;
  193.     case "linkCW":
  194.       colorObj.Type = "Link";
  195.       colorObj.TextColor = customLinkColor;
  196.       break;
  197.     case "activeCW":
  198.       colorObj.Type = "ActiveLink";
  199.       colorObj.TextColor = customActiveColor;
  200.       break;
  201.     case "visitedCW":
  202.       colorObj.Type = "VisitedLink";
  203.       colorObj.TextColor = customVisitedColor;
  204.       break;
  205.     case "backgroundCW":
  206.       colorObj.Type = "Page";
  207.       colorObj.PageColor = customBackgroundColor;
  208.       break;
  209.   }
  210.  
  211.   window.openDialog("chrome://editor/content/EdColorPicker.xul", "_blank", "chrome,close,titlebar,modal", "", colorObj);
  212.  
  213.   // User canceled the dialog
  214.   if (colorObj.Cancel)
  215.     return;
  216.  
  217.   var color = "";
  218.   switch( ColorWellID )
  219.   {
  220.     case "textCW":
  221.       color = customTextColor = colorObj.TextColor;
  222.       break;
  223.     case "linkCW":
  224.       color = customLinkColor = colorObj.TextColor;
  225.       break;
  226.     case "activeCW":
  227.       color = customActiveColor = colorObj.TextColor;
  228.       break;
  229.     case "visitedCW":
  230.       color = customVisitedColor = colorObj.TextColor;
  231.       break;
  232.     case "backgroundCW":
  233.       color = customBackgroundColor = colorObj.BackgroundColor;
  234.       break;
  235.   }
  236.  
  237.   setColorWell(ColorWellID, color); 
  238.   SetColorPreview(ColorWellID, color);
  239. }
  240.  
  241. function SetColorPreview(ColorWellID, color)
  242. {
  243.   switch( ColorWellID )
  244.   {
  245.     case "textCW":
  246.       gDialog.NormalText.setAttribute(styleStr,colorStyle+color);
  247.       break;
  248.     case "linkCW":
  249.       gDialog.LinkText.setAttribute(styleStr,colorStyle+color);
  250.       break;
  251.     case "activeCW":
  252.       gDialog.ActiveLinkText.setAttribute(styleStr,colorStyle+color);
  253.       break;
  254.     case "visitedCW":
  255.       gDialog.VisitedLinkText.setAttribute(styleStr,colorStyle+color);
  256.       break;
  257.     case "backgroundCW":
  258.       // Must combine background color and image style values
  259.       var styleValue = backColorStyle+color;
  260.       if (gBackgroundImage)
  261.         styleValue += ";"+backImageStyle+gBackgroundImage+");";
  262.  
  263.       gDialog.ColorPreview.setAttribute(styleStr,styleValue);
  264.       previewBGColor = color;
  265.       break;
  266.   }
  267. }
  268.  
  269. function UseCustomColors()
  270. {
  271.   SetElementEnabledById("TextButton", true);
  272.   SetElementEnabledById("LinkButton", true);
  273.   SetElementEnabledById("ActiveLinkButton", true);
  274.   SetElementEnabledById("VisitedLinkButton", true);
  275.   SetElementEnabledById("BackgroundButton", true);
  276.   SetElementEnabledById("Text", true);
  277.   SetElementEnabledById("Link", true);
  278.   SetElementEnabledById("Active", true);
  279.   SetElementEnabledById("Visited", true);
  280.   SetElementEnabledById("Background", true);
  281.  
  282.   SetColorPreview("textCW",       customTextColor);
  283.   SetColorPreview("linkCW",       customLinkColor);
  284.   SetColorPreview("activeCW",     customActiveColor);
  285.   SetColorPreview("visitedCW",    customVisitedColor);
  286.   SetColorPreview("backgroundCW", customBackgroundColor);
  287.  
  288.   setColorWell("textCW",          customTextColor);
  289.   setColorWell("linkCW",          customLinkColor);
  290.   setColorWell("activeCW",        customActiveColor);
  291.   setColorWell("visitedCW",       customVisitedColor);
  292.   setColorWell("backgroundCW",    customBackgroundColor);
  293. }
  294.  
  295. function UseDefaultColors()
  296. {
  297.   SetColorPreview("textCW",       defaultTextColor);
  298.   SetColorPreview("linkCW",       defaultLinkColor);
  299.   SetColorPreview("activeCW",     defaultActiveColor);
  300.   SetColorPreview("visitedCW",    defaultVisitedColor);
  301.   SetColorPreview("backgroundCW", defaultBackgroundColor);
  302.  
  303.   // Setting to blank color will remove color from buttons,
  304.   setColorWell("textCW",       "");
  305.   setColorWell("linkCW",       "");
  306.   setColorWell("activeCW",     "");
  307.   setColorWell("visitedCW",    "");
  308.   setColorWell("backgroundCW", "");
  309.  
  310.   // Disable color buttons and labels
  311.   SetElementEnabledById("TextButton", false);
  312.   SetElementEnabledById("LinkButton", false);
  313.   SetElementEnabledById("ActiveLinkButton", false);
  314.   SetElementEnabledById("VisitedLinkButton", false);
  315.   SetElementEnabledById("BackgroundButton", false);
  316.   SetElementEnabledById("Text", false);
  317.   SetElementEnabledById("Link", false);
  318.   SetElementEnabledById("Active", false);
  319.   SetElementEnabledById("Visited", false);
  320.   SetElementEnabledById("Background", false);
  321. }
  322.  
  323. function chooseFile()
  324. {
  325.   // Get a local image file, converted into URL format
  326.   var fileName = GetLocalFileURL("img");
  327.   if (fileName)
  328.   {
  329.     // Always try to relativize local file URLs
  330.     if (gHaveDocumentUrl)
  331.       fileName = MakeRelativeUrl(fileName);
  332.  
  333.     gDialog.BackgroundImageInput.value = fileName;
  334.  
  335.     SetRelativeCheckbox();
  336.  
  337.     ValidateAndPreviewImage(true);
  338.   }
  339.   SetTextboxFocus(gDialog.BackgroundImageInput);
  340. }
  341.  
  342. function ChangeBackgroundImage()
  343. {
  344.   // Don't show error message for image while user is typing
  345.   ValidateAndPreviewImage(false);
  346.   SetRelativeCheckbox();
  347. }
  348.  
  349. function ValidateAndPreviewImage(ShowErrorMessage)
  350. {
  351.   // First make a string with just background color
  352.   var styleValue = backColorStyle+previewBGColor+";";
  353.  
  354.   var retVal = true;
  355.   var image = TrimString(gDialog.BackgroundImageInput.value);
  356.   if (image)
  357.   {
  358.     gBackgroundImage = image;
  359.  
  360.     // Display must use absolute URL if possible
  361.     var displayImage = gHaveDocumentUrl ? MakeAbsoluteUrl(image) : image;
  362.     styleValue += backImageStyle+displayImage+");";
  363.   }
  364.   else gBackgroundImage = null;
  365.  
  366.   // Set style on preview (removes image if not valid)
  367.   gDialog.ColorPreview.setAttribute(styleStr, styleValue);
  368.  
  369.   // Note that an "empty" string is valid
  370.   return retVal;
  371. }
  372.  
  373. function ValidateData()
  374. {
  375.   var editor = GetCurrentEditor();
  376.   try {
  377.     // Colors values are updated as they are picked, no validation necessary
  378.     if (gDialog.DefaultColorsRadio.selected)
  379.     {
  380.       editor.removeAttributeOrEquivalent(globalElement, textStr, true);
  381.       globalElement.removeAttribute(linkStr);
  382.       globalElement.removeAttribute(vlinkStr);
  383.       globalElement.removeAttribute(alinkStr);
  384.       editor.removeAttributeOrEquivalent(globalElement, bgcolorStr, true);
  385.     }
  386.     else
  387.     {
  388.       //Do NOT accept the CSS "WindowsOS" color strings!
  389.       // Problem: We really should try to get the actual color values
  390.       //  from windows, but I don't know how to do that!
  391.       var tmpColor = customTextColor.toLowerCase();
  392.       if (tmpColor != "windowtext")
  393.         globalElement.setAttribute(textStr,    customTextColor);
  394.       else
  395.         editor.removeAttributeOrEquivalent(globalElement, textStr, true);
  396.  
  397.       tmpColor = customBackgroundColor.toLowerCase();
  398.       if (tmpColor != "window")
  399.         globalElement.setAttribute(bgcolorStr, customBackgroundColor);
  400.       else
  401.         editor.removeAttributeOrEquivalent(globalElement, bgcolorStr, true);
  402.  
  403.       globalElement.setAttribute(linkStr,    customLinkColor);
  404.       globalElement.setAttribute(vlinkStr,   customVisitedColor);
  405.       globalElement.setAttribute(alinkStr,   customActiveColor);
  406.     }
  407.  
  408.     if (ValidateAndPreviewImage(true))
  409.     {
  410.       // A valid image may be null for no image
  411.       if (gBackgroundImage)
  412.         globalElement.setAttribute(backgroundStr, gBackgroundImage);
  413.       else
  414.         editor.removeAttributeOrEquivalent(globalElement, backgroundStr, true);
  415.   
  416.       return true;
  417.     }  
  418.   } catch (e) {}
  419.   return false;
  420. }
  421.  
  422. function onAccept()
  423. {
  424.   if (ValidateData())
  425.   {
  426.     // Copy attributes to element we are changing
  427.     try {
  428.       GetCurrentEditor().cloneAttributes(gBodyElement, globalElement);
  429.     } catch (e) {}
  430.  
  431.     SaveWindowLocation();
  432.     return true; // do close the window
  433.   }
  434.   return false;
  435. }
  436.