@jupyterlab/apputils 0.19.1-alpha.0 → 0.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/dialog.js CHANGED
@@ -1,540 +1,540 @@
1
- "use strict";
2
- // Copyright (c) Jupyter Development Team.
3
- // Distributed under the terms of the Modified BSD License.
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- const algorithm_1 = require("@phosphor/algorithm");
6
- const coreutils_1 = require("@phosphor/coreutils");
7
- const messaging_1 = require("@phosphor/messaging");
8
- const widgets_1 = require("@phosphor/widgets");
9
- const vdom_1 = require("./vdom");
10
- const styling_1 = require("./styling");
11
- /**
12
- * Create and show a dialog.
13
- *
14
- * @param options - The dialog setup options.
15
- *
16
- * @returns A promise that resolves with whether the dialog was accepted.
17
- */
18
- function showDialog(options = {}) {
19
- let dialog = new Dialog(options);
20
- return dialog.launch();
21
- }
22
- exports.showDialog = showDialog;
23
- /**
24
- * Show an error message dialog.
25
- *
26
- * @param title - The title of the dialog box.
27
- *
28
- * @param error - the error to show in the dialog body (either a string
29
- * or an object with a string `message` property).
30
- */
31
- function showErrorMessage(title, error) {
32
- console.warn('Showing error:', error);
33
- return showDialog({
34
- title: title,
35
- body: error.message || title,
36
- buttons: [Dialog.okButton({ label: 'DISMISS' })]
37
- }).then(() => {
38
- /* no-op */
39
- });
40
- }
41
- exports.showErrorMessage = showErrorMessage;
42
- /**
43
- * A modal dialog widget.
44
- */
45
- class Dialog extends widgets_1.Widget {
46
- /**
47
- * Create a dialog panel instance.
48
- *
49
- * @param options - The dialog setup options.
50
- */
51
- constructor(options = {}) {
52
- super();
53
- this._focusNodeSelector = '';
54
- this.addClass('jp-Dialog');
55
- let normalized = Private.handleOptions(options);
56
- let renderer = normalized.renderer;
57
- this._host = normalized.host;
58
- this._defaultButton = normalized.defaultButton;
59
- this._buttons = normalized.buttons;
60
- this._buttonNodes = algorithm_1.toArray(algorithm_1.map(this._buttons, button => {
61
- return renderer.createButtonNode(button);
62
- }));
63
- let layout = (this.layout = new widgets_1.PanelLayout());
64
- let content = new widgets_1.Panel();
65
- content.addClass('jp-Dialog-content');
66
- layout.addWidget(content);
67
- this._body = normalized.body;
68
- let header = renderer.createHeader(normalized.title);
69
- let body = renderer.createBody(normalized.body);
70
- let footer = renderer.createFooter(this._buttonNodes);
71
- content.addWidget(header);
72
- content.addWidget(body);
73
- content.addWidget(footer);
74
- this._primary = this._buttonNodes[this._defaultButton];
75
- this._focusNodeSelector = options.focusNodeSelector;
76
- }
77
- /**
78
- * Dispose of the resources used by the dialog.
79
- */
80
- dispose() {
81
- const promise = this._promise;
82
- if (promise) {
83
- this._promise = null;
84
- promise.reject(void 0);
85
- algorithm_1.ArrayExt.removeFirstOf(Private.launchQueue, promise.promise);
86
- }
87
- super.dispose();
88
- }
89
- /**
90
- * Launch the dialog as a modal window.
91
- *
92
- * @returns a promise that resolves with the result of the dialog.
93
- */
94
- launch() {
95
- // Return the existing dialog if already open.
96
- if (this._promise) {
97
- return this._promise.promise;
98
- }
99
- const promise = (this._promise = new coreutils_1.PromiseDelegate());
100
- let promises = Promise.all(Private.launchQueue);
101
- Private.launchQueue.push(this._promise.promise);
102
- return promises.then(() => {
103
- widgets_1.Widget.attach(this, this._host);
104
- return promise.promise;
105
- });
106
- }
107
- /**
108
- * Resolve the current dialog.
109
- *
110
- * @param index - An optional index to the button to resolve.
111
- *
112
- * #### Notes
113
- * Will default to the defaultIndex.
114
- * Will resolve the current `show()` with the button value.
115
- * Will be a no-op if the dialog is not shown.
116
- */
117
- resolve(index) {
118
- if (!this._promise) {
119
- return;
120
- }
121
- if (index === undefined) {
122
- index = this._defaultButton;
123
- }
124
- this._resolve(this._buttons[index]);
125
- }
126
- /**
127
- * Reject the current dialog with a default reject value.
128
- *
129
- * #### Notes
130
- * Will be a no-op if the dialog is not shown.
131
- */
132
- reject() {
133
- if (!this._promise) {
134
- return;
135
- }
136
- this._resolve(Dialog.cancelButton());
137
- }
138
- /**
139
- * Handle the DOM events for the directory listing.
140
- *
141
- * @param event - The DOM event sent to the widget.
142
- *
143
- * #### Notes
144
- * This method implements the DOM `EventListener` interface and is
145
- * called in response to events on the panel's DOM node. It should
146
- * not be called directly by user code.
147
- */
148
- handleEvent(event) {
149
- switch (event.type) {
150
- case 'keydown':
151
- this._evtKeydown(event);
152
- break;
153
- case 'click':
154
- this._evtClick(event);
155
- break;
156
- case 'focus':
157
- this._evtFocus(event);
158
- break;
159
- case 'contextmenu':
160
- event.preventDefault();
161
- event.stopPropagation();
162
- break;
163
- default:
164
- break;
165
- }
166
- }
167
- /**
168
- * A message handler invoked on an `'after-attach'` message.
169
- */
170
- onAfterAttach(msg) {
171
- let node = this.node;
172
- node.addEventListener('keydown', this, true);
173
- node.addEventListener('contextmenu', this, true);
174
- node.addEventListener('click', this, true);
175
- document.addEventListener('focus', this, true);
176
- this._first = Private.findFirstFocusable(this.node);
177
- this._original = document.activeElement;
178
- if (this._focusNodeSelector) {
179
- let body = this.node.querySelector('.jp-Dialog-body');
180
- let el = body.querySelector(this._focusNodeSelector);
181
- if (el) {
182
- this._primary = el;
183
- }
184
- }
185
- this._primary.focus();
186
- }
187
- /**
188
- * A message handler invoked on an `'after-detach'` message.
189
- */
190
- onAfterDetach(msg) {
191
- let node = this.node;
192
- node.removeEventListener('keydown', this, true);
193
- node.removeEventListener('contextmenu', this, true);
194
- node.removeEventListener('click', this, true);
195
- document.removeEventListener('focus', this, true);
196
- this._original.focus();
197
- }
198
- /**
199
- * A message handler invoked on a `'close-request'` message.
200
- */
201
- onCloseRequest(msg) {
202
- if (this._promise) {
203
- this.reject();
204
- }
205
- super.onCloseRequest(msg);
206
- }
207
- /**
208
- * Handle the `'click'` event for a dialog button.
209
- *
210
- * @param event - The DOM event sent to the widget
211
- */
212
- _evtClick(event) {
213
- let content = this.node.getElementsByClassName('jp-Dialog-content')[0];
214
- if (!content.contains(event.target)) {
215
- event.stopPropagation();
216
- event.preventDefault();
217
- return;
218
- }
219
- for (let buttonNode of this._buttonNodes) {
220
- if (buttonNode.contains(event.target)) {
221
- let index = this._buttonNodes.indexOf(buttonNode);
222
- this.resolve(index);
223
- }
224
- }
225
- }
226
- /**
227
- * Handle the `'keydown'` event for the widget.
228
- *
229
- * @param event - The DOM event sent to the widget
230
- */
231
- _evtKeydown(event) {
232
- // Check for escape key
233
- switch (event.keyCode) {
234
- case 27: // Escape.
235
- event.stopPropagation();
236
- event.preventDefault();
237
- this.reject();
238
- break;
239
- case 9: // Tab.
240
- // Handle a tab on the last button.
241
- let node = this._buttonNodes[this._buttons.length - 1];
242
- if (document.activeElement === node && !event.shiftKey) {
243
- event.stopPropagation();
244
- event.preventDefault();
245
- this._first.focus();
246
- }
247
- break;
248
- case 13: // Enter.
249
- event.stopPropagation();
250
- event.preventDefault();
251
- this.resolve();
252
- break;
253
- default:
254
- break;
255
- }
256
- }
257
- /**
258
- * Handle the `'focus'` event for the widget.
259
- *
260
- * @param event - The DOM event sent to the widget
261
- */
262
- _evtFocus(event) {
263
- let target = event.target;
264
- if (!this.node.contains(target)) {
265
- event.stopPropagation();
266
- this._buttonNodes[this._defaultButton].focus();
267
- }
268
- }
269
- /**
270
- * Resolve a button item.
271
- */
272
- _resolve(button) {
273
- // Prevent loopback.
274
- const promise = this._promise;
275
- if (!promise) {
276
- this.dispose();
277
- return;
278
- }
279
- this._promise = null;
280
- algorithm_1.ArrayExt.removeFirstOf(Private.launchQueue, promise.promise);
281
- let body = this._body;
282
- let value = null;
283
- if (button.accept &&
284
- body instanceof widgets_1.Widget &&
285
- typeof body.getValue === 'function') {
286
- value = body.getValue();
287
- }
288
- this.dispose();
289
- promise.resolve({ button, value });
290
- }
291
- }
292
- exports.Dialog = Dialog;
293
- /**
294
- * The namespace for Dialog class statics.
295
- */
296
- (function (Dialog) {
297
- /**
298
- * Create an accept button.
299
- */
300
- function okButton(options = {}) {
301
- options.accept = true;
302
- return createButton(options);
303
- }
304
- Dialog.okButton = okButton;
305
- /**
306
- * Create a reject button.
307
- */
308
- function cancelButton(options = {}) {
309
- options.accept = false;
310
- return createButton(options);
311
- }
312
- Dialog.cancelButton = cancelButton;
313
- /**
314
- * Create a warn button.
315
- */
316
- function warnButton(options = {}) {
317
- options.displayType = 'warn';
318
- return createButton(options);
319
- }
320
- Dialog.warnButton = warnButton;
321
- /**
322
- * Create a button item.
323
- */
324
- function createButton(value) {
325
- value.accept = value.accept !== false;
326
- let defaultLabel = value.accept ? 'OK' : 'CANCEL';
327
- return {
328
- label: value.label || defaultLabel,
329
- iconClass: value.iconClass || '',
330
- iconLabel: value.iconLabel || '',
331
- caption: value.caption || '',
332
- className: value.className || '',
333
- accept: value.accept,
334
- displayType: value.displayType || 'default'
335
- };
336
- }
337
- Dialog.createButton = createButton;
338
- /**
339
- * The default implementation of a dialog renderer.
340
- */
341
- class Renderer {
342
- /**
343
- * Create the header of the dialog.
344
- *
345
- * @param title - The title of the dialog.
346
- *
347
- * @returns A widget for the dialog header.
348
- */
349
- createHeader(title) {
350
- let header;
351
- if (typeof title === 'string') {
352
- header = new widgets_1.Widget({ node: document.createElement('span') });
353
- header.node.textContent = title;
354
- }
355
- else {
356
- header = new vdom_1.ReactElementWidget(title);
357
- }
358
- header.addClass('jp-Dialog-header');
359
- styling_1.Styling.styleNode(header.node);
360
- return header;
361
- }
362
- /**
363
- * Create the body of the dialog.
364
- *
365
- * @param value - The input value for the body.
366
- *
367
- * @returns A widget for the body.
368
- */
369
- createBody(value) {
370
- let body;
371
- if (typeof value === 'string') {
372
- body = new widgets_1.Widget({ node: document.createElement('span') });
373
- body.node.textContent = value;
374
- }
375
- else if (value instanceof widgets_1.Widget) {
376
- body = value;
377
- }
378
- else {
379
- body = new vdom_1.ReactElementWidget(value);
380
- // Immediately update the body even though it has not yet attached in
381
- // order to trigger a render of the DOM nodes from the React element.
382
- messaging_1.MessageLoop.sendMessage(body, widgets_1.Widget.Msg.UpdateRequest);
383
- }
384
- body.addClass('jp-Dialog-body');
385
- styling_1.Styling.styleNode(body.node);
386
- return body;
387
- }
388
- /**
389
- * Create the footer of the dialog.
390
- *
391
- * @param buttonNodes - The buttons nodes to add to the footer.
392
- *
393
- * @returns A widget for the footer.
394
- */
395
- createFooter(buttons) {
396
- let footer = new widgets_1.Widget();
397
- footer.addClass('jp-Dialog-footer');
398
- algorithm_1.each(buttons, button => {
399
- footer.node.appendChild(button);
400
- });
401
- styling_1.Styling.styleNode(footer.node);
402
- return footer;
403
- }
404
- /**
405
- * Create a button node for the dialog.
406
- *
407
- * @param button - The button data.
408
- *
409
- * @returns A node for the button.
410
- */
411
- createButtonNode(button) {
412
- const e = document.createElement('button');
413
- e.className = this.createItemClass(button);
414
- e.appendChild(this.renderIcon(button));
415
- e.appendChild(this.renderLabel(button));
416
- return e;
417
- }
418
- /**
419
- * Create the class name for the button.
420
- *
421
- * @param data - The data to use for the class name.
422
- *
423
- * @returns The full class name for the button.
424
- */
425
- createItemClass(data) {
426
- // Setup the initial class name.
427
- let name = 'jp-Dialog-button';
428
- // Add the other state classes.
429
- if (data.accept) {
430
- name += ' jp-mod-accept';
431
- }
432
- else {
433
- name += ' jp-mod-reject';
434
- }
435
- if (data.displayType === 'warn') {
436
- name += ' jp-mod-warn';
437
- }
438
- // Add the extra class.
439
- let extra = data.className;
440
- if (extra) {
441
- name += ` ${extra}`;
442
- }
443
- // Return the complete class name.
444
- return name;
445
- }
446
- /**
447
- * Render an icon element for a dialog item.
448
- *
449
- * @param data - The data to use for rendering the icon.
450
- *
451
- * @returns An HTML element representing the icon.
452
- */
453
- renderIcon(data) {
454
- const e = document.createElement('div');
455
- e.className = this.createIconClass(data);
456
- e.appendChild(document.createTextNode(data.iconLabel));
457
- return e;
458
- }
459
- /**
460
- * Create the class name for the button icon.
461
- *
462
- * @param data - The data to use for the class name.
463
- *
464
- * @returns The full class name for the item icon.
465
- */
466
- createIconClass(data) {
467
- let name = 'jp-Dialog-buttonIcon';
468
- let extra = data.iconClass;
469
- return extra ? `${name} ${extra}` : name;
470
- }
471
- /**
472
- * Render the label element for a button.
473
- *
474
- * @param data - The data to use for rendering the label.
475
- *
476
- * @returns An HTML element representing the item label.
477
- */
478
- renderLabel(data) {
479
- const e = document.createElement('div');
480
- e.className = 'jp-Dialog-buttonLabel';
481
- e.title = data.caption;
482
- e.appendChild(document.createTextNode(data.label));
483
- return e;
484
- }
485
- }
486
- Dialog.Renderer = Renderer;
487
- /**
488
- * The default renderer instance.
489
- */
490
- Dialog.defaultRenderer = new Renderer();
491
- })(Dialog = exports.Dialog || (exports.Dialog = {}));
492
- /**
493
- * The namespace for module private data.
494
- */
495
- var Private;
496
- (function (Private) {
497
- /**
498
- * The queue for launching dialogs.
499
- */
500
- Private.launchQueue = [];
501
- /**
502
- * Handle the input options for a dialog.
503
- *
504
- * @param options - The input options.
505
- *
506
- * @returns A new options object with defaults applied.
507
- */
508
- function handleOptions(options = {}) {
509
- let buttons = options.buttons || [Dialog.cancelButton(), Dialog.okButton()];
510
- let defaultButton = buttons.length - 1;
511
- if (options.defaultButton !== undefined) {
512
- defaultButton = options.defaultButton;
513
- }
514
- return {
515
- title: options.title || '',
516
- body: options.body || '',
517
- host: options.host || document.body,
518
- buttons,
519
- defaultButton,
520
- renderer: options.renderer || Dialog.defaultRenderer,
521
- focusNodeSelector: options.focusNodeSelector || ''
522
- };
523
- }
524
- Private.handleOptions = handleOptions;
525
- /**
526
- * Find the first focusable item in the dialog.
527
- */
528
- function findFirstFocusable(node) {
529
- let candidateSelectors = [
530
- 'input',
531
- 'select',
532
- 'a[href]',
533
- 'textarea',
534
- 'button',
535
- '[tabindex]'
536
- ].join(',');
537
- return node.querySelectorAll(candidateSelectors)[0];
538
- }
539
- Private.findFirstFocusable = findFirstFocusable;
540
- })(Private || (Private = {}));
1
+ "use strict";
2
+ // Copyright (c) Jupyter Development Team.
3
+ // Distributed under the terms of the Modified BSD License.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ const algorithm_1 = require("@phosphor/algorithm");
6
+ const coreutils_1 = require("@phosphor/coreutils");
7
+ const messaging_1 = require("@phosphor/messaging");
8
+ const widgets_1 = require("@phosphor/widgets");
9
+ const vdom_1 = require("./vdom");
10
+ const styling_1 = require("./styling");
11
+ /**
12
+ * Create and show a dialog.
13
+ *
14
+ * @param options - The dialog setup options.
15
+ *
16
+ * @returns A promise that resolves with whether the dialog was accepted.
17
+ */
18
+ function showDialog(options = {}) {
19
+ let dialog = new Dialog(options);
20
+ return dialog.launch();
21
+ }
22
+ exports.showDialog = showDialog;
23
+ /**
24
+ * Show an error message dialog.
25
+ *
26
+ * @param title - The title of the dialog box.
27
+ *
28
+ * @param error - the error to show in the dialog body (either a string
29
+ * or an object with a string `message` property).
30
+ */
31
+ function showErrorMessage(title, error) {
32
+ console.warn('Showing error:', error);
33
+ return showDialog({
34
+ title: title,
35
+ body: error.message || title,
36
+ buttons: [Dialog.okButton({ label: 'DISMISS' })]
37
+ }).then(() => {
38
+ /* no-op */
39
+ });
40
+ }
41
+ exports.showErrorMessage = showErrorMessage;
42
+ /**
43
+ * A modal dialog widget.
44
+ */
45
+ class Dialog extends widgets_1.Widget {
46
+ /**
47
+ * Create a dialog panel instance.
48
+ *
49
+ * @param options - The dialog setup options.
50
+ */
51
+ constructor(options = {}) {
52
+ super();
53
+ this._focusNodeSelector = '';
54
+ this.addClass('jp-Dialog');
55
+ let normalized = Private.handleOptions(options);
56
+ let renderer = normalized.renderer;
57
+ this._host = normalized.host;
58
+ this._defaultButton = normalized.defaultButton;
59
+ this._buttons = normalized.buttons;
60
+ this._buttonNodes = algorithm_1.toArray(algorithm_1.map(this._buttons, button => {
61
+ return renderer.createButtonNode(button);
62
+ }));
63
+ let layout = (this.layout = new widgets_1.PanelLayout());
64
+ let content = new widgets_1.Panel();
65
+ content.addClass('jp-Dialog-content');
66
+ layout.addWidget(content);
67
+ this._body = normalized.body;
68
+ let header = renderer.createHeader(normalized.title);
69
+ let body = renderer.createBody(normalized.body);
70
+ let footer = renderer.createFooter(this._buttonNodes);
71
+ content.addWidget(header);
72
+ content.addWidget(body);
73
+ content.addWidget(footer);
74
+ this._primary = this._buttonNodes[this._defaultButton];
75
+ this._focusNodeSelector = options.focusNodeSelector;
76
+ }
77
+ /**
78
+ * Dispose of the resources used by the dialog.
79
+ */
80
+ dispose() {
81
+ const promise = this._promise;
82
+ if (promise) {
83
+ this._promise = null;
84
+ promise.reject(void 0);
85
+ algorithm_1.ArrayExt.removeFirstOf(Private.launchQueue, promise.promise);
86
+ }
87
+ super.dispose();
88
+ }
89
+ /**
90
+ * Launch the dialog as a modal window.
91
+ *
92
+ * @returns a promise that resolves with the result of the dialog.
93
+ */
94
+ launch() {
95
+ // Return the existing dialog if already open.
96
+ if (this._promise) {
97
+ return this._promise.promise;
98
+ }
99
+ const promise = (this._promise = new coreutils_1.PromiseDelegate());
100
+ let promises = Promise.all(Private.launchQueue);
101
+ Private.launchQueue.push(this._promise.promise);
102
+ return promises.then(() => {
103
+ widgets_1.Widget.attach(this, this._host);
104
+ return promise.promise;
105
+ });
106
+ }
107
+ /**
108
+ * Resolve the current dialog.
109
+ *
110
+ * @param index - An optional index to the button to resolve.
111
+ *
112
+ * #### Notes
113
+ * Will default to the defaultIndex.
114
+ * Will resolve the current `show()` with the button value.
115
+ * Will be a no-op if the dialog is not shown.
116
+ */
117
+ resolve(index) {
118
+ if (!this._promise) {
119
+ return;
120
+ }
121
+ if (index === undefined) {
122
+ index = this._defaultButton;
123
+ }
124
+ this._resolve(this._buttons[index]);
125
+ }
126
+ /**
127
+ * Reject the current dialog with a default reject value.
128
+ *
129
+ * #### Notes
130
+ * Will be a no-op if the dialog is not shown.
131
+ */
132
+ reject() {
133
+ if (!this._promise) {
134
+ return;
135
+ }
136
+ this._resolve(Dialog.cancelButton());
137
+ }
138
+ /**
139
+ * Handle the DOM events for the directory listing.
140
+ *
141
+ * @param event - The DOM event sent to the widget.
142
+ *
143
+ * #### Notes
144
+ * This method implements the DOM `EventListener` interface and is
145
+ * called in response to events on the panel's DOM node. It should
146
+ * not be called directly by user code.
147
+ */
148
+ handleEvent(event) {
149
+ switch (event.type) {
150
+ case 'keydown':
151
+ this._evtKeydown(event);
152
+ break;
153
+ case 'click':
154
+ this._evtClick(event);
155
+ break;
156
+ case 'focus':
157
+ this._evtFocus(event);
158
+ break;
159
+ case 'contextmenu':
160
+ event.preventDefault();
161
+ event.stopPropagation();
162
+ break;
163
+ default:
164
+ break;
165
+ }
166
+ }
167
+ /**
168
+ * A message handler invoked on an `'after-attach'` message.
169
+ */
170
+ onAfterAttach(msg) {
171
+ let node = this.node;
172
+ node.addEventListener('keydown', this, true);
173
+ node.addEventListener('contextmenu', this, true);
174
+ node.addEventListener('click', this, true);
175
+ document.addEventListener('focus', this, true);
176
+ this._first = Private.findFirstFocusable(this.node);
177
+ this._original = document.activeElement;
178
+ if (this._focusNodeSelector) {
179
+ let body = this.node.querySelector('.jp-Dialog-body');
180
+ let el = body.querySelector(this._focusNodeSelector);
181
+ if (el) {
182
+ this._primary = el;
183
+ }
184
+ }
185
+ this._primary.focus();
186
+ }
187
+ /**
188
+ * A message handler invoked on an `'after-detach'` message.
189
+ */
190
+ onAfterDetach(msg) {
191
+ let node = this.node;
192
+ node.removeEventListener('keydown', this, true);
193
+ node.removeEventListener('contextmenu', this, true);
194
+ node.removeEventListener('click', this, true);
195
+ document.removeEventListener('focus', this, true);
196
+ this._original.focus();
197
+ }
198
+ /**
199
+ * A message handler invoked on a `'close-request'` message.
200
+ */
201
+ onCloseRequest(msg) {
202
+ if (this._promise) {
203
+ this.reject();
204
+ }
205
+ super.onCloseRequest(msg);
206
+ }
207
+ /**
208
+ * Handle the `'click'` event for a dialog button.
209
+ *
210
+ * @param event - The DOM event sent to the widget
211
+ */
212
+ _evtClick(event) {
213
+ let content = this.node.getElementsByClassName('jp-Dialog-content')[0];
214
+ if (!content.contains(event.target)) {
215
+ event.stopPropagation();
216
+ event.preventDefault();
217
+ return;
218
+ }
219
+ for (let buttonNode of this._buttonNodes) {
220
+ if (buttonNode.contains(event.target)) {
221
+ let index = this._buttonNodes.indexOf(buttonNode);
222
+ this.resolve(index);
223
+ }
224
+ }
225
+ }
226
+ /**
227
+ * Handle the `'keydown'` event for the widget.
228
+ *
229
+ * @param event - The DOM event sent to the widget
230
+ */
231
+ _evtKeydown(event) {
232
+ // Check for escape key
233
+ switch (event.keyCode) {
234
+ case 27: // Escape.
235
+ event.stopPropagation();
236
+ event.preventDefault();
237
+ this.reject();
238
+ break;
239
+ case 9: // Tab.
240
+ // Handle a tab on the last button.
241
+ let node = this._buttonNodes[this._buttons.length - 1];
242
+ if (document.activeElement === node && !event.shiftKey) {
243
+ event.stopPropagation();
244
+ event.preventDefault();
245
+ this._first.focus();
246
+ }
247
+ break;
248
+ case 13: // Enter.
249
+ event.stopPropagation();
250
+ event.preventDefault();
251
+ this.resolve();
252
+ break;
253
+ default:
254
+ break;
255
+ }
256
+ }
257
+ /**
258
+ * Handle the `'focus'` event for the widget.
259
+ *
260
+ * @param event - The DOM event sent to the widget
261
+ */
262
+ _evtFocus(event) {
263
+ let target = event.target;
264
+ if (!this.node.contains(target)) {
265
+ event.stopPropagation();
266
+ this._buttonNodes[this._defaultButton].focus();
267
+ }
268
+ }
269
+ /**
270
+ * Resolve a button item.
271
+ */
272
+ _resolve(button) {
273
+ // Prevent loopback.
274
+ const promise = this._promise;
275
+ if (!promise) {
276
+ this.dispose();
277
+ return;
278
+ }
279
+ this._promise = null;
280
+ algorithm_1.ArrayExt.removeFirstOf(Private.launchQueue, promise.promise);
281
+ let body = this._body;
282
+ let value = null;
283
+ if (button.accept &&
284
+ body instanceof widgets_1.Widget &&
285
+ typeof body.getValue === 'function') {
286
+ value = body.getValue();
287
+ }
288
+ this.dispose();
289
+ promise.resolve({ button, value });
290
+ }
291
+ }
292
+ exports.Dialog = Dialog;
293
+ /**
294
+ * The namespace for Dialog class statics.
295
+ */
296
+ (function (Dialog) {
297
+ /**
298
+ * Create an accept button.
299
+ */
300
+ function okButton(options = {}) {
301
+ options.accept = true;
302
+ return createButton(options);
303
+ }
304
+ Dialog.okButton = okButton;
305
+ /**
306
+ * Create a reject button.
307
+ */
308
+ function cancelButton(options = {}) {
309
+ options.accept = false;
310
+ return createButton(options);
311
+ }
312
+ Dialog.cancelButton = cancelButton;
313
+ /**
314
+ * Create a warn button.
315
+ */
316
+ function warnButton(options = {}) {
317
+ options.displayType = 'warn';
318
+ return createButton(options);
319
+ }
320
+ Dialog.warnButton = warnButton;
321
+ /**
322
+ * Create a button item.
323
+ */
324
+ function createButton(value) {
325
+ value.accept = value.accept !== false;
326
+ let defaultLabel = value.accept ? 'OK' : 'CANCEL';
327
+ return {
328
+ label: value.label || defaultLabel,
329
+ iconClass: value.iconClass || '',
330
+ iconLabel: value.iconLabel || '',
331
+ caption: value.caption || '',
332
+ className: value.className || '',
333
+ accept: value.accept,
334
+ displayType: value.displayType || 'default'
335
+ };
336
+ }
337
+ Dialog.createButton = createButton;
338
+ /**
339
+ * The default implementation of a dialog renderer.
340
+ */
341
+ class Renderer {
342
+ /**
343
+ * Create the header of the dialog.
344
+ *
345
+ * @param title - The title of the dialog.
346
+ *
347
+ * @returns A widget for the dialog header.
348
+ */
349
+ createHeader(title) {
350
+ let header;
351
+ if (typeof title === 'string') {
352
+ header = new widgets_1.Widget({ node: document.createElement('span') });
353
+ header.node.textContent = title;
354
+ }
355
+ else {
356
+ header = new vdom_1.ReactElementWidget(title);
357
+ }
358
+ header.addClass('jp-Dialog-header');
359
+ styling_1.Styling.styleNode(header.node);
360
+ return header;
361
+ }
362
+ /**
363
+ * Create the body of the dialog.
364
+ *
365
+ * @param value - The input value for the body.
366
+ *
367
+ * @returns A widget for the body.
368
+ */
369
+ createBody(value) {
370
+ let body;
371
+ if (typeof value === 'string') {
372
+ body = new widgets_1.Widget({ node: document.createElement('span') });
373
+ body.node.textContent = value;
374
+ }
375
+ else if (value instanceof widgets_1.Widget) {
376
+ body = value;
377
+ }
378
+ else {
379
+ body = new vdom_1.ReactElementWidget(value);
380
+ // Immediately update the body even though it has not yet attached in
381
+ // order to trigger a render of the DOM nodes from the React element.
382
+ messaging_1.MessageLoop.sendMessage(body, widgets_1.Widget.Msg.UpdateRequest);
383
+ }
384
+ body.addClass('jp-Dialog-body');
385
+ styling_1.Styling.styleNode(body.node);
386
+ return body;
387
+ }
388
+ /**
389
+ * Create the footer of the dialog.
390
+ *
391
+ * @param buttonNodes - The buttons nodes to add to the footer.
392
+ *
393
+ * @returns A widget for the footer.
394
+ */
395
+ createFooter(buttons) {
396
+ let footer = new widgets_1.Widget();
397
+ footer.addClass('jp-Dialog-footer');
398
+ algorithm_1.each(buttons, button => {
399
+ footer.node.appendChild(button);
400
+ });
401
+ styling_1.Styling.styleNode(footer.node);
402
+ return footer;
403
+ }
404
+ /**
405
+ * Create a button node for the dialog.
406
+ *
407
+ * @param button - The button data.
408
+ *
409
+ * @returns A node for the button.
410
+ */
411
+ createButtonNode(button) {
412
+ const e = document.createElement('button');
413
+ e.className = this.createItemClass(button);
414
+ e.appendChild(this.renderIcon(button));
415
+ e.appendChild(this.renderLabel(button));
416
+ return e;
417
+ }
418
+ /**
419
+ * Create the class name for the button.
420
+ *
421
+ * @param data - The data to use for the class name.
422
+ *
423
+ * @returns The full class name for the button.
424
+ */
425
+ createItemClass(data) {
426
+ // Setup the initial class name.
427
+ let name = 'jp-Dialog-button';
428
+ // Add the other state classes.
429
+ if (data.accept) {
430
+ name += ' jp-mod-accept';
431
+ }
432
+ else {
433
+ name += ' jp-mod-reject';
434
+ }
435
+ if (data.displayType === 'warn') {
436
+ name += ' jp-mod-warn';
437
+ }
438
+ // Add the extra class.
439
+ let extra = data.className;
440
+ if (extra) {
441
+ name += ` ${extra}`;
442
+ }
443
+ // Return the complete class name.
444
+ return name;
445
+ }
446
+ /**
447
+ * Render an icon element for a dialog item.
448
+ *
449
+ * @param data - The data to use for rendering the icon.
450
+ *
451
+ * @returns An HTML element representing the icon.
452
+ */
453
+ renderIcon(data) {
454
+ const e = document.createElement('div');
455
+ e.className = this.createIconClass(data);
456
+ e.appendChild(document.createTextNode(data.iconLabel));
457
+ return e;
458
+ }
459
+ /**
460
+ * Create the class name for the button icon.
461
+ *
462
+ * @param data - The data to use for the class name.
463
+ *
464
+ * @returns The full class name for the item icon.
465
+ */
466
+ createIconClass(data) {
467
+ let name = 'jp-Dialog-buttonIcon';
468
+ let extra = data.iconClass;
469
+ return extra ? `${name} ${extra}` : name;
470
+ }
471
+ /**
472
+ * Render the label element for a button.
473
+ *
474
+ * @param data - The data to use for rendering the label.
475
+ *
476
+ * @returns An HTML element representing the item label.
477
+ */
478
+ renderLabel(data) {
479
+ const e = document.createElement('div');
480
+ e.className = 'jp-Dialog-buttonLabel';
481
+ e.title = data.caption;
482
+ e.appendChild(document.createTextNode(data.label));
483
+ return e;
484
+ }
485
+ }
486
+ Dialog.Renderer = Renderer;
487
+ /**
488
+ * The default renderer instance.
489
+ */
490
+ Dialog.defaultRenderer = new Renderer();
491
+ })(Dialog = exports.Dialog || (exports.Dialog = {}));
492
+ /**
493
+ * The namespace for module private data.
494
+ */
495
+ var Private;
496
+ (function (Private) {
497
+ /**
498
+ * The queue for launching dialogs.
499
+ */
500
+ Private.launchQueue = [];
501
+ /**
502
+ * Handle the input options for a dialog.
503
+ *
504
+ * @param options - The input options.
505
+ *
506
+ * @returns A new options object with defaults applied.
507
+ */
508
+ function handleOptions(options = {}) {
509
+ let buttons = options.buttons || [Dialog.cancelButton(), Dialog.okButton()];
510
+ let defaultButton = buttons.length - 1;
511
+ if (options.defaultButton !== undefined) {
512
+ defaultButton = options.defaultButton;
513
+ }
514
+ return {
515
+ title: options.title || '',
516
+ body: options.body || '',
517
+ host: options.host || document.body,
518
+ buttons,
519
+ defaultButton,
520
+ renderer: options.renderer || Dialog.defaultRenderer,
521
+ focusNodeSelector: options.focusNodeSelector || ''
522
+ };
523
+ }
524
+ Private.handleOptions = handleOptions;
525
+ /**
526
+ * Find the first focusable item in the dialog.
527
+ */
528
+ function findFirstFocusable(node) {
529
+ let candidateSelectors = [
530
+ 'input',
531
+ 'select',
532
+ 'a[href]',
533
+ 'textarea',
534
+ 'button',
535
+ '[tabindex]'
536
+ ].join(',');
537
+ return node.querySelectorAll(candidateSelectors)[0];
538
+ }
539
+ Private.findFirstFocusable = findFirstFocusable;
540
+ })(Private || (Private = {}));