-
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
search #23
base: gh-pages
Are you sure you want to change the base?
search #23
Changes from 5 commits
6acbe96
6e24452
11b47db
71d3b85
b133339
b556b1c
03345c4
de62ed2
37df703
4a95614
340bd9e
551a2c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
(function() { | ||
|
||
'use strict'; | ||
|
||
/* global require */ | ||
|
||
var HMS = require('hindley-milner-search'); | ||
|
||
// initSigNodes :: Node -> [Node] | ||
function initSigNodes(resultsContainer) { | ||
return Array.prototype.reduce.call( | ||
document.getElementById('toc').querySelectorAll('li a code'), | ||
function(r, _el) { | ||
var el = document.createElement('div'); | ||
el.appendChild(_el.parentNode.cloneNode(true)); | ||
el.className = 'search-result'; | ||
var k = el.childNodes[0].getAttribute('href'); | ||
if (!Object.prototype.hasOwnProperty.call(r.set, k)) { | ||
r.set[k] = true; | ||
r.uniqs.push(el); | ||
} | ||
return r; | ||
}, | ||
{set: {}, uniqs: []} | ||
).uniqs; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👾 |
||
window.addEventListener('DOMContentLoaded', function() { | ||
var $container = document.getElementById('yes-matches'); | ||
var $containerNo = document.getElementById('no-matches'); | ||
var $els = initSigNodes(document.getElementById('search-results')); | ||
var search = HMS.init(Array.prototype.map.call($els, function(el) { | ||
return el.innerText.trim().split(/\s+/).join(' '); | ||
}), {fuzzy: false}).search; | ||
var $prevSearch = ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In 6e24452 I replaced the |
||
var searchInput = document.getElementById('search-input'); | ||
searchInput.focus(); | ||
searchInput.addEventListener('keyup', function(event) { | ||
var q = event.target.value; | ||
if (q !== $prevSearch) { | ||
$prevSearch = q; | ||
var matches = search(q); | ||
$containerNo.style.display = | ||
matches.length === 0 && q !== '' ? 'block' : 'none'; | ||
while ($container.childNodes.length > 0) { | ||
$container.removeChild($container.childNodes[0]); | ||
} | ||
// if there is no query, do not show any matches | ||
if (q !== '') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I originally had the empty string return all matches simply because I was culling results from the current DOM rather than building a new list. I'm not sure which would be more common, but I can add an option to return all or none on empty input and we wouldn't need this conditional in userland either way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we'll be displaying results at /search/ rather than at /, displaying all results for an empty query seems right to me. I've removed this check. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to move this logic a little in the latest release. Not a million percent sure we're on the same page but just let me know if empty string is not doing what you want now. |
||
matches.forEach(function(match) { | ||
$container.appendChild($els[match.pointer]); | ||
}); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
}()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't much like the
show
andhide
functions as they're impure. I'd prefer this mutation to appear in situ: