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} */ |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame^] | 56 | (this.$('tabstrip-alert-indicators')); |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 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} */ |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame^] | 63 | this.closeButtonEl_ = /** @type {!HTMLElement} */ (this.$('#close')); |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 64 | this.closeButtonEl_.setAttribute( |
| 65 | 'aria-label', loadTimeData.getString('closeTab')); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 66 | |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 67 | /** @private {!HTMLElement} */ |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame^] | 68 | this.dragImageEl_ = /** @type {!HTMLElement} */ (this.$('#dragImage')); |
John Lee | dff2fc8 | 2019-12-19 04:06:16 | [diff] [blame] | 69 | |
| 70 | /** @private {!HTMLElement} */ |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame^] | 71 | this.tabEl_ = /** @type {!HTMLElement} */ (this.$('#tab')); |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 72 | |
| 73 | /** @private {!HTMLElement} */ |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame^] | 74 | this.faviconEl_ = /** @type {!HTMLElement} */ (this.$('#favicon')); |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 75 | |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 76 | /** @private {!HTMLElement} */ |
| 77 | this.thumbnailContainer_ = |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame^] | 78 | /** @type {!HTMLElement} */ (this.$('#thumbnail')); |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 79 | |
| 80 | /** @private {!Image} */ |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame^] | 81 | this.thumbnail_ = /** @type {!Image} */ (this.$('#thumbnailImg')); |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 82 | |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 83 | /** @private {!TabData} */ |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 84 | this.tab_; |
| 85 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 86 | /** @private {!TabsApiProxy} */ |
| 87 | this.tabsApi_ = TabsApiProxy.getInstance(); |
| 88 | |
Collin Baker | 1ac2e43 | 2019-10-16 18:17:21 | [diff] [blame] | 89 | /** @private {!TabStripEmbedderProxy} */ |
| 90 | this.embedderApi_ = TabStripEmbedderProxy.getInstance(); |
| 91 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 92 | /** @private {!HTMLElement} */ |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame^] | 93 | this.titleTextEl_ = /** @type {!HTMLElement} */ (this.$('#titleText')); |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 94 | |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 95 | this.tabEl_.addEventListener('click', () => this.onClick_()); |
| 96 | this.tabEl_.addEventListener('contextmenu', e => this.onContextMenu_(e)); |
John Lee | 7f0c192 | 2019-11-08 22:06:25 | [diff] [blame] | 97 | this.tabEl_.addEventListener( |
| 98 | 'keydown', e => this.onKeyDown_(/** @type {!KeyboardEvent} */ (e))); |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 99 | this.closeButtonEl_.addEventListener('click', e => this.onClose_(e)); |
| 100 | this.addEventListener('swipe', () => this.onSwipe_()); |
| 101 | |
| 102 | /** @private @const {!TabSwiper} */ |
| 103 | this.tabSwiper_ = new TabSwiper(this); |
Robert Liao | 6ab5f71 | 2019-12-04 02:15:01 | [diff] [blame] | 104 | |
| 105 | /** @private {!Function} */ |
| 106 | this.onTabActivating_ = (tabId) => {}; |
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 | |
Robert Liao | 6ab5f71 | 2019-12-04 02:15:01 | [diff] [blame] | 172 | /** @param {!Function} callback */ |
| 173 | set onTabActivating(callback) { |
| 174 | this.onTabActivating_ = callback; |
| 175 | } |
| 176 | |
John Lee | 7f0c192 | 2019-11-08 22:06:25 | [diff] [blame] | 177 | focus() { |
| 178 | this.tabEl_.focus(); |
| 179 | } |
| 180 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 181 | /** @return {!HTMLElement} */ |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 182 | getDragImage() { |
John Lee | dff2fc8 | 2019-12-19 04:06:16 | [diff] [blame] | 183 | return this.dragImageEl_; |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 187 | * @param {string} imgData |
| 188 | */ |
| 189 | updateThumbnail(imgData) { |
| 190 | this.thumbnail_.src = imgData; |
| 191 | } |
| 192 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 193 | /** @private */ |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 194 | onClick_() { |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 195 | if (!this.tab_ || this.tabSwiper_.wasSwiping()) { |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | |
Robert Liao | 6ab5f71 | 2019-12-04 02:15:01 | [diff] [blame] | 199 | const tabId = this.tab_.id; |
| 200 | this.onTabActivating_(tabId); |
| 201 | this.tabsApi_.activateTab(tabId); |
John Lee | 4bcbaf1 | 2019-10-21 23:29:46 | [diff] [blame] | 202 | |
| 203 | if (tabStripOptions.autoCloseEnabled) { |
| 204 | this.embedderApi_.closeContainer(); |
| 205 | } |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 206 | } |
| 207 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 208 | /** |
| 209 | * @param {!Event} event |
| 210 | * @private |
| 211 | */ |
Collin Baker | dc3d211 | 2019-10-10 18:28:20 | [diff] [blame] | 212 | onContextMenu_(event) { |
| 213 | event.preventDefault(); |
| 214 | |
| 215 | if (!this.tab_) { |
| 216 | return; |
| 217 | } |
| 218 | |
Collin Baker | 1ac2e43 | 2019-10-16 18:17:21 | [diff] [blame] | 219 | this.embedderApi_.showTabContextMenu( |
Collin Baker | dc3d211 | 2019-10-10 18:28:20 | [diff] [blame] | 220 | this.tab_.id, event.clientX, event.clientY); |
Collin Baker | 48a096b8 | 2019-11-26 01:21:27 | [diff] [blame] | 221 | event.stopPropagation(); |
Collin Baker | dc3d211 | 2019-10-10 18:28:20 | [diff] [blame] | 222 | } |
| 223 | |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 224 | /** |
| 225 | * @param {!Event} event |
| 226 | * @private |
| 227 | */ |
| 228 | onClose_(event) { |
| 229 | if (!this.tab_) { |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | event.stopPropagation(); |
John Lee | adf33c8 | 2019-12-12 18:21:33 | [diff] [blame] | 234 | this.tabsApi_.closeTab(this.tab_.id, CloseTabAction.CLOSE_BUTTON); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 235 | } |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 236 | |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 237 | /** @private */ |
| 238 | onSwipe_() { |
| 239 | // Prevent slideOut animation from playing. |
| 240 | this.remove(); |
John Lee | adf33c8 | 2019-12-12 18:21:33 | [diff] [blame] | 241 | this.tabsApi_.closeTab(this.tab_.id, CloseTabAction.SWIPED_TO_CLOSE); |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 242 | } |
| 243 | |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 244 | /** |
John Lee | 7f0c192 | 2019-11-08 22:06:25 | [diff] [blame] | 245 | * @param {!KeyboardEvent} event |
| 246 | * @private |
| 247 | */ |
| 248 | onKeyDown_(event) { |
| 249 | if (event.key === 'Enter' || event.key === ' ') { |
| 250 | this.onClick_(); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /** |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 255 | * @param {boolean} dragging |
| 256 | */ |
| 257 | setDragging(dragging) { |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 258 | this.toggleAttribute('dragging_', dragging); |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /** |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 262 | * @return {!Promise} |
| 263 | */ |
| 264 | slideIn() { |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 265 | const paddingInlineEnd = getPaddingInlineEndProperty(); |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 266 | |
John Lee | 41620f83 | 2019-11-27 22:41:05 | [diff] [blame] | 267 | // If this TabElement is the last tab, there needs to be enough space for |
| 268 | // the view to scroll to it. Therefore, immediately take up all the space |
| 269 | // it needs to and only animate the scale. |
| 270 | const isLastChild = this.nextElementSibling === null; |
| 271 | |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 272 | const startState = { |
John Lee | 41620f83 | 2019-11-27 22:41:05 | [diff] [blame] | 273 | maxWidth: isLastChild ? 'var(--tabstrip-tab-width)' : 0, |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 274 | transform: `scale(0)`, |
| 275 | }; |
John Lee | 41620f83 | 2019-11-27 22:41:05 | [diff] [blame] | 276 | startState[paddingInlineEnd] = |
| 277 | isLastChild ? 'var(--tabstrip-tab-spacing)' : 0; |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 278 | |
| 279 | const finishState = { |
| 280 | maxWidth: `var(--tabstrip-tab-width)`, |
| 281 | transform: `scale(1)`, |
| 282 | }; |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 283 | finishState[paddingInlineEnd] = 'var(--tabstrip-tab-spacing)'; |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 284 | |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 285 | return new Promise(resolve => { |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 286 | const animation = this.animate([startState, finishState], { |
| 287 | duration: 300, |
| 288 | easing: 'cubic-bezier(.4, 0, 0, 1)', |
| 289 | }); |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 290 | animation.onfinish = () => { |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 291 | resolve(); |
| 292 | }; |
John Lee | 29411b5 | 2019-12-20 02:16:15 | [diff] [blame] | 293 | |
| 294 | // TODO(crbug.com/1035678) By the next animation frame, the animation |
| 295 | // should start playing. By the time another animation frame happens, |
| 296 | // force play the animation if the animation has not yet begun. Remove |
| 297 | // if/when the Blink issue has been fixed. |
| 298 | requestAnimationFrame(() => { |
| 299 | requestAnimationFrame(() => { |
| 300 | if (animation.pending) { |
| 301 | animation.play(); |
| 302 | } |
| 303 | }); |
| 304 | }); |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 305 | }); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * @return {!Promise} |
| 310 | */ |
| 311 | slideOut() { |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 312 | if (!this.embedderApi_.isVisible() || this.tab_.pinned) { |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 313 | // There is no point in animating if the tab strip is hidden. |
| 314 | this.remove(); |
| 315 | return Promise.resolve(); |
| 316 | } |
| 317 | |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 318 | return new Promise(resolve => { |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 319 | const finishCallback = () => { |
| 320 | this.remove(); |
| 321 | resolve(); |
| 322 | }; |
| 323 | |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 324 | const translateAnimation = this.animate( |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 325 | { |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 326 | transform: ['translateY(0)', 'translateY(-100%)'], |
| 327 | }, |
| 328 | { |
| 329 | duration: 150, |
| 330 | easing: 'cubic-bezier(.4, 0, 1, 1)', |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 331 | fill: 'forwards', |
| 332 | }); |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 333 | const opacityAnimation = this.animate( |
| 334 | { |
| 335 | opacity: [1, 0], |
| 336 | }, |
| 337 | { |
| 338 | delay: 97.5, |
| 339 | duration: 50, |
| 340 | fill: 'forwards', |
| 341 | }); |
| 342 | |
| 343 | const widthAnimationKeyframes = { |
| 344 | maxWidth: ['var(--tabstrip-tab-width)', 0], |
| 345 | }; |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 346 | widthAnimationKeyframes[getPaddingInlineEndProperty()] = |
| 347 | ['var(--tabstrip-tab-spacing)', 0]; |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 348 | const widthAnimation = this.animate(widthAnimationKeyframes, { |
| 349 | delay: 97.5, |
| 350 | duration: 300, |
| 351 | easing: 'cubic-bezier(.4, 0, 0, 1)', |
| 352 | fill: 'forwards', |
| 353 | }); |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 354 | |
| 355 | const visibilityChangeListener = () => { |
| 356 | if (!this.embedderApi_.isVisible()) { |
| 357 | // If a tab strip becomes hidden during the animation, the onfinish |
| 358 | // event will not get fired until the tab strip becomes visible again. |
| 359 | // Therefore, when the tab strip becomes hidden, immediately call the |
| 360 | // finish callback. |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 361 | translateAnimation.cancel(); |
| 362 | opacityAnimation.cancel(); |
| 363 | widthAnimation.cancel(); |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 364 | finishCallback(); |
| 365 | } |
| 366 | }; |
| 367 | |
| 368 | document.addEventListener( |
| 369 | 'visibilitychange', visibilityChangeListener, {once: true}); |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 370 | // The onfinish handler is put on the width animation, as it will end |
| 371 | // last. |
| 372 | widthAnimation.onfinish = () => { |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 373 | document.removeEventListener( |
| 374 | 'visibilitychange', visibilityChangeListener); |
| 375 | finishCallback(); |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 376 | }; |
| 377 | }); |
| 378 | } |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 379 | } |
| 380 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 381 | customElements.define('tabstrip-tab', TabElement); |