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