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 | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 10 | |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 11 | import {AlertIndicatorsElement} from './alert_indicators.js'; |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 12 | import {CustomElement} from './custom_element.js'; |
Collin Baker | 1ac2e43 | 2019-10-16 18:17:21 | [diff] [blame] | 13 | import {TabStripEmbedderProxy} from './tab_strip_embedder_proxy.js'; |
John Lee | 4bcbaf1 | 2019-10-21 23:29:46 | [diff] [blame] | 14 | import {tabStripOptions} from './tab_strip_options.js'; |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 15 | import {TabData, TabNetworkState, TabsApiProxy} from './tabs_api_proxy.js'; |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 16 | |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 17 | export const DEFAULT_ANIMATION_DURATION = 125; |
| 18 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 19 | /** |
| 20 | * @param {!TabData} tab |
| 21 | * @return {string} |
| 22 | */ |
| 23 | function getAccessibleTitle(tab) { |
| 24 | const tabTitle = tab.title; |
| 25 | |
| 26 | if (tab.crashed) { |
| 27 | return loadTimeData.getStringF('tabCrashed', tabTitle); |
| 28 | } |
| 29 | |
| 30 | if (tab.networkState === TabNetworkState.ERROR) { |
| 31 | return loadTimeData.getStringF('tabNetworkError', tabTitle); |
| 32 | } |
| 33 | |
| 34 | return tabTitle; |
| 35 | } |
| 36 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 37 | export class TabElement extends CustomElement { |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 38 | static get template() { |
| 39 | return `{__html_template__}`; |
| 40 | } |
| 41 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 42 | constructor() { |
| 43 | super(); |
| 44 | |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 45 | this.alertIndicatorsEl_ = /** @type {!AlertIndicatorsElement} */ |
| 46 | (this.shadowRoot.querySelector('tabstrip-alert-indicators')); |
| 47 | // Normally, custom elements will get upgraded automatically once added to |
| 48 | // the DOM, but TabElement may need to update properties on |
| 49 | // AlertIndicatorElement before this happens, so upgrade it manually. |
| 50 | customElements.upgrade(this.alertIndicatorsEl_); |
| 51 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 52 | /** @private {!HTMLElement} */ |
| 53 | this.closeButtonEl_ = |
| 54 | /** @type {!HTMLElement} */ (this.shadowRoot.querySelector('#close')); |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 55 | this.closeButtonEl_.setAttribute( |
| 56 | 'aria-label', loadTimeData.getString('closeTab')); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 57 | |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 58 | /** @private {!HTMLElement} */ |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 59 | this.tabEl_ = |
| 60 | /** @type {!HTMLElement} */ (this.shadowRoot.querySelector('#tab')); |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 61 | |
| 62 | /** @private {!HTMLElement} */ |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 63 | this.faviconEl_ = |
| 64 | /** @type {!HTMLElement} */ (this.shadowRoot.querySelector('#favicon')); |
| 65 | |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 66 | /** @private {!HTMLElement} */ |
| 67 | this.thumbnailContainer_ = |
| 68 | /** @type {!HTMLElement} */ ( |
| 69 | this.shadowRoot.querySelector('#thumbnail')); |
| 70 | |
| 71 | /** @private {!Image} */ |
| 72 | this.thumbnail_ = |
| 73 | /** @type {!Image} */ (this.shadowRoot.querySelector('#thumbnailImg')); |
| 74 | |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 75 | /** @private {!TabData} */ |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 76 | this.tab_; |
| 77 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 78 | /** @private {!TabsApiProxy} */ |
| 79 | this.tabsApi_ = TabsApiProxy.getInstance(); |
| 80 | |
Collin Baker | 1ac2e43 | 2019-10-16 18:17:21 | [diff] [blame] | 81 | /** @private {!TabStripEmbedderProxy} */ |
| 82 | this.embedderApi_ = TabStripEmbedderProxy.getInstance(); |
| 83 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 84 | /** @private {!HTMLElement} */ |
| 85 | this.titleTextEl_ = /** @type {!HTMLElement} */ ( |
| 86 | this.shadowRoot.querySelector('#titleText')); |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 87 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 88 | this.tabEl_.addEventListener('click', this.onClick_.bind(this)); |
| 89 | this.tabEl_.addEventListener('contextmenu', this.onContextMenu_.bind(this)); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 90 | this.closeButtonEl_.addEventListener('click', this.onClose_.bind(this)); |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 91 | } |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 92 | |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 93 | /** @return {!TabData} */ |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 94 | get tab() { |
| 95 | return this.tab_; |
| 96 | } |
| 97 | |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 98 | /** @param {!TabData} tab */ |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 99 | set tab(tab) { |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 100 | assert(this.tab_ !== tab); |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 101 | this.toggleAttribute('active', tab.active); |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 102 | this.tabEl_.setAttribute('aria-selected', tab.active.toString()); |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 103 | this.toggleAttribute('hide-icon_', !tab.showIcon); |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 104 | this.toggleAttribute( |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 105 | 'waiting_', |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 106 | !tab.shouldHideThrobber && |
| 107 | tab.networkState === TabNetworkState.WAITING); |
| 108 | this.toggleAttribute( |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 109 | 'loading_', |
John Lee | 99367a6 | 2019-10-10 19:03:49 | [diff] [blame] | 110 | !tab.shouldHideThrobber && |
| 111 | tab.networkState === TabNetworkState.LOADING); |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 112 | this.toggleAttribute('pinned', tab.pinned); |
John Lee | 4734ca1 | 2019-10-14 19:34:01 | [diff] [blame] | 113 | this.toggleAttribute('blocked_', tab.blocked); |
John Lee | 4bcbaf1 | 2019-10-21 23:29:46 | [diff] [blame] | 114 | this.setAttribute('draggable', true); |
John Lee | 02b3a74 | 2019-10-15 20:00:56 | [diff] [blame] | 115 | this.toggleAttribute('crashed_', tab.crashed); |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 116 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 117 | if (!this.tab_ || this.tab_.title !== tab.title) { |
| 118 | this.titleTextEl_.textContent = tab.title; |
| 119 | } |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 120 | this.titleTextEl_.setAttribute('aria-label', getAccessibleTitle(tab)); |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 121 | |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 122 | if (tab.networkState === TabNetworkState.WAITING || |
| 123 | (tab.networkState === TabNetworkState.LOADING && |
| 124 | tab.isDefaultFavicon)) { |
| 125 | this.faviconEl_.style.backgroundImage = 'none'; |
| 126 | } else if (tab.favIconUrl) { |
| 127 | this.faviconEl_.style.backgroundImage = `url(${tab.favIconUrl})`; |
John Lee | d14bec6 | 2019-09-23 22:41:32 | [diff] [blame] | 128 | } else { |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 129 | this.faviconEl_.style.backgroundImage = getFavicon(''); |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 130 | } |
| 131 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 132 | // Expose the ID to an attribute to allow easy querySelector use |
| 133 | this.setAttribute('data-tab-id', tab.id); |
| 134 | |
John Lee | a8e39e68 | 2019-10-18 02:19:38 | [diff] [blame] | 135 | this.alertIndicatorsEl_.updateAlertStates(tab.alertStates) |
| 136 | .then((alertIndicatorsCount) => { |
| 137 | this.toggleAttribute('has-alert-states_', alertIndicatorsCount > 0); |
| 138 | }); |
| 139 | |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 140 | if (!this.tab_ || this.tab_.id !== tab.id) { |
John Lee | 815c2fb | 2019-09-19 01:25:08 | [diff] [blame] | 141 | this.tabsApi_.trackThumbnailForTab(tab.id); |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 142 | } |
| 143 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 144 | this.tab_ = Object.freeze(tab); |
| 145 | } |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 146 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 147 | /** @return {!HTMLElement} */ |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 148 | getDragImage() { |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 149 | return this.tabEl_; |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame] | 153 | * @param {string} imgData |
| 154 | */ |
| 155 | updateThumbnail(imgData) { |
| 156 | this.thumbnail_.src = imgData; |
| 157 | } |
| 158 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 159 | /** @private */ |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 160 | onClick_() { |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 161 | if (!this.tab_) { |
| 162 | return; |
| 163 | } |
| 164 | |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 165 | this.tabsApi_.activateTab(this.tab_.id); |
John Lee | 4bcbaf1 | 2019-10-21 23:29:46 | [diff] [blame] | 166 | |
| 167 | if (tabStripOptions.autoCloseEnabled) { |
| 168 | this.embedderApi_.closeContainer(); |
| 169 | } |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 170 | } |
| 171 | |
John Lee | 61bc1b5 | 2019-10-24 22:27:23 | [diff] [blame] | 172 | /** |
| 173 | * @param {!Event} event |
| 174 | * @private |
| 175 | */ |
Collin Baker | dc3d211 | 2019-10-10 18:28:20 | [diff] [blame] | 176 | onContextMenu_(event) { |
| 177 | event.preventDefault(); |
| 178 | |
| 179 | if (!this.tab_) { |
| 180 | return; |
| 181 | } |
| 182 | |
Collin Baker | 1ac2e43 | 2019-10-16 18:17:21 | [diff] [blame] | 183 | this.embedderApi_.showTabContextMenu( |
Collin Baker | dc3d211 | 2019-10-10 18:28:20 | [diff] [blame] | 184 | this.tab_.id, event.clientX, event.clientY); |
| 185 | } |
| 186 | |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 187 | /** |
| 188 | * @param {!Event} event |
| 189 | * @private |
| 190 | */ |
| 191 | onClose_(event) { |
| 192 | if (!this.tab_) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | event.stopPropagation(); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 197 | this.tabsApi_.closeTab(this.tab_.id); |
| 198 | } |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 199 | |
| 200 | /** |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 201 | * @param {boolean} dragging |
| 202 | */ |
| 203 | setDragging(dragging) { |
John Lee | 8101110 | 2019-10-11 20:05:07 | [diff] [blame] | 204 | this.toggleAttribute('dragging_', dragging); |
John Lee | 3b309d5 | 2019-09-18 19:08:09 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /** |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 208 | * @return {!Promise} |
| 209 | */ |
| 210 | slideIn() { |
| 211 | return new Promise(resolve => { |
| 212 | const animation = this.animate( |
| 213 | [ |
| 214 | {maxWidth: 0, opacity: 0}, |
John Lee | 4df86c4 | 2019-10-25 20:00:03 | [diff] [blame^] | 215 | {maxWidth: 'var(--tabstrip-tab-width)', opacity: 1}, |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 216 | ], |
| 217 | { |
| 218 | duration: DEFAULT_ANIMATION_DURATION, |
| 219 | fill: 'forwards', |
| 220 | }); |
| 221 | animation.onfinish = resolve; |
| 222 | }); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * @return {!Promise} |
| 227 | */ |
| 228 | slideOut() { |
| 229 | return new Promise(resolve => { |
| 230 | const animation = this.animate( |
| 231 | [ |
John Lee | 4df86c4 | 2019-10-25 20:00:03 | [diff] [blame^] | 232 | {maxWidth: 'var(--tabstrip-tab-width)', opacity: 1}, |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 233 | {maxWidth: 0, opacity: 0}, |
| 234 | ], |
| 235 | { |
| 236 | duration: DEFAULT_ANIMATION_DURATION, |
| 237 | fill: 'forwards', |
| 238 | }); |
| 239 | animation.onfinish = () => { |
| 240 | this.remove(); |
| 241 | resolve(); |
| 242 | }; |
| 243 | }); |
| 244 | } |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 245 | } |
| 246 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 247 | customElements.define('tabstrip-tab', TabElement); |