!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.
15 *
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1998
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Alec Flett <alecf@netscape.com>
23 * Ben Goodger <ben@netscape.com>
24 * Blake Ross <blakeross@telocity.com>
25 * Joe Hewitt <hewitt@netscape.com>
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
38 *
39 * ***** END LICENSE BLOCK ***** */
40
41 // parameters to gCommonDialogParam.Get() are defined in nsPIPromptService.idl
42 var gCommonDialogParam =
43 window.arguments[0].QueryInterface(Components.interfaces.nsIDialogParamBlock);
44
showControls
45 function showControls()
46 {
47 // This is called before onload fires, so we can't be certain that any elements
48 // in the document have their bindings ready, so don't call any methods/properties
49 // here on xul elements that come from xbl bindings.
50
51 // show the required textboxes and set their initial values
52 var nTextBoxes = gCommonDialogParam.GetInt(3);
53 if (nTextBoxes == 2) {
54 if (gCommonDialogParam.GetInt(4) == 1) {
55 initTextbox("password1", 4, 6, false);
56 initTextbox("password2", 5, 7, false);
57 }
58 else {
59 initTextbox("login", 4, 6, false);
60 initTextbox("password1", 5, 7, false);
61 }
62 } else if (nTextBoxes == 1) {
63 if (gCommonDialogParam.GetInt(4) == 1)
64 initTextbox("password1", -1, 6, true);
65 else
66 initTextbox("login", 4, 6, true);
67 }
68 }
69
setLabelForNode
70 function setLabelForNode(aNode, aLabel, aIsLabelFlag)
71 {
72 // This is for labels which may contain embedded access keys.
73 // If we end in (&X) where X represents the access key, optionally preceded
74 // by spaces and/or followed by the ':' character, store the access key and
75 // remove the access key placeholder + leading spaces from the label.
76 // Otherwise a character preceded by one but not two &s is the access key.
77 // Store it and remove the &.
78
79 // Note that if you change the following code, see the comment of
80 // nsTextBoxFrame::UpdateAccessTitle.
81 var accessKey = null;
82 if (/ *\(\&([^&])\)(:)?$/.test(aLabel)) {
83 aLabel = RegExp.leftContext + RegExp.$2;
84 accessKey = RegExp.$1;
85 } else if (/^(.*[^&])?\&(([^&]).*$)/.test(aLabel)) {
86 aLabel = RegExp.$1 + RegExp.$2;
87 accessKey = RegExp.$3;
88 }
89
90 // && is the magic sequence to embed an & in your label.
91 aLabel = aLabel.replace(/\&\&/g, "&");
92 if (aIsLabelFlag) { // Set text for <label> element
93 aNode.setAttribute("value", aLabel);
94 } else { // Set text for other xul elements
95 aNode.label = aLabel;
96 }
97
98 // XXXjag bug 325251
99 // Need to set this after aNode.setAttribute("value", aLabel);
100 if (accessKey)
101 aNode.accessKey = accessKey;
102 }
103
commonDialogOnLoad
104 function commonDialogOnLoad()
105 {
106 // set the document title
107 //@line 108 "/home/visbrero/mnt/roisin/rev_control/hg/mozilla/toolkit/content/commonDialog.js"
108 setElementText("info.title", gCommonDialogParam.GetString(12), true);
109 //@line 112 "/home/visbrero/mnt/roisin/rev_control/hg/mozilla/toolkit/content/commonDialog.js"
110
111 // set the number of command buttons
112 var nButtons = gCommonDialogParam.GetInt(2);
113 var dialog = document.documentElement;
114 switch (nButtons) {
115 case 1:
116 dialog.getButton("cancel").hidden = true;
117 break;
118 case 4:
119 dialog.getButton("extra2").hidden = false;
120 case 3:
121 dialog.getButton("extra1").hidden = false;
122 }
123
124 // display the main text
125 // XXX the substr(0, 10000) part is a workaround for bug 317334
126 var croppedMessage = gCommonDialogParam.GetString(0).substr(0, 10000);
127 setElementText("info.body", croppedMessage, true);
128
129 setElementText("info.header", gCommonDialogParam.GetString(3), true);
130
131 // set the icon
132 var iconElement = document.getElementById("info.icon");
133 var iconClass = gCommonDialogParam.GetString(2);
134 if (!iconClass)
135 iconClass = "message-icon";
136 iconElement.setAttribute("class", iconElement.getAttribute("class") + " " + iconClass);
137
138 switch (nButtons) {
139 case 4:
140 setLabelForNode(document.documentElement.getButton("extra2"), gCommonDialogParam.GetString(11));
141 // fall through
142 case 3:
143 setLabelForNode(document.documentElement.getButton("extra1"), gCommonDialogParam.GetString(10));
144 // fall through
145 default:
146 case 2:
147 var string = gCommonDialogParam.GetString(9);
148 if (string)
149 setLabelForNode(document.documentElement.getButton("cancel"), string);
150 // fall through
151 case 1:
152 string = gCommonDialogParam.GetString(8);
153 if (string)
154 setLabelForNode(document.documentElement.getButton("accept"), string);
155 break;
156 }
157
158 // set default result to cancelled
159 gCommonDialogParam.SetInt(0, 1);
160
161 // initialize the checkbox
162 setCheckbox(gCommonDialogParam.GetString(1), gCommonDialogParam.GetInt(1));
163
164 if (gCommonDialogParam.GetInt(3) == 0) // If no text fields
165 {
166 var dlgButtons = ['accept', 'cancel', 'extra1', 'extra2'];
167
168 // Set the default button and focus it on non-OS X systems
169 var dButton = dlgButtons[gCommonDialogParam.GetInt(5)];
170 document.documentElement.defaultButton = dButton;
171 //@line 176 "/home/visbrero/mnt/roisin/rev_control/hg/mozilla/toolkit/content/commonDialog.js"
172 }
173
174 if (gCommonDialogParam.GetInt(6) != 0) // delay button enable
175 {
176 var delayInterval = 2000;
177 try {
178 var prefs = Components.classes["@mozilla.org/preferences-service;1"]
179 .getService(Components.interfaces.nsIPrefBranch);
180 delayInterval = prefs.getIntPref("security.dialog_enable_delay");
181 } catch (e) {}
182
183 document.documentElement.getButton("accept").disabled = true;
184 document.documentElement.getButton("extra1").disabled = true;
185 document.documentElement.getButton("extra2").disabled = true;
186
187 setTimeout(commonDialogReenableButtons, delayInterval);
188
189 addEventListener("blur", commonDialogBlur, false);
190 addEventListener("focus", commonDialogFocus, false);
191 }
192
193 getAttention();
194 }
195
196 var gDelayExpired = false;
197 var gBlurred = false;
198
commonDialogBlur
199 function commonDialogBlur(aEvent)
200 {
201 if (aEvent.target != document)
202 return;
203 gBlurred = true;
204 document.documentElement.getButton("accept").disabled = true;
205 document.documentElement.getButton("extra1").disabled = true;
206 document.documentElement.getButton("extra2").disabled = true;
207 }
208
commonDialogFocus
209 function commonDialogFocus(aEvent)
210 {
211 if (aEvent.target != document)
212 return;
213 gBlurred = false;
214 // When refocusing the window, don't enable the buttons unless the countdown
215 // delay has expired.
216 if (gDelayExpired) {
217 var script = "document.documentElement.getButton('accept').disabled = false; ";
218 script += "document.documentElement.getButton('extra1').disabled = false; ";
219 script += "document.documentElement.getButton('extra2').disabled = false;";
220 setTimeout(script, 250);
221 }
222 }
223
commonDialogReenableButtons
224 function commonDialogReenableButtons()
225 {
226 // Don't automatically enable the buttons if we're not in the foreground
227 if (!gBlurred) {
228 document.documentElement.getButton("accept").disabled = false;
229 document.documentElement.getButton("extra1").disabled = false;
230 document.documentElement.getButton("extra2").disabled = false;
231 }
232 gDelayExpired = true;
233 }
234
initTextbox
235 function initTextbox(aName, aLabelIndex, aValueIndex, aAlwaysLabel)
236 {
237 unHideElementById(aName+"Container");
238
239 var label = aLabelIndex < 0 ? "" : gCommonDialogParam.GetString(aLabelIndex);
240 if (label || aAlwaysLabel && !label)
241 setElementText(aName+"Label", label);
242
243 var value = aValueIndex < 0 ? "" : gCommonDialogParam.GetString(aValueIndex);
244 var textbox = document.getElementById(aName + "Textbox");
245 textbox.setAttribute("value", value);
246 }
247
setElementText
248 function setElementText(aElementID, aValue, aChildNodeFlag)
249 {
250 var element = document.getElementById(aElementID);
251 if (!aChildNodeFlag && element) {
252 setLabelForNode(element, aValue, true);
253 } else if (aChildNodeFlag && element) {
254 element.appendChild(document.createTextNode(aValue));
255 }
256 }
257
setCheckbox
258 function setCheckbox(aChkMsg, aChkValue)
259 {
260 if (aChkMsg) {
261 unHideElementById("checkboxContainer");
262
263 var checkboxElement = document.getElementById("checkbox");
264 setLabelForNode(checkboxElement, aChkMsg);
265 checkboxElement.checked = aChkValue > 0;
266 }
267 }
268
unHideElementById
269 function unHideElementById(aElementID)
270 {
271 var element = document.getElementById(aElementID);
272 element.hidden = false;
273 }
274
hideElementById
275 function hideElementById(aElementID)
276 {
277 var element = document.getElementById(aElementID)
278 element.hidden = true;
279 }
280
isVisible
281 function isVisible(aElementId)
282 {
283 return document.getElementById(aElementId).hasAttribute("hidden");
284 }
285
onCheckboxClick
286 function onCheckboxClick(aCheckboxElement)
287 {
288 gCommonDialogParam.SetInt(1, aCheckboxElement.checked);
289 }
290
commonDialogOnAccept
291 function commonDialogOnAccept()
292 {
293 gCommonDialogParam.SetInt(0, 0); // say that ok was pressed
294
295 var numTextBoxes = gCommonDialogParam.GetInt(3);
296 var textboxIsPassword1 = gCommonDialogParam.GetInt(4) == 1;
297
298 if (numTextBoxes >= 1) {
299 var editField1;
300 if (textboxIsPassword1)
301 editField1 = document.getElementById("password1Textbox");
302 else
303 editField1 = document.getElementById("loginTextbox");
304 gCommonDialogParam.SetString(6, editField1.value);
305 }
306
307 if (numTextBoxes == 2) {
308 var editField2;
309 if (textboxIsPassword1)
310 // we had two password fields
311 editField2 = document.getElementById("password2Textbox");
312 else
313 // we only had one password field (and one login field)
314 editField2 = document.getElementById("password1Textbox");
315 gCommonDialogParam.SetString(7, editField2.value);
316 }
317 }
318
319 function commonDialogOnExtra1()
320 {
321 gCommonDialogParam.SetInt(0, 2);
322 window.close();
323 }
324
325 function commonDialogOnExtra2()
326 {
327 gCommonDialogParam.SetInt(0, 3);
328 window.close();
329 }