| 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.lastChild.getAttribute('href'); |
| 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 |
}()); |