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