1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Mozilla.org.
16 * The Initial Developer of the Original Code is
18 * Portions created by the Initial Developer are Copyright (C) 2003
19 * the Initial Developer. All Rights Reserved.
22 * Neil Marshall (neil.marshall@sympatico.ca), Original author
23 * Daniel Glazman (glazman@disruptive-innovations.com), on behalf of Linspire Inc.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 var satSlider = new objColour();
41 var hexChars = "0123456789ABCDEF";
42 var selectedColour = 0;
43 var mouseDown = false;
44 var eventInitiator = null;
45 var mouseX, mouseY, offsetLeft, offsetTop;
48 var LastPickedColor = "";
49 var ColorType = "Text";
51 var HighlightType = false;
52 var TableOrCell = false;
53 var LastPickedIsDefault = false;
54 var NoDefault = false;
60 if (!window.arguments[1])
62 dump("colourPicker: Missing color object param\n");
66 // window.arguments[1] is object to get initial values and return color data
67 gColorObj = window.arguments[1];
68 gColorObj.Cancel = false;
70 gDialog.red = document.getElementById("red");
71 gDialog.blue = document.getElementById("blue");
72 gDialog.green = document.getElementById("green");
73 gDialog.hue = document.getElementById("hue");
74 gDialog.saturation = document.getElementById("saturation");
75 gDialog.brightness = document.getElementById("brightness");
76 gDialog.hexColour = document.getElementById("hexColour");
77 gDialog.nameColour = document.getElementById("nameColour");
79 gDialog.redLabel = document.getElementById("redLabel");
80 gDialog.blueLabel = document.getElementById("blueLabel");
81 gDialog.greenLabel = document.getElementById("greenLabel");
82 gDialog.hueLabel = document.getElementById("hueLabel");
83 gDialog.saturationLabel = document.getElementById("saturationLabel");
84 gDialog.brightnessLabel = document.getElementById("brightnessLabel");
85 gDialog.hexColourLabel = document.getElementById("hexColourLabel");
86 gDialog.nameColourLabel = document.getElementById("nameColourLabel");
88 gDialog.hueAndSaturationImg = document.getElementById("hueAndSaturationImg");
89 gDialog.hueAndSaturationCrosshair = document.getElementById("hueAndSaturationCrosshair");
90 gDialog.brightnessImg = document.getElementById("brightnessImg");
91 gDialog.swatch = document.getElementById("swatch");
92 gDialog.brightnessArrow = document.getElementById("brightnessArrow");
93 gDialog.colorpicker = document.getElementById("colorpicker");
94 gDialog.Ok = document.documentElement.getButton("accept");
95 gDialog.transparencyCheckbox = document.getElementById("transparencyCheckbox");
97 gDialog.CellOrTableGroup = document.getElementById("CellOrTableGroup");
98 gDialog.TableRadio = document.getElementById("TableRadio");
99 gDialog.CellRadio = document.getElementById("CellRadio");
100 gDialog.LastPickedColor = document.getElementById("LastPickedColor");
102 // The type of color we are setting:
103 // text: Text, Link, ActiveLink, VisitedLink,
104 // or background: Page, Table, or Cell
105 var prefs = GetPrefs();
108 ColorType = gColorObj.Type;
109 // Get string for dialog title from passed-in type
110 // (note constraint on editor.properties string name)
111 var IsCSSPrefChecked = prefs.getBoolPref("editor.use_css");
113 if (GetCurrentEditor())
115 window.title = GetString(ColorType+"Color");
116 if (ColorType == "Page" && IsCSSPrefChecked && IsHTMLEditor())
117 window.title = GetString("BlockColor");
121 window.title = GetString("Color");
123 colours = new objColour();
125 makeDraggable(gDialog.hueAndSaturationImg);
126 makeDraggable(gDialog.hueAndSaturationCrosshair);
127 makeDraggable(gDialog.brightnessImg);
129 gDialog.hexColour.value = "";
131 var haveTableRadio = false;
132 var showTransparencyCheckbox = false;
137 tmpColor = gColorObj.PageColor;
138 if (tmpColor && tmpColor.toLowerCase() != "window")
140 showTransparencyCheckbox = true;
143 if (gColorObj.TableColor)
144 gColor = gColorObj.TableColor;
147 if (gColorObj.CellColor)
148 gColor = gColorObj.CellColor;
152 document.getElementById("TableOrCellGroup").collapsed = false;
153 showTransparencyCheckbox = true;
154 haveTableRadio = true;
155 if (gColorObj.SelectedType == "Cell")
157 gColor = gColorObj.CellColor;
158 gDialog.CellOrTableGroup.selectedItem = gDialog.CellRadio;
159 gDialog.CellRadio.focus();
163 gColor = gColorObj.TableColor;
164 gDialog.CellOrTableGroup.selectedItem = gDialog.TableRadio;
165 gDialog.TableRadio.focus();
169 HighlightType = true;
170 if (gColorObj.HighlightColor)
171 gColor = gColorObj.HighlightColor;
172 showTransparencyCheckbox = true;
175 // Any other type will change some kind of text,
177 tmpColor = gColorObj.TextColor;
178 if (tmpColor && tmpColor.toLowerCase() != "windowtext")
179 gColor = gColorObj.TextColor;
185 var useCustomColors = prefs.getBoolPref("editor.use_custom_colors");
191 gColor = prefs.getCharPref("editor.background_color");
194 gColor = prefs.getCharPref("browser.display.background_color");
200 gColor = "transparent";
201 showTransparencyCheckbox = true;
205 gColor = prefs.getCharPref("editor.text_color");
208 gColor = prefs.getCharPref("browser.display.foreground_color");
214 if (!showTransparencyCheckbox)
215 gDialog.transparencyCheckbox.setAttribute("hidden", true);
217 // Use last-picked colors passed in, or those persistent on dialog
220 if ( !("LastTextColor" in gColorObj) || !gColorObj.LastTextColor)
221 gColorObj.LastTextColor = gDialog.LastPickedColor.getAttribute("LastTextColor");
222 LastPickedColor = gColorObj.LastTextColor;
224 else if (HighlightType)
226 if ( !("LastHighlightColor" in gColorObj) || !gColorObj.LastHighlightColor)
227 gColorObj.LastHighlightColor = gDialog.LastPickedColor.getAttribute("LastHighlightColor");
228 LastPickedColor = gColorObj.LastHighlightColor;
232 if ( !("LastBackgroundColor" in gColorObj) || !gColorObj.LastBackgroundColor)
233 gColorObj.LastBackgroundColor = gDialog.LastPickedColor.getAttribute("LastBackgroundColor");
234 LastPickedColor = gColorObj.LastBackgroundColor;
236 gDialog.LastPickedColor.setAttribute("style","background-color: "+LastPickedColor);
238 // Set initial color in input field and in the colorpicker
239 SetCurrentColor(gColor);
240 if (!showTransparencyCheckbox)
241 gDialog.colorpicker.initColor(gColor);
243 // Caller can prevent user from submitting an empty, i.e., default color
244 NoDefault = gColorObj.NoDefault;
247 // Hide the "Default button -- user must pick a color
248 var defaultColorButton = document.getElementById("DefaultColorButton");
249 if (defaultColorButton)
250 defaultColorButton.collapsed = true;
253 // Set focus to colorpicker if not set to table radio buttons above
255 gDialog.colorpicker.focus();
260 function ValidateData()
262 if (gDialog.transparencyCheckbox.checked)
263 gColor = "transparent";
264 else if (LastPickedIsDefault)
265 gColor = LastPickedColor;
267 gColor = gDialog.hexColour.value;
269 if (ColorType == "TableOrCell" &&
270 gColor == "transparent")
272 gColor = TrimString(gColor).toLowerCase();
274 // TODO: Validate the color string!
276 if (NoDefault && !gColor)
278 ShowInputErrorMessage(GetString("NoColorError"));
279 SetTextboxFocus(gDialog.hexColour);
290 // Set return values and save in persistent color attributes
293 gColorObj.TextColor = gColor;
294 if (gColor.length > 0)
296 gDialog.LastPickedColor.setAttribute("LastTextColor", gColor);
297 gColorObj.LastTextColor = gColor;
300 else if (HighlightType)
302 gColorObj.HighlightColor = gColor;
303 if (gColor.length > 0)
305 gDialog.LastPickedColor.setAttribute("LastHighlightColor", gColor);
306 gColorObj.LastHighlightColor = gColor;
311 gColorObj.BackgroundColor = gColor;
312 if (gColor.length > 0)
314 gDialog.LastPickedColor.setAttribute("LastBackgroundColor", gColor);
315 gColorObj.LastBackgroundColor = gColor;
317 // If table or cell requested, tell caller which element to set on
318 if (TableOrCell && gDialog.TableRadio.selected)
319 gColorObj.Type = "Table";
321 SaveWindowLocation();
323 return true; // do close the window