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