blob: 25dac4657633dc003b24e4e420f8df825358852f [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);
48
John Lee3f8dae92019-08-09 22:37:0949 if (!this.tab_ || this.tab_.title !== tab.title) {
50 this.titleTextEl_.textContent = tab.title;
51 }
52
John Lee2bafb2f2019-08-21 19:20:0353 if (tab.favIconUrl &&
54 (!this.tab_ || this.tab_.favIconUrl !== tab.favIconUrl)) {
55 this.faviconEl_.style.backgroundImage = getFavicon(tab.favIconUrl);
56 } else if (!this.tab_ || this.tab_.url !== tab.url) {
57 this.faviconEl_.style.backgroundImage =
58 getFaviconForPageURL(tab.url, false);
59 }
60
John Lee3f8dae92019-08-09 22:37:0961 // Expose the ID to an attribute to allow easy querySelector use
62 this.setAttribute('data-tab-id', tab.id);
63
64 this.tab_ = Object.freeze(tab);
65 }
John Lee7d416b782019-08-12 22:32:1366
67 /** @private */
John Lee8e253602019-08-14 22:22:5168 onClick_() {
John Lee7d416b782019-08-12 22:32:1369 if (!this.tab_) {
70 return;
71 }
72
John Lee8e253602019-08-14 22:22:5173 this.tabsApi_.activateTab(this.tab_.id);
74 }
75
76 /**
77 * @param {!Event} event
78 * @private
79 */
80 onClose_(event) {
81 if (!this.tab_) {
82 return;
83 }
84
85 event.stopPropagation();
John Lee7d416b782019-08-12 22:32:1386 this.tabsApi_.closeTab(this.tab_.id);
87 }
John Lee912fb9c02019-08-02 01:28:2188}
89
John Lee3f8dae92019-08-09 22:37:0990customElements.define('tabstrip-tab', TabElement);