Skip to content

Commit 3a0c2c4

Browse files
committed
search inside of strings
1 parent 272cc44 commit 3a0c2c4

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

lib/resources/script.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,48 @@ function shiftWindow() {
5959
}
6060

6161
function initSearch() {
62-
var elements = new Bloodhound({
63-
datumTokenizer: function(d) {
64-
return Bloodhound.tokenizers.whitespace(d.name);
62+
var index;
63+
64+
function findMatches(q, cb) {
65+
var matches, substringRegex;
66+
67+
// an array that will be populated with substring matches
68+
matches = [];
69+
70+
// regex used to determine if a string contains the substring `q`
71+
substrRegex = new RegExp(q, 'i');
72+
73+
// iterate through the pool of strings and for any string that
74+
// contains the substring `q`, add it to the `matches` array
75+
$.each(index, function(i, element) {
76+
if (substrRegex.test(element.name)) {
77+
matches.push(element.name);
78+
}
79+
});
80+
81+
cb(matches);
82+
};
83+
84+
function initTypeahead() {
85+
$('#search-box.typeahead').typeahead({
86+
hint: true,
87+
highlight: true,
88+
minLength: 3
6589
},
66-
queryTokenizer: Bloodhound.tokenizers.whitespace,
67-
prefetch: 'index.json'
68-
});
90+
{
91+
name: 'elements',
92+
source: findMatches
93+
});
94+
}
6995

70-
$('#search-box.typeahead').typeahead({
71-
hint: true,
72-
highlight: true,
73-
minLength: 3
74-
},
75-
{
76-
name: 'elements',
77-
source: elements,
78-
displayKey: 'name',
79-
limit: 10
96+
var jsonReq = new XMLHttpRequest();
97+
jsonReq.open('GET', 'index.json', true);
98+
jsonReq.addEventListener('load', function() {
99+
index = JSON.parse(jsonReq.responseText);
100+
initTypeahead();
80101
});
102+
jsonReq.send();
103+
81104
}
82105

83106
document.addEventListener("DOMContentLoaded", function() {

0 commit comments

Comments
 (0)