Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 31 | import {GlassPane, MarginBehavior, SizeBehavior} from './GlassPane.js'; |
| 32 | |
| 33 | export class PopoverHelper { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 34 | /** |
| 35 | * @param {!Element} container |
Tim van der Lippe | aa76aa2 | 2020-02-14 14:38:24 | [diff] [blame] | 36 | * @param {function(!MouseEvent):?PopoverRequest} getRequest |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 37 | */ |
| 38 | constructor(container, getRequest) { |
| 39 | this._disableOnClick = false; |
| 40 | this._hasPadding = false; |
| 41 | this._getRequest = getRequest; |
| 42 | this._scheduledRequest = null; |
Tim van der Lippe | ee97fa3 | 2020-04-23 15:20:56 | [diff] [blame] | 43 | /** @type {?function():void} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 44 | this._hidePopoverCallback = null; |
| 45 | this._container = container; |
| 46 | this._showTimeout = 0; |
| 47 | this._hideTimeout = 0; |
| 48 | /** @type {?number} */ |
| 49 | this._hidePopoverTimer = null; |
| 50 | /** @type {?number} */ |
| 51 | this._showPopoverTimer = null; |
| 52 | this._boundMouseDown = this._mouseDown.bind(this); |
| 53 | this._boundMouseMove = this._mouseMove.bind(this); |
| 54 | this._boundMouseOut = this._mouseOut.bind(this); |
| 55 | this._container.addEventListener('mousedown', this._boundMouseDown, false); |
| 56 | this._container.addEventListener('mousemove', this._boundMouseMove, false); |
| 57 | this._container.addEventListener('mouseout', this._boundMouseOut, false); |
| 58 | this.setTimeout(1000); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @param {number} showTimeout |
| 63 | * @param {number=} hideTimeout |
| 64 | */ |
| 65 | setTimeout(showTimeout, hideTimeout) { |
| 66 | this._showTimeout = showTimeout; |
| 67 | this._hideTimeout = typeof hideTimeout === 'number' ? hideTimeout : showTimeout / 2; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param {boolean} hasPadding |
| 72 | */ |
| 73 | setHasPadding(hasPadding) { |
| 74 | this._hasPadding = hasPadding; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param {boolean} disableOnClick |
| 79 | */ |
| 80 | setDisableOnClick(disableOnClick) { |
| 81 | this._disableOnClick = disableOnClick; |
| 82 | } |
| 83 | |
| 84 | /** |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 85 | * @param {!Event} ev |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 86 | * @return {boolean} |
| 87 | */ |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 88 | _eventInScheduledContent(ev) { |
| 89 | const event = /** @type {!MouseEvent} */ (ev); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 90 | return this._scheduledRequest ? this._scheduledRequest.box.contains(event.clientX, event.clientY) : false; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @param {!Event} event |
| 95 | */ |
| 96 | _mouseDown(event) { |
| 97 | if (this._disableOnClick) { |
| 98 | this.hidePopover(); |
| 99 | return; |
| 100 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 101 | if (this._eventInScheduledContent(event)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 102 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 103 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 104 | |
| 105 | this._startHidePopoverTimer(0); |
| 106 | this._stopShowPopoverTimer(); |
| 107 | this._startShowPopoverTimer(/** @type {!MouseEvent} */ (event), 0); |
| 108 | } |
| 109 | |
| 110 | /** |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 111 | * @param {!Event} ev |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 112 | */ |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 113 | _mouseMove(ev) { |
| 114 | const event = /** @type {!MouseEvent} */ (ev); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 115 | // Pretend that nothing has happened. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 116 | if (this._eventInScheduledContent(event)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 117 | return; |
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 | |
| 120 | this._startHidePopoverTimer(this._hideTimeout); |
| 121 | this._stopShowPopoverTimer(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 122 | if (event.which && this._disableOnClick) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 123 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 124 | } |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 125 | this._startShowPopoverTimer(event, this.isPopoverVisible() ? this._showTimeout * 0.6 : this._showTimeout); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param {!Event} event |
| 130 | */ |
| 131 | _popoverMouseMove(event) { |
| 132 | this._stopHidePopoverTimer(); |
| 133 | } |
| 134 | |
| 135 | /** |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 136 | * @param {!GlassPane} popover |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 137 | * @param {!Event} ev |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 138 | */ |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 139 | _popoverMouseOut(popover, ev) { |
| 140 | const event = /** @type {!MouseEvent} */ (ev); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 141 | if (!popover.isShowing()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 142 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 143 | } |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 144 | const node = /** @type {?Node} */ (event.relatedTarget); |
| 145 | if (node && !node.isSelfOrDescendant(popover.contentElement)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 146 | this._startHidePopoverTimer(this._hideTimeout); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 147 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @param {!Event} event |
| 152 | */ |
| 153 | _mouseOut(event) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 154 | if (!this.isPopoverVisible()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 155 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 156 | } |
| 157 | if (!this._eventInScheduledContent(event)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 158 | this._startHidePopoverTimer(this._hideTimeout); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 159 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @param {number} timeout |
| 164 | */ |
| 165 | _startHidePopoverTimer(timeout) { |
| 166 | // User has |timeout| ms to reach the popup. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 167 | if (!this._hidePopoverCallback || this._hidePopoverTimer) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 168 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 169 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 170 | |
Tim van der Lippe | 4dcae8f | 2020-10-01 11:12:30 | [diff] [blame] | 171 | this._hidePopoverTimer = window.setTimeout(() => { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 172 | this._hidePopover(); |
| 173 | this._hidePopoverTimer = null; |
| 174 | }, timeout); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @param {!MouseEvent} event |
| 179 | * @param {number} timeout |
| 180 | */ |
| 181 | _startShowPopoverTimer(event, timeout) { |
| 182 | this._scheduledRequest = this._getRequest.call(null, event); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 183 | if (!this._scheduledRequest) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 184 | return; |
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 | |
Tim van der Lippe | 4dcae8f | 2020-10-01 11:12:30 | [diff] [blame] | 187 | this._showPopoverTimer = window.setTimeout(() => { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 188 | this._showPopoverTimer = null; |
| 189 | this._stopHidePopoverTimer(); |
| 190 | this._hidePopover(); |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 191 | const document = /** @type {!Document} */ (/** @type {!Node} */ (event.target).ownerDocument); |
| 192 | this._showPopover(document); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 193 | }, timeout); |
| 194 | } |
| 195 | |
| 196 | _stopShowPopoverTimer() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 197 | if (!this._showPopoverTimer) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 198 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 199 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 200 | clearTimeout(this._showPopoverTimer); |
| 201 | this._showPopoverTimer = null; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * @return {boolean} |
| 206 | */ |
| 207 | isPopoverVisible() { |
| 208 | return !!this._hidePopoverCallback; |
| 209 | } |
| 210 | |
| 211 | hidePopover() { |
| 212 | this._stopShowPopoverTimer(); |
| 213 | this._hidePopover(); |
| 214 | } |
| 215 | |
| 216 | _hidePopover() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 217 | if (!this._hidePopoverCallback) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 218 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 219 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 220 | this._hidePopoverCallback.call(null); |
| 221 | this._hidePopoverCallback = null; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @param {!Document} document |
| 226 | */ |
| 227 | _showPopover(document) { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 228 | const popover = new GlassPane(); |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame^] | 229 | popover.registerRequiredCSS('ui/popover.css', {enableLegacyPatching: true}); |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 230 | popover.setSizeBehavior(SizeBehavior.MeasureContent); |
| 231 | popover.setMarginBehavior(MarginBehavior.Arrow); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 232 | const request = this._scheduledRequest; |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 233 | if (!request) { |
| 234 | return; |
| 235 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 236 | request.show.call(null, popover).then(success => { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 237 | if (!success) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 238 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 239 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 240 | |
| 241 | if (this._scheduledRequest !== request) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 242 | if (request.hide) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 243 | request.hide.call(null); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 244 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 245 | return; |
| 246 | } |
| 247 | |
| 248 | // This should not happen, but we hide previous popover to be on the safe side. |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 249 | if (popoverHelperInstance) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 250 | console.error('One popover is already visible'); |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 251 | popoverHelperInstance.hidePopover(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 252 | } |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 253 | popoverHelperInstance = this; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 254 | |
| 255 | popover.contentElement.classList.toggle('has-padding', this._hasPadding); |
| 256 | popover.contentElement.addEventListener('mousemove', this._popoverMouseMove.bind(this), true); |
| 257 | popover.contentElement.addEventListener('mouseout', this._popoverMouseOut.bind(this, popover), true); |
| 258 | popover.setContentAnchorBox(request.box); |
| 259 | popover.show(document); |
| 260 | |
| 261 | this._hidePopoverCallback = () => { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 262 | if (request.hide) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 263 | request.hide.call(null); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 264 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 265 | popover.hide(); |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 266 | popoverHelperInstance = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 267 | }; |
| 268 | }); |
| 269 | } |
| 270 | |
| 271 | _stopHidePopoverTimer() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 272 | if (!this._hidePopoverTimer) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 273 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 274 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 275 | clearTimeout(this._hidePopoverTimer); |
| 276 | this._hidePopoverTimer = null; |
| 277 | |
| 278 | // We know that we reached the popup, but we might have moved over other elements. |
| 279 | // Discard pending command. |
| 280 | this._stopShowPopoverTimer(); |
| 281 | } |
| 282 | |
| 283 | dispose() { |
| 284 | this._container.removeEventListener('mousedown', this._boundMouseDown, false); |
| 285 | this._container.removeEventListener('mousemove', this._boundMouseMove, false); |
| 286 | this._container.removeEventListener('mouseout', this._boundMouseOut, false); |
| 287 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 288 | } |
Tim van der Lippe | aa76aa2 | 2020-02-14 14:38:24 | [diff] [blame] | 289 | |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 290 | /** @type {?PopoverHelper} */ |
| 291 | let popoverHelperInstance = null; |
| 292 | |
Tim van der Lippe | 4dcae8f | 2020-10-01 11:12:30 | [diff] [blame] | 293 | /** @typedef {{box: !AnchorBox, show:(function(!GlassPane):!Promise<boolean>), hide:((function():void)|undefined)}} */ |
Simon Zünd | 5bdf788 | 2020-08-20 07:28:44 | [diff] [blame] | 294 | // @ts-ignore typedef |
Tim van der Lippe | aa76aa2 | 2020-02-14 14:38:24 | [diff] [blame] | 295 | export let PopoverRequest; |