annotate sidebar/js/tab-collection-manager.js @ 6:152e886b6e28 version-2

Release version 2
author Guido Berhoerster <guido+set-aside@berhoerster.name>
date Thu, 13 Dec 2018 14:07:17 +0100
parents 49ec0da1e698
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
1 /*
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
2 * Copyright (C) 2018 Guido Berhoerster <guido+set-aside@berhoerster.name>
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
3 *
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
4 * This Source Code Form is subject to the terms of the Mozilla Public
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
7 */
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
8
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
9 'use strict';
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
10
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
11 var tabManager;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
12
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
13 class TabManager {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
14 constructor() {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
15 this.tabCollectionTemplate =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
16 document.querySelector('#tab-collection-template');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
17 this.tabItemTemplate = document.querySelector('#tab-item-template');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
18 this.tabCollectionsElement = document.querySelector('#tab-collections');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
19 this.port = browser.runtime.connect({name: 'tab-collection-manager'});
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
20 this.port.onMessage.addListener(this.onMessage.bind(this));
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
21 this.port.onDisconnect.addListener(this.onMessage.bind(this));
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
22 this.port.postMessage({type: 'getTabCollections'});
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
23 this.isInitialized = false;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
24 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
25
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
26 initTabCollections(tabCollections) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
27 if (this.isInitialized) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
28 return;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
29 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
30
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
31 for (let tabCollection of tabCollections.values()) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
32 this.prependTabCollection(tabCollection);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
33 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
34 this.sortTabCollections();
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
35
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
36 document.querySelector('#message').textContent =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
37 browser.i18n.getMessage('emptySidebarMessage');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
38
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
39 document.body.addEventListener('click', this);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
40
2
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
41 window.addEventListener('optimizedResize', this);
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
42
0
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
43 this.isInitialized = true;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
44 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
45
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
46 createTabCollectionNode(tabCollection) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
47 let tabCollectionNode =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
48 document.importNode(this.tabCollectionTemplate.content, true);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
49
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
50 tabCollectionNode.querySelector('.tab-collection')
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
51 .dataset.tabCollectionUuid = tabCollection.uuid;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
52
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
53 tabCollectionNode.querySelector('.tab-collection-title').textContent =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
54 browser.i18n.getMessage('collectionTitle',
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
55 tabCollection.tabs.size);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
56
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
57 let tabCollectionCtimeElement =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
58 tabCollectionNode.querySelector('.tab-collection-ctime');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
59 tabCollectionCtimeElement.dateTime = tabCollection.date.toISOString();
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
60 tabCollectionCtimeElement.textContent =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
61 tabCollection.date.toLocaleString();
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
62
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
63 let tabCollectionRestoreElement =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
64 tabCollectionNode.querySelector('.restore-tab-collection');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
65 tabCollectionRestoreElement.title =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
66 tabCollectionRestoreElement.textContent =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
67 browser.i18n.getMessage('restoreTabsButtonTitle');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
68 tabCollectionNode.querySelector('.remove-tab-collection').title =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
69 browser.i18n.getMessage('removeTabsButtonTitle');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
70
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
71 let tabListElement =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
72 tabCollectionNode.querySelector('.tab-collection-tabs');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
73 for (let tab of tabCollection.tabs.values()) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
74 let tabItemNode = document.importNode(this.tabItemTemplate.content,
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
75 true);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
76
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
77 tabItemNode.querySelector('.tab-item').dataset.tabUuid = tab.uuid;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
78
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
79 let tabLinkElement = tabItemNode.querySelector('.tab-link');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
80 tabLinkElement.href = tab.url;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
81 tabLinkElement.title = tab.title;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
82
1
b0827360b8e4 Store favicons and thumbnails in local database
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 0
diff changeset
83 if (tab.thumbnail !== null) {
0
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
84 tabItemNode.querySelector('.tab-thumbnail').src =
1
b0827360b8e4 Store favicons and thumbnails in local database
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 0
diff changeset
85 URL.createObjectURL(tab.thumbnail);
0
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
86 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
87
1
b0827360b8e4 Store favicons and thumbnails in local database
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 0
diff changeset
88 if (tab.favIcon !== null) {
b0827360b8e4 Store favicons and thumbnails in local database
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 0
diff changeset
89 tabItemNode.querySelector('.tab-favicon').src =
b0827360b8e4 Store favicons and thumbnails in local database
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 0
diff changeset
90 URL.createObjectURL(tab.favIcon);
0
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
91 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
92
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
93 tabItemNode.querySelector('.tab-title').textContent = tab.title;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
94
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
95 tabItemNode.querySelector('.remove-tab').title =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
96 browser.i18n.getMessage('removeTabTitle');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
97
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
98 tabListElement.append(tabItemNode);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
99 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
100
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
101 return tabCollectionNode;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
102 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
103
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
104 prependTabCollection(tabCollection) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
105 console.log('prepending tab collection', tabCollection,
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
106 'to tab collections');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
107 this.tabCollectionsElement
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
108 .prepend(this.createTabCollectionNode(tabCollection));
2
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
109 this.handleResize();
0
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
110 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
111
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
112 replaceTabCollection(tabCollection) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
113 console.log('replacing tab collection', tabCollection);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
114 this.tabCollectionsElement.querySelector(`[data-tab-collection-uuid=` +
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
115 `"${tabCollection.uuid}"]`)
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
116 .replaceWith(this.createTabCollectionNode(tabCollection));
2
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
117 this.handleResize();
0
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
118 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
119
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
120 removeTabCollection(tabCollectionUuid) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
121 console.log('removing tab collection %s', tabCollectionUuid);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
122 this.tabCollectionsElement
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
123 .querySelector(`[data-tab-collection-uuid=` +
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
124 `"${tabCollectionUuid}"]`)
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
125 .remove();
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
126
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
127 if (this.tabCollectionsElement.childElementCount === 0) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
128 // remove any text nodes so that the :empty CSS selectora applies
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
129 while (this.tabCollectionsElement.firstChild !== null) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
130 this.tabCollectionsElement
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
131 .removeChild(this.tabCollectionsElement.firstChild);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
132 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
133 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
134 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
135
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
136 sortTabCollections() {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
137 Array.from(this.tabCollectionsElement.children)
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
138 .map(element =>
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
139 [element.querySelector('.tab-collection-ctime').dateTime,
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
140 element])
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
141 .sort((a, b) => a[0] < b[0] ? 1 : a[0] > b[0] ? -1 : 0)
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
142 .forEach(([, element]) =>
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
143 this.tabCollectionsElement.append(element));
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
144 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
145
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
146 onMessage(message, port) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
147 console.log('received message', message, 'on port', port);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
148 switch (message.type) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
149 case 'tabCollections':
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
150 this.initTabCollections(message.tabCollections);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
151 break;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
152 case 'tabCollectionCreated':
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
153 this.prependTabCollection(message.tabCollection);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
154 break;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
155 case 'tabCollectionRemoved':
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
156 this.removeTabCollection(message.tabCollectionUuid);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
157 break;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
158 case 'tabCollectionChanged':
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
159 this.replaceTabCollection(message.tabCollection);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
160 break;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
161 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
162 this.sortTabCollections();
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
163 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
164
2
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
165 handleTabCollectionChanged(tabCollectionContainerElement) {
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
166 let tabsElement = tabCollectionContainerElement
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
167 .querySelector('.tab-collection-tabs');
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
168 let scrollLeftElement = tabCollectionContainerElement
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
169 .querySelector('.scroll-left');
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
170 let scrollRightElement = tabCollectionContainerElement
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
171 .querySelector('.scroll-right');
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
172 if (tabsElement.scrollWidth > tabsElement.clientWidth) {
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
173 scrollLeftElement.classList.add('scroll-visible');
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
174 scrollRightElement.classList.add('scroll-visible');
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
175 } else {
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
176 scrollLeftElement.classList.remove('scroll-visible');
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
177 scrollRightElement.classList.remove('scroll-visible');
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
178 }
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
179 }
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
180
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
181 handleResize() {
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
182 let tabCollectionContainerElements =
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
183 document.querySelectorAll('.tab-collection-tabs-container');
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
184 for (let tabCollectionContainerElement of
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
185 tabCollectionContainerElements) {
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
186 this.handleTabCollectionChanged(tabCollectionContainerElement);
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
187 }
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
188 }
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
189
0
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
190 handleEvent(ev) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
191 console.log('DOM event', ev);
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
192 if (ev.type === 'click') {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
193 ev.preventDefault();
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
194 if (ev.target.classList.contains('restore-tab-collection')) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
195 // restore tab collection
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
196 let tabCollectionUuid = ev.target.closest('.tab-collection')
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
197 .dataset.tabCollectionUuid;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
198 this.port.postMessage({
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
199 type: 'restoreTabCollection',
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
200 tabCollectionUuid,
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
201 windowId: browser.windows.WINDOW_ID_CURRENT
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
202 });
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
203 } else if (ev.target.classList.contains('remove-tab-collection')) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
204 // remove tab collection
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
205 let tabCollectionUuid = ev.target.closest('.tab-collection')
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
206 .dataset.tabCollectionUuid;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
207 this.port.postMessage({
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
208 type: 'removeTabCollection',
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
209 tabCollectionUuid
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
210 });
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
211 } else if (ev.target.classList.contains('remove-tab')) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
212 // remove tab from collection
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
213 let tabItemElement = ev.target.closest('.tab-item');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
214 let tabCollectionUuid =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
215 tabItemElement.closest('.tab-collection')
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
216 .dataset.tabCollectionUuid;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
217 let tabUuid = tabItemElement.dataset.tabUuid;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
218 this.port.postMessage({
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
219 type: 'removeTab',
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
220 tabCollectionUuid,
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
221 tabUuid
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
222 });
2
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
223 } else if (ev.target.classList.contains('tabs-scroll-button')) {
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
224 // scroll tab list
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
225 let tabsElement = ev.target
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
226 .closest('.tab-collection-tabs-container')
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
227 .querySelector('.tab-collection-tabs');
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
228 if (ev.target.classList.contains('scroll-left')) {
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
229 tabsElement.scrollLeft = Math.max(tabsElement.scrollLeft -
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
230 tabsElement.clientWidth * .75, 0);
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
231 } else {
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
232 tabsElement.scrollLeft = Math.min(tabsElement.scrollLeft +
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
233 tabsElement.clientWidth * .75,
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
234 tabsElement.scrollLeftMax);
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
235 }
0
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
236 } else {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
237 let tabItemElement = ev.target.closest('.tab-item');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
238 if (tabItemElement !== null) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
239 // restore tab from collection
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
240 let tabCollectionUuid =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
241 tabItemElement.closest('.tab-collection')
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
242 .dataset.tabCollectionUuid;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
243 let tabUuid = tabItemElement.dataset.tabUuid;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
244 this.port.postMessage({
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
245 type: 'restoreTab',
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
246 tabCollectionUuid,
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
247 tabUuid,
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
248 windowId: browser.windows.WINDOW_ID_CURRENT
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
249 });
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
250 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
251 }
2
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
252 } else if (ev.type === 'optimizedResize') {
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
253 // window has been resized
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
254 this.handleResize();
0
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
255 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
256 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
257 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
258
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
259 browser.windows.getCurrent().then(currentWindow => {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
260 // disable the sidebar for incognito windows
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
261 if (currentWindow.incognito) {
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
262 document.querySelector('#message').textContent =
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
263 browser.i18n.getMessage('incognitoModeMessage');
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
264 return;
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
265 }
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
266
2
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
267 function throttleResize(type, name, obj) {
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
268 obj = obj || window;
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
269 var isRunning = false;
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
270 var func = function() {
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
271 if (isRunning) {
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
272 return;
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
273 }
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
274
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
275 isRunning = true;
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
276 requestAnimationFrame(function() {
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
277 obj.dispatchEvent(new CustomEvent(name));
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
278 isRunning = false;
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
279 });
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
280 };
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
281 obj.addEventListener(type, func);
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
282 };
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
283 throttleResize('resize', 'optimizedResize');
49ec0da1e698 Replace tab list scroll bar with scroll buttons
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents: 1
diff changeset
284
0
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
285 tabManager = new TabManager();
d13d59494613 Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff changeset
286 });