Skip to content

Commit 0111847

Browse files
committed
Merge pull request #2514 from captbaritone/search
Make functions in docs filterable
2 parents 28e9704 + ef9ee4b commit 0111847

File tree

3 files changed

+246
-156
lines changed

3 files changed

+246
-156
lines changed

docs/.eslintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"globals": {
3+
"_": true
4+
}
5+
}

docs/main.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
(function() {
2+
var functions = document.querySelectorAll('[data-name]');
3+
var sections = document.querySelectorAll('.searchable_section');
4+
var searchInput = document.getElementById('function_filter');
5+
6+
function strIn(a, b) {
7+
a = a.toLowerCase();
8+
b = b.toLowerCase();
9+
return b.indexOf(a) >= 0;
10+
}
11+
12+
function doesMatch(element) {
13+
var name = element.getAttribute('data-name');
14+
var aliases = element.getAttribute('data-aliases') || '';
15+
return strIn(searchInput.value, name) || strIn(searchInput.value, aliases);
16+
}
17+
18+
function filterElement(element) {
19+
element.style.display = doesMatch(element) ? '' : 'none';
20+
}
21+
22+
function filterToc() {
23+
_.each(functions, filterElement);
24+
25+
var emptySearch = searchInput.value === '';
26+
27+
// Hide the titles of empty sections
28+
_.each(sections, function(section) {
29+
var sectionFunctions = section.querySelectorAll('[data-name]');
30+
var showSection = emptySearch || _.some(sectionFunctions, doesMatch);
31+
section.style.display = showSection ? '' : 'none';
32+
});
33+
}
34+
35+
function gotoFirst() {
36+
var firstFunction = _.find(functions, doesMatch);
37+
if(firstFunction) {
38+
window.location.hash = firstFunction.getAttribute('data-name');
39+
searchInput.focus();
40+
}
41+
}
42+
43+
searchInput.addEventListener('input', filterToc, false);
44+
45+
// Press "Enter" to jump to the first matching function
46+
searchInput.addEventListener('keypress', function(e) {
47+
if (e.which === 13) {
48+
gotoFirst();
49+
}
50+
});
51+
52+
// Press "/" to search
53+
document.body.addEventListener('keyup', function(event) {
54+
if (191 === event.which) {
55+
searchInput.focus();
56+
}
57+
});
58+
}());

0 commit comments

Comments
 (0)