-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathmain.js
63 lines (52 loc) · 1.72 KB
/
main.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
(function() {
var functions = document.querySelectorAll('[data-name]');
var sections = document.querySelectorAll('.searchable_section');
var searchInput = document.getElementById('function_filter');
function searchValue() {
return searchInput.value.trim().replace(/^_\.?/, '');
}
function strIn(a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return b.indexOf(a) >= 0;
}
function doesMatch(element) {
var name = element.getAttribute('data-name');
var aliases = element.getAttribute('data-aliases') || '';
var value = searchValue();
return strIn(value, name) || strIn(value, aliases);
}
function filterElement(element) {
element.style.display = doesMatch(element) ? '' : 'none';
}
function filterToc() {
_.each(functions, filterElement);
var emptySearch = searchValue() === '';
// Hide the titles of empty sections
_.each(sections, function(section) {
var sectionFunctions = section.querySelectorAll('[data-name]');
var showSection = emptySearch || _.some(sectionFunctions, doesMatch);
section.style.display = showSection ? '' : 'none';
});
}
function gotoFirst() {
var firstFunction = _.find(functions, doesMatch);
if (firstFunction) {
window.location.hash = firstFunction.getAttribute('data-name');
searchInput.focus();
}
}
searchInput.addEventListener('input', filterToc, false);
// Press "Enter" to jump to the first matching function
searchInput.addEventListener('keypress', function(e) {
if (e.which === 13) {
gotoFirst();
}
});
// Press "/" to search
document.body.addEventListener('keyup', function(event) {
if (191 === event.which) {
searchInput.focus();
}
});
}());