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 / EdColorPicker.js < prev    next >
Text File  |  2003-06-08  |  9KB  |  324 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.  *   Daniel Glazman (glazman@netscape.com)
  22.  *   Charles Manske (cmanske@netscape.com)
  23.  */
  24.  
  25.  
  26. //Cancel() is in EdDialogCommon.js
  27. var insertNew = true;
  28. var tagname = "TAG NAME"
  29. var gColor = "";
  30. var LastPickedColor = "";
  31. var ColorType = "Text";
  32. var TextType = false;
  33. var HighlightType = false;
  34. var TableOrCell = false;
  35. var LastPickedIsDefault = true;
  36. var NoDefault = false;
  37. var gColorObj;
  38.  
  39. // dialog initialization code
  40. function Startup()
  41.   if (!window.arguments[1])
  42.   {
  43.     dump("EdColorPicker: Missing color object param\n");
  44.     return;
  45.   }
  46.  
  47.   // window.arguments[1] is object to get initial values and return color data
  48.   gColorObj = window.arguments[1];
  49.   gColorObj.Cancel = false;
  50.  
  51.   gDialog.ColorPicker      = document.getElementById("ColorPicker");
  52.   gDialog.ColorInput       = document.getElementById("ColorInput");
  53.   gDialog.LastPickedButton = document.getElementById("LastPickedButton");
  54.   gDialog.LastPickedColor  = document.getElementById("LastPickedColor");
  55.   gDialog.CellOrTableGroup = document.getElementById("CellOrTableGroup");
  56.   gDialog.TableRadio       = document.getElementById("TableRadio");
  57.   gDialog.CellRadio        = document.getElementById("CellRadio");
  58.   gDialog.ColorSwatch      = document.getElementById("ColorPickerSwatch");
  59.   gDialog.Ok               = document.documentElement.getButton("accept");
  60.  
  61.   // The type of color we are setting: 
  62.   //  text: Text, Link, ActiveLink, VisitedLink, 
  63.   //  or background: Page, Table, or Cell
  64.   if (gColorObj.Type)
  65.   {
  66.     ColorType = gColorObj.Type;
  67.     // Get string for dialog title from passed-in type 
  68.     //   (note constraint on editor.properties string name)
  69.     var prefs = GetPrefs();
  70.     var IsCSSPrefChecked = prefs.getBoolPref("editor.use_css");
  71.  
  72.     if (GetCurrentEditor())
  73.     {
  74.       window.title = GetString(ColorType+"Color");
  75.       if (ColorType == "Page" && IsCSSPrefChecked && IsHTMLEditor())
  76.         window.title = GetString("BlockColor");
  77.     }
  78.   }
  79.   if (!window.title)
  80.     window.title = GetString("Color");
  81.  
  82.  
  83.   gDialog.ColorInput.value = "";
  84.   var tmpColor;
  85.   var haveTableRadio = false;
  86.  
  87.   switch (ColorType)
  88.   {
  89.     case "Page":
  90.       tmpColor = gColorObj.PageColor;
  91.       if (tmpColor && tmpColor.toLowerCase() != "window")
  92.         gColor = tmpColor;
  93.       break;
  94.     case "Table":
  95.       if (gColorObj.TableColor)
  96.         gColor = gColorObj.TableColor;
  97.       break;
  98.     case "Cell":
  99.       if (gColorObj.CellColor)
  100.         gColor = gColorObj.CellColor;
  101.       break;
  102.     case "TableOrCell":
  103.       TableOrCell = true;
  104.       document.getElementById("TableOrCellGroup").collapsed = false;
  105.       haveTableRadio = true;
  106.       if (gColorObj.SelectedType == "Cell")
  107.       {
  108.         gColor = gColorObj.CellColor;
  109.         gDialog.CellOrTableGroup.selectedItem = gDialog.CellRadio;
  110.         gDialog.CellRadio.focus();
  111.       }
  112.       else
  113.       {
  114.         gColor = gColorObj.TableColor;
  115.         gDialog.CellOrTableGroup.selectedItem = gDialog.TableRadio;
  116.         gDialog.TableRadio.focus();
  117.       }
  118.       break;
  119.     case "Highlight":
  120.       HighlightType = true;
  121.       if (gColorObj.HighlightColor)
  122.         gColor = gColorObj.HighlightColor;
  123.       break;
  124.     default:
  125.       // Any other type will change some kind of text,
  126.       TextType = true;
  127.       tmpColor = gColorObj.TextColor;
  128.       if (tmpColor && tmpColor.toLowerCase() != "windowtext")
  129.         gColor = gColorObj.TextColor;
  130.       break;
  131.   }
  132.  
  133.   // Set initial color in input field and in the colorpicker
  134.   SetCurrentColor(gColor);
  135.   gDialog.ColorPicker.initColor(gColor);
  136.  
  137.   // Use last-picked colors passed in, or those persistent on dialog
  138.   if (TextType)
  139.   {
  140.     if ( !("LastTextColor" in gColorObj) || !gColorObj.LastTextColor)
  141.       gColorObj.LastTextColor = gDialog.LastPickedColor.getAttribute("LastTextColor");
  142.     LastPickedColor = gColorObj.LastTextColor;
  143.   }
  144.   else if (HighlightType)
  145.   {
  146.     if ( !("LastHighlightColor" in gColorObj) || !gColorObj.LastHighlightColor)
  147.       gColorObj.LastHighlightColor = gDialog.LastPickedColor.getAttribute("LastHighlightColor");
  148.     LastPickedColor = gColorObj.LastHighlightColor;
  149.   }
  150.   else
  151.   {
  152.     if ( !("LastBackgroundColor" in gColorObj) || !gColorObj.LastBackgroundColor)
  153.       gColorObj.LastBackgroundColor = gDialog.LastPickedColor.getAttribute("LastBackgroundColor");
  154.     LastPickedColor = gColorObj.LastBackgroundColor;
  155.   }
  156.   gDialog.LastPickedColor.setAttribute("style","background-color: "+LastPickedColor);
  157.  
  158.   // Set method to detect clicking on OK button
  159.   //  so we don't get fooled by changing "default" behavior
  160.   gDialog.Ok.setAttribute("onclick", "SetDefaultToOk()");
  161.  
  162.   // Make the "Last-picked" the default button
  163.   //  until the user selects a color
  164.   gDialog.Ok.removeAttribute("default");
  165.   gDialog.LastPickedButton.setAttribute("default","true");
  166.  
  167.   // Caller can prevent user from submitting an empty, i.e., default color
  168.   NoDefault = gColorObj.NoDefault;
  169.   if (NoDefault)
  170.   {
  171.     // Hide the "Default button -- user must pick a color
  172.     document.getElementById("DefaultColorButton").collapsed = true;
  173.   }
  174.  
  175.   // Set focus to colorpicker if not set to table radio buttons above
  176.   if (!haveTableRadio)
  177.     gDialog.ColorPicker.focus();
  178.  
  179.   SetWindowLocation();
  180. }
  181.  
  182. function ChangePalette(palette)
  183. {
  184.   gDialog.ColorPicker.setAttribute("palettename", palette);
  185.   window.sizeToContent();
  186. }
  187.  
  188. function SelectColor()
  189. {
  190.   var color = gDialog.ColorPicker.color;
  191.   if (color)
  192.     SetCurrentColor(color);
  193. }
  194.  
  195. function RemoveColor()
  196. {
  197.   SetCurrentColor("");
  198.   gDialog.ColorInput.focus();
  199.   SetDefaultToOk();
  200. }
  201.  
  202. function SelectColorByKeypress(aEvent)
  203. {
  204.   if (aEvent.charCode == aEvent.DOM_VK_SPACE)
  205.   {
  206.     SelectColor();
  207.     SetDefaultToOk();
  208.   }
  209. }
  210.  
  211. function SelectLastPickedColor()
  212. {
  213.   SetCurrentColor(LastPickedColor);
  214.   if ( onAccept() )
  215.     //window.close();
  216.     return true;
  217.  
  218.   return false;
  219. }
  220.  
  221. function SetCurrentColor(color)
  222. {
  223.   // TODO: Validate color?
  224.   if(!color) color = "";
  225.   gColor = TrimString(color).toLowerCase();
  226.   if (gColor == "mixed")
  227.     gColor = "";
  228.   gDialog.ColorInput.value = gColor;
  229.   SetColorSwatch();
  230. }
  231.  
  232. function SetColorSwatch()
  233. {
  234.   // TODO: DON'T ALLOW SPACES?
  235.   var color = TrimString(gDialog.ColorInput.value);
  236.   if (color)
  237.   {
  238.     gDialog.ColorSwatch.setAttribute("style",("background-color:"+color));
  239.     gDialog.ColorSwatch.removeAttribute("default");
  240.   }
  241.   else
  242.   {
  243.     gDialog.ColorSwatch.setAttribute("style",("background-color:inherit"));
  244.     gDialog.ColorSwatch.setAttribute("default","true");
  245.   }
  246. }
  247.  
  248. function SetDefaultToOk()
  249. {
  250.   gDialog.LastPickedButton.removeAttribute("default");
  251.   gDialog.Ok.setAttribute("default","true");
  252.   LastPickedIsDefault = false;
  253. }
  254.  
  255. function ValidateData()
  256. {
  257.   if (LastPickedIsDefault)
  258.     gColor = LastPickedColor;
  259.   else
  260.     gColor = gDialog.ColorInput.value;
  261.   
  262.   gColor = TrimString(gColor).toLowerCase();
  263.  
  264.   // TODO: Validate the color string!
  265.  
  266.   if (NoDefault && !gColor)
  267.   {
  268.     ShowInputErrorMessage(GetString("NoColorError"));
  269.     SetTextboxFocus(gDialog.ColorInput);
  270.     return false;   
  271.   }
  272.   return true;
  273. }
  274.  
  275. function onAccept()
  276. {
  277.   if (!ValidateData())
  278.     return false;
  279.  
  280.   // Set return values and save in persistent color attributes
  281.   if (TextType)
  282.   {
  283.     gColorObj.TextColor = gColor;
  284.     if (gColor.length > 0)
  285.     {
  286.       gDialog.LastPickedColor.setAttribute("LastTextColor", gColor);
  287.       gColorObj.LastTextColor = gColor;
  288.     }
  289.   }
  290.   else if (HighlightType)
  291.   {
  292.     gColorObj.HighlightColor = gColor;
  293.     if (gColor.length > 0)
  294.     {
  295.       gDialog.LastPickedColor.setAttribute("LastHighlightColor", gColor);
  296.       gColorObj.LastHighlightColor = gColor;
  297.     }
  298.   }
  299.   else
  300.   {
  301.     gColorObj.BackgroundColor = gColor;
  302.     if (gColor.length > 0)
  303.     {
  304.       gDialog.LastPickedColor.setAttribute("LastBackgroundColor", gColor);
  305.       gColorObj.LastBackgroundColor = gColor;
  306.     }
  307.     // If table or cell requested, tell caller which element to set on
  308.     if (TableOrCell && gDialog.TableRadio.selected)
  309.       gColorObj.Type = "Table";
  310.   }
  311.   SaveWindowLocation();
  312.  
  313.   return true; // do close the window
  314. }
  315.  
  316. function onCancelColor()
  317. {
  318.   // Tells caller that user canceled
  319.   gColorObj.Cancel = true;
  320.   SaveWindowLocation();
  321.   return true;
  322. }
  323.