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 | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 5 | import {getFavicon, getFaviconForPageURL} from 'chrome://resources/js/icon.m.js'; |
| 6 | |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 7 | import {CustomElement} from './custom_element.js'; |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 8 | import {TabsApiProxy} from './tabs_api_proxy.js'; |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 9 | |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 10 | export const DEFAULT_ANIMATION_DURATION = 125; |
| 11 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 12 | export class TabElement extends CustomElement { |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 13 | static get template() { |
| 14 | return `{__html_template__}`; |
| 15 | } |
| 16 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 17 | constructor() { |
| 18 | super(); |
| 19 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 20 | /** @private {!HTMLElement} */ |
| 21 | this.closeButtonEl_ = |
| 22 | /** @type {!HTMLElement} */ (this.shadowRoot.querySelector('#close')); |
| 23 | |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 24 | /** @private {!HTMLElement} */ |
| 25 | this.faviconEl_ = |
| 26 | /** @type {!HTMLElement} */ (this.shadowRoot.querySelector('#favicon')); |
| 27 | |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame^] | 28 | /** @private {!HTMLElement} */ |
| 29 | this.thumbnailContainer_ = |
| 30 | /** @type {!HTMLElement} */ ( |
| 31 | this.shadowRoot.querySelector('#thumbnail')); |
| 32 | |
| 33 | /** @private {!Image} */ |
| 34 | this.thumbnail_ = |
| 35 | /** @type {!Image} */ (this.shadowRoot.querySelector('#thumbnailImg')); |
| 36 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 37 | /** @private {!Tab} */ |
| 38 | this.tab_; |
| 39 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 40 | /** @private {!TabsApiProxy} */ |
| 41 | this.tabsApi_ = TabsApiProxy.getInstance(); |
| 42 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 43 | /** @private {!HTMLElement} */ |
| 44 | this.titleTextEl_ = /** @type {!HTMLElement} */ ( |
| 45 | this.shadowRoot.querySelector('#titleText')); |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 46 | |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 47 | this.addEventListener('click', this.onClick_.bind(this)); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 48 | this.closeButtonEl_.addEventListener('click', this.onClose_.bind(this)); |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 49 | } |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 50 | |
| 51 | /** @return {!Tab} */ |
| 52 | get tab() { |
| 53 | return this.tab_; |
| 54 | } |
| 55 | |
| 56 | /** @param {!Tab} tab */ |
| 57 | set tab(tab) { |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 58 | this.toggleAttribute('active', tab.active); |
John Lee | d7611b2 | 2019-08-26 21:26:03 | [diff] [blame] | 59 | this.toggleAttribute('pinned', tab.pinned); |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 60 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 61 | if (!this.tab_ || this.tab_.title !== tab.title) { |
| 62 | this.titleTextEl_.textContent = tab.title; |
| 63 | } |
| 64 | |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 65 | if (tab.favIconUrl && |
| 66 | (!this.tab_ || this.tab_.favIconUrl !== tab.favIconUrl)) { |
| 67 | this.faviconEl_.style.backgroundImage = getFavicon(tab.favIconUrl); |
| 68 | } else if (!this.tab_ || this.tab_.url !== tab.url) { |
| 69 | this.faviconEl_.style.backgroundImage = |
| 70 | getFaviconForPageURL(tab.url, false); |
| 71 | } |
| 72 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 73 | // Expose the ID to an attribute to allow easy querySelector use |
| 74 | this.setAttribute('data-tab-id', tab.id); |
| 75 | |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame^] | 76 | if (!this.tab_ || this.tab_.id !== tab.id) { |
| 77 | // Request thumbnail updates |
| 78 | chrome.send('addTrackedTab', [tab.id]); |
| 79 | } |
| 80 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 81 | this.tab_ = Object.freeze(tab); |
| 82 | } |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 83 | |
Collin Baker | e21f723d | 2019-09-05 20:05:41 | [diff] [blame^] | 84 | /** |
| 85 | * @param {string} imgData |
| 86 | */ |
| 87 | updateThumbnail(imgData) { |
| 88 | this.thumbnail_.src = imgData; |
| 89 | } |
| 90 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 91 | /** @private */ |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 92 | onClick_() { |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 93 | if (!this.tab_) { |
| 94 | return; |
| 95 | } |
| 96 | |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 97 | this.tabsApi_.activateTab(this.tab_.id); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param {!Event} event |
| 102 | * @private |
| 103 | */ |
| 104 | onClose_(event) { |
| 105 | if (!this.tab_) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | event.stopPropagation(); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 110 | this.tabsApi_.closeTab(this.tab_.id); |
| 111 | } |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * @return {!Promise} |
| 115 | */ |
| 116 | slideIn() { |
| 117 | return new Promise(resolve => { |
| 118 | const animation = this.animate( |
| 119 | [ |
| 120 | {maxWidth: 0, opacity: 0}, |
| 121 | {maxWidth: '280px', opacity: 1}, |
| 122 | ], |
| 123 | { |
| 124 | duration: DEFAULT_ANIMATION_DURATION, |
| 125 | fill: 'forwards', |
| 126 | }); |
| 127 | animation.onfinish = resolve; |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @return {!Promise} |
| 133 | */ |
| 134 | slideOut() { |
| 135 | return new Promise(resolve => { |
| 136 | const animation = this.animate( |
| 137 | [ |
| 138 | {maxWidth: '280px', opacity: 1}, |
| 139 | {maxWidth: 0, opacity: 0}, |
| 140 | ], |
| 141 | { |
| 142 | duration: DEFAULT_ANIMATION_DURATION, |
| 143 | fill: 'forwards', |
| 144 | }); |
| 145 | animation.onfinish = () => { |
| 146 | this.remove(); |
| 147 | resolve(); |
| 148 | }; |
| 149 | }); |
| 150 | } |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 151 | } |
| 152 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 153 | customElements.define('tabstrip-tab', TabElement); |