Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
| 3 | * Copyright (C) 2011 Google Inc. All Rights Reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | /** |
| 28 | * @unrestricted |
| 29 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 30 | export default class Widget extends Common.Object { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 31 | /** |
| 32 | * @param {boolean=} isWebComponent |
Joel Einbinder | 7fbe24c | 2019-01-24 05:19:01 | [diff] [blame] | 33 | * @param {boolean=} delegatesFocus |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 34 | */ |
Joel Einbinder | 7fbe24c | 2019-01-24 05:19:01 | [diff] [blame] | 35 | constructor(isWebComponent, delegatesFocus) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 36 | super(); |
| 37 | this.contentElement = createElementWithClass('div', 'widget'); |
| 38 | if (isWebComponent) { |
| 39 | this.element = createElementWithClass('div', 'vbox flex-auto'); |
Joel Einbinder | 7fbe24c | 2019-01-24 05:19:01 | [diff] [blame] | 40 | this._shadowRoot = UI.createShadowRootWithCoreStyles(this.element, undefined, delegatesFocus); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 41 | this._shadowRoot.appendChild(this.contentElement); |
| 42 | } else { |
| 43 | this.element = this.contentElement; |
| 44 | } |
| 45 | this._isWebComponent = isWebComponent; |
| 46 | this.element.__widget = this; |
| 47 | this._visible = false; |
| 48 | this._isRoot = false; |
| 49 | this._isShowing = false; |
| 50 | this._children = []; |
| 51 | this._hideOnDetach = false; |
| 52 | this._notificationDepth = 0; |
| 53 | this._invalidationsSuspended = 0; |
| 54 | this._defaultFocusedChild = null; |
| 55 | } |
| 56 | |
| 57 | static _incrementWidgetCounter(parentElement, childElement) { |
| 58 | const count = (childElement.__widgetCounter || 0) + (childElement.__widget ? 1 : 0); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 59 | if (!count) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 60 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 61 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 62 | |
| 63 | while (parentElement) { |
| 64 | parentElement.__widgetCounter = (parentElement.__widgetCounter || 0) + count; |
| 65 | parentElement = parentElement.parentElementOrShadowHost(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static _decrementWidgetCounter(parentElement, childElement) { |
| 70 | const count = (childElement.__widgetCounter || 0) + (childElement.__widget ? 1 : 0); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 71 | if (!count) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 72 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 73 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 74 | |
| 75 | while (parentElement) { |
| 76 | parentElement.__widgetCounter -= count; |
| 77 | parentElement = parentElement.parentElementOrShadowHost(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static __assert(condition, message) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 82 | if (!condition) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 83 | throw new Error(message); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 84 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param {?Node} node |
| 89 | */ |
| 90 | static focusWidgetForNode(node) { |
| 91 | while (node) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 92 | if (node.__widget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 93 | break; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 94 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 95 | node = node.parentNodeOrShadowHost(); |
| 96 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 97 | if (!node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 98 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 99 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 100 | |
| 101 | let widget = node.__widget; |
| 102 | while (widget._parentWidget) { |
| 103 | widget._parentWidget._defaultFocusedChild = widget; |
| 104 | widget = widget._parentWidget; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | markAsRoot() { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 109 | Widget.__assert(!this.element.parentElement, 'Attempt to mark as root attached node'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 110 | this._isRoot = true; |
| 111 | } |
| 112 | |
| 113 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 114 | * @return {?Widget} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 115 | */ |
| 116 | parentWidget() { |
| 117 | return this._parentWidget; |
| 118 | } |
| 119 | |
| 120 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 121 | * @return {!Array.<!Widget>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 122 | */ |
| 123 | children() { |
| 124 | return this._children; |
| 125 | } |
| 126 | |
| 127 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 128 | * @param {!Widget} widget |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 129 | * @protected |
| 130 | */ |
| 131 | childWasDetached(widget) { |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @return {boolean} |
| 136 | */ |
| 137 | isShowing() { |
| 138 | return this._isShowing; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @return {boolean} |
| 143 | */ |
| 144 | shouldHideOnDetach() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 145 | if (!this.element.parentElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 146 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 147 | } |
| 148 | if (this._hideOnDetach) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 149 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 150 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 151 | for (const child of this._children) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 152 | if (child.shouldHideOnDetach()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 153 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 154 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | setHideOnDetach() { |
| 160 | this._hideOnDetach = true; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @return {boolean} |
| 165 | */ |
| 166 | _inNotification() { |
| 167 | return !!this._notificationDepth || (this._parentWidget && this._parentWidget._inNotification()); |
| 168 | } |
| 169 | |
| 170 | _parentIsShowing() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 171 | if (this._isRoot) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 172 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 173 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 174 | return !!this._parentWidget && this._parentWidget.isShowing(); |
| 175 | } |
| 176 | |
| 177 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 178 | * @param {function(this:Widget)} method |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 179 | */ |
| 180 | _callOnVisibleChildren(method) { |
| 181 | const copy = this._children.slice(); |
| 182 | for (let i = 0; i < copy.length; ++i) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 183 | if (copy[i]._parentWidget === this && copy[i]._visible) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 184 | method.call(copy[i]); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 185 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | _processWillShow() { |
| 190 | this._callOnVisibleChildren(this._processWillShow); |
| 191 | this._isShowing = true; |
| 192 | } |
| 193 | |
| 194 | _processWasShown() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 195 | if (this._inNotification()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 196 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 197 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 198 | this.restoreScrollPositions(); |
| 199 | this._notify(this.wasShown); |
| 200 | this._callOnVisibleChildren(this._processWasShown); |
| 201 | } |
| 202 | |
| 203 | _processWillHide() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 204 | if (this._inNotification()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 205 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 206 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 207 | this.storeScrollPositions(); |
| 208 | |
| 209 | this._callOnVisibleChildren(this._processWillHide); |
| 210 | this._notify(this.willHide); |
| 211 | this._isShowing = false; |
| 212 | } |
| 213 | |
| 214 | _processWasHidden() { |
| 215 | this._callOnVisibleChildren(this._processWasHidden); |
| 216 | } |
| 217 | |
| 218 | _processOnResize() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 219 | if (this._inNotification()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 220 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 221 | } |
| 222 | if (!this.isShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 223 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 224 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 225 | this._notify(this.onResize); |
| 226 | this._callOnVisibleChildren(this._processOnResize); |
| 227 | } |
| 228 | |
| 229 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 230 | * @param {function(this:Widget)} notification |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 231 | */ |
| 232 | _notify(notification) { |
| 233 | ++this._notificationDepth; |
| 234 | try { |
| 235 | notification.call(this); |
| 236 | } finally { |
| 237 | --this._notificationDepth; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | wasShown() { |
| 242 | } |
| 243 | |
| 244 | willHide() { |
| 245 | } |
| 246 | |
| 247 | onResize() { |
| 248 | } |
| 249 | |
| 250 | onLayout() { |
| 251 | } |
| 252 | |
| 253 | ownerViewDisposed() { |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * @param {!Element} parentElement |
| 258 | * @param {?Node=} insertBefore |
| 259 | */ |
| 260 | show(parentElement, insertBefore) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 261 | Widget.__assert(parentElement, 'Attempt to attach widget with no parent element'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 262 | |
| 263 | if (!this._isRoot) { |
| 264 | // Update widget hierarchy. |
| 265 | let currentParent = parentElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 266 | while (currentParent && !currentParent.__widget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 267 | currentParent = currentParent.parentElementOrShadowHost(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 268 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 269 | Widget.__assert(currentParent, 'Attempt to attach widget to orphan node'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 270 | this._attach(currentParent.__widget); |
| 271 | } |
| 272 | |
| 273 | this._showWidget(parentElement, insertBefore); |
| 274 | } |
| 275 | |
| 276 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 277 | * @param {!Widget} parentWidget |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 278 | */ |
| 279 | _attach(parentWidget) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 280 | if (parentWidget === this._parentWidget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 281 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 282 | } |
| 283 | if (this._parentWidget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 284 | this.detach(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 285 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 286 | this._parentWidget = parentWidget; |
| 287 | this._parentWidget._children.push(this); |
| 288 | this._isRoot = false; |
| 289 | } |
| 290 | |
| 291 | showWidget() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 292 | if (this._visible) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 293 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 294 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 295 | Widget.__assert(this.element.parentElement, 'Attempt to show widget that is not hidden using hideWidget().'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 296 | this._showWidget(/** @type {!Element} */ (this.element.parentElement), this.element.nextSibling); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * @param {!Element} parentElement |
| 301 | * @param {?Node=} insertBefore |
| 302 | */ |
| 303 | _showWidget(parentElement, insertBefore) { |
| 304 | let currentParent = parentElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 305 | while (currentParent && !currentParent.__widget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 306 | currentParent = currentParent.parentElementOrShadowHost(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 307 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 308 | |
| 309 | if (this._isRoot) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 310 | Widget.__assert(!currentParent, 'Attempt to show root widget under another widget'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 311 | } else { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 312 | Widget.__assert( |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 313 | currentParent && currentParent.__widget === this._parentWidget, |
| 314 | 'Attempt to show under node belonging to alien widget'); |
| 315 | } |
| 316 | |
| 317 | const wasVisible = this._visible; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 318 | if (wasVisible && this.element.parentElement === parentElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 319 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 320 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 321 | |
| 322 | this._visible = true; |
| 323 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 324 | if (!wasVisible && this._parentIsShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 325 | this._processWillShow(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 326 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 327 | |
| 328 | this.element.classList.remove('hidden'); |
| 329 | |
| 330 | // Reparent |
| 331 | if (this.element.parentElement !== parentElement) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 332 | if (!this._externallyManaged) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 333 | Widget._incrementWidgetCounter(parentElement, this.element); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 334 | } |
| 335 | if (insertBefore) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 336 | Widget._originalInsertBefore.call(parentElement, this.element, insertBefore); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 337 | } else { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 338 | Widget._originalAppendChild.call(parentElement, this.element); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 339 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 340 | } |
| 341 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 342 | if (!wasVisible && this._parentIsShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 343 | this._processWasShown(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 344 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 345 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 346 | if (this._parentWidget && this._hasNonZeroConstraints()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 347 | this._parentWidget.invalidateConstraints(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 348 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 349 | this._processOnResize(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 350 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | hideWidget() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 354 | if (!this._visible) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 355 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 356 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 357 | this._hideWidget(false); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * @param {boolean} removeFromDOM |
| 362 | */ |
| 363 | _hideWidget(removeFromDOM) { |
| 364 | this._visible = false; |
| 365 | const parentElement = this.element.parentElement; |
| 366 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 367 | if (this._parentIsShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 368 | this._processWillHide(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 369 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 370 | |
| 371 | if (removeFromDOM) { |
| 372 | // Force legal removal |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 373 | Widget._decrementWidgetCounter(parentElement, this.element); |
| 374 | Widget._originalRemoveChild.call(parentElement, this.element); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 375 | } else { |
| 376 | this.element.classList.add('hidden'); |
| 377 | } |
| 378 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 379 | if (this._parentIsShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 380 | this._processWasHidden(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 381 | } |
| 382 | if (this._parentWidget && this._hasNonZeroConstraints()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 383 | this._parentWidget.invalidateConstraints(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 384 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /** |
| 388 | * @param {boolean=} overrideHideOnDetach |
| 389 | */ |
| 390 | detach(overrideHideOnDetach) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 391 | if (!this._parentWidget && !this._isRoot) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 392 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 393 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 394 | |
| 395 | // hideOnDetach means that we should never remove element from dom - content |
| 396 | // has iframes and detaching it will hurt. |
| 397 | // |
| 398 | // overrideHideOnDetach will override hideOnDetach and the client takes |
| 399 | // responsibility for the consequences. |
| 400 | const removeFromDOM = overrideHideOnDetach || !this.shouldHideOnDetach(); |
| 401 | if (this._visible) { |
| 402 | this._hideWidget(removeFromDOM); |
| 403 | } else if (removeFromDOM && this.element.parentElement) { |
| 404 | const parentElement = this.element.parentElement; |
| 405 | // Force kick out from DOM. |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 406 | Widget._decrementWidgetCounter(parentElement, this.element); |
| 407 | Widget._originalRemoveChild.call(parentElement, this.element); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | // Update widget hierarchy. |
| 411 | if (this._parentWidget) { |
| 412 | const childIndex = this._parentWidget._children.indexOf(this); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 413 | Widget.__assert(childIndex >= 0, 'Attempt to remove non-child widget'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 414 | this._parentWidget._children.splice(childIndex, 1); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 415 | if (this._parentWidget._defaultFocusedChild === this) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 416 | this._parentWidget._defaultFocusedChild = null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 417 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 418 | this._parentWidget.childWasDetached(this); |
| 419 | this._parentWidget = null; |
| 420 | } else { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 421 | Widget.__assert(this._isRoot, 'Removing non-root widget from DOM'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | detachChildWidgets() { |
| 426 | const children = this._children.slice(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 427 | for (let i = 0; i < children.length; ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 428 | children[i].detach(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 429 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /** |
| 433 | * @return {!Array.<!Element>} |
| 434 | */ |
| 435 | elementsToRestoreScrollPositionsFor() { |
| 436 | return [this.element]; |
| 437 | } |
| 438 | |
| 439 | storeScrollPositions() { |
| 440 | const elements = this.elementsToRestoreScrollPositionsFor(); |
| 441 | for (let i = 0; i < elements.length; ++i) { |
| 442 | const container = elements[i]; |
| 443 | container._scrollTop = container.scrollTop; |
| 444 | container._scrollLeft = container.scrollLeft; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | restoreScrollPositions() { |
| 449 | const elements = this.elementsToRestoreScrollPositionsFor(); |
| 450 | for (let i = 0; i < elements.length; ++i) { |
| 451 | const container = elements[i]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 452 | if (container._scrollTop) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 453 | container.scrollTop = container._scrollTop; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 454 | } |
| 455 | if (container._scrollLeft) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 456 | container.scrollLeft = container._scrollLeft; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 457 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | doResize() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 462 | if (!this.isShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 463 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 464 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 465 | // No matter what notification we are in, dispatching onResize is not needed. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 466 | if (!this._inNotification()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 467 | this._callOnVisibleChildren(this._processOnResize); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 468 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | doLayout() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 472 | if (!this.isShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 473 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 474 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 475 | this._notify(this.onLayout); |
| 476 | this.doResize(); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * @param {string} cssFile |
| 481 | */ |
| 482 | registerRequiredCSS(cssFile) { |
| 483 | UI.appendStyle(this._isWebComponent ? this._shadowRoot : this.element, cssFile); |
| 484 | } |
| 485 | |
| 486 | printWidgetHierarchy() { |
| 487 | const lines = []; |
| 488 | this._collectWidgetHierarchy('', lines); |
| 489 | console.log(lines.join('\n')); // eslint-disable-line no-console |
| 490 | } |
| 491 | |
| 492 | _collectWidgetHierarchy(prefix, lines) { |
| 493 | lines.push(prefix + '[' + this.element.className + ']' + (this._children.length ? ' {' : '')); |
| 494 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 495 | for (let i = 0; i < this._children.length; ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 496 | this._children[i]._collectWidgetHierarchy(prefix + ' ', lines); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 497 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 498 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 499 | if (this._children.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 500 | lines.push(prefix + '}'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 501 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /** |
| 505 | * @param {?Element} element |
| 506 | */ |
| 507 | setDefaultFocusedElement(element) { |
| 508 | this._defaultFocusedElement = element; |
| 509 | } |
| 510 | |
| 511 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 512 | * @param {!Widget} child |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 513 | */ |
| 514 | setDefaultFocusedChild(child) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 515 | Widget.__assert(child._parentWidget === this, 'Attempt to set non-child widget as default focused.'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 516 | this._defaultFocusedChild = child; |
| 517 | } |
| 518 | |
| 519 | focus() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 520 | if (!this.isShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 521 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 522 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 523 | |
| 524 | const element = this._defaultFocusedElement; |
| 525 | if (element) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 526 | if (!element.hasFocus()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 527 | element.focus(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 528 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 529 | return; |
| 530 | } |
| 531 | |
| 532 | if (this._defaultFocusedChild && this._defaultFocusedChild._visible) { |
| 533 | this._defaultFocusedChild.focus(); |
| 534 | } else { |
| 535 | for (const child of this._children) { |
| 536 | if (child._visible) { |
| 537 | child.focus(); |
| 538 | return; |
| 539 | } |
| 540 | } |
| 541 | let child = this.contentElement.traverseNextNode(this.contentElement); |
| 542 | while (child) { |
| 543 | if (child instanceof UI.XWidget) { |
| 544 | child.focus(); |
| 545 | return; |
| 546 | } |
| 547 | child = child.traverseNextNode(this.contentElement); |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * @return {boolean} |
| 554 | */ |
| 555 | hasFocus() { |
| 556 | return this.element.hasFocus(); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * @return {!UI.Constraints} |
| 561 | */ |
| 562 | calculateConstraints() { |
| 563 | return new UI.Constraints(); |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * @return {!UI.Constraints} |
| 568 | */ |
| 569 | constraints() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 570 | if (typeof this._constraints !== 'undefined') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 571 | return this._constraints; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 572 | } |
| 573 | if (typeof this._cachedConstraints === 'undefined') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 574 | this._cachedConstraints = this.calculateConstraints(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 575 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 576 | return this._cachedConstraints; |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * @param {number} width |
| 581 | * @param {number} height |
| 582 | * @param {number} preferredWidth |
| 583 | * @param {number} preferredHeight |
| 584 | */ |
| 585 | setMinimumAndPreferredSizes(width, height, preferredWidth, preferredHeight) { |
| 586 | this._constraints = new UI.Constraints(new UI.Size(width, height), new UI.Size(preferredWidth, preferredHeight)); |
| 587 | this.invalidateConstraints(); |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * @param {number} width |
| 592 | * @param {number} height |
| 593 | */ |
| 594 | setMinimumSize(width, height) { |
| 595 | this._constraints = new UI.Constraints(new UI.Size(width, height)); |
| 596 | this.invalidateConstraints(); |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * @return {boolean} |
| 601 | */ |
| 602 | _hasNonZeroConstraints() { |
| 603 | const constraints = this.constraints(); |
| 604 | return !!( |
| 605 | constraints.minimum.width || constraints.minimum.height || constraints.preferred.width || |
| 606 | constraints.preferred.height); |
| 607 | } |
| 608 | |
| 609 | suspendInvalidations() { |
| 610 | ++this._invalidationsSuspended; |
| 611 | } |
| 612 | |
| 613 | resumeInvalidations() { |
| 614 | --this._invalidationsSuspended; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 615 | if (!this._invalidationsSuspended && this._invalidationsRequested) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 616 | this.invalidateConstraints(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 617 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | invalidateConstraints() { |
| 621 | if (this._invalidationsSuspended) { |
| 622 | this._invalidationsRequested = true; |
| 623 | return; |
| 624 | } |
| 625 | this._invalidationsRequested = false; |
| 626 | const cached = this._cachedConstraints; |
| 627 | delete this._cachedConstraints; |
| 628 | const actual = this.constraints(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 629 | if (!actual.isEqual(cached) && this._parentWidget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 630 | this._parentWidget.invalidateConstraints(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 631 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 632 | this.doLayout(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 633 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 634 | } |
Olivia Flynn | 1d938e4 | 2019-09-23 08:13:40 | [diff] [blame] | 635 | |
| 636 | // Excludes the widget from being tracked by its parents/ancestors via |
| 637 | // __widgetCounter because the widget is being handled by external code. |
| 638 | // Widgets marked as being externally managed are responsible for |
| 639 | // finishing out their own lifecycle (i.e. calling detach() before being |
| 640 | // removed from the DOM). This is e.g. used for CodeMirror. |
| 641 | // |
| 642 | // Also note that this must be called before the widget is shown so that |
| 643 | // so that its ancestor's __widgetCounter is not incremented. |
| 644 | markAsExternallyManaged() { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 645 | Widget.__assert(!this._parentWidget, 'Attempt to mark widget as externally managed after insertion to the DOM'); |
Olivia Flynn | 1d938e4 | 2019-09-23 08:13:40 | [diff] [blame] | 646 | this._externallyManaged = true; |
| 647 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 648 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 649 | |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 650 | export const _originalAppendChild = Element.prototype.appendChild; |
| 651 | export const _originalInsertBefore = Element.prototype.insertBefore; |
| 652 | export const _originalRemoveChild = Element.prototype.removeChild; |
| 653 | export const _originalRemoveChildren = Element.prototype.removeChildren; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 654 | |
| 655 | |
| 656 | /** |
| 657 | * @unrestricted |
| 658 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 659 | export class VBox extends Widget { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 660 | /** |
| 661 | * @param {boolean=} isWebComponent |
Joel Einbinder | 7fbe24c | 2019-01-24 05:19:01 | [diff] [blame] | 662 | * @param {boolean=} delegatesFocus |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 663 | */ |
Joel Einbinder | 7fbe24c | 2019-01-24 05:19:01 | [diff] [blame] | 664 | constructor(isWebComponent, delegatesFocus) { |
| 665 | super(isWebComponent, delegatesFocus); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 666 | this.contentElement.classList.add('vbox'); |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * @override |
| 671 | * @return {!UI.Constraints} |
| 672 | */ |
| 673 | calculateConstraints() { |
| 674 | let constraints = new UI.Constraints(); |
| 675 | |
| 676 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 677 | * @this {!Widget} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 678 | * @suppressReceiverCheck |
| 679 | */ |
| 680 | function updateForChild() { |
| 681 | const child = this.constraints(); |
| 682 | constraints = constraints.widthToMax(child); |
| 683 | constraints = constraints.addHeight(child); |
| 684 | } |
| 685 | |
| 686 | this._callOnVisibleChildren(updateForChild); |
| 687 | return constraints; |
| 688 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 689 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 690 | |
| 691 | /** |
| 692 | * @unrestricted |
| 693 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 694 | export class HBox extends Widget { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 695 | /** |
| 696 | * @param {boolean=} isWebComponent |
| 697 | */ |
| 698 | constructor(isWebComponent) { |
| 699 | super(isWebComponent); |
| 700 | this.contentElement.classList.add('hbox'); |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * @override |
| 705 | * @return {!UI.Constraints} |
| 706 | */ |
| 707 | calculateConstraints() { |
| 708 | let constraints = new UI.Constraints(); |
| 709 | |
| 710 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 711 | * @this {!Widget} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 712 | * @suppressReceiverCheck |
| 713 | */ |
| 714 | function updateForChild() { |
| 715 | const child = this.constraints(); |
| 716 | constraints = constraints.addWidth(child); |
| 717 | constraints = constraints.heightToMax(child); |
| 718 | } |
| 719 | |
| 720 | this._callOnVisibleChildren(updateForChild); |
| 721 | return constraints; |
| 722 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 723 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 724 | |
| 725 | /** |
| 726 | * @unrestricted |
| 727 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 728 | export class VBoxWithResizeCallback extends VBox { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 729 | /** |
| 730 | * @param {function()} resizeCallback |
| 731 | */ |
| 732 | constructor(resizeCallback) { |
| 733 | super(); |
| 734 | this._resizeCallback = resizeCallback; |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * @override |
| 739 | */ |
| 740 | onResize() { |
| 741 | this._resizeCallback(); |
| 742 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 743 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 744 | |
| 745 | /** |
| 746 | * @unrestricted |
| 747 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 748 | export class WidgetFocusRestorer { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 749 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 750 | * @param {!Widget} widget |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 751 | */ |
| 752 | constructor(widget) { |
| 753 | this._widget = widget; |
| 754 | this._previous = widget.element.ownerDocument.deepActiveElement(); |
| 755 | widget.focus(); |
| 756 | } |
| 757 | |
| 758 | restore() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 759 | if (!this._widget) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 760 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 761 | } |
| 762 | if (this._widget.hasFocus() && this._previous) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 763 | this._previous.focus(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 764 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 765 | this._previous = null; |
| 766 | this._widget = null; |
| 767 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 768 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 769 | |
| 770 | /** |
| 771 | * @override |
| 772 | * @param {?Node} child |
| 773 | * @return {!Node} |
| 774 | * @suppress {duplicate} |
| 775 | */ |
| 776 | Element.prototype.appendChild = function(child) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 777 | Widget.__assert(!child.__widget || child.parentElement === this, 'Attempt to add widget via regular DOM operation.'); |
| 778 | return Widget._originalAppendChild.call(this, child); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 779 | }; |
| 780 | |
| 781 | /** |
| 782 | * @override |
| 783 | * @param {?Node} child |
| 784 | * @param {?Node} anchor |
| 785 | * @return {!Node} |
| 786 | * @suppress {duplicate} |
| 787 | */ |
| 788 | Element.prototype.insertBefore = function(child, anchor) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 789 | Widget.__assert(!child.__widget || child.parentElement === this, 'Attempt to add widget via regular DOM operation.'); |
| 790 | return Widget._originalInsertBefore.call(this, child, anchor); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 791 | }; |
| 792 | |
| 793 | /** |
| 794 | * @override |
| 795 | * @param {?Node} child |
| 796 | * @return {!Node} |
| 797 | * @suppress {duplicate} |
| 798 | */ |
| 799 | Element.prototype.removeChild = function(child) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 800 | Widget.__assert( |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 801 | !child.__widgetCounter && !child.__widget, |
| 802 | 'Attempt to remove element containing widget via regular DOM operation'); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 803 | return Widget._originalRemoveChild.call(this, child); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 804 | }; |
| 805 | |
| 806 | Element.prototype.removeChildren = function() { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 807 | Widget.__assert(!this.__widgetCounter, 'Attempt to remove element containing widget via regular DOM operation'); |
| 808 | Widget._originalRemoveChildren.call(this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 809 | }; |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame^] | 810 | |
| 811 | /* Legacy exported object*/ |
| 812 | self.UI = self.UI || {}; |
| 813 | |
| 814 | /* Legacy exported object*/ |
| 815 | UI = UI || {}; |
| 816 | |
| 817 | /** @constructor */ |
| 818 | UI.Widget = Widget; |
| 819 | |
| 820 | Widget._originalAppendChild = _originalAppendChild; |
| 821 | Widget._originalInsertBefore = _originalInsertBefore; |
| 822 | Widget._originalRemoveChild = _originalRemoveChild; |
| 823 | Widget._originalRemoveChildren = _originalRemoveChildren; |
| 824 | |
| 825 | /** @constructor */ |
| 826 | UI.HBox = HBox; |
| 827 | |
| 828 | /** @constructor */ |
| 829 | UI.VBox = VBox; |
| 830 | |
| 831 | /** @constructor */ |
| 832 | UI.WidgetFocusRestorer = WidgetFocusRestorer; |
| 833 | |
| 834 | /** @constructor */ |
| 835 | UI.VBoxWithResizeCallback = VBoxWithResizeCallback; |