blob: cfa7d2e3b329d48f63aaf4e38b7604b7dc2f7056 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31UI.Dialog = class extends UI.GlassPane {
John Emau83763642019-07-16 23:56:4232
33 constructor() {
Blink Reformat4c46d092018-04-07 15:32:3734 super();
35 this.registerRequiredCSS('ui/dialog.css');
36 this.contentElement.tabIndex = 0;
37 this.contentElement.addEventListener('focus', () => this.widget().focus(), false);
38 this.contentElement.addEventListener('keydown', this._onKeyDown.bind(this), false);
39 this.widget().setDefaultFocusedElement(this.contentElement);
40 this.setPointerEventsBehavior(UI.GlassPane.PointerEventsBehavior.BlockedByGlassPane);
41 this.setOutsideClickCallback(event => {
42 this.hide();
43 event.consume(true);
44 });
John Emau4b568be2019-09-25 16:13:3745 UI.ARIAUtils.markAsModalDialog(this.contentElement);
Blink Reformat4c46d092018-04-07 15:32:3746 /** @type {!Map<!HTMLElement, number>} */
47 this._tabIndexMap = new Map();
48 /** @type {?UI.WidgetFocusRestorer} */
49 this._focusRestorer = null;
50 this._closeOnEscape = true;
51 }
52
53 /**
54 * @return {boolean}
55 */
56 static hasInstance() {
57 return !!UI.Dialog._instance;
58 }
59
60 /**
61 * @override
62 * @param {!Document|!Element=} where
63 */
64 show(where) {
65 const document = /** @type {!Document} */ (
66 where instanceof Document ? where : (where || UI.inspectorView.element).ownerDocument);
Tim van der Lippe1d6e57a2019-09-30 11:55:3467 if (UI.Dialog._instance) {
Blink Reformat4c46d092018-04-07 15:32:3768 UI.Dialog._instance.hide();
Tim van der Lippe1d6e57a2019-09-30 11:55:3469 }
Blink Reformat4c46d092018-04-07 15:32:3770 UI.Dialog._instance = this;
71 this._disableTabIndexOnElements(document);
72 super.show(document);
73 this._focusRestorer = new UI.WidgetFocusRestorer(this.widget());
74 }
75
76 /**
77 * @override
78 */
79 hide() {
80 this._focusRestorer.restore();
81 super.hide();
82 this._restoreTabIndexOnElements();
83 delete UI.Dialog._instance;
84 }
85
86 /**
87 * @param {boolean} close
88 */
89 setCloseOnEscape(close) {
90 this._closeOnEscape = close;
91 }
92
93 addCloseButton() {
94 const closeButton = this.contentElement.createChild('div', 'dialog-close-button', 'dt-close-button');
95 closeButton.gray = true;
96 closeButton.addEventListener('click', () => this.hide(), false);
97 }
98
99 /**
100 * @param {!Document} document
101 */
102 _disableTabIndexOnElements(document) {
103 this._tabIndexMap.clear();
104 for (let node = document; node; node = node.traverseNextNode(document)) {
105 if (node instanceof HTMLElement) {
106 const element = /** @type {!HTMLElement} */ (node);
107 const tabIndex = element.tabIndex;
108 if (tabIndex >= 0) {
109 this._tabIndexMap.set(element, tabIndex);
110 element.tabIndex = -1;
111 }
112 }
113 }
114 }
115
116 _restoreTabIndexOnElements() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34117 for (const element of this._tabIndexMap.keys()) {
Blink Reformat4c46d092018-04-07 15:32:37118 element.tabIndex = /** @type {number} */ (this._tabIndexMap.get(element));
Tim van der Lippe1d6e57a2019-09-30 11:55:34119 }
Blink Reformat4c46d092018-04-07 15:32:37120 this._tabIndexMap.clear();
121 }
122
123 /**
124 * @param {!Event} event
125 */
126 _onKeyDown(event) {
Erik Luo4c6826a2018-12-11 02:37:53127 if (this._closeOnEscape && event.keyCode === UI.KeyboardShortcut.Keys.Esc.code &&
128 UI.KeyboardShortcut.hasNoModifiers(event)) {
Blink Reformat4c46d092018-04-07 15:32:37129 event.consume(true);
130 this.hide();
131 }
132 }
133};