-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmercury.js
192 lines (152 loc) · 5.5 KB
/
mercury.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
var noop = function(){};
var pick = function(key){ return function(obj){ return obj[key]; }; };
var indexOf = function(match){ return function(obj){ return obj.indexOf(match) }};
var tokenizerStr = '([^a-zA-Z0-9]+)';
var tokenizer = new RegExp(tokenizerStr);
var search = document.getElementById('search');
var container = document.getElementById('tablist');
var deathGround = document.getElementById('tab-death');
var tplcache = {};
var tpl = function(name,values){
return Object.keys(values||{}).reduce(
function(context, key){ return context.replace(new RegExp('{{'+key+'}}','g'),values[key]); },
tplcache[name] || (tplcache[name] = document.getElementById(name).innerHTML)
);
};
var row = function(tab){
return tpl('row',{
favIconUrl:tab.favIconUrl || '',
title:tab.title,
url:tab.url
})
};
var create = function(html){
var dummy = document.createElement('div');
dummy.innerHTML = html;
return dummy.children[0]
}
var getInput = function(){ return search.value.toLowerCase() };
var renderTabs = function(tabs){
container.innerHTML = tabs.map( row ).join('');
};
var getEl = function(index){
return container.children[index];
}
var isElementInViewport = function(el) {
var rect = el.getBoundingClientRect();
return (
rect.top >= search.offsetHeight &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
);
}
var selectResult = function(i){
var oldIndex = index;
index = i;
[].slice.apply(container.children).forEach(function(child){
child.className = child.className.replace( /(?:^|\s)active(?!\S)/ , '' )
});
container.children[index].className += ' active';
if (!isElementInViewport(container.children[index])){
container.children[index].scrollIntoView((oldIndex > i || i == 0))
}
};
var activate = function(tab){
chrome.tabs.update(tab.id, {active:true}, function(){
if (tab.windowId !== chrome.windows.WINDOW_ID_CURRENT){
chrome.windows.update(tab.windowId, {focused:true});
}
});
};
var closeTab = function(tab,index,context){
var el = getEl(index);
var dummyEl = create(tpl('dummy',{half:(el.offsetHeight/2)}));
var top = el.offsetTop;
var dyingTabEl = el.parentElement.replaceChild(dummyEl,el)
deathGround.appendChild(dyingTabEl);
dyingTabEl.style.top = top+'px';
dummyEl.addEventListener('animationend', function(){
dummyEl.parentElement.removeChild(dummyEl);
chrome.tabs.remove(tab.id);
results.splice(index,1);
selectResult( (index >= results.length) ? results.length-1 : index );
});
el.addEventListener('animationend', function(){
deathGround.removeChild(el);
});
dummyEl.className = 'shrink'
el.className = 'boom';
};
var clickedIndex = function(ev){
var el = ev.target;
while (el.tagName.toLowerCase() != "li" && el) el = el.parentElement;
return Array.prototype.indexOf.call(el.parentNode.children,el);
};
var score = function(terms){ return function(tab){
var titleTerms = tab.title.toLowerCase().split(tokenizer);
var urlTerms = tab.url.toLowerCase().split(tokenizer);
var allTerms = [].concat(titleTerms,urlTerms);
var matches = terms.map(function(term){
return allTerms.map( indexOf(term) ).indexOf(0);
});
var hasMisMatch = matches.indexOf(-1) >= 0 || !matches.length;
return {
score: hasMisMatch ? -1 : matches.reduce(function(a,b){ return a+b },0),
tab: tab
}
}};
var getSortedResults = function(tabInfos){
var inputTokens = getInput().split(tokenizer).filter(function(term){
return !term.match(tokenizer);
});
return getInput() == ""
? tabInfos
: tabInfos
.map( score(inputTokens) )
.filter( function(st){ return st.score != -1 } )
.sort( function(a,b){ return a.score - b.score } )
.map( pick('tab') );
};
var results = [];
var index = 0;
chrome.tabs.query({}, function(tabInfos){
var filterAndDraw = function(){
results = getSortedResults(tabInfos);
renderTabs(results);
selectResult(0);
};
container.addEventListener('click', function(e){
index = clickedIndex(e);
activate(results[index]);
});
search.addEventListener('input', filterAndDraw );
search.addEventListener('keydown', function(ev){
var forTab = function(fn){ return function(){ fn(results[index], index, results); }; };
var capture = function(fn){ return function(){ fn(); ev.preventDefault(); return false } };
var xor = function(a,b){ return function(fn){
return (a(fn) == b(fn)) ? noop : fn;
}};
var ctrl = function(fn){ return ev.ctrlKey ? fn : noop };
var meta = function(fn){ return ev.metaKey ? fn : noop };
var dir = function(x){ return capture(function(){
var i = index + x + results.length;
i %= results.length;
selectResult(i);
})};
var cmds = {
38: dir(-1),
40: dir(1),
78: ctrl(dir(1)),
80: ctrl(dir(-1)),
13: forTab(activate),
8: xor(meta,ctrl)(capture(forTab(closeTab)))
};
return (cmds[ev.keyCode] || noop)();
});
filterAndDraw();
tablist.removeAttribute('class'); // remove heightHack
});
document.addEventListener('keydown', function(){
search.focus();
});