forked from jashkenas/underscore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jashkenas#2514 from captbaritone/search
Make functions in docs filterable
- Loading branch information
Showing
3 changed files
with
246 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"globals": { | ||
"_": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
(function() { | ||
var functions = document.querySelectorAll('[data-name]'); | ||
var sections = document.querySelectorAll('.searchable_section'); | ||
var searchInput = document.getElementById('function_filter'); | ||
|
||
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') || ''; | ||
return strIn(searchInput.value, name) || strIn(searchInput.value, aliases); | ||
} | ||
|
||
function filterElement(element) { | ||
element.style.display = doesMatch(element) ? '' : 'none'; | ||
} | ||
|
||
function filterToc() { | ||
_.each(functions, filterElement); | ||
|
||
var emptySearch = searchInput.value === ''; | ||
|
||
// 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(); | ||
} | ||
}); | ||
}()); |
Oops, something went wrong.