-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathsearch.js
More file actions
240 lines (199 loc) · 7.21 KB
/
search.js
File metadata and controls
240 lines (199 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
var searchData;
var dataLoading = false;
var itemLoadTimer, searchKeystrokeEventTimer;
var autoselectedSearchResult = null;
var projectResultArea, docResultArea, newsResultArea, miscResultArea;
$(document).ready(function () {
docResultArea = $('#search-results-docs > div');
projectResultArea = $('#search-results-projects > div');
newsResultArea = $('#search-results-news > div');
miscResultArea = $('#search-results-misc > div');
var searchQuery = getQueryParam("search");
if (searchQuery) { //If a search query was provided in the query strings, execute the search
loadSearchData(function () {
searchUpdate();
});
$("#search-input").val(searchQuery);
}
//Close the search results if the input and results lose focus
$('body').click(function (event) {
//Check if the click registered on a non search-related element
if (!($(event.target).parents('#search-container').length)) {
hideSearchDropdown();
}
});
$('#search-input')
.focus(searchFocus)
.keyup(function(event) {
if(event.which == 13) {
if(searchKeystrokeEventTimer == undefined && autoselectedSearchResult)
window.location = autoselectedSearchResult;
return;
}
searchTextChanged()
});
$('#search-no-data-warning').hide();
});
Array.prototype.clean = function (deleteValue) {
if (deleteValue == undefined)
deleteValue = '';
for (var i = 0; i < this.length; i++) {
if (this[i] == deleteValue) {
this.splice(i, 1);
i--;
}
}
return this;
};
//Load the search data if the user seems like they intend to enter a query
//If they have already entered text, open the suggestions
function searchFocus() {
ga('send', 'event', 'search', 'focus');
if (!searchData)
loadSearchData(function () {
searchUpdate();
});
else
searchUpdate();
}
function searchTextChanged() {
if (searchKeystrokeEventTimer != undefined)
clearTimeout(searchKeystrokeEventTimer);
searchKeystrokeEventTimer = setTimeout(function () {
searchKeystrokeEventTimer = undefined;
searchUpdate();
}, 400);
}
//Loads the data from the JSON document
function loadSearchData(callback, numRetries) {
if (dataLoading)
return;
dataLoading = true;
$.getJSON("/search-index.json")
.done(function(loadedData) {
dataLoading = false;
searchData = loadedData.slice(0, -1);
for(var dataIndex = 0; dataIndex < searchData.length; dataIndex++) {
searchData[dataIndex].category = searchData[dataIndex].category.split(' ');
}
if (callback != undefined)
callback();
})
.fail(function(error) {
dataLoading = false;
if(numRetries == undefined || numRetries > 0) {
ga('send', 'event', 'search', 'load fail (retry)');
loadSearchData(callback, (Number(numRetries) || 3) - 1);
}
else {
ga('send', 'event', 'search', 'load fail');
}
});
}
//Function to actually execute the search (currently only searches title)
function findResults(term) {
if (!searchData)
return null;
//Split by word
var terms = term.toLowerCase().split(/\W/g);
terms = terms.clean();
var result = [];
if (terms.length <= 0)
return result;
//Iterate over the searchable data
for (var i in searchData) {
//Skip this item if there is no title
if (searchData[i].title == undefined)
continue;
var numMatches = 0;
var title = searchData[i].title.toLowerCase();
//Count the number of terms (words) that are present in the title
for (var termIndex = 0; termIndex < terms.length; termIndex++) {
if (title.search(terms[termIndex]) != -1)
numMatches++;
}
//If all terms match, this one is a hit
if (numMatches >= terms.length)
result.push(searchData[i]);
}
return result;
}
//Function to read query parameters
function getQueryParam(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
//Updates the search function as a whole, and manages the visuals as necessary
function searchUpdate() {
var searchString = $("#search-input").val();
if (searchString == undefined || searchString.length == 0) {
hideSearchDropdown();
}
else {
doSearch(searchString);
}
}
//Executes a search and displays the results
function doSearch(query) {
//Stop all animations
if (itemLoadTimer)
clearTimeout(itemLoadTimer);
$("#search-dropdown .search-result").stop(false, true);
$("#search-dropdown .search-result").show();
//Clear existing results
docResultArea.children('.search-result').remove();
projectResultArea.children('.search-result').remove();
newsResultArea.children('.search-result').remove();
miscResultArea.children('.search-result').remove();
var results = findResults(query);
$('#search-no-data-warning').toggle(!results);
ga('send', 'event', {
eventCategory: 'search',
eventAction: 'query',
eventLabel: query,
eventValue: !!results ? results.length : 0
});
docResultArea.parent().toggle(!!results);
projectResultArea.parent().toggle(!!results);
newsResultArea.parent().toggle(!!results);
miscResultArea.parent().toggle(!!results);
$('#search-dropdown').css('height', results ? '' : 'auto' );
//Start the dropdown box's 'open' animation
if (!$('#search-dropdown').is(":visible"))
$('#search-dropdown').slideDown(400);
if(results && results.length > 0) {
autoselectedSearchResult = null;
(function loadItem(startIndex) {
var resultArea = miscResultArea;
var categoryTags = results[startIndex].category;
if (categoryTags.indexOf('docs') != -1)
resultArea = docResultArea;
else if (categoryTags.indexOf('projects') != -1)
resultArea = projectResultArea;
else if (categoryTags.indexOf('news') != -1)
resultArea = newsResultArea;
resultArea.loadTemplate($('#search-result-template'), results[startIndex], { append: true });
if (startIndex < results.length - 1) {
itemLoadTimer = setTimeout(function () {
loadItem(startIndex + 1)
}, 0);
}
else {
autoselectedSearchResult = $('.search-result a').first().attr('href');
}
})(0)
}
}
//Hides the search dropdown and ties up any loose animations
function hideSearchDropdown() {
if (itemLoadTimer)
clearTimeout(itemLoadTimer);
$("#search-dropdown .search-result").stop(false, true);
$("#search-dropdown .search-result").show();
if ($('#search-dropdown').is(":visible")) {
$('#search-dropdown').slideUp(400, function () {
});
}
}