blob: 769d91ed6e516c061e9d27b0c324a380a40568b0 [file] [log] [blame]
John Lee912fb9c02019-08-02 01:28:211// 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 Lee2bafb2f2019-08-21 19:20:035import {getFavicon, getFaviconForPageURL} from 'chrome://resources/js/icon.m.js';
6
John Lee912fb9c02019-08-02 01:28:217import {CustomElement} from './custom_element.js';
John Lee7d416b782019-08-12 22:32:138import {TabsApiProxy} from './tabs_api_proxy.js';
John Lee912fb9c02019-08-02 01:28:219
John Lee3f8dae92019-08-09 22:37:0910export class TabElement extends CustomElement {
John Lee912fb9c02019-08-02 01:28:2111 static get template() {
12 return `{__html_template__}`;
13 }
14
John Lee3f8dae92019-08-09 22:37:0915 constructor() {
16 super();
17
John Lee7d416b782019-08-12 22:32:1318 /** @private {!HTMLElement} */
19 this.closeButtonEl_ =
20 /** @type {!HTMLElement} */ (this.shadowRoot.querySelector('#close'));
21
John Lee2bafb2f2019-08-21 19:20:0322 /** @private {!HTMLElement} */
23 this.faviconEl_ =
24 /** @type {!HTMLElement} */ (this.shadowRoot.querySelector('#favicon'));
25
John Lee3f8dae92019-08-09 22:37:0926 /** @private {!Tab} */
27 this.tab_;
28
John Lee7d416b782019-08-12 22:32:1329 /** @private {!TabsApiProxy} */
30 this.tabsApi_ = TabsApiProxy.getInstance();
31
John Lee3f8dae92019-08-09 22:37:0932 /** @private {!HTMLElement} */
33 this.titleTextEl_ = /** @type {!HTMLElement} */ (
34 this.shadowRoot.querySelector('#titleText'));
John Lee3f8dae92019-08-09 22:37:0935
John Lee8e253602019-08-14 22:22:5136 this.addEventListener('click', this.onClick_.bind(this));
John Lee7d416b782019-08-12 22:32:1337 this.closeButtonEl_.addEventListener('click', this.onClose_.bind(this));
John Lee912fb9c02019-08-02 01:28:2138 }
John Lee3f8dae92019-08-09 22:37:0939
40 /** @return {!Tab} */
41 get tab() {
42 return this.tab_;
43 }
44
45 /** @param {!Tab} tab */
46 set tab(tab) {
John Lee8e253602019-08-14 22:22:5147 this.toggleAttribute('active', tab.active);
John Leed7611b22019-08-26 21:26:0348 this.toggleAttribute('pinned', tab.pinned);
John Lee8e253602019-08-14 22:22:5149
John Lee3f8dae92019-08-09 22:37:0950 if (!this.tab_ || this.tab_.title !== tab.title) {
51 this.titleTextEl_.textContent = tab.title;
52 }
53
John Lee2bafb2f2019-08-21 19:20:0354 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 Lee3f8dae92019-08-09 22:37:0962 // 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 Lee7d416b782019-08-12 22:32:1367
68 /** @private */
John Lee8e253602019-08-14 22:22:5169 onClick_() {
John Lee7d416b782019-08-12 22:32:1370 if (!this.tab_) {
71 return;
72 }
73
John Lee8e253602019-08-14 22:22:5174 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 Lee7d416b782019-08-12 22:32:1387 this.tabsApi_.closeTab(this.tab_.id);
88 }
John Lee912fb9c02019-08-02 01:28:2189}
90
John Lee3f8dae92019-08-09 22:37:0991customElements.define('tabstrip-tab', TabElement);