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); |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 107 | } |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 108 | |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 109 | /** @return {!TabData} */ |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 110 | get tab() { |
| 111 | return this.tab_; |
| 112 | } |
| 113 | |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 114 | /** @param {!TabData} tab */ |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 115 | set tab(tab) { |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 116 | assert(this.tab_ !== tab); |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 117 | this.toggleAttribute('active', tab.active); |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 118 | this.tabEl_.setAttribute('aria-selected', tab.active.toString()); |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 119 | this.toggleAttribute('hide-icon_', !tab.showIcon); |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 120 | this.toggleAttribute( |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 121 | 'waiting_', |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 122 | !tab.shouldHideThrobber && |
| 123 | tab.networkState === TabNetworkState.WAITING); |
| 124 | this.toggleAttribute( |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 125 | 'loading_', |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 126 | !tab.shouldHideThrobber && |
| 127 | tab.networkState === TabNetworkState.LOADING); |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 128 | this.toggleAttribute('pinned', tab.pinned); |
John Lee | 4734ca1 | 2019-10-14 19:34:01 | [diff] [blame] | 129 | this.toggleAttribute('blocked_', tab.blocked); |
John Lee | 4bcbaf1 | 2019-10-21 23:29:46 | [diff] [blame] | 130 | this.setAttribute('draggable', true); |
John Lee | 02b3a74 | 2019-10-15 20:00:56 | [diff] [blame] | 131 | this.toggleAttribute('crashed_', tab.crashed); |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 132 | |
John Lee | 2f3baa7 | 2019-11-12 19:58:14 | [diff] [blame] | 133 | if (tab.title) { |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 134 | this.titleTextEl_.textContent = tab.title; |
John Lee | 2f3baa7 | 2019-11-12 19:58:14 | [diff] [blame] | 135 | } else if ( |
| 136 | !tab.shouldHideThrobber && |
| 137 | (tab.networkState === TabNetworkState.WAITING || |
| 138 | tab.networkState === TabNetworkState.LOADING)) { |
| 139 | this.titleTextEl_.textContent = loadTimeData.getString('loadingTab'); |
| 140 | } else { |
| 141 | this.titleTextEl_.textContent = loadTimeData.getString('defaultTabTitle'); |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 142 | } |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 143 | this.titleTextEl_.setAttribute('aria-label', getAccessibleTitle(tab)); |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 144 | |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 145 | if (tab.networkState === TabNetworkState.WAITING || |
| 146 | (tab.networkState === TabNetworkState.LOADING && |
| 147 | tab.isDefaultFavicon)) { |
| 148 | this.faviconEl_.style.backgroundImage = 'none'; |
| 149 | } else if (tab.favIconUrl) { |
| 150 | this.faviconEl_.style.backgroundImage = `url(${tab.favIconUrl})`; |
John Lee | d14bec6 | 2019-09-23 22:41:32 | [diff] [blame] | 151 | } else { |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 152 | this.faviconEl_.style.backgroundImage = getFavicon(''); |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 153 | } |
| 154 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 155 | // Expose the ID to an attribute to allow easy querySelector use |
| 156 | this.setAttribute('data-tab-id', tab.id); |
| 157 | |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 158 | this.alertIndicatorsEl_.updateAlertStates(tab.alertStates) |
| 159 | .then((alertIndicatorsCount) => { |
| 160 | this.toggleAttribute('has-alert-states_', alertIndicatorsCount > 0); |
| 161 | }); |
| 162 | |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 163 | if (!this.tab_ || (this.tab_.pinned !== tab.pinned && !tab.pinned)) { |
| 164 | this.tabSwiper_.startObserving(); |
| 165 | } else if (this.tab_.pinned !== tab.pinned && tab.pinned) { |
| 166 | this.tabSwiper_.stopObserving(); |
| 167 | } |
| 168 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 169 | this.tab_ = Object.freeze(tab); |
| 170 | } |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 171 | |
John Lee | 7f0c192 | 2019-11-08 22:06:25 | [diff] [blame] | 172 | focus() { |
| 173 | this.tabEl_.focus(); |
| 174 | } |
| 175 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 176 | /** @return {!HTMLElement} */ |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 177 | getDragImage() { |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 178 | return this.tabEl_; |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 182 | * @param {string} imgData |
| 183 | */ |
| 184 | updateThumbnail(imgData) { |
| 185 | this.thumbnail_.src = imgData; |
| 186 | } |
| 187 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 188 | /** @private */ |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 189 | onClick_() { |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 190 | if (!this.tab_ || this.tabSwiper_.wasSwiping()) { |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 194 | this.tabsApi_.activateTab(this.tab_.id); |
John Lee | 4bcbaf1 | 2019-10-21 23:29:46 | [diff] [blame] | 195 | |
| 196 | if (tabStripOptions.autoCloseEnabled) { |
| 197 | this.embedderApi_.closeContainer(); |
| 198 | } |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 199 | } |
| 200 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 201 | /** |
| 202 | * @param {!Event} event |
| 203 | * @private |
| 204 | */ |
Collin Baker | dc3d211 | 2019-10-10 18:28:20 | [diff] [blame] | 205 | onContextMenu_(event) { |
| 206 | event.preventDefault(); |
| 207 | |
| 208 | if (!this.tab_) { |
| 209 | return; |
| 210 | } |
| 211 | |
Collin Baker | 1ac2e43 | 2019-10-16 18:17:21 | [diff] [blame] | 212 | this.embedderApi_.showTabContextMenu( |
Collin Baker | dc3d211 | 2019-10-10 18:28:20 | [diff] [blame] | 213 | this.tab_.id, event.clientX, event.clientY); |
Collin Baker | 48a096b8 | 2019-11-26 01:21:27 | [diff] [blame] | 214 | event.stopPropagation(); |
Collin Baker | dc3d211 | 2019-10-10 18:28:20 | [diff] [blame] | 215 | } |
| 216 | |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 217 | /** |
| 218 | * @param {!Event} event |
| 219 | * @private |
| 220 | */ |
| 221 | onClose_(event) { |
| 222 | if (!this.tab_) { |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | event.stopPropagation(); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 227 | this.tabsApi_.closeTab(this.tab_.id); |
| 228 | } |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 229 | |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 230 | /** @private */ |
| 231 | onSwipe_() { |
| 232 | // Prevent slideOut animation from playing. |
| 233 | this.remove(); |
| 234 | this.tabsApi_.closeTab(this.tab_.id); |
| 235 | } |
| 236 | |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 237 | /** |
John Lee | 7f0c192 | 2019-11-08 22:06:25 | [diff] [blame] | 238 | * @param {!KeyboardEvent} event |
| 239 | * @private |
| 240 | */ |
| 241 | onKeyDown_(event) { |
| 242 | if (event.key === 'Enter' || event.key === ' ') { |
| 243 | this.onClick_(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 248 | * @param {boolean} dragging |
| 249 | */ |
| 250 | setDragging(dragging) { |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 251 | this.toggleAttribute('dragging_', dragging); |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 255 | * @return {!Promise} |
| 256 | */ |
| 257 | slideIn() { |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 258 | const paddingInlineEnd = getPaddingInlineEndProperty(); |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 259 | |
John Lee | 41620f83 | 2019-11-27 22:41:05 | [diff] [blame^] | 260 | // If this TabElement is the last tab, there needs to be enough space for |
| 261 | // the view to scroll to it. Therefore, immediately take up all the space |
| 262 | // it needs to and only animate the scale. |
| 263 | const isLastChild = this.nextElementSibling === null; |
| 264 | |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 265 | const startState = { |
John Lee | 41620f83 | 2019-11-27 22:41:05 | [diff] [blame^] | 266 | maxWidth: isLastChild ? 'var(--tabstrip-tab-width)' : 0, |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 267 | transform: `scale(0)`, |
| 268 | }; |
John Lee | 41620f83 | 2019-11-27 22:41:05 | [diff] [blame^] | 269 | startState[paddingInlineEnd] = |
| 270 | isLastChild ? 'var(--tabstrip-tab-spacing)' : 0; |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 271 | |
| 272 | const finishState = { |
| 273 | maxWidth: `var(--tabstrip-tab-width)`, |
| 274 | transform: `scale(1)`, |
| 275 | }; |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 276 | finishState[paddingInlineEnd] = 'var(--tabstrip-tab-spacing)'; |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 277 | |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 278 | return new Promise(resolve => { |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 279 | const animation = this.animate([startState, finishState], { |
| 280 | duration: 300, |
| 281 | easing: 'cubic-bezier(.4, 0, 0, 1)', |
| 282 | }); |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 283 | animation.onfinish = () => { |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 284 | resolve(); |
| 285 | }; |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 286 | }); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @return {!Promise} |
| 291 | */ |
| 292 | slideOut() { |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 293 | if (!this.embedderApi_.isVisible() || this.tab_.pinned) { |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 294 | // There is no point in animating if the tab strip is hidden. |
| 295 | this.remove(); |
| 296 | return Promise.resolve(); |
| 297 | } |
| 298 | |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 299 | return new Promise(resolve => { |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 300 | const finishCallback = () => { |
| 301 | this.remove(); |
| 302 | resolve(); |
| 303 | }; |
| 304 | |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 305 | const translateAnimation = this.animate( |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 306 | { |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 307 | transform: ['translateY(0)', 'translateY(-100%)'], |
| 308 | }, |
| 309 | { |
| 310 | duration: 150, |
| 311 | easing: 'cubic-bezier(.4, 0, 1, 1)', |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 312 | fill: 'forwards', |
| 313 | }); |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 314 | const opacityAnimation = this.animate( |
| 315 | { |
| 316 | opacity: [1, 0], |
| 317 | }, |
| 318 | { |
| 319 | delay: 97.5, |
| 320 | duration: 50, |
| 321 | fill: 'forwards', |
| 322 | }); |
| 323 | |
| 324 | const widthAnimationKeyframes = { |
| 325 | maxWidth: ['var(--tabstrip-tab-width)', 0], |
| 326 | }; |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 327 | widthAnimationKeyframes[getPaddingInlineEndProperty()] = |
| 328 | ['var(--tabstrip-tab-spacing)', 0]; |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 329 | const widthAnimation = this.animate(widthAnimationKeyframes, { |
| 330 | delay: 97.5, |
| 331 | duration: 300, |
| 332 | easing: 'cubic-bezier(.4, 0, 0, 1)', |
| 333 | fill: 'forwards', |
| 334 | }); |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 335 | |
| 336 | const visibilityChangeListener = () => { |
| 337 | if (!this.embedderApi_.isVisible()) { |
| 338 | // If a tab strip becomes hidden during the animation, the onfinish |
| 339 | // event will not get fired until the tab strip becomes visible again. |
| 340 | // Therefore, when the tab strip becomes hidden, immediately call the |
| 341 | // finish callback. |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 342 | translateAnimation.cancel(); |
| 343 | opacityAnimation.cancel(); |
| 344 | widthAnimation.cancel(); |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 345 | finishCallback(); |
| 346 | } |
| 347 | }; |
| 348 | |
| 349 | document.addEventListener( |
| 350 | 'visibilitychange', visibilityChangeListener, {once: true}); |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 351 | // The onfinish handler is put on the width animation, as it will end |
| 352 | // last. |
| 353 | widthAnimation.onfinish = () => { |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 354 | document.removeEventListener( |
| 355 | 'visibilitychange', visibilityChangeListener); |
| 356 | finishCallback(); |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 357 | }; |
| 358 | }); |
| 359 | } |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 360 | } |
| 361 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 362 | customElements.define('tabstrip-tab', TabElement); |