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'; |
John Lee | 103bb77b | 2020-05-19 18:59:21 | [diff] [blame] | 14 | import {TabStripEmbedderProxy, TabStripEmbedderProxyImpl} from './tab_strip_embedder_proxy.js'; |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 15 | import {TabSwiper} from './tab_swiper.js'; |
dpapad | 3d9fd736 | 2020-05-15 17:12:01 | [diff] [blame] | 16 | import {CloseTabAction, TabData, TabNetworkState, TabsApiProxy, TabsApiProxyImpl} from './tabs_api_proxy.js'; |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 17 | |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 18 | const DEFAULT_ANIMATION_DURATION = 125; |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 19 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 20 | /** |
| 21 | * @param {!TabData} tab |
| 22 | * @return {string} |
| 23 | */ |
| 24 | function getAccessibleTitle(tab) { |
| 25 | const tabTitle = tab.title; |
| 26 | |
| 27 | if (tab.crashed) { |
| 28 | return loadTimeData.getStringF('tabCrashed', tabTitle); |
| 29 | } |
| 30 | |
| 31 | if (tab.networkState === TabNetworkState.ERROR) { |
| 32 | return loadTimeData.getStringF('tabNetworkError', tabTitle); |
| 33 | } |
| 34 | |
| 35 | return tabTitle; |
| 36 | } |
| 37 | |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 38 | /** |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 39 | * TODO(crbug.com/1025390): padding-inline-end cannot be animated yet. |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 40 | * @return {string} |
| 41 | */ |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 42 | function getPaddingInlineEndProperty() { |
| 43 | return isRTL() ? 'paddingLeft' : 'paddingRight'; |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 44 | } |
| 45 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 46 | export class TabElement extends CustomElement { |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 47 | static get template() { |
| 48 | return `{__html_template__}`; |
| 49 | } |
| 50 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 51 | constructor() { |
| 52 | super(); |
| 53 | |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 54 | this.alertIndicatorsEl_ = /** @type {!AlertIndicatorsElement} */ |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame] | 55 | (this.$('tabstrip-alert-indicators')); |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 56 | // Normally, custom elements will get upgraded automatically once added to |
| 57 | // the DOM, but TabElement may need to update properties on |
| 58 | // AlertIndicatorElement before this happens, so upgrade it manually. |
| 59 | customElements.upgrade(this.alertIndicatorsEl_); |
| 60 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 61 | /** @private {!HTMLElement} */ |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame] | 62 | this.closeButtonEl_ = /** @type {!HTMLElement} */ (this.$('#close')); |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 63 | this.closeButtonEl_.setAttribute( |
| 64 | 'aria-label', loadTimeData.getString('closeTab')); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 65 | |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 66 | /** @private {!HTMLElement} */ |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame] | 67 | this.dragImageEl_ = /** @type {!HTMLElement} */ (this.$('#dragImage')); |
John Lee | dff2fc8 | 2019-12-19 04:06:16 | [diff] [blame] | 68 | |
| 69 | /** @private {!HTMLElement} */ |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame] | 70 | this.tabEl_ = /** @type {!HTMLElement} */ (this.$('#tab')); |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 71 | |
| 72 | /** @private {!HTMLElement} */ |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame] | 73 | this.faviconEl_ = /** @type {!HTMLElement} */ (this.$('#favicon')); |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 74 | |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 75 | /** @private {!HTMLElement} */ |
| 76 | this.thumbnailContainer_ = |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame] | 77 | /** @type {!HTMLElement} */ (this.$('#thumbnail')); |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 78 | |
| 79 | /** @private {!Image} */ |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame] | 80 | this.thumbnail_ = /** @type {!Image} */ (this.$('#thumbnailImg')); |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 81 | |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 82 | /** @private {!TabData} */ |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 83 | this.tab_; |
| 84 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 85 | /** @private {!TabsApiProxy} */ |
dpapad | 3d9fd736 | 2020-05-15 17:12:01 | [diff] [blame] | 86 | this.tabsApi_ = TabsApiProxyImpl.getInstance(); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 87 | |
Collin Baker | 1ac2e43 | 2019-10-16 18:17:21 | [diff] [blame] | 88 | /** @private {!TabStripEmbedderProxy} */ |
John Lee | 103bb77b | 2020-05-19 18:59:21 | [diff] [blame] | 89 | this.embedderApi_ = TabStripEmbedderProxyImpl.getInstance(); |
Collin Baker | 1ac2e43 | 2019-10-16 18:17:21 | [diff] [blame] | 90 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 91 | /** @private {!HTMLElement} */ |
Demetrios Papadopoulos | 94a1513 | 2020-01-27 21:45:28 | [diff] [blame] | 92 | this.titleTextEl_ = /** @type {!HTMLElement} */ (this.$('#titleText')); |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 93 | |
John Lee | 1a8ab21 | 2020-06-05 18:06:43 | [diff] [blame] | 94 | /** |
| 95 | * Flag indicating if this TabElement can accept dragover events. This |
| 96 | * is used to pause dragover events while animating as animating causes |
| 97 | * the elements below the pointer to shift. |
| 98 | * @private {boolean} |
| 99 | */ |
| 100 | this.isValidDragOverTarget_ = true; |
| 101 | |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 102 | this.tabEl_.addEventListener('click', () => this.onClick_()); |
| 103 | this.tabEl_.addEventListener('contextmenu', e => this.onContextMenu_(e)); |
John Lee | 7f0c192 | 2019-11-08 22:06:25 | [diff] [blame] | 104 | this.tabEl_.addEventListener( |
| 105 | 'keydown', e => this.onKeyDown_(/** @type {!KeyboardEvent} */ (e))); |
John Lee | a00d58b | 2020-10-09 23:28:27 | [diff] [blame^] | 106 | this.tabEl_.addEventListener( |
| 107 | 'pointerup', e => this.onPointerUp_(/** @type {!PointerEvent} */ (e))); |
| 108 | |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 109 | this.closeButtonEl_.addEventListener('click', e => this.onClose_(e)); |
| 110 | this.addEventListener('swipe', () => this.onSwipe_()); |
| 111 | |
| 112 | /** @private @const {!TabSwiper} */ |
| 113 | this.tabSwiper_ = new TabSwiper(this); |
Robert Liao | 6ab5f71 | 2019-12-04 02:15:01 | [diff] [blame] | 114 | |
| 115 | /** @private {!Function} */ |
| 116 | this.onTabActivating_ = (tabId) => {}; |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 117 | } |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 118 | |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 119 | /** @return {!TabData} */ |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 120 | get tab() { |
| 121 | return this.tab_; |
| 122 | } |
| 123 | |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 124 | /** @param {!TabData} tab */ |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 125 | set tab(tab) { |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 126 | assert(this.tab_ !== tab); |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 127 | this.toggleAttribute('active', tab.active); |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 128 | this.tabEl_.setAttribute('aria-selected', tab.active.toString()); |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 129 | this.toggleAttribute('hide-icon_', !tab.showIcon); |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 130 | this.toggleAttribute( |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 131 | 'waiting_', |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 132 | !tab.shouldHideThrobber && |
| 133 | tab.networkState === TabNetworkState.WAITING); |
| 134 | this.toggleAttribute( |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 135 | 'loading_', |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 136 | !tab.shouldHideThrobber && |
| 137 | tab.networkState === TabNetworkState.LOADING); |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 138 | this.toggleAttribute('pinned', tab.pinned); |
John Lee | 4734ca1 | 2019-10-14 19:34:01 | [diff] [blame] | 139 | this.toggleAttribute('blocked_', tab.blocked); |
John Lee | 4bcbaf1 | 2019-10-21 23:29:46 | [diff] [blame] | 140 | this.setAttribute('draggable', true); |
John Lee | 02b3a74 | 2019-10-15 20:00:56 | [diff] [blame] | 141 | this.toggleAttribute('crashed_', tab.crashed); |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 142 | |
John Lee | 2f3baa7 | 2019-11-12 19:58:14 | [diff] [blame] | 143 | if (tab.title) { |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 144 | this.titleTextEl_.textContent = tab.title; |
John Lee | 2f3baa7 | 2019-11-12 19:58:14 | [diff] [blame] | 145 | } else if ( |
| 146 | !tab.shouldHideThrobber && |
| 147 | (tab.networkState === TabNetworkState.WAITING || |
| 148 | tab.networkState === TabNetworkState.LOADING)) { |
| 149 | this.titleTextEl_.textContent = loadTimeData.getString('loadingTab'); |
| 150 | } else { |
| 151 | this.titleTextEl_.textContent = loadTimeData.getString('defaultTabTitle'); |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 152 | } |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 153 | this.titleTextEl_.setAttribute('aria-label', getAccessibleTitle(tab)); |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 154 | |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 155 | if (tab.networkState === TabNetworkState.WAITING || |
| 156 | (tab.networkState === TabNetworkState.LOADING && |
| 157 | tab.isDefaultFavicon)) { |
| 158 | this.faviconEl_.style.backgroundImage = 'none'; |
| 159 | } else if (tab.favIconUrl) { |
| 160 | this.faviconEl_.style.backgroundImage = `url(${tab.favIconUrl})`; |
John Lee | d14bec6 | 2019-09-23 22:41:32 | [diff] [blame] | 161 | } else { |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 162 | this.faviconEl_.style.backgroundImage = getFavicon(''); |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 163 | } |
| 164 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 165 | // Expose the ID to an attribute to allow easy querySelector use |
| 166 | this.setAttribute('data-tab-id', tab.id); |
| 167 | |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 168 | this.alertIndicatorsEl_.updateAlertStates(tab.alertStates) |
| 169 | .then((alertIndicatorsCount) => { |
| 170 | this.toggleAttribute('has-alert-states_', alertIndicatorsCount > 0); |
| 171 | }); |
| 172 | |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 173 | if (!this.tab_ || (this.tab_.pinned !== tab.pinned && !tab.pinned)) { |
| 174 | this.tabSwiper_.startObserving(); |
| 175 | } else if (this.tab_.pinned !== tab.pinned && tab.pinned) { |
| 176 | this.tabSwiper_.stopObserving(); |
| 177 | } |
| 178 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 179 | this.tab_ = Object.freeze(tab); |
| 180 | } |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 181 | |
John Lee | 1a8ab21 | 2020-06-05 18:06:43 | [diff] [blame] | 182 | /** @return {boolean} */ |
| 183 | get isValidDragOverTarget() { |
| 184 | return !this.hasAttribute('dragging_') && this.isValidDragOverTarget_; |
| 185 | } |
| 186 | |
| 187 | /** @param {boolean} isValid */ |
| 188 | set isValidDragOverTarget(isValid) { |
| 189 | this.isValidDragOverTarget_ = isValid; |
| 190 | } |
| 191 | |
Robert Liao | 6ab5f71 | 2019-12-04 02:15:01 | [diff] [blame] | 192 | /** @param {!Function} callback */ |
| 193 | set onTabActivating(callback) { |
| 194 | this.onTabActivating_ = callback; |
| 195 | } |
| 196 | |
John Lee | 7f0c192 | 2019-11-08 22:06:25 | [diff] [blame] | 197 | focus() { |
| 198 | this.tabEl_.focus(); |
| 199 | } |
| 200 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 201 | /** @return {!HTMLElement} */ |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 202 | getDragImage() { |
John Lee | dff2fc8 | 2019-12-19 04:06:16 | [diff] [blame] | 203 | return this.dragImageEl_; |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 204 | } |
| 205 | |
John Lee | 10e2eac | 2020-07-20 18:27:54 | [diff] [blame] | 206 | /** @return {!HTMLElement} */ |
| 207 | getDragImageCenter() { |
| 208 | // dragImageEl_ has padding, so the drag image should be centered relative |
| 209 | // to tabEl_, the element within the padding. |
| 210 | return this.tabEl_; |
| 211 | } |
| 212 | |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 213 | /** |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 214 | * @param {string} imgData |
| 215 | */ |
| 216 | updateThumbnail(imgData) { |
| 217 | this.thumbnail_.src = imgData; |
| 218 | } |
| 219 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 220 | /** @private */ |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 221 | onClick_() { |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 222 | if (!this.tab_ || this.tabSwiper_.wasSwiping()) { |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 223 | return; |
| 224 | } |
| 225 | |
Robert Liao | 6ab5f71 | 2019-12-04 02:15:01 | [diff] [blame] | 226 | const tabId = this.tab_.id; |
| 227 | this.onTabActivating_(tabId); |
| 228 | this.tabsApi_.activateTab(tabId); |
John Lee | 4bcbaf1 | 2019-10-21 23:29:46 | [diff] [blame] | 229 | |
John Lee | 13f336f | 2020-07-08 02:27:42 | [diff] [blame] | 230 | this.embedderApi_.closeContainer(); |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 231 | } |
| 232 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 233 | /** |
| 234 | * @param {!Event} event |
| 235 | * @private |
| 236 | */ |
Collin Baker | dc3d211 | 2019-10-10 18:28:20 | [diff] [blame] | 237 | onContextMenu_(event) { |
| 238 | event.preventDefault(); |
Collin Baker | 48a096b8 | 2019-11-26 01:21:27 | [diff] [blame] | 239 | event.stopPropagation(); |
Collin Baker | dc3d211 | 2019-10-10 18:28:20 | [diff] [blame] | 240 | } |
| 241 | |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 242 | /** |
| 243 | * @param {!Event} event |
| 244 | * @private |
| 245 | */ |
| 246 | onClose_(event) { |
John Lee | c24f482 | 2020-03-10 21:32:55 | [diff] [blame] | 247 | assert(this.tab_); |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 248 | event.stopPropagation(); |
John Lee | adf33c8 | 2019-12-12 18:21:33 | [diff] [blame] | 249 | this.tabsApi_.closeTab(this.tab_.id, CloseTabAction.CLOSE_BUTTON); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 250 | } |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 251 | |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 252 | /** @private */ |
| 253 | onSwipe_() { |
John Lee | c24f482 | 2020-03-10 21:32:55 | [diff] [blame] | 254 | assert(this.tab_); |
John Lee | adf33c8 | 2019-12-12 18:21:33 | [diff] [blame] | 255 | this.tabsApi_.closeTab(this.tab_.id, CloseTabAction.SWIPED_TO_CLOSE); |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 256 | } |
| 257 | |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 258 | /** |
John Lee | 7f0c192 | 2019-11-08 22:06:25 | [diff] [blame] | 259 | * @param {!KeyboardEvent} event |
| 260 | * @private |
| 261 | */ |
| 262 | onKeyDown_(event) { |
| 263 | if (event.key === 'Enter' || event.key === ' ') { |
| 264 | this.onClick_(); |
| 265 | } |
| 266 | } |
| 267 | |
John Lee | a00d58b | 2020-10-09 23:28:27 | [diff] [blame^] | 268 | /** |
| 269 | * @param {!PointerEvent} event |
| 270 | * @private |
| 271 | */ |
| 272 | onPointerUp_(event) { |
| 273 | if (event.pointerType !== 'touch' && event.button === 2) { |
| 274 | this.embedderApi_.showTabContextMenu( |
| 275 | this.tab.id, event.clientX, event.clientY); |
| 276 | } |
| 277 | } |
| 278 | |
John Lee | c24f482 | 2020-03-10 21:32:55 | [diff] [blame] | 279 | resetSwipe() { |
| 280 | this.tabSwiper_.reset(); |
| 281 | } |
| 282 | |
John Lee | 7f0c192 | 2019-11-08 22:06:25 | [diff] [blame] | 283 | /** |
John Lee | 870be419 | 2020-01-27 23:23:02 | [diff] [blame] | 284 | * @param {boolean} isDragging |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 285 | */ |
John Lee | 870be419 | 2020-01-27 23:23:02 | [diff] [blame] | 286 | setDragging(isDragging) { |
| 287 | this.toggleAttribute('dragging_', isDragging); |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 288 | } |
| 289 | |
John Lee | 656beed | 2020-07-07 22:45:52 | [diff] [blame] | 290 | /** @param {boolean} isDraggedOut */ |
| 291 | setDraggedOut(isDraggedOut) { |
| 292 | this.toggleAttribute('dragged-out_', isDraggedOut); |
| 293 | } |
| 294 | |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 295 | /** |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 296 | * @return {!Promise} |
| 297 | */ |
| 298 | slideIn() { |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 299 | const paddingInlineEnd = getPaddingInlineEndProperty(); |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 300 | |
John Lee | 41620f83 | 2019-11-27 22:41:05 | [diff] [blame] | 301 | // If this TabElement is the last tab, there needs to be enough space for |
| 302 | // the view to scroll to it. Therefore, immediately take up all the space |
| 303 | // it needs to and only animate the scale. |
| 304 | const isLastChild = this.nextElementSibling === null; |
| 305 | |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 306 | const startState = { |
John Lee | 41620f83 | 2019-11-27 22:41:05 | [diff] [blame] | 307 | maxWidth: isLastChild ? 'var(--tabstrip-tab-width)' : 0, |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 308 | transform: `scale(0)`, |
| 309 | }; |
John Lee | 41620f83 | 2019-11-27 22:41:05 | [diff] [blame] | 310 | startState[paddingInlineEnd] = |
| 311 | isLastChild ? 'var(--tabstrip-tab-spacing)' : 0; |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 312 | |
| 313 | const finishState = { |
| 314 | maxWidth: `var(--tabstrip-tab-width)`, |
| 315 | transform: `scale(1)`, |
| 316 | }; |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 317 | finishState[paddingInlineEnd] = 'var(--tabstrip-tab-spacing)'; |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 318 | |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 319 | return new Promise(resolve => { |
John Lee | c6bc69d | 2019-11-18 21:53:17 | [diff] [blame] | 320 | const animation = this.animate([startState, finishState], { |
| 321 | duration: 300, |
| 322 | easing: 'cubic-bezier(.4, 0, 0, 1)', |
| 323 | }); |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 324 | animation.onfinish = () => { |
John Lee | 6b1d957 | 2019-11-05 00:33:06 | [diff] [blame] | 325 | resolve(); |
| 326 | }; |
John Lee | 29411b5 | 2019-12-20 02:16:15 | [diff] [blame] | 327 | |
| 328 | // TODO(crbug.com/1035678) By the next animation frame, the animation |
| 329 | // should start playing. By the time another animation frame happens, |
| 330 | // force play the animation if the animation has not yet begun. Remove |
| 331 | // if/when the Blink issue has been fixed. |
| 332 | requestAnimationFrame(() => { |
| 333 | requestAnimationFrame(() => { |
| 334 | if (animation.pending) { |
| 335 | animation.play(); |
| 336 | } |
| 337 | }); |
| 338 | }); |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 339 | }); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * @return {!Promise} |
| 344 | */ |
| 345 | slideOut() { |
John Lee | c24f482 | 2020-03-10 21:32:55 | [diff] [blame] | 346 | if (!this.embedderApi_.isVisible() || this.tab_.pinned || |
| 347 | this.tabSwiper_.wasSwiping()) { |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 348 | this.remove(); |
| 349 | return Promise.resolve(); |
| 350 | } |
| 351 | |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 352 | return new Promise(resolve => { |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 353 | const finishCallback = () => { |
| 354 | this.remove(); |
| 355 | resolve(); |
| 356 | }; |
| 357 | |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 358 | const translateAnimation = this.animate( |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 359 | { |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 360 | transform: ['translateY(0)', 'translateY(-100%)'], |
| 361 | }, |
| 362 | { |
| 363 | duration: 150, |
| 364 | easing: 'cubic-bezier(.4, 0, 1, 1)', |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 365 | fill: 'forwards', |
| 366 | }); |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 367 | const opacityAnimation = this.animate( |
| 368 | { |
| 369 | opacity: [1, 0], |
| 370 | }, |
| 371 | { |
| 372 | delay: 97.5, |
| 373 | duration: 50, |
| 374 | fill: 'forwards', |
| 375 | }); |
| 376 | |
| 377 | const widthAnimationKeyframes = { |
| 378 | maxWidth: ['var(--tabstrip-tab-width)', 0], |
| 379 | }; |
John Lee | d96ee053 | 2019-11-22 22:06:39 | [diff] [blame] | 380 | widthAnimationKeyframes[getPaddingInlineEndProperty()] = |
| 381 | ['var(--tabstrip-tab-spacing)', 0]; |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 382 | const widthAnimation = this.animate(widthAnimationKeyframes, { |
| 383 | delay: 97.5, |
| 384 | duration: 300, |
| 385 | easing: 'cubic-bezier(.4, 0, 0, 1)', |
| 386 | fill: 'forwards', |
| 387 | }); |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 388 | |
| 389 | const visibilityChangeListener = () => { |
| 390 | if (!this.embedderApi_.isVisible()) { |
| 391 | // If a tab strip becomes hidden during the animation, the onfinish |
| 392 | // event will not get fired until the tab strip becomes visible again. |
| 393 | // Therefore, when the tab strip becomes hidden, immediately call the |
| 394 | // finish callback. |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 395 | translateAnimation.cancel(); |
| 396 | opacityAnimation.cancel(); |
| 397 | widthAnimation.cancel(); |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 398 | finishCallback(); |
| 399 | } |
| 400 | }; |
| 401 | |
| 402 | document.addEventListener( |
| 403 | 'visibilitychange', visibilityChangeListener, {once: true}); |
John Lee | 7bbf7ad | 2019-11-21 23:38:51 | [diff] [blame] | 404 | // The onfinish handler is put on the width animation, as it will end |
| 405 | // last. |
| 406 | widthAnimation.onfinish = () => { |
John Lee | d71aaac | 2019-11-14 20:37:50 | [diff] [blame] | 407 | document.removeEventListener( |
| 408 | 'visibilitychange', visibilityChangeListener); |
| 409 | finishCallback(); |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 410 | }; |
| 411 | }); |
| 412 | } |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 413 | } |
| 414 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 415 | customElements.define('tabstrip-tab', TabElement); |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 416 | |
| 417 | /** |
| 418 | * @param {!Element} element |
| 419 | * @return {boolean} |
| 420 | */ |
| 421 | export function isTabElement(element) { |
| 422 | return element.tagName === 'TABSTRIP-TAB'; |
| 423 | } |