forked from ev3dev/ev3dev.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
202 lines (163 loc) · 5.78 KB
/
search.js
File metadata and controls
202 lines (163 loc) · 5.78 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
var searchData;
var searchData;
var dataLoading = false;
var itemLoadTimer, searchKeystrokeEventTimer;
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 != undefined && 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(searchTextChanged);
});
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() {
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) {
if (dataLoading)
return;
dataLoading = true;
$.getJSON("/search-index.json", function (e) {
dataLoading = false;
searchData = e.slice(0, -1);
if (callback != undefined)
callback();
});
}
//Function to actually execute the search (currently only searches title)
function findResults(term) {
if (!searchData)
return undefined;
//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();
//Start the dropdown box's 'open' animation
if (!$('#search-dropdown').is(":visible"))
$('#search-dropdown').slideDown(400, function () {
});
var results = findResults(query);
//Load and animate the search results (use good JavaScript practices and do it recursively using acync callbacks)
function loadItem(i) {
var resultArea = miscResultArea;
var categoryTags = results[i].category.split(' ');
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[i], { append: true });
resultArea.children().last().show(20);
if (i < results.length - 1) {
itemLoadTimer = setTimeout(function () {
loadItem(i + 1)
}, 5);
}
}
if (results.length > 0)
loadItem(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 () {
});
}
}