1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
// This is loaded into all XUL windows. Wrap in a block to prevent
// leaking to window scope.
{
const { Services } = ChromeUtils.import(
"resource://gre/modules/Services.jsm"
);
const { AppConstants } = ChromeUtils.import(
"resource://gre/modules/AppConstants.jsm"
);
class MozDialog extends MozXULElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
document.addEventListener(
"keypress",
event => {
if (event.keyCode == KeyEvent.DOM_VK_RETURN) {
this._hitEnter(event);
} else if (
event.keyCode == KeyEvent.DOM_VK_ESCAPE &&
!event.defaultPrevented
) {
this.cancelDialog();
}
},
{ mozSystemGroup: true }
);
if (AppConstants.platform == "macosx") {
document.addEventListener(
"keypress",
event => {
if (event.key == "." && event.metaKey) {
this.cancelDialog();
}
},
true
);
} else {
this.addEventListener("focus", this, true);
this.shadowRoot.addEventListener("focus", this, true);
}
// listen for when window is closed via native close buttons
window.addEventListener("close", event => {
if (!this.cancelDialog()) {
event.preventDefault();
}
});
// for things that we need to initialize after onload fires
window.addEventListener("load", event => this.postLoadInit(event));
}
static get observedAttributes() {
return super.observedAttributes.concat("subdialog");
}
attributeChangedCallback(name, oldValue, newValue) {
if (name == "subdialog") {
console.assert(
newValue,
`Turning off subdialog style is not supported`
);
if (this.isConnectedAndReady && !oldValue && newValue) {
this.shadowRoot.appendChild(
MozXULElement.parseXULToFragment(this.inContentStyle)
);
}
return;
}
super.attributeChangedCallback(name, oldValue, newValue);
}
static get inheritedAttributes() {
return {
".dialog-button-box":
"pack=buttonpack,align=buttonalign,dir=buttondir,orient=buttonorient",
"[dlgtype='accept']": "disabled=buttondisabledaccept",
};
}
get inContentStyle() {
return `
<html:link rel="stylesheet" href="chrome://global/skin/in-content/common.css" />
`;
}
get _markup() {
let buttons = AppConstants.XP_UNIX
? `
<hbox class="dialog-button-box">
<button dlgtype="disclosure" hidden="true"/>
<button dlgtype="help" hidden="true"/>
<button dlgtype="extra2" hidden="true"/>
<button dlgtype="extra1" hidden="true"/>
<spacer class="button-spacer" part="button-spacer" flex="1"/>
<button dlgtype="cancel"/>
<button dlgtype="accept"/>
</hbox>`
: `
<hbox class="dialog-button-box" pack="end">
<button dlgtype="extra2" hidden="true"/>
<spacer class="button-spacer" part="button-spacer" flex="1" hidden="true"/>
<button dlgtype="accept"/>
<button dlgtype="extra1" hidden="true"/>
<button dlgtype="cancel"/>
<button dlgtype="help" hidden="true"/>
<button dlgtype="disclosure" hidden="true"/>
</hbox>`;
let key =
AppConstants.platform == "macosx"
? `<key phase="capturing"
oncommand="document.querySelector('dialog').openHelp(event)"
key="&openHelpMac.commandkey;" modifiers="accel"/>`
: `<key phase="capturing"
oncommand="document.querySelector('dialog').openHelp(event)"
keycode="&openHelp.commandkey;"/>`;
return `
<html:link rel="stylesheet" href="chrome://global/skin/button.css"/>
<html:link rel="stylesheet" href="chrome://global/skin/dialog.css"/>
${this.hasAttribute("subdialog") ? this.inContentStyle : ""}
<vbox class="box-inherit dialog-content-box" part="content-box" flex="1">
<html:slot></html:slot>
</vbox>
${buttons}
<keyset>${key}</keyset>`;
}
connectedCallback() {
if (this.delayConnectedCallback()) {
return;
}
document.documentElement.setAttribute("role", "dialog");
this.shadowRoot.textContent = "";
this.shadowRoot.appendChild(
MozXULElement.parseXULToFragment(this._markup, [
"chrome://global/locale/globalKeys.dtd",
])
);
this.initializeAttributeInheritance();
/**
* Gets populated by elements that are passed to document.l10n.setAttributes
* to localize the dialog buttons. Needed to properly size the dialog after
* the asynchronous translation.
*/
this._l10nButtons = [];
this._configureButtons(this.buttons);
window.moveToAlertPosition = this.moveToAlertPosition;
window.centerWindowOnScreen = this.centerWindowOnScreen;
}
set buttons(val) {
this._configureButtons(val);
return val;
}
get buttons() {
return this.getAttribute("buttons");
}
set defaultButton(val) {
this._setDefaultButton(val);
return val;
}
get defaultButton() {
if (this.hasAttribute("defaultButton")) {
return this.getAttribute("defaultButton");
}
return "accept"; // default to the accept button
}
get _strBundle() {
if (!this.__stringBundle) {
this.__stringBundle = Services.strings.createBundle(
"chrome://global/locale/dialog.properties"
);
}
return this.__stringBundle;
}
acceptDialog() {
return this._doButtonCommand("accept");
}
cancelDialog() {
return this._doButtonCommand("cancel");
}
getButton(aDlgType) {
return this._buttons[aDlgType];
}
get buttonBox() {
return this.shadowRoot.querySelector(".dialog-button-box");
}
moveToAlertPosition() {
// hack. we need this so the window has something like its final size
if (window.outerWidth == 1) {
dump(
"Trying to position a sizeless window; caller should have called sizeToContent() or sizeTo(). See bug 75649.\n"
);
sizeToContent();
}
if (opener) {
var xOffset = (opener.outerWidth - window.outerWidth) / 2;
var yOffset = opener.outerHeight / 5;
var newX = opener.screenX + xOffset;
var newY = opener.screenY + yOffset;
} else {
newX = (screen.availWidth - window.outerWidth) / 2;
newY = (screen.availHeight - window.outerHeight) / 2;
}
// ensure the window is fully onscreen (if smaller than the screen)
if (newX < screen.availLeft) {
newX = screen.availLeft + 20;
}
if (newX + window.outerWidth > screen.availLeft + screen.availWidth) {
newX = screen.availLeft + screen.availWidth - window.outerWidth - 20;
}
if (newY < screen.availTop) {
newY = screen.availTop + 20;
}
if (newY + window.outerHeight > screen.availTop + screen.availHeight) {
newY = screen.availTop + screen.availHeight - window.outerHeight - 60;
}
window.moveTo(newX, newY);
}
centerWindowOnScreen() {
var xOffset = screen.availWidth / 2 - window.outerWidth / 2;
var yOffset = screen.availHeight / 2 - window.outerHeight / 2;
xOffset = xOffset > 0 ? xOffset : 0;
yOffset = yOffset > 0 ? yOffset : 0;
window.moveTo(xOffset, yOffset);
}
postLoadInit(aEvent) {
let focusInit = () => {
const defaultButton = this.getButton(this.defaultButton);
// give focus to the first focusable element in the dialog
let focusedElt = document.commandDispatcher.focusedElement;
if (!focusedElt) {
document.commandDispatcher.advanceFocusIntoSubtree(this);
focusedElt = document.commandDispatcher.focusedElement;
if (focusedElt) {
var initialFocusedElt = focusedElt;
while (
focusedElt.localName == "tab" ||
focusedElt.getAttribute("noinitialfocus") == "true"
) {
document.commandDispatcher.advanceFocusIntoSubtree(focusedElt);
focusedElt = document.commandDispatcher.focusedElement;
if (focusedElt) {
if (focusedElt == initialFocusedElt) {
if (focusedElt.getAttribute("noinitialfocus") == "true") {
focusedElt.blur();
}
break;
}
}
}
if (initialFocusedElt.localName == "tab") {
if (focusedElt.hasAttribute("dlgtype")) {
// We don't want to focus on anonymous OK, Cancel, etc. buttons,
// so return focus to the tab itself
initialFocusedElt.focus();
}
} else if (
AppConstants.platform != "macosx" &&
focusedElt.hasAttribute("dlgtype") &&
focusedElt != defaultButton
) {
defaultButton.focus();
}
}
}
try {
if (defaultButton) {
window.notifyDefaultButtonLoaded(defaultButton);
}
} catch (e) {}
};
// Give focus after onload completes, see bug 103197.
setTimeout(focusInit, 0);
if (this._l10nButtons.length) {
document.l10n.translateElements(this._l10nButtons).then(() => {
window.sizeToContent();
});
}
}
openHelp(event) {
var helpButton = this.getButton("help");
if (helpButton.disabled || helpButton.hidden) {
return;
}
this._fireButtonEvent("help");
event.stopPropagation();
event.preventDefault();
}
_configureButtons(aButtons) {
// by default, get all the anonymous button elements
var buttons = {};
this._buttons = buttons;
for (let type of [
"accept",
"cancel",
"extra1",
"extra2",
"help",
"disclosure",
]) {
buttons[type] = this.shadowRoot.querySelector(`[dlgtype="${type}"]`);
}
// look for any overriding explicit button elements
var exBtns = this.getElementsByAttribute("dlgtype", "*");
var dlgtype;
for (let i = 0; i < exBtns.length; ++i) {
dlgtype = exBtns[i].getAttribute("dlgtype");
buttons[dlgtype].hidden = true; // hide the anonymous button
buttons[dlgtype] = exBtns[i];
}
// add the label and oncommand handler to each button
for (dlgtype in buttons) {
var button = buttons[dlgtype];
button.addEventListener(
"command",
this._handleButtonCommand.bind(this),
true
);
// don't override custom labels with pre-defined labels on explicit buttons
if (!button.hasAttribute("label")) {
// dialog attributes override the default labels in dialog.properties
if (this.hasAttribute("buttonlabel" + dlgtype)) {
button.setAttribute(
"label",
this.getAttribute("buttonlabel" + dlgtype)
);
if (this.hasAttribute("buttonaccesskey" + dlgtype)) {
button.setAttribute(
"accesskey",
this.getAttribute("buttonaccesskey" + dlgtype)
);
}
} else if (this.hasAttribute("buttonid" + dlgtype)) {
document.l10n.setAttributes(
button,
this.getAttribute("buttonid" + dlgtype)
);
this._l10nButtons.push(button);
} else if (dlgtype != "extra1" && dlgtype != "extra2") {
button.setAttribute(
"label",
this._strBundle.GetStringFromName("button-" + dlgtype)
);
var accessKey = this._strBundle.GetStringFromName(
"accesskey-" + dlgtype
);
if (accessKey) {
button.setAttribute("accesskey", accessKey);
}
}
}
}
// ensure that hitting enter triggers the default button command
// eslint-disable-next-line no-self-assign
this.defaultButton = this.defaultButton;
// if there is a special button configuration, use it
if (aButtons) {
// expect a comma delimited list of dlgtype values
var list = aButtons.split(",");
// mark shown dlgtypes as true
var shown = {
accept: false,
cancel: false,
help: false,
disclosure: false,
extra1: false,
extra2: false,
};
for (let i = 0; i < list.length; ++i) {
shown[list[i].replace(/ /g, "")] = true;
}
// hide/show the buttons we want
for (dlgtype in buttons) {
buttons[dlgtype].hidden = !shown[dlgtype];
}
// show the spacer on Windows only when the extra2 button is present
if (AppConstants.platform == "win") {
let spacer = this.shadowRoot.querySelector(".button-spacer");
spacer.removeAttribute("hidden");
spacer.setAttribute("flex", shown.extra2 ? "1" : "0");
}
}
}
_setDefaultButton(aNewDefault) {
// remove the default attribute from the previous default button, if any
var oldDefaultButton = this.getButton(this.defaultButton);
if (oldDefaultButton) {
oldDefaultButton.removeAttribute("default");
}
var newDefaultButton = this.getButton(aNewDefault);
if (newDefaultButton) {
this.setAttribute("defaultButton", aNewDefault);
newDefaultButton.setAttribute("default", "true");
} else {
this.setAttribute("defaultButton", "none");
if (aNewDefault != "none") {
dump(
"invalid new default button: " + aNewDefault + ", assuming: none\n"
);
}
}
}
_handleButtonCommand(aEvent) {
return this._doButtonCommand(aEvent.target.getAttribute("dlgtype"));
}
_doButtonCommand(aDlgType) {
var button = this.getButton(aDlgType);
if (!button.disabled) {
var noCancel = this._fireButtonEvent(aDlgType);
if (noCancel) {
if (aDlgType == "accept" || aDlgType == "cancel") {
var closingEvent = new CustomEvent("dialogclosing", {
bubbles: true,
detail: { button: aDlgType },
});
this.dispatchEvent(closingEvent);
window.close();
}
}
return noCancel;
}
return true;
}
_fireButtonEvent(aDlgType) {
var event = document.createEvent("Events");
event.initEvent("dialog" + aDlgType, true, true);
// handle dom event handlers
return this.dispatchEvent(event);
}
_hitEnter(evt) {
if (evt.defaultPrevented) {
return;
}
var btn = this.getButton(this.defaultButton);
if (btn) {
this._doButtonCommand(this.defaultButton);
}
}
on_focus(event) {
let btn = this.getButton(this.defaultButton);
if (btn) {
btn.setAttribute(
"default",
event.originalTarget == btn ||
!(
event.originalTarget.localName == "button" ||
event.originalTarget.localName == "toolbarbutton"
)
);
}
}
}
customElements.define("dialog", MozDialog);
}
|