blob: 437872e49bb4ff04579aa757789c3925490c3008 [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 Leea97ceab6f2019-09-04 22:26:4610export const DEFAULT_ANIMATION_DURATION = 125;
11
John Lee3f8dae92019-08-09 22:37:0912export class TabElement extends CustomElement {
John Lee912fb9c02019-08-02 01:28:2113 static get template() {
14 return `{__html_template__}`;
15 }
16
John Lee3f8dae92019-08-09 22:37:0917 constructor() {
18 super();
19
John Lee7d416b782019-08-12 22:32:1320 /** @private {!HTMLElement} */
21 this.closeButtonEl_ =
22 /** @type {!HTMLElement} */ (this.shadowRoot.querySelector('#close'));
23
John Lee2bafb2f2019-08-21 19:20:0324 /** @private {!HTMLElement} */
25 this.faviconEl_ =
26 /** @type {!HTMLElement} */ (this.shadowRoot.querySelector('#favicon'));
27
John Lee3f8dae92019-08-09 22:37:0928 /** @private {!Tab} */
29 this.tab_;
30
John Lee7d416b782019-08-12 22:32:1331 /** @private {!TabsApiProxy} */
32 this.tabsApi_ = TabsApiProxy.getInstance();
33
John Lee3f8dae92019-08-09 22:37:0934 /** @private {!HTMLElement} */
35 this.titleTextEl_ = /** @type {!HTMLElement} */ (
36 this.shadowRoot.querySelector('#titleText'));
John Lee3f8dae92019-08-09 22:37:0937
John Lee8e253602019-08-14 22:22:5138 this.addEventListener('click', this.onClick_.bind(this));
John Lee7d416b782019-08-12 22:32:1339 this.closeButtonEl_.addEventListener('click', this.onClose_.bind(this));
John Lee912fb9c02019-08-02 01:28:2140 }
John Lee3f8dae92019-08-09 22:37:0941
42 /** @return {!Tab} */
43 get tab() {
44 return this.tab_;
45 }
46
47 /** @param {!Tab} tab */
48 set tab(tab) {
John Lee8e253602019-08-14 22:22:5149 this.toggleAttribute('active', tab.active);
John Leed7611b22019-08-26 21:26:0350 this.toggleAttribute('pinned', tab.pinned);
John Lee8e253602019-08-14 22:22:5151
John Lee3f8dae92019-08-09 22:37:0952 if (!this.tab_ || this.tab_.title !== tab.title) {
53 this.titleTextEl_.textContent = tab.title;
54 }
55
John Lee2bafb2f2019-08-21 19:20:0356 if (tab.favIconUrl &&
57 (!this.tab_ || this.tab_.favIconUrl !== tab.favIconUrl)) {
58 this.faviconEl_.style.backgroundImage = getFavicon(tab.favIconUrl);
59 } else if (!this.tab_ || this.tab_.url !== tab.url) {
60 this.faviconEl_.style.backgroundImage =
61 getFaviconForPageURL(tab.url, false);
62 }
63
John Lee3f8dae92019-08-09 22:37:0964 // Expose the ID to an attribute to allow easy querySelector use
65 this.setAttribute('data-tab-id', tab.id);
66
67 this.tab_ = Object.freeze(tab);
68 }
John Lee7d416b782019-08-12 22:32:1369
70 /** @private */
John Lee8e253602019-08-14 22:22:5171 onClick_() {
John Lee7d416b782019-08-12 22:32:1372 if (!this.tab_) {
73 return;
74 }
75
John Lee8e253602019-08-14 22:22:5176 this.tabsApi_.activateTab(this.tab_.id);
77 }
78
79 /**
80 * @param {!Event} event
81 * @private
82 */
83 onClose_(event) {
84 if (!this.tab_) {
85 return;
86 }
87
88 event.stopPropagation();
John Lee7d416b782019-08-12 22:32:1389 this.tabsApi_.closeTab(this.tab_.id);
90 }
John Leea97ceab6f2019-09-04 22:26:4691
92 /**
93 * @return {!Promise}
94 */
95 slideIn() {
96 return new Promise(resolve => {
97 const animation = this.animate(
98 [
99 {maxWidth: 0, opacity: 0},
100 {maxWidth: '280px', opacity: 1},
101 ],
102 {
103 duration: DEFAULT_ANIMATION_DURATION,
104 fill: 'forwards',
105 });
106 animation.onfinish = resolve;
107 });
108 }
109
110 /**
111 * @return {!Promise}
112 */
113 slideOut() {
114 return new Promise(resolve => {
115 const animation = this.animate(
116 [
117 {maxWidth: '280px', opacity: 1},
118 {maxWidth: 0, opacity: 0},
119 ],
120 {
121 duration: DEFAULT_ANIMATION_DURATION,
122 fill: 'forwards',
123 });
124 animation.onfinish = () => {
125 this.remove();
126 resolve();
127 };
128 });
129 }
John Lee912fb9c02019-08-02 01:28:21130}
131
John Lee3f8dae92019-08-09 22:37:09132customElements.define('tabstrip-tab', TabElement);