-
Notifications
You must be signed in to change notification settings - Fork 3
/
panel.js
421 lines (366 loc) · 12.4 KB
/
panel.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
const $create = document.createElement.bind(document)
document.addEventListener("contextmenu", event => event.preventDefault())
if (navigator.platform == "Win32") document.body.classList.add("windows")
browser.runtime.getBrowserInfo().then(browserInfo => {
browserVersion = parseFloat(browserInfo.version)
if (navigator.platform.startsWith("Linux") &&
(browserVersion == 90 || browserVersion == 91 || browserVersion == 94 ||
browserVersion == 95)
) {
document.body.classList.add("linux90")
}
})
const $container = document.querySelector("#tabs")
var window_id
var current_tab_id = null
var tabs_by_id = {}
var detached_tabs = {} // maps which detached tab refers to which real tab
function on_tab_click(e) {
let id = parseInt(this.dataset.id)
if (e.buttons === 4) { // if middle click
browser.tabs.remove(id) // close the tab
e.preventDefault()
return
}
// switch to clicked tab
browser.tabs.update(id, {active: true})
//.then((tab) => { console.log("click on tab", tab) })
}
function on_sound_icon_click(e) {
let id = parseInt(this.parentNode.dataset.id)
var muted = false
browser.tabs.get(id).then((tab) => {
muted = !tab.mutedInfo.muted
browser.tabs.update(id, {muted: muted})
})
e.stopPropagation()
}
function ensure_sound_icon_is_present($tab) {
if ($tab.children.length != 3) {
var icon = document.createElement("div")
icon.className = "soundicon"
$tab.appendChild(icon)
icon.addEventListener("click", on_sound_icon_click)
}
}
function create_li(tab) {
var li = $create("li")
li.dataset.id = tab.id
tabs_by_id[tab.id] = li
li.className = "tab"
if (tab.highlighted) {
li.classList.add("highlighted")
}
if (!tab.discarded) {
li.classList.add("loaded")
}
if (tab.active) {
unhighlight_current_tab()
current_tab_id = tab.id
}
var favicon = $create("div")
favicon.className = "favicon"
var img = $create("img")
img.onerror = function () {
favicon.classList.add("no-favicon")
}
if (tab.favIconUrl) {
img.src = tab.favIconUrl
} else {
favicon.classList.add("no-favicon")
}
favicon.appendChild(img)
li.appendChild(favicon)
var title = $create("span")
title.textContent = tab.title
li.appendChild(title)
if (tab.mutedInfo.reason || tab.audible) {
ensure_sound_icon_is_present(li)
}
if (tab.audible) {
li.classList.add("audible")
}
add_drag_events(li)
li.addEventListener("mousedown", on_tab_click)
return li
}
function scroll_into_view($el) {
let visible_top_y = window.scrollY
let visible_bottom_y = visible_top_y + window.innerHeight
let sizing_info = $el.getBoundingClientRect()
let el_top_y = sizing_info.y + window.scrollY
let el_bottom_y = sizing_info.y + window.scrollY + sizing_info.height
if (el_bottom_y > visible_bottom_y) {
$el.scrollIntoView({behavior: "smooth", block: "end"})
} else if (el_top_y < visible_top_y) {
$el.scrollIntoView({behavior: "smooth", block: "start"})
}
}
function fill_content() {
browser.tabs.query({windowId: window_id}).then((window_tabs) => {
$container.textContent = ""
tabs_by_id = {}
window_tabs.forEach((tab) => {
var li = create_li(tab)
$container.appendChild(li)
})
let $current_tab = tabs_by_id[current_tab_id]
scroll_into_view($current_tab)
})
}
function on_remove_tab(tabId, windowInfo) {
if (window_id != windowInfo.windowId) {
return
}
//console.info("on_remove_tab", current_tab_id, tabId)
if (current_tab_id == tabId) {
current_tab_id = null
}
var $tab = tabs_by_id[tabId]
if (!$tab && detached_tabs[tabId]) {
// tab isn't in the list because it's detached, remove its linked tab
$tab = tabs_by_id[detached_tabs[tabId]]
}
$tab.remove()
delete tabs_by_id[tabId]
}
function on_update_tab(tabId, changes, state) {
if (window_id != state.windowId) {
return // doesn't concert this window
}
var $tab = tabs_by_id[tabId]
if (!$tab) {
//console.error("Tab hasn't been created yet? on_update_tab can't proceed")
return // nothing we can do, tab hasn't been created yet
}
let favicon = $tab.children[0]
if (changes.status == "loading") {
favicon.children[0].src = "loading.png"
favicon.classList.remove("no-favicon")
}
if (changes.title) {
$tab.children[1].textContent = changes.title
}
if (typeof changes.audible != "undefined") {
ensure_sound_icon_is_present($tab)
if (changes.audible) {
$tab.classList.add("audible")
} else if (state.mutedInfo.muted == false) {
$tab.classList.remove("audible")
}
}
if (changes.mutedInfo) {
ensure_sound_icon_is_present($tab)
if (changes.mutedInfo.muted) {
$tab.classList.add("muted")
} else {
$tab.classList.remove("muted")
}
}
if (state.status == "complete") {
if (state.favIconUrl) {
favicon.children[0].src = state.favIconUrl
favicon.classList.remove("no-favicon")
} else {
favicon.classList.add("no-favicon")
}
}
}
function insert_tab_at(index, tab) {
var $tabs = $container.querySelectorAll(".tab")
$container.insertBefore(tab, $tabs[index])
}
function on_create_tab(tab) {
if (window_id != tab.windowId) {
return
}
//console.info("on_create_tab", tab)
if (tab.highlighted) {
unhighlight_current_tab()
}
var li = create_li(tab)
insert_tab_at(tab.index, li)
tabs_by_id[tab.id] = li
scroll_into_view(li)
}
function on_moved_tab(tabId, opts) {
if (window_id != opts.windowId) {
return // doesn't concern this window
}
var parent_id = detached_tabs[tabId]
var $tab = tabs_by_id[parent_id] || tabs_by_id[tabId]
var new_index = opts.toIndex
if (opts.fromIndex < new_index) {
new_index += 1
}
insert_tab_at(new_index, $tab)
}
function unhighlight_current_tab() {
if (current_tab_id) {
//console.info("unhighlight_current_tab", current_tab_id)
var $tab = tabs_by_id[current_tab_id]
$tab.classList.remove("highlighted")
}
}
function change_current_tab(active_info) {
if (window_id != active_info.windowId) {
return
}
let new_current_id = active_info.tabId
if (detached_tabs[new_current_id]) {
//console.info("USING DETACHED TAB: from", new_current_id,
// " to ", detached_tabs[new_current_id])
new_current_id = detached_tabs[new_current_id]
}
// current active tab
var $tab = tabs_by_id[new_current_id]
if (!$tab) {
//console.error("ABORT change_current_tab")
//console.info("new:", new_current_id, tabs_by_id[new_current_id])
//console.info("old:", current_tab_id, tabs_by_id[current_tab_id])
return // abort, maybe a tab detached or something
}
// previous active tab
unhighlight_current_tab()
$tab.classList.add("highlighted")
$tab.classList.add("loaded")
scroll_into_view($tab)
current_tab_id = new_current_id
}
function on_detach_tab(tabId, opts) {
if (opts.oldWindowId != window_id) {
return // doesn't concern this window
}
//console.info("on_detach_tab", tabId, opts)
var $tabs = $container.querySelectorAll(".tab")
let li = $tabs[opts.oldPosition]
tabs_by_id[tabId] = li
if (li) {
$container.removeChild(li) // li might not have been inserted
}
// detached tab's parent is current_tab when opening responsive mode,
// and itself when tab is moved (doesn't support moving tabs
// without them being selected yet anyway)
detached_tabs[tabId] = current_tab_id
}
function on_attach_tab(tabId, opts) {
if (opts.newWindowId != window_id) {
return // doesn't concern this window
}
browser.tabs.get(tabId).then((tab) => {
let li = create_li(tab)
insert_tab_at(opts.newPosition, li)
tabs_by_id[tab.id] = li
})
}
// eslint-disable-next-line no-unused-vars
function attach_logger(event_prop) {
function listener(arg1, arg2, arg3) {
var windowId = arg1.windowId
if (!windowId && arg2) {
windowId = arg2.windowId
}
if (!windowId && arg3) {
windowId = arg3.windowId
}
if (!windowId || windowId == window_id) {
// eslint-disable-next-line no-console
console.debug(event_prop, arg1, arg2, arg3)
}
}
browser.tabs[event_prop].addListener(listener)
}
// eslint-disable-next-line no-unused-vars
const to_log = [
"onActivated", "onAttached", "onCreated",
"onDetached", "onMoved", "onReplaced", "onRemoved",
"onUpdated", "onZoomChange"
//"onHighlighted",
]
//to_log.forEach(attach_logger)
browser.tabs.onUpdated.addListener(on_update_tab)
browser.tabs.onActivated.addListener(change_current_tab)
browser.tabs.onRemoved.addListener(on_remove_tab)
browser.tabs.onCreated.addListener(on_create_tab)
browser.tabs.onMoved.addListener(on_moved_tab)
browser.tabs.onDetached.addListener(on_detach_tab)
browser.tabs.onAttached.addListener(on_attach_tab)
browser.windows.getCurrent().then((window_info) => {
window_id = window_info.id
fill_content()
})
window.addEventListener("resize", event => {
let $current_tab = tabs_by_id[current_tab_id]
scroll_into_view($current_tab)
})
//---------- Drag & Drop tabs on the sidebar ----------
//=====================================================
const $dragline = document.querySelector("#drag-line")
const $emptyspace = document.querySelector("#empty-space")
function add_drag_events($el) {
$el.setAttribute("draggable", "true")
$el.addEventListener("dragstart", on_drag_start)
$el.addEventListener("dragend", on_drag_end)
$el.addEventListener("dragenter", on_drag_enter)
$el.addEventListener("dragover", on_drag_over)
$el.addEventListener("drop", on_drop)
}
function on_drag_start(e) {
let tab_index = [].indexOf.call($container.children, this)
let data = this.dataset.id + "|" + tab_index + "&" + window_id
e.dataTransfer.setData("tab/id", data)
e.effectAllowed = "move"
}
// position the dotted line indicating a drop where would insert the tab
function on_drag_enter(e) {
e.preventDefault()
var line_y = window.scrollY
if (this === $emptyspace) {
let $tabs = $container.querySelectorAll(".tab")
let sizing_info = $tabs[$tabs.length - 1].getBoundingClientRect()
line_y += sizing_info.bottom
} else {
let sizing_info = this.getBoundingClientRect()
line_y += sizing_info.top
}
$dragline.style.display = "block"
$dragline.style.top = line_y + "px"
}
function on_drop(e) {
e.preventDefault()
let data = e.dataTransfer.getData("tab/id")
let sep_index = data.indexOf("|")
let sep2_index = data.indexOf("&")
let tab_to_move_id = parseInt(data.substring(0, sep_index))
let tab_to_move_index = parseInt(data.substring(sep_index + 1, sep2_index))
let origin_window = parseInt(data.substring(sep2_index + 1))
if (this === $emptyspace) {
var index = $container.querySelectorAll(".tab").length
browser.tabs.move(tab_to_move_id, {index: index, windowId: window_id})
//console.info("drop emptyspace", index, "window:", window_id)
} else {
var destination_tab = parseInt(this.dataset.id)
browser.tabs.get(destination_tab).then((tab) => {
let index = tab.index
if (window_id === origin_window && tab_to_move_index < index) {
index -= 1
}
browser.tabs.move(tab_to_move_id,
{index: index, windowId: window_id})
})
//console.info("drop tab", tab_to_move_id, "to:", destination_tab,
// "window:", window_id)
}
// hide dragline in the context/window where the tab is dropped
$dragline.style.display = "none"
}
function on_drag_over(e) {
e.preventDefault()
}
function on_drag_end() {
// hide dragline in the context/window where the tab is from
$dragline.style.display = "none"
}
$emptyspace.addEventListener("dragenter", on_drag_enter)
$emptyspace.addEventListener("dragover", on_drag_over)
$emptyspace.addEventListener("drop", on_drop)