backout the latest landing b=463209
[wine-gecko.git] / toolkit / content / commonDialog.js
blob2f3cfa5304a0c89a89ed1c6f78cf225dcd80a7f6
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
12 * License.
14 * The Original Code is Mozilla Communicator client code.
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.
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>
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.
39 * ***** END LICENSE BLOCK ***** */
41 // parameters to gCommonDialogParam.Get() are defined in nsPIPromptService.idl
42 var gCommonDialogParam =
43 window.arguments[0].QueryInterface(Components.interfaces.nsIDialogParamBlock);
45 function showControls()
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.
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);
58 else {
59 initTextbox("login", 4, 6, false);
60 initTextbox("password1", 5, 7, false);
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);
70 function setLabelForNode(aNode, aLabel, aIsLabelFlag)
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 &.
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;
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;
98 // XXXjag bug 325251
99 // Need to set this after aNode.setAttribute("value", aLabel);
100 if (accessKey)
101 aNode.accessKey = accessKey;
104 function commonDialogOnLoad()
106 // set the document title
107 #ifdef XP_MACOSX
108 setElementText("info.title", gCommonDialogParam.GetString(12), true);
109 #else
110 document.title = gCommonDialogParam.GetString(12);
111 #endif
113 // set the number of command buttons
114 var nButtons = gCommonDialogParam.GetInt(2);
115 var dialog = document.documentElement;
116 switch (nButtons) {
117 case 1:
118 dialog.getButton("cancel").hidden = true;
119 break;
120 case 4:
121 dialog.getButton("extra2").hidden = false;
122 case 3:
123 dialog.getButton("extra1").hidden = false;
126 // display the main text
127 // XXX the substr(0, 10000) part is a workaround for bug 317334
128 var croppedMessage = gCommonDialogParam.GetString(0).substr(0, 10000);
129 setElementText("info.body", croppedMessage, true);
131 setElementText("info.header", gCommonDialogParam.GetString(3), true);
133 // set the icon
134 var iconElement = document.getElementById("info.icon");
135 var iconClass = gCommonDialogParam.GetString(2);
136 if (!iconClass)
137 iconClass = "message-icon";
138 iconElement.setAttribute("class", iconElement.getAttribute("class") + " " + iconClass);
140 switch (nButtons) {
141 case 4:
142 setLabelForNode(document.documentElement.getButton("extra2"), gCommonDialogParam.GetString(11));
143 // fall through
144 case 3:
145 setLabelForNode(document.documentElement.getButton("extra1"), gCommonDialogParam.GetString(10));
146 // fall through
147 default:
148 case 2:
149 var string = gCommonDialogParam.GetString(9);
150 if (string)
151 setLabelForNode(document.documentElement.getButton("cancel"), string);
152 // fall through
153 case 1:
154 string = gCommonDialogParam.GetString(8);
155 if (string)
156 setLabelForNode(document.documentElement.getButton("accept"), string);
157 break;
160 // set default result to cancelled
161 gCommonDialogParam.SetInt(0, 1);
163 // initialize the checkbox
164 setCheckbox(gCommonDialogParam.GetString(1), gCommonDialogParam.GetInt(1));
166 if (gCommonDialogParam.GetInt(3) == 0) // If no text fields
168 var dlgButtons = ['accept', 'cancel', 'extra1', 'extra2'];
170 // Set the default button and focus it on non-OS X systems
171 var dButton = dlgButtons[gCommonDialogParam.GetInt(5)];
172 document.documentElement.defaultButton = dButton;
173 #ifndef XP_MACOSX
174 document.documentElement.getButton(dButton).focus();
175 #endif
178 if (gCommonDialogParam.GetInt(6) != 0) // delay button enable
180 var delayInterval = 2000;
181 try {
182 var prefs = Components.classes["@mozilla.org/preferences-service;1"]
183 .getService(Components.interfaces.nsIPrefBranch);
184 delayInterval = prefs.getIntPref("security.dialog_enable_delay");
185 } catch (e) {}
187 document.documentElement.getButton("accept").disabled = true;
188 document.documentElement.getButton("extra1").disabled = true;
189 document.documentElement.getButton("extra2").disabled = true;
191 setTimeout(commonDialogReenableButtons, delayInterval);
193 addEventListener("blur", commonDialogBlur, false);
194 addEventListener("focus", commonDialogFocus, false);
197 getAttention();
200 var gDelayExpired = false;
201 var gBlurred = false;
203 function commonDialogBlur(aEvent)
205 if (aEvent.target != document)
206 return;
207 gBlurred = true;
208 document.documentElement.getButton("accept").disabled = true;
209 document.documentElement.getButton("extra1").disabled = true;
210 document.documentElement.getButton("extra2").disabled = true;
213 function commonDialogFocus(aEvent)
215 if (aEvent.target != document)
216 return;
217 gBlurred = false;
218 // When refocusing the window, don't enable the buttons unless the countdown
219 // delay has expired.
220 if (gDelayExpired) {
221 var script = "document.documentElement.getButton('accept').disabled = false; ";
222 script += "document.documentElement.getButton('extra1').disabled = false; ";
223 script += "document.documentElement.getButton('extra2').disabled = false;";
224 setTimeout(script, 250);
228 function commonDialogReenableButtons()
230 // Don't automatically enable the buttons if we're not in the foreground
231 if (!gBlurred) {
232 document.documentElement.getButton("accept").disabled = false;
233 document.documentElement.getButton("extra1").disabled = false;
234 document.documentElement.getButton("extra2").disabled = false;
236 gDelayExpired = true;
239 function initTextbox(aName, aLabelIndex, aValueIndex, aAlwaysLabel)
241 unHideElementById(aName+"Container");
243 var label = aLabelIndex < 0 ? "" : gCommonDialogParam.GetString(aLabelIndex);
244 if (label || aAlwaysLabel && !label)
245 setElementText(aName+"Label", label);
247 var value = aValueIndex < 0 ? "" : gCommonDialogParam.GetString(aValueIndex);
248 var textbox = document.getElementById(aName + "Textbox");
249 textbox.setAttribute("value", value);
252 function setElementText(aElementID, aValue, aChildNodeFlag)
254 var element = document.getElementById(aElementID);
255 if (!aChildNodeFlag && element) {
256 setLabelForNode(element, aValue, true);
257 } else if (aChildNodeFlag && element) {
258 element.appendChild(document.createTextNode(aValue));
262 function setCheckbox(aChkMsg, aChkValue)
264 if (aChkMsg) {
265 unHideElementById("checkboxContainer");
267 var checkboxElement = document.getElementById("checkbox");
268 setLabelForNode(checkboxElement, aChkMsg);
269 checkboxElement.checked = aChkValue > 0;
273 function unHideElementById(aElementID)
275 var element = document.getElementById(aElementID);
276 element.hidden = false;
279 function hideElementById(aElementID)
281 var element = document.getElementById(aElementID)
282 element.hidden = true;
285 function isVisible(aElementId)
287 return document.getElementById(aElementId).hasAttribute("hidden");
290 function onCheckboxClick(aCheckboxElement)
292 gCommonDialogParam.SetInt(1, aCheckboxElement.checked);
295 function commonDialogOnAccept()
297 gCommonDialogParam.SetInt(0, 0); // say that ok was pressed
299 var numTextBoxes = gCommonDialogParam.GetInt(3);
300 var textboxIsPassword1 = gCommonDialogParam.GetInt(4) == 1;
302 if (numTextBoxes >= 1) {
303 var editField1;
304 if (textboxIsPassword1)
305 editField1 = document.getElementById("password1Textbox");
306 else
307 editField1 = document.getElementById("loginTextbox");
308 gCommonDialogParam.SetString(6, editField1.value);
311 if (numTextBoxes == 2) {
312 var editField2;
313 if (textboxIsPassword1)
314 // we had two password fields
315 editField2 = document.getElementById("password2Textbox");
316 else
317 // we only had one password field (and one login field)
318 editField2 = document.getElementById("password1Textbox");
319 gCommonDialogParam.SetString(7, editField2.value);
323 function commonDialogOnExtra1()
325 gCommonDialogParam.SetInt(0, 2);
326 window.close();
329 function commonDialogOnExtra2()
331 gCommonDialogParam.SetInt(0, 3);
332 window.close();