-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsidebar.js
523 lines (471 loc) · 15.6 KB
/
sidebar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/**
* Escapes any occurances of &, ", <, > or / with XML entities.
*
* @param {string} str
* The string to escape.
* @return {string} The escaped string.
*/
function escapeXML(str) {
const replacements = {"&": "&", "\"": """, "'": "'", "<": "<", ">": ">", "/": "/"};
return String(str).replace(/[&"'<>/]/g, m => replacements[m]);
}
/**
* A tagged template function which escapes any XML metacharacters in
* interpolated values.
*
* @param {Array<string>} strings
* An array of literal strings extracted from the templates.
* @param {Array} values
* An array of interpolated values extracted from the template.
* @returns {string}
* The result of the escaped values interpolated with the literal
* strings.
*/
function escaped(strings, ...values) {
const result = [];
for (const [i, string] of strings.entries()) {
result.push(string);
if (i < values.length)
result.push(escapeXML(values[i]));
}
return result.join("");
}
function debug() {
if (false) {
console.log(...arguments);
}
}
const DEFAULT_FAVICON = "/img/defaultFavicon.svg";
const DEFAULT_LOADING = "chrome://global/skin/icons/[email protected]";
const tabManager = {
currentContainers: [],
currentTabs: new Map(),
sidebar: null,
draggedOver: null,
draggingTab: null,
loaded: false,
showContainersAdvert: false,
// fired when context menu is triggered
currentContext: null,
init() {
this.getContainers();
this.loadTabs();
this.addListeners();
},
load() {
this.sidebar = document.getElementById("sidebarContainer");
const tabItemMenu = document.getElementById("tab-item-menu");
document.addEventListener("click", this);
tabItemMenu.addEventListener("click", this);
this.loaded = true;
this.render();
},
addListeners() {
browser.tabs.onActivated.addListener((activated) => {
const activeTab = this.getTabById(activated.tabId);
if (activeTab) {
this.currentTabs.forEach((tabInstance) => {
tabInstance.active = false;
});
activeTab.active = true;
}
});
browser.tabs.onRemoved.addListener((removed) => {
const tabInstance = this.getTabById(removed);
if (tabInstance) {
tabInstance.remove();
}
});
/* not sure if I need these yet
browser.tabs.onAttached.addListener(refreshTabs);
browser.tabs.onDetached.addListener(refreshTabs);
browser.tabs.onReplaced.addListener(refreshTabs);
*/
// We could potentially stale check here but it might get out of date
// tracking the tabs state in memory might be more performant though
const refreshTabs = (tab) => {
// In case the user adds more containers (we need events really etc)
//this.getContainers();
this.loadTabs();
};
browser.tabs.onCreated.addListener(refreshTabs);
// TODO clear these up, also handle pinning here as move is called
browser.tabs.onMoved.addListener(refreshTabs);
// Once I handle everything this can be removed
// This overfires right now but clears up stale tabs
browser.tabs.onUpdated.addListener((tabId, update, tab) => {
debug("update", tabId, update, tab);
const tabInstance = this.getTabById(tabId);
if (tabInstance) {
tabInstance.update(tab);
}
});
},
getTabById(id) {
return this.currentTabs.get(id);
},
loadTabs() {
return browser.tabs.query({
windowId: browser.windows.WINDOW_ID_CURRENT
}).then((tabs) => {
debug('tab', tabs);
this.currentTabs = new Map();
tabs.forEach((tab) => {
this.currentTabs.set(tab.id, this.createTabInstance(tab));
});
return this.renderTabs();
});
},
hasContainersSupport() {
return browser.contextualIdentities !== undefined;
},
getContainers() {
const defaultContainer = {
cookieStoreId: 'firefox-default',
name: "Default"
};
this.currentContainers = [];
this.showContainersAdvert = false;
if (this.hasContainersSupport()) {
browser.contextualIdentities.query({
}).then((containers) => {
debug('got containers', containers);
this.currentContainers = containers;
this.currentContainers.unshift(defaultContainer);
this.render();
});
} else {
this.showContainersAdvert = true;
this.currentContainers.unshift(defaultContainer);
this.render();
}
},
toggleAdvert() {
const containersAdvertElement = document.getElementById("containersAdvert");
containersAdvertElement.hidden = !this.showContainersAdvert;
},
render() {
debug('rendering', this.currentContainers, this.currentTabs);
if (!this.loaded) {
return;
}
this.toggleAdvert();
const fragment = document.createDocumentFragment();
this.currentContainers.forEach((container) => {
const containerElement = document.createElement("section");
containerElement.className = "closed";
containerElement.innerHTML = escaped`<div class="container">
<div class="usercontext-icon"></div>
<div class="container-name">
${container.name}
</div>
<span class="tab-count"></span>
<div class="pinned-tabs"></div>
<button class="new-tab" title="Open new ${container.name} tab"></button>
</div>`;
containerElement.setAttribute("data-identity-icon", container.icon);
containerElement.setAttribute("data-identity-color", container.color);
containerElement.setAttribute("data-cookie-store-id", container.cookieStoreId);
const tabContainerElement = document.createElement("div");
tabContainerElement.className = "tab-container";
containerElement.appendChild(tabContainerElement);
fragment.appendChild(containerElement);
});
this.sidebar.innerHTML = "";
this.sidebar.appendChild(fragment);
this.renderTabs();
},
containerOpen(sectionElement, toggle) {
let isOpen = false;
if (!sectionElement.classList.contains("closed")) {
isOpen = true;
}
[...this.sidebar.querySelectorAll("section")].forEach((section) => {
section.classList.add("closed");
});
if (!toggle || !isOpen) {
sectionElement.classList.remove("closed");
if (toggle) {
// Lets open the first tab on user click
// This was annoying
//this.tabActivate(sectionElement.querySelector('.tab-item'));
}
}
},
menuItemFire(e) {
debug("menuitem", e, this.currentContext, e.target.parentNode, e.target.parentNode.parentNode);
if (this.currentContext) {
switch (e.target.id) {
case "pinTab":
this.currentContext.pin();
break;
case "unpinTab":
this.currentContext.pin(false);
break;
case "reloadTab":
this.currentContext.reload();
break;
case "muteTab":
this.currentContext.mute();
break;
case "unmuteTab":
this.currentContext.mute(false);
break;
}
this.currentContext = false;
}
},
handleEvent(e) {
debug("event", e);
switch (e.type) {
case "click":
if (e.target.tagName === "MENUITEM") {
this.menuItemFire(e);
} else {
const sectionElement = e.target.closest("section");
if (e.target.tagName === "BUTTON") {
browser.tabs.create({
cookieStoreId: sectionElement.getAttribute("data-cookie-store-id")
});
return;
}
// If middle click is pressed, for some reason firefox doesn't support it locally on the menuitem
// https://bugzilla.mozilla.org/show_bug.cgi?id=864096
if (e.button === 1) {
let tabElement = e.target;
tabElement = tabElement.closest(".tab-item");
// close tab.
browser.tabs.remove(Number(tabElement.dataset.tabId));
e.preventDefault();
return;
}
this.containerOpen(sectionElement, true);
}
case "dragstart":
this.draggingTab = e.target;
this.draggingTab.classList.add("dragging");
e.dataTransfer.setData("text/plain", e.target.id);
break;
case "dragover":
if (this.draggedOver === e.target) {
return;
}
if (this.draggedOver) {
this.draggedOver.classList.remove("over");
}
const thisDraggingOverTabElement = e.target.closest(".tab-item");
thisDraggingOverTabElement.classList.add("over");
this.draggedOver = thisDraggingOverTabElement;
break;
case "dragend":
this.draggingTab.classList.remove("dragging");
const tabId = Number(this.draggingTab.getAttribute("data-tab-id"));
if (tabId && this.draggedOver) {
const draggingOverTabId = Number(this.draggedOver.getAttribute("data-tab-id"));
browser.tabs.get(draggingOverTabId).then((draggingOverTab) => {
browser.tabs.move(tabId, {
index: draggingOverTab.index + 1
});
});
}
this.draggingTab = null;
this.draggedOver = null;
break;
}
},
createTabInstance(tab) {
return new TabInstance(tab);
},
renderTabs() {
const containerTabs = {};
const pinnedTabs = {};
let activeTab;
const makeTab = (storage, tabInstance, pinned) => {
const cookieStoreId = tabInstance.cookieStoreId;
if (!(cookieStoreId in storage)) {
storage[cookieStoreId] = document.createDocumentFragment();
}
if (tabInstance.active) {
activeTab = tabInstance;
}
storage[cookieStoreId].appendChild(tabInstance.render());
};
this.currentTabs.forEach((tabInstance) => {
if (tabInstance.pinned) {
makeTab(pinnedTabs, tabInstance, true);
} else {
makeTab(containerTabs, tabInstance);
}
});
[...document.querySelectorAll('section')].forEach((section) => {
const tabCountElement = section.querySelector(".tab-count");
const tabContainer = section.querySelector(".tab-container");
const cookieStoreId = section.getAttribute("data-cookie-store-id");
debug("found section", tabContainer, cookieStoreId, section, containerTabs[cookieStoreId]);
if (cookieStoreId in containerTabs) {
const tabCount = containerTabs[cookieStoreId].childNodes.length;
if (tabCount > 0) {
section.classList.add("expandable");
tabCountElement.innerText = `(${tabCount})`;
}
tabContainer.innerHTML = "";
tabContainer.appendChild(containerTabs[cookieStoreId]);
}
if (cookieStoreId in pinnedTabs) {
const pinnedSection = section.querySelector(".pinned-tabs");
pinnedSection.innerHTML = "";
pinnedSection.appendChild(pinnedTabs[cookieStoreId]);
}
});
return activeTab;
}
};
class TabInstance {
constructor(tabData) {
this.tabData = tabData;
this.cookieStoreId = tabData.cookieStoreId;
}
get pinned() {
return this.tabData.pinned;
}
get active() {
return this.tabData.active;
}
set active(isActive) {
if (isActive !== this.tabData.active) {
this.tabData.active = isActive;
this.render();
if (isActive) {
this.scrollIntoView();
/* neaten this up to message pass */
const sectionElement = this.tabElement.closest("section");
tabManager.containerOpen(sectionElement);
}
}
return this.tabData.active;
}
update(tabData) {
this.tabData = tabData;
this.render();
}
render() {
const cookieStoreId = this.tabData.cookieStoreId;
const tabElement = document.createElement("a");
tabElement.className = "tab-item";
tabElement.setAttribute("contextmenu", "tab-item-menu");
tabElement.setAttribute("href", this.tabData.url);
const pinned = this.tabData.pinned;
if (!pinned) {
tabElement.setAttribute("draggable", true);
}
tabElement.setAttribute("data-tab-id", this.tabData.id);
// These methods really should move to somewhere else however they rely on state tracking TODO
tabElement.addEventListener("dragstart", tabManager);
tabElement.addEventListener("dragover", tabManager);
tabElement.addEventListener("dragend", tabManager);
debug("Found tab", this.tabData);
if (this.tabData.active) {
tabElement.classList.add("active");
}
let favIconUrl = DEFAULT_FAVICON;
if (this.tabData.status !== "complete") {
favIconUrl = DEFAULT_LOADING;
} else {
if ("favIconUrl" in this.tabData) {
favIconUrl = this.tabData.favIconUrl;
}
}
if (pinned) {
tabElement.innerHTML = escaped`
<img src="${favIconUrl}" class="favicon offpage" title="${this.tabData.title}" />
`;
} else {
tabElement.innerHTML = escaped`
<img src="${favIconUrl}" class="offpage" title="${this.tabData.title}" />
<div class="tab-title">${this.tabData.title}</div>
<button class="close-tab" title="Close tab"></button>`;
}
if (this.tabData.audible) {
tabElement.classList.add("sound-playing");
}
const muted = this.tabData.mutedInfo.muted;
if (muted) {
tabElement.classList.add("muted");
}
const imageElement = tabElement.querySelector("img");
imageElement.addEventListener("error", this);
imageElement.addEventListener("load", this);
tabElement.addEventListener("click", this);
tabElement.addEventListener("keydown", this);
tabElement.addEventListener("contextmenu", this);
if (this.tabElement) {
this.tabElement.replaceWith(tabElement);
}
this.tabElement = tabElement;
return tabElement;
}
handleEvent(e) {
debug("tab event", e);
switch (e.type) {
case "contextmenu":
tabManager.currentContext = this;
const pinned = this.tabData.pinned;
const muted = this.tabData.mutedInfo.muted;
document.getElementById("unpinTab").hidden = !pinned;
document.getElementById("pinTab").hidden = pinned;
document.getElementById("unmuteTab").hidden = !muted;
document.getElementById("muteTab").hidden = muted;
break;
case "error":
/* load and error handle missing favicons, about: and WhatsApp have issues here.
This causes the app to flicker on rerender... however this indicates a redraw, let's fix how regular that is before anything else like caching
*/
e.target.setAttribute("src", DEFAULT_FAVICON);
break;
case "load":
e.target.classList.remove("offpage");
e.target.removeEventListener("load", this);
e.target.removeEventListener("error", this);
break;
case "click":
if (e.target.tagName === "BUTTON") {
this.tabClose();
} else {
this.tabActivate();
}
/* Stop us triggering clicks in the parent section */
e.stopPropagation();
/* As we are a link element stop loading the tab in a new tab */
e.preventDefault();
break;
}
}
tabClose() {
browser.tabs.remove(Number(this.tabData.id));
}
tabActivate() {
browser.tabs.update(Number(this.tabData.id), {
active: true
});
}
scrollIntoView() {
this.tabElement.scrollIntoView({block: "end", behavior: "smooth"});
}
/* fired on tab close event. clear up event handlers here */
remove() {
this.tabElement.remove();
}
reload() {
browser.tabs.reload(this.tabData.id);
}
mute(state = true) {
browser.tabs.update(this.tabData.id, {muted: state});
}
pin(state = true) {
browser.tabs.update(this.tabData.id, {pinned: state});
}
}
tabManager.init();
document.addEventListener("DOMContentLoaded", () => {
tabManager.load();
});