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 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 28 | /** @private {!Tab} */ |
| 29 | this.tab_; |
| 30 | |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 31 | /** @private {!TabsApiProxy} */ |
| 32 | this.tabsApi_ = TabsApiProxy.getInstance(); |
| 33 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 34 | /** @private {!HTMLElement} */ |
| 35 | this.titleTextEl_ = /** @type {!HTMLElement} */ ( |
| 36 | this.shadowRoot.querySelector('#titleText')); |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 37 | |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 38 | this.addEventListener('click', this.onClick_.bind(this)); |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 39 | this.closeButtonEl_.addEventListener('click', this.onClose_.bind(this)); |
John Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 40 | } |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 41 | |
| 42 | /** @return {!Tab} */ |
| 43 | get tab() { |
| 44 | return this.tab_; |
| 45 | } |
| 46 | |
| 47 | /** @param {!Tab} tab */ |
| 48 | set tab(tab) { |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 49 | this.toggleAttribute('active', tab.active); |
John Lee | d7611b2 | 2019-08-26 21:26:03 | [diff] [blame] | 50 | this.toggleAttribute('pinned', tab.pinned); |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 51 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 52 | if (!this.tab_ || this.tab_.title !== tab.title) { |
| 53 | this.titleTextEl_.textContent = tab.title; |
| 54 | } |
| 55 | |
John Lee | 2bafb2f | 2019-08-21 19:20:03 | [diff] [blame] | 56 | 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 Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 64 | // 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 Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 69 | |
| 70 | /** @private */ |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 71 | onClick_() { |
John Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 72 | if (!this.tab_) { |
| 73 | return; |
| 74 | } |
| 75 | |
John Lee | 8e25360 | 2019-08-14 22:22:51 | [diff] [blame] | 76 | 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 Lee | 7d416b78 | 2019-08-12 22:32:13 | [diff] [blame] | 89 | this.tabsApi_.closeTab(this.tab_.id); |
| 90 | } |
John Lee | a97ceab6f | 2019-09-04 22:26:46 | [diff] [blame^] | 91 | |
| 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 Lee | 912fb9c0 | 2019-08-02 01:28:21 | [diff] [blame] | 130 | } |
| 131 | |
John Lee | 3f8dae9 | 2019-08-09 22:37:09 | [diff] [blame] | 132 | customElements.define('tabstrip-tab', TabElement); |