John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 5 | import './strings.m.js'; |
| 6 | |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 7 | import {assert} from 'chrome://resources/js/assert.m.js'; |
| 8 | import {getFavicon} from 'chrome://resources/js/icon.m.js'; |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 9 | import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js'; |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 10 | import {isRTL} from 'chrome://resources/js/util.m.js'; |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 11 | |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 12 | import {AlertIndicatorsElement} from './alert_indicators.js'; |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 13 | import {CustomElement} from './custom_element.js'; |
Collin Baker | 1ac2e43 | 2019-10-16 18:17:21 | [diff] [blame] | 14 | import {TabStripEmbedderProxy} from './tab_strip_embedder_proxy.js'; |
John Lee | 4bcbaf1 | 2019-10-21 23:29:46 | [diff] [blame] | 15 | import {tabStripOptions} from './tab_strip_options.js'; |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 16 | import {TabSwiper} from './tab_swiper.js'; |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 17 | import {TabData, TabNetworkState, TabsApiProxy} from './tabs_api_proxy.js'; |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 18 | |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 19 | const DEFAULT_ANIMATION_DURATION = 125; |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 20 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 21 | /** |
| 22 | * @param {!TabData} tab |
| 23 | * @return {string} |
| 24 | */ |
| 25 | function getAccessibleTitle(tab) { |
| 26 | const tabTitle = tab.title; |
| 27 | |
| 28 | if (tab.crashed) { |
| 29 | return loadTimeData.getStringF('tabCrashed', tabTitle); |
| 30 | } |
| 31 | |
| 32 | if (tab.networkState === TabNetworkState.ERROR) { |
| 33 | return loadTimeData.getStringF('tabNetworkError', tabTitle); |
| 34 | } |
| 35 | |
| 36 | return tabTitle; |
| 37 | } |
| 38 | |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 39 | /** |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 40 | * TODO(crbug.com/1025390): padding-inline-end cannot be animated yet. |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 41 | * @return {string} |
| 42 | */ |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 43 | function getPaddingInlineEndProperty() { |
| 44 | return isRTL() ? 'paddingLeft' : 'paddingRight'; |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 45 | } |
| 46 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 47 | export class TabElement extends CustomElement { |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 48 | static get template() { |
| 49 | return `{__html_template__}`; |
| 50 | } |
| 51 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 52 | constructor() { |
| 53 | super(); |
| 54 | |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 55 | this.alertIndicatorsEl_ = /** @type {!AlertIndicatorsElement} */ |
| 56 | (this.shadowRoot.querySelector('tabstrip-alert-indicators')); |
| 57 | // Normally, custom elements will get upgraded automatically once added to |
| 58 | // the DOM, but TabElement may need to update properties on |
| 59 | // AlertIndicatorElement before this happens, so upgrade it manually. |
| 60 | customElements.upgrade(this.alertIndicatorsEl_); |
| 61 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 62 | /** @private {!HTMLElement} */ |
| 63 | this.closeButtonEl_ = |
| 64 | /** @type {!HTMLElement} */ (this.shadowRoot.querySelector('#close')); |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 65 | this.closeButtonEl_.setAttribute( |
| 66 | 'aria-label', loadTimeData.getString('closeTab')); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 67 | |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 68 | /** @private {!HTMLElement} */ |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 69 | this.tabEl_ = |
| 70 | /** @type {!HTMLElement} */ (this.shadowRoot.querySelector('#tab')); |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 71 | |
| 72 | /** @private {!HTMLElement} */ |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 73 | this.faviconEl_ = |
| 74 | /** @type {!HTMLElement} */ (this.shadowRoot.querySelector('#favicon')); |
| 75 | |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 76 | /** @private {!HTMLElement} */ |
| 77 | this.thumbnailContainer_ = |
| 78 | /** @type {!HTMLElement} */ ( |
| 79 | this.shadowRoot.querySelector('#thumbnail')); |
| 80 | |
| 81 | /** @private {!Image} */ |
| 82 | this.thumbnail_ = |
| 83 | /** @type {!Image} */ (this.shadowRoot.querySelector('#thumbnailImg')); |
| 84 | |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 85 | /** @private {!TabData} */ |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 86 | this.tab_; |
| 87 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 88 | /** @private {!TabsApiProxy} */ |
| 89 | this.tabsApi_ = TabsApiProxy.getInstance(); |
| 90 | |
Collin Baker | 1ac2e43 | 2019-10-16 18:17:21 | [diff] [blame] | 91 | /** @private {!TabStripEmbedderProxy} */ |
| 92 | this.embedderApi_ = TabStripEmbedderProxy.getInstance(); |
| 93 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 94 | /** @private {!HTMLElement} */ |
| 95 | this.titleTextEl_ = /** @type {!HTMLElement} */ ( |
| 96 | this.shadowRoot.querySelector('#titleText')); |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 97 | |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 98 | this.tabEl_.addEventListener('click', () => this.onClick_()); |
| 99 | this.tabEl_.addEventListener('contextmenu', e => this.onContextMenu_(e)); |
John Lee | 7f0c192 | 2019-11-08 22:06:25 | [diff] [blame] | 100 | this.tabEl_.addEventListener( |
| 101 | 'keydown', e => this.onKeyDown_(/** @type {!KeyboardEvent} */ (e))); |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 102 | this.closeButtonEl_.addEventListener('click', e => this.onClose_(e)); |
| 103 | this.addEventListener('swipe', () => this.onSwipe_()); |
| 104 | |
| 105 | /** @private @const {!TabSwiper} */ |
| 106 | this.tabSwiper_ = new TabSwiper(this); |
Robert Liao | 6ab5f71 | 2019-12-04 02:15:01 | [diff] [blame] | 107 | |
| 108 | /** @private {!Function} */ |
| 109 | this.onTabActivating_ = (tabId) => {}; |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 110 | } |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 111 | |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 112 | /** @return {!TabData} */ |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 113 | get tab() { |
| 114 | return this.tab_; |
| 115 | } |
| 116 | |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 117 | /** @param {!TabData} tab */ |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 118 | set tab(tab) { |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 119 | assert(this.tab_ !== tab); |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 120 | this.toggleAttribute('active', tab.active); |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 121 | this.tabEl_.setAttribute('aria-selected', tab.active.toString()); |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 122 | this.toggleAttribute('hide-icon_', !tab.showIcon); |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 123 | this.toggleAttribute( |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 124 | 'waiting_', |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 125 | !tab.shouldHideThrobber && |
| 126 | tab.networkState === TabNetworkState.WAITING); |
| 127 | this.toggleAttribute( |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 128 | 'loading_', |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 129 | !tab.shouldHideThrobber && |
| 130 | tab.networkState === TabNetworkState.LOADING); |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 131 | this.toggleAttribute('pinned', tab.pinned); |
John Lee | 4734ca1 | 2019-10-14 19:34:01 | [diff] [blame] | 132 | this.toggleAttribute('blocked_', tab.blocked); |
John Lee | 4bcbaf1 | 2019-10-21 23:29:46 | [diff] [blame] | 133 | this.setAttribute('draggable', true); |
John Lee | 02b3a74 | 2019-10-15 20:00:56 | [diff] [blame] | 134 | this.toggleAttribute('crashed_', tab.crashed); |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 135 | |
John Lee | 2f3baa7 | 2019-11-12 19:58:14 | [diff] [blame] | 136 | if (tab.title) { |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 137 | this.titleTextEl_.textContent = tab.title; |
John Lee | 2f3baa7 | 2019-11-12 19:58:14 | [diff] [blame] | 138 | } else if ( |
| 139 | !tab.shouldHideThrobber && |
| 140 | (tab.networkState === TabNetworkState.WAITING || |
| 141 | tab.networkState === TabNetworkState.LOADING)) { |
| 142 | this.titleTextEl_.textContent = loadTimeData.getString('loadingTab'); |
| 143 | } else { |
| 144 | this.titleTextEl_.textContent = loadTimeData.getString('defaultTabTitle'); |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 145 | } |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 146 | this.titleTextEl_.setAttribute('aria-label', getAccessibleTitle(tab)); |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 147 | |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 148 | if (tab.networkState === TabNetworkState.WAITING || |
| 149 | (tab.networkState === TabNetworkState.LOADING && |
| 150 | tab.isDefaultFavicon)) { |
| 151 | this.faviconEl_.style.backgroundImage = 'none'; |
| 152 | } else if (tab.favIconUrl) { |
| 153 | this.faviconEl_.style.backgroundImage = `url(${tab.favIconUrl})`; |
John Lee | d14bec6 | 2019-09-23 22:41:32 | [diff] [blame] | 154 | } else { |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 155 | this.faviconEl_.style.backgroundImage = getFavicon(''); |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 156 | } |
| 157 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 158 | // Expose the ID to an attribute to allow easy querySelector use |
| 159 | this.setAttribute('data-tab-id', tab.id); |
| 160 | |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 161 | this.alertIndicatorsEl_.updateAlertStates(tab.alertStates) |
| 162 | .then((alertIndicatorsCount) => { |
| 163 | this.toggleAttribute('has-alert-states_', alertIndicatorsCount > 0); |
| 164 | }); |
| 165 | |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 166 | if (!this.tab_ || (this.tab_.pinned !== tab.pinned && !tab.pinned)) { |
| 167 | this.tabSwiper_.startObserving(); |
| 168 | } else if (this.tab_.pinned !== tab.pinned && tab.pinned) { |
| 169 | this.tabSwiper_.stopObserving(); |
| 170 | } |
| 171 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 172 | this.tab_ = Object.freeze(tab); |
| 173 | } |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 174 | |
Robert Liao | 6ab5f71 | 2019-12-04 02:15:01 | [diff] [blame] | 175 | /** @param {!Function} callback */ |
| 176 | set onTabActivating(callback) { |
| 177 | this.onTabActivating_ = callback; |
| 178 | } |
| 179 | |
John Lee | 7f0c192 | 2019-11-08 22:06:25 | [diff] [blame] | 180 | focus() { |
| 181 | this.tabEl_.focus(); |
| 182 | } |
| 183 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 184 | /** @return {!HTMLElement} */ |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 185 | getDragImage() { |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 186 | return this.tabEl_; |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /** |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 190 | * @param {string} imgData |
| 191 | */ |
| 192 | updateThumbnail(imgData) { |
| 193 | this.thumbnail_.src = imgData; |
| 194 | } |
| 195 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 196 | /** @private */ |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 197 | onClick_() { |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 198 | if (!this.tab_ || this.tabSwiper_.wasSwiping()) { |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | |
Robert Liao | 6ab5f71 | 2019-12-04 02:15:01 | [diff] [blame] | 202 | const tabId = this.tab_.id; |
| 203 | this.onTabActivating_(tabId); |
| 204 | this.tabsApi_.activateTab(tabId); |
John Lee | 4bcbaf1 | 2019-10-21 23:29:46 | [diff] [blame] | 205 | |
| 206 | if (tabStripOptions.autoCloseEnabled) { |
| 207 | this.embedderApi_.closeContainer(); |
| 208 | } |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 209 | } |
| 210 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 211 | /** |
| 212 | * @param {!Event} event |
| 213 | * @private |
| 214 | */ |
Collin Baker | dc3d211 | 2019-10-10 18:28:20 | [diff] [blame] | 215 | onContextMenu_(event) { |
| 216 | event.preventDefault(); |
| 217 | |
| 218 | if (!this.tab_) { |
| 219 | return; |
| 220 | } |
| 221 | |
Collin Baker | 1ac2e43 | 2019-10-16 18:17:21 | [diff] [blame] | 222 | this.embedderApi_.showTabContextMenu( |
Collin Baker | dc3d211 | 2019-10-10 18:28:20 | [diff] [blame] | 223 | this.tab_.id, event.clientX, event.clientY); |
Collin Baker | 48a096b8 | 2019-11-26 01:21:27 | [diff] [blame] | 224 | event.stopPropagation(); |
Collin Baker | dc3d211 | 2019-10-10 18:28:20 | [diff] [blame] | 225 | } |
| 226 | |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 227 | /** |
| 228 | * @param {!Event} event |
| 229 | * @private |
| 230 | */ |
| 231 | onClose_(event) { |
| 232 | if (!this.tab_) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | event.stopPropagation(); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 237 | this.tabsApi_.closeTab(this.tab_.id); |
| 238 | } |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 239 | |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 240 | /** @private */ |
| 241 | onSwipe_() { |
| 242 | // Prevent slideOut animation from playing. |
| 243 | this.remove(); |
| 244 | this.tabsApi_.closeTab(this.tab_.id); |
| 245 | } |
| 246 | |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 247 | /** |
John Lee | 7f0c192 | 2019-11-08 22:06:25 | [diff] [blame] | 248 | * @param {!KeyboardEvent} event |
| 249 | * @private |
| 250 | */ |
| 251 | onKeyDown_(event) { |
| 252 | if (event.key === 'Enter' || event.key === ' ') { |
| 253 | this.onClick_(); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 258 | * @param {boolean} dragging |
| 259 | */ |
| 260 | setDragging(dragging) { |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 261 | this.toggleAttribute('dragging_', dragging); |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 265 | * @return {!Promise} |
| 266 | */ |
| 267 | slideIn() { |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 268 | const paddingInlineEnd = getPaddingInlineEndProperty(); |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 269 | |
John Lee | 41620f83 | 2019-11-27 22:41:05 | [diff] [blame] | 270 | // If this TabElement is the last tab, there needs to be enough space for |
| 271 | // the view to scroll to it. Therefore, immediately take up all the space |
| 272 | // it needs to and only animate the scale. |
| 273 | const isLastChild = this.nextElementSibling === null; |
| 274 | |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 275 | const startState = { |
John Lee | 41620f83 | 2019-11-27 22:41:05 | [diff] [blame] | 276 | maxWidth: isLastChild ? 'var(--tabstrip-tab-width)' : 0, |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 277 | transform: `scale(0)`, |
| 278 | }; |
John Lee | 41620f83 | 2019-11-27 22:41:05 | [diff] [blame] | 279 | startState[paddingInlineEnd] = |
| 280 | isLastChild ? 'var(--tabstrip-tab-spacing)' : 0; |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 281 | |
| 282 | const finishState = { |
| 283 | maxWidth: `var(--tabstrip-tab-width)`, |
| 284 | transform: `scale(1)`, |
| 285 | }; |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 286 | finishState[paddingInlineEnd] = 'var(--tabstrip-tab-spacing)'; |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 287 | |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 288 | return new Promise(resolve => { |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 289 | const animation = this.animate([startState, finishState], { |
| 290 | duration: 300, |
| 291 | easing: 'cubic-bezier(.4, 0, 0, 1)', |
| 292 | }); |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 293 | animation.onfinish = () => { |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 294 | resolve(); |
| 295 | }; |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 296 | }); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * @return {!Promise} |
| 301 | */ |
| 302 | slideOut() { |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 303 | if (!this.embedderApi_.isVisible() || this.tab_.pinned) { |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 304 | // There is no point in animating if the tab strip is hidden. |
| 305 | this.remove(); |
| 306 | return Promise.resolve(); |
| 307 | } |
| 308 | |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 309 | return new Promise(resolve => { |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 310 | const finishCallback = () => { |
| 311 | this.remove(); |
| 312 | resolve(); |
| 313 | }; |
| 314 | |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 315 | const translateAnimation = this.animate( |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 316 | { |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 317 | transform: ['translateY(0)', 'translateY(-100%)'], |
| 318 | }, |
| 319 | { |
| 320 | duration: 150, |
| 321 | easing: 'cubic-bezier(.4, 0, 1, 1)', |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 322 | fill: 'forwards', |
| 323 | }); |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 324 | const opacityAnimation = this.animate( |
| 325 | { |
| 326 | opacity: [1, 0], |
| 327 | }, |
| 328 | { |
| 329 | delay: 97.5, |
| 330 | duration: 50, |
| 331 | fill: 'forwards', |
| 332 | }); |
| 333 | |
| 334 | const widthAnimationKeyframes = { |
| 335 | maxWidth: ['var(--tabstrip-tab-width)', 0], |
| 336 | }; |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 337 | widthAnimationKeyframes[getPaddingInlineEndProperty()] = |
| 338 | ['var(--tabstrip-tab-spacing)', 0]; |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 339 | const widthAnimation = this.animate(widthAnimationKeyframes, { |
| 340 | delay: 97.5, |
| 341 | duration: 300, |
| 342 | easing: 'cubic-bezier(.4, 0, 0, 1)', |
| 343 | fill: 'forwards', |
| 344 | }); |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 345 | |
| 346 | const visibilityChangeListener = () => { |
| 347 | if (!this.embedderApi_.isVisible()) { |
| 348 | // If a tab strip becomes hidden during the animation, the onfinish |
| 349 | // event will not get fired until the tab strip becomes visible again. |
| 350 | // Therefore, when the tab strip becomes hidden, immediately call the |
| 351 | // finish callback. |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 352 | translateAnimation.cancel(); |
| 353 | opacityAnimation.cancel(); |
| 354 | widthAnimation.cancel(); |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 355 | finishCallback(); |
| 356 | } |
| 357 | }; |
| 358 | |
| 359 | document.addEventListener( |
| 360 | 'visibilitychange', visibilityChangeListener, {once: true}); |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 361 | // The onfinish handler is put on the width animation, as it will end |
| 362 | // last. |
| 363 | widthAnimation.onfinish = () => { |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 364 | document.removeEventListener( |
| 365 | 'visibilitychange', visibilityChangeListener); |
| 366 | finishCallback(); |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 367 | }; |
| 368 | }); |
| 369 | } |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 370 | } |
| 371 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 372 | customElements.define('tabstrip-tab', TabElement); |