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 | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 10 | export class TabElement extends CustomElement { |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 11 | static get template() { |
| 12 | return `{__html_template__}`; |
| 13 | } |
| 14 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 15 | constructor() { |
| 16 | super(); |
| 17 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 18 | /** @private {!HTMLElement} */ |
| 19 | this.closeButtonEl_ = |
| 20 | /** @type {!HTMLElement} */ (this.shadowRoot.querySelector('#close')); |
| 21 | |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 22 | /** @private {!HTMLElement} */ |
| 23 | this.faviconEl_ = |
| 24 | /** @type {!HTMLElement} */ (this.shadowRoot.querySelector('#favicon')); |
| 25 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 26 | /** @private {!Tab} */ |
| 27 | this.tab_; |
| 28 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 29 | /** @private {!TabsApiProxy} */ |
| 30 | this.tabsApi_ = TabsApiProxy.getInstance(); |
| 31 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 32 | /** @private {!HTMLElement} */ |
| 33 | this.titleTextEl_ = /** @type {!HTMLElement} */ ( |
| 34 | this.shadowRoot.querySelector('#titleText')); |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 35 | |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 36 | this.addEventListener('click', this.onClick_.bind(this)); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 37 | this.closeButtonEl_.addEventListener('click', this.onClose_.bind(this)); |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 38 | } |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 39 | |
| 40 | /** @return {!Tab} */ |
| 41 | get tab() { |
| 42 | return this.tab_; |
| 43 | } |
| 44 | |
| 45 | /** @param {!Tab} tab */ |
| 46 | set tab(tab) { |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 47 | this.toggleAttribute('active', tab.active); |
John Lee | d7611b2 | 2019-08-26 21:26:03 | [diff] [blame] | 48 | this.toggleAttribute('pinned', tab.pinned); |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 49 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 50 | if (!this.tab_ || this.tab_.title !== tab.title) { |
| 51 | this.titleTextEl_.textContent = tab.title; |
| 52 | } |
| 53 | |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 54 | if (tab.favIconUrl && |
| 55 | (!this.tab_ || this.tab_.favIconUrl !== tab.favIconUrl)) { |
| 56 | this.faviconEl_.style.backgroundImage = getFavicon(tab.favIconUrl); |
| 57 | } else if (!this.tab_ || this.tab_.url !== tab.url) { |
| 58 | this.faviconEl_.style.backgroundImage = |
| 59 | getFaviconForPageURL(tab.url, false); |
| 60 | } |
| 61 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 62 | // Expose the ID to an attribute to allow easy querySelector use |
| 63 | this.setAttribute('data-tab-id', tab.id); |
| 64 | |
| 65 | this.tab_ = Object.freeze(tab); |
| 66 | } |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 67 | |
| 68 | /** @private */ |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 69 | onClick_() { |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 70 | if (!this.tab_) { |
| 71 | return; |
| 72 | } |
| 73 | |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 74 | this.tabsApi_.activateTab(this.tab_.id); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param {!Event} event |
| 79 | * @private |
| 80 | */ |
| 81 | onClose_(event) { |
| 82 | if (!this.tab_) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | event.stopPropagation(); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 87 | this.tabsApi_.closeTab(this.tab_.id); |
| 88 | } |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 89 | } |
| 90 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 91 | customElements.define('tabstrip-tab', TabElement); |