blob: 1d2345c6cea98d2aabc359d801392c0565166dd4 [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
Tim van der Lippe0830b3d2019-10-03 13:20:0731export default class Dialog extends UI.GlassPane {
John Emau83763642019-07-16 23:56:4232 constructor() {
Blink Reformat4c46d092018-04-07 15:32:3733 super();
34 this.registerRequiredCSS('ui/dialog.css');
35 this.contentElement.tabIndex = 0;
36 this.contentElement.addEventListener('focus', () => this.widget().focus(), false);
37 this.contentElement.addEventListener('keydown', this._onKeyDown.bind(this), false);
38 this.widget().setDefaultFocusedElement(this.contentElement);
39 this.setPointerEventsBehavior(UI.GlassPane.PointerEventsBehavior.BlockedByGlassPane);
40 this.setOutsideClickCallback(event => {
41 this.hide();
42 event.consume(true);
43 });
John Emau4b568be2019-09-25 16:13:3744 UI.ARIAUtils.markAsModalDialog(this.contentElement);
Blink Reformat4c46d092018-04-07 15:32:3745 /** @type {!Map<!HTMLElement, number>} */
46 this._tabIndexMap = new Map();
47 /** @type {?UI.WidgetFocusRestorer} */
48 this._focusRestorer = null;
49 this._closeOnEscape = true;
50 }
51
52 /**
53 * @return {boolean}
54 */
55 static hasInstance() {
56 return !!UI.Dialog._instance;
57 }
58
59 /**
60 * @override
61 * @param {!Document|!Element=} where
62 */
63 show(where) {
64 const document = /** @type {!Document} */ (
65 where instanceof Document ? where : (where || UI.inspectorView.element).ownerDocument);
Tim van der Lippe1d6e57a2019-09-30 11:55:3466 if (UI.Dialog._instance) {
Blink Reformat4c46d092018-04-07 15:32:3767 UI.Dialog._instance.hide();
Tim van der Lippe1d6e57a2019-09-30 11:55:3468 }
Blink Reformat4c46d092018-04-07 15:32:3769 UI.Dialog._instance = this;
70 this._disableTabIndexOnElements(document);
71 super.show(document);
72 this._focusRestorer = new UI.WidgetFocusRestorer(this.widget());
73 }
74
75 /**
76 * @override
77 */
78 hide() {
79 this._focusRestorer.restore();
80 super.hide();
81 this._restoreTabIndexOnElements();
82 delete UI.Dialog._instance;
83 }
84
85 /**
86 * @param {boolean} close
87 */
88 setCloseOnEscape(close) {
89 this._closeOnEscape = close;
90 }
91
92 addCloseButton() {
93 const closeButton = this.contentElement.createChild('div', 'dialog-close-button', 'dt-close-button');
94 closeButton.gray = true;
95 closeButton.addEventListener('click', () => this.hide(), false);
96 }
97
98 /**
99 * @param {!Document} document
100 */
101 _disableTabIndexOnElements(document) {
102 this._tabIndexMap.clear();
103 for (let node = document; node; node = node.traverseNextNode(document)) {
104 if (node instanceof HTMLElement) {
105 const element = /** @type {!HTMLElement} */ (node);
106 const tabIndex = element.tabIndex;
107 if (tabIndex >= 0) {
108 this._tabIndexMap.set(element, tabIndex);
109 element.tabIndex = -1;
110 }
111 }
112 }
113 }
114
115 _restoreTabIndexOnElements() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34116 for (const element of this._tabIndexMap.keys()) {
Blink Reformat4c46d092018-04-07 15:32:37117 element.tabIndex = /** @type {number} */ (this._tabIndexMap.get(element));
Tim van der Lippe1d6e57a2019-09-30 11:55:34118 }
Blink Reformat4c46d092018-04-07 15:32:37119 this._tabIndexMap.clear();
120 }
121
122 /**
123 * @param {!Event} event
124 */
125 _onKeyDown(event) {
Erik Luo4c6826a2018-12-11 02:37:53126 if (this._closeOnEscape && event.keyCode === UI.KeyboardShortcut.Keys.Esc.code &&
127 UI.KeyboardShortcut.hasNoModifiers(event)) {
Blink Reformat4c46d092018-04-07 15:32:37128 event.consume(true);
129 this.hide();
130 }
131 }
Tim van der Lippe0830b3d2019-10-03 13:20:07132}
133
134/* Legacy exported object*/
135self.UI = self.UI || {};
136
137/* Legacy exported object*/
138UI = UI || {};
139
140/** @constructor */
141UI.Dialog = Dialog;