-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtab-finder.js
124 lines (103 loc) · 2.77 KB
/
tab-finder.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
/**
* Tab finder
*
* The tab finder js that controlls the logic for finding and switching tabs.
* @author Will Fowler
*/
// The tablist that will contain all our tabs.
const tabList = new TabList();
// The active item controller.
const activeItem = new ActiveItem();
// Attaching behaviour to search and tabs after the popup has loaded.
document.addEventListener('DOMContentLoaded', () => {
activeItem.reset();
// Search element behaviour.
const searchElement = document.getElementById('search-input');
searchElement.focus();
searchElement.addEventListener('keydown', (keydownEvent) => {
tabList.updateFilter(searchElement.value);
renderTabList();
if (keydownEvent.which !== 38 && keydownEvent.which !== 40 && keydownEvent.which !== 13) {
activeItem.reset();
}
})
// Collect the active tabs.
browser.tabs.query({currentWindow: true})
.then((tabs) => {
tabList.setList(tabs);
renderTabList();
activeItem.reset();
});
});
// Tab Element list behaviour.
window.addEventListener('keydown', (e) => {
let keyPressed = e.which;
if (keyPressed === 38) {
// Going up.
activeItem.increment();
}
else if(keyPressed === 40) {
// Going down.
activeItem.decrement();
}
else if (keyPressed === 13) {
// Navigating to tab.
activeItem.getActive().firstChild.click();
}
else {
// Don't care about any other ones.
return;
}
e.preventDefault();
return;
});
/**
* Builds an HTMLElement tab link.
* @param {tabs.Tab} tab
* @returns {HTMLElement}
*/
function tabLinkFactory(tab) {
let tabLink = document.createElement('a');
tabLink.textContent = tab.title || tab.id;
let audioClass = null;
if (tab.audible === true) {
audioClass = 'audible';
}
if (tab.mutedInfo.muted === true) {
audioClass = 'muted'
}
tabLink.setAttribute('class', audioClass)
tabLink.setAttribute('href', tab.id);
// Attach a navigation action to click events.
tabLink.addEventListener('click', (e) => {
browser.tabs.update(Number(e.target.getAttribute('href')), {
active: true
})
.then(() => {
window.close();
})
e.preventDefault();
});
return tabLink;
}
/**
* Render the tab list.
*/
function renderTabList() {
let tabElementsList = document.getElementById('tabs__list');
// Remove all existing tabs
while (tabElementsList.hasChildNodes()) {
tabElementsList.removeChild(tabElementsList.lastChild);
}
tabs = tabList.getFilteredList();
for (let tab of tabs) {
// Ignore the tab they're currently on.
if (tab.active) {
continue;
}
let tabItem = document.createElement('li');
tabItem.appendChild(tabLinkFactory(tab));
tabItem.classList.add('tabs__item');
tabElementsList.appendChild(tabItem);
}
}