Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 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 Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 31 | export default class Dialog extends UI.GlassPane { |
John Emau | 8376364 | 2019-07-16 23:56:42 | [diff] [blame] | 32 | constructor() { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 33 | 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 Emau | 4b568be | 2019-09-25 16:13:37 | [diff] [blame] | 44 | UI.ARIAUtils.markAsModalDialog(this.contentElement); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 45 | /** @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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 66 | if (UI.Dialog._instance) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 67 | UI.Dialog._instance.hide(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 68 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 69 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 116 | for (const element of this._tabIndexMap.keys()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 117 | element.tabIndex = /** @type {number} */ (this._tabIndexMap.get(element)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 118 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 119 | this._tabIndexMap.clear(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param {!Event} event |
| 124 | */ |
| 125 | _onKeyDown(event) { |
Erik Luo | 4c6826a | 2018-12-11 02:37:53 | [diff] [blame] | 126 | if (this._closeOnEscape && event.keyCode === UI.KeyboardShortcut.Keys.Esc.code && |
| 127 | UI.KeyboardShortcut.hasNoModifiers(event)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 128 | event.consume(true); |
| 129 | this.hide(); |
| 130 | } |
| 131 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 132 | } |
| 133 | |
| 134 | /* Legacy exported object*/ |
| 135 | self.UI = self.UI || {}; |
| 136 | |
| 137 | /* Legacy exported object*/ |
| 138 | UI = UI || {}; |
| 139 | |
| 140 | /** @constructor */ |
| 141 | UI.Dialog = Dialog; |