Mercurial > addons > firefox-addons > set-aside
annotate sidebar/js/tab-collection-manager.js @ 1:b0827360b8e4
Store favicons and thumbnails in local database
author | Guido Berhoerster <guido+set-aside@berhoerster.name> |
---|---|
date | Mon, 19 Nov 2018 17:26:17 +0100 |
parents | d13d59494613 |
children | 49ec0da1e698 |
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 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
41 this.isInitialized = true; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
42 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
43 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
44 createTabCollectionNode(tabCollection) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
45 let tabCollectionNode = |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
46 document.importNode(this.tabCollectionTemplate.content, true); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
47 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
48 tabCollectionNode.querySelector('.tab-collection') |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
49 .dataset.tabCollectionUuid = tabCollection.uuid; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
50 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
51 tabCollectionNode.querySelector('.tab-collection-title').textContent = |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
52 browser.i18n.getMessage('collectionTitle', |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
53 tabCollection.tabs.size); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
54 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
55 let tabCollectionCtimeElement = |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
56 tabCollectionNode.querySelector('.tab-collection-ctime'); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
57 tabCollectionCtimeElement.dateTime = tabCollection.date.toISOString(); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
58 tabCollectionCtimeElement.textContent = |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
59 tabCollection.date.toLocaleString(); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
60 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
61 let tabCollectionRestoreElement = |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
62 tabCollectionNode.querySelector('.restore-tab-collection'); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
63 tabCollectionRestoreElement.title = |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
64 tabCollectionRestoreElement.textContent = |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
65 browser.i18n.getMessage('restoreTabsButtonTitle'); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
66 tabCollectionNode.querySelector('.remove-tab-collection').title = |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
67 browser.i18n.getMessage('removeTabsButtonTitle'); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
68 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
69 let tabListElement = |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
70 tabCollectionNode.querySelector('.tab-collection-tabs'); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
71 for (let tab of tabCollection.tabs.values()) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
72 let tabItemNode = document.importNode(this.tabItemTemplate.content, |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
73 true); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
74 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
75 tabItemNode.querySelector('.tab-item').dataset.tabUuid = tab.uuid; |
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 let tabLinkElement = tabItemNode.querySelector('.tab-link'); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
78 tabLinkElement.href = tab.url; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
79 tabLinkElement.title = tab.title; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
80 |
1
b0827360b8e4
Store favicons and thumbnails in local database
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
0
diff
changeset
|
81 if (tab.thumbnail !== null) { |
0
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
82 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
|
83 URL.createObjectURL(tab.thumbnail); |
0
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
84 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
85 |
1
b0827360b8e4
Store favicons and thumbnails in local database
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
0
diff
changeset
|
86 if (tab.favIcon !== null) { |
b0827360b8e4
Store favicons and thumbnails in local database
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
0
diff
changeset
|
87 tabItemNode.querySelector('.tab-favicon').src = |
b0827360b8e4
Store favicons and thumbnails in local database
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
0
diff
changeset
|
88 URL.createObjectURL(tab.favIcon); |
0
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
89 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
90 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
91 tabItemNode.querySelector('.tab-title').textContent = tab.title; |
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('.remove-tab').title = |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
94 browser.i18n.getMessage('removeTabTitle'); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
95 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
96 tabListElement.append(tabItemNode); |
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 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
99 return tabCollectionNode; |
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 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
102 prependTabCollection(tabCollection) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
103 console.log('prepending tab collection', tabCollection, |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
104 'to tab collections'); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
105 this.tabCollectionsElement |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
106 .prepend(this.createTabCollectionNode(tabCollection)); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
107 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
108 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
109 replaceTabCollection(tabCollection) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
110 console.log('replacing tab collection', tabCollection); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
111 this.tabCollectionsElement.querySelector(`[data-tab-collection-uuid=` + |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
112 `"${tabCollection.uuid}"]`) |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
113 .replaceWith(this.createTabCollectionNode(tabCollection)); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
114 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
115 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
116 removeTabCollection(tabCollectionUuid) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
117 console.log('removing tab collection %s', tabCollectionUuid); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
118 this.tabCollectionsElement |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
119 .querySelector(`[data-tab-collection-uuid=` + |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
120 `"${tabCollectionUuid}"]`) |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
121 .remove(); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
122 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
123 if (this.tabCollectionsElement.childElementCount === 0) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
124 // remove any text nodes so that the :empty CSS selectora applies |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
125 while (this.tabCollectionsElement.firstChild !== null) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
126 this.tabCollectionsElement |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
127 .removeChild(this.tabCollectionsElement.firstChild); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
128 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
129 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
130 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
131 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
132 sortTabCollections() { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
133 Array.from(this.tabCollectionsElement.children) |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
134 .map(element => |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
135 [element.querySelector('.tab-collection-ctime').dateTime, |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
136 element]) |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
137 .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
|
138 .forEach(([, element]) => |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
139 this.tabCollectionsElement.append(element)); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
140 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
141 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
142 onMessage(message, port) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
143 console.log('received message', message, 'on port', port); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
144 switch (message.type) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
145 case 'tabCollections': |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
146 this.initTabCollections(message.tabCollections); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
147 break; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
148 case 'tabCollectionCreated': |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
149 this.prependTabCollection(message.tabCollection); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
150 break; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
151 case 'tabCollectionRemoved': |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
152 this.removeTabCollection(message.tabCollectionUuid); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
153 break; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
154 case 'tabCollectionChanged': |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
155 this.replaceTabCollection(message.tabCollection); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
156 break; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
157 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
158 this.sortTabCollections(); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
159 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
160 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
161 handleEvent(ev) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
162 console.log('DOM event', ev); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
163 if (ev.type === 'click') { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
164 ev.preventDefault(); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
165 if (ev.target.classList.contains('restore-tab-collection')) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
166 // restore tab collection |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
167 let tabCollectionUuid = ev.target.closest('.tab-collection') |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
168 .dataset.tabCollectionUuid; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
169 this.port.postMessage({ |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
170 type: 'restoreTabCollection', |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
171 tabCollectionUuid, |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
172 windowId: browser.windows.WINDOW_ID_CURRENT |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
173 }); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
174 } else if (ev.target.classList.contains('remove-tab-collection')) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
175 // remove tab collection |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
176 let tabCollectionUuid = ev.target.closest('.tab-collection') |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
177 .dataset.tabCollectionUuid; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
178 this.port.postMessage({ |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
179 type: 'removeTabCollection', |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
180 tabCollectionUuid |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
181 }); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
182 } else if (ev.target.classList.contains('remove-tab')) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
183 // remove tab from collection |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
184 let tabItemElement = ev.target.closest('.tab-item'); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
185 let tabCollectionUuid = |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
186 tabItemElement.closest('.tab-collection') |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
187 .dataset.tabCollectionUuid; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
188 let tabUuid = tabItemElement.dataset.tabUuid; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
189 this.port.postMessage({ |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
190 type: 'removeTab', |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
191 tabCollectionUuid, |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
192 tabUuid |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
193 }); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
194 } else { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
195 let tabItemElement = ev.target.closest('.tab-item'); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
196 if (tabItemElement !== null) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
197 // restore tab from collection |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
198 let tabCollectionUuid = |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
199 tabItemElement.closest('.tab-collection') |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
200 .dataset.tabCollectionUuid; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
201 let tabUuid = tabItemElement.dataset.tabUuid; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
202 this.port.postMessage({ |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
203 type: 'restoreTab', |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
204 tabCollectionUuid, |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
205 tabUuid, |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
206 windowId: browser.windows.WINDOW_ID_CURRENT |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
207 }); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
208 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
209 } |
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 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
212 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
213 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
214 browser.windows.getCurrent().then(currentWindow => { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
215 // disable the sidebar for incognito windows |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
216 if (currentWindow.incognito) { |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
217 document.querySelector('#message').textContent = |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
218 browser.i18n.getMessage('incognitoModeMessage'); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
219 return; |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
220 } |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
221 |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
222 tabManager = new TabManager(); |
d13d59494613
Initial revision
Guido Berhoerster <guido+set-aside@berhoerster.name>
parents:
diff
changeset
|
223 }); |