-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
163 additions
and
3 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
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
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
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
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,125 @@ | ||
var HMS = require('hindley-milner-search'); | ||
|
||
var state = { | ||
// [Node], the dom elements we will be showing/hiding when user searches | ||
els: null, | ||
// Node, container for results | ||
container: null, | ||
// Node, container for notification if there are no matches | ||
containerNo: null, | ||
// [Signature], the signatures as compiled by HMS | ||
index: [], | ||
// [Signature], most recent matches | ||
matches: [], | ||
// String, most recent user input | ||
prevSearch: 'placeholder', | ||
}; | ||
|
||
function map(f, x) { | ||
return Array.prototype.map.call(x, f); | ||
} | ||
|
||
// Node -> Node | ||
function getGrandparent(el) { | ||
return el.parentNode.parentNode; | ||
} | ||
|
||
// Node -> Node | ||
function getParent(el) { | ||
return el.parentNode; | ||
} | ||
|
||
// Node -> Node | ||
function initSigResult(apiLink) { | ||
var el = document.createElement('div'); | ||
var clone = apiLink.cloneNode(true); | ||
el.appendChild(clone); | ||
return el; | ||
} | ||
|
||
// Node -> Node -> () | ||
function attachSigResult(container) { | ||
return function(el) { | ||
container.appendChild(el); | ||
}; | ||
} | ||
|
||
function findAndCloneSignatures(resultsContainer) { | ||
var codes = document.getElementById('toc').querySelectorAll('li a code'); | ||
var as = map(getParent, codes); | ||
var newEls = map(initSigResult, as); | ||
return newEls; | ||
} | ||
|
||
// Node -> String | ||
function extractSignatureText(el) { | ||
return el.innerText.trim().replace(/\s+/g, ' '); | ||
} | ||
|
||
// Node -> () | ||
function initState(resultsContainer) { | ||
state.container = document.getElementById('yes-matches'); | ||
state.containerNo = document.getElementById('no-matches'); | ||
state.els = findAndCloneSignatures(resultsContainer); | ||
state.index = HMS.index(map(extractSignatureText, state.els)); | ||
} | ||
|
||
// Node -> () | ||
function clearChildren(container) { | ||
while (container.childNodes.length > 0) { | ||
container.removeChild(container.childNodes[0]); | ||
} | ||
} | ||
function show(el) { | ||
el.style.display = 'block'; | ||
} | ||
|
||
function hide(el) { | ||
el.style.display = 'none'; | ||
} | ||
|
||
function showMatches(q, els, matches) { | ||
if (matches.length === 0 && q.length > 0) { | ||
show(state.containerNo); | ||
} | ||
else { | ||
hide(state.containerNo); | ||
} | ||
clearChildren(state.container); | ||
// if there is no query, do not show any matches | ||
if (q.length > 0) { | ||
for (var i = 0; i < matches.length; ++i) { | ||
var match = matches[i]; | ||
var el = state.els[match.pointer]; | ||
state.container.appendChild(el); | ||
} | ||
} | ||
} | ||
|
||
function handleSearchKeyUp(e) { | ||
var q = e.target.value; | ||
if (q === state.prevSearch) { | ||
return; | ||
} else { | ||
// if current search is substring of previous search and previous search has at least 1 word | ||
// character, use previous matches as search space | ||
// todo: we need to know how the search was parsed to properly do this | ||
// eg, if the type changes, we need to reset everything: | ||
// if q is 'a', will search for the name 'a' | ||
// but if q becomes 'a -> a', will search for function | ||
// should we move logic to HMS? | ||
//var space = q.indexOf(state.prevSearch) === 0 && /(^| ).*\w/.test(state.prevSearch) ? state.matches : state.index; | ||
var space = state.index; | ||
state.prevSearch = q; | ||
state.matches = HMS.search(space, q); | ||
showMatches(q, state.els, state.matches); | ||
} | ||
} | ||
|
||
window.addEventListener('DOMContentLoaded', function() { | ||
initState(document.getElementById('search-results')); | ||
var searchInput = document.getElementById('search-input'); | ||
searchInput.focus(); | ||
searchInput.addEventListener('keyup', handleSearchKeyUp); | ||
}); | ||
|
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