-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsearch.js
More file actions
240 lines (215 loc) · 6.34 KB
/
search.js
File metadata and controls
240 lines (215 loc) · 6.34 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
/**
* A simple JSON search
* Requires jQuery (v 1.7+)
*
* @author Mat Hayward - Erskine Design
* @author Rishikesh Darandale <Rishikesh.Darandale@gmail.com>
* @version 1.1
*/
/* ==========================================================================
Initialisation
========================================================================== */
var q, jsonFeedUrl = '/feeds/feed.json',
$searchForm = $('[data-search-form]'),
$searchInput = $('[data-search-input]'),
$resultTemplate = $('#search-result'),
$resultsPlaceholder = $('[data-search-results]'),
$foundContainer = $('[data-search-found]'),
$foundTerm = $('[data-search-found-term]'),
$foundCount = $('[data-search-found-count]'),
allowEmpty = true,
showLoader = true,
loadingClass = 'is--loading',
indexVar;
$(document).ready( function() {
// hide items found string
$foundContainer.hide();
// initiate search functionality
initSearch();
});
/* ==========================================================================
Search functions
========================================================================== */
/**
* Initiate search functionality.
* Shows results based on querystring if present.
* Binds search function to form submission.
*/
function initSearch() {
if(!sessionStorage.getItem('lunrIndex')) {
// get the data
getData();
} else {
// Get search results if q parameter is set in querystring
if (getParameterByName('q')) {
q = decodeURIComponent(getParameterByName('q'));
$searchInput.val(q);
execSearch(q);
}
}
// Get search results on submission of form
$(document).on('submit', $searchForm, function(e) {
e.preventDefault();
q = $searchInput.val();
execSearch(q);
});
}
/**
* Get the JSON data
* Get the generated feeds/feed.json file so lunr.js can search it locally.
* Store the index in sessionStorage
*/
function getData(indexVar) {
jqxhr = $.getJSON(jsonFeedUrl)
.done(function(loaded_data){
// save the actual data as well
sessionStorage.setItem('actualData', JSON.stringify(loaded_data));
// set the index fields
indexVar = lunr(function () {
this.field('id');
this.field('title');
this.field('content', { boost: 10 });
this.field('author');
loaded_data.forEach(function (doc, index) {
if ( doc.search_omit != 'true' ) {
// console.log('adding to index: ' + doc.title);
this.add($.extend({ 'id': index }, doc));
}
}, this)
});
// store the index in sessionStorage
sessionStorage.setItem('lunrIndex', JSON.stringify(indexVar));
// Get search results if q parameter is set in querystring
if (getParameterByName('q')) {
q = decodeURIComponent(getParameterByName('q'));
$searchInput.val(q);
execSearch(q);
}
})
.fail( function() {
console.log('get json failed…');
})
.always( function() {
console.log('finally…');
});
}
/**
* Get the search result from lunr
* @param {String} q
* @returns search results
*/
function getResults(q) {
var savedIndexData = JSON.parse(sessionStorage.getItem('lunrIndex'));
// console.log('Indexed var from sessionStorage: ' + savedIndexData);
return lunr.Index.load(savedIndexData).search(q);
}
/**
* Executes search
* @param {String} q
* @return null
*/
function execSearch(q) {
if (q != '' || allowEmpty) {
if (showLoader) {
toggleLoadingClass();
}
processResultData(getResults(q));
}
}
/**
* Toggles loading class on results and found string
* @return null
*/
function toggleLoadingClass() {
$resultsPlaceholder.toggleClass(loadingClass);
$foundContainer.toggleClass(loadingClass);
}
/**
* Process search result data
* @return null
*/
function processResultData(searchResults) {
$results = [];
// console.log('Search Results: ' + searchResults);
var resultsCount = 0,
results = '';
// Iterate over the results
searchResults.forEach(function(result) {
var loaded_data = JSON.parse(sessionStorage.getItem('actualData'));
var item = loaded_data[result.ref];
var result = populateResultContent($resultTemplate.html(), item);
resultsCount++;
results += result;
});
if (showLoader) {
toggleLoadingClass();
}
populateResultsString(resultsCount);
showSearchResults(results);
}
/**
* Add search results to placeholder
* @param {String} results
* @return null
*/
function showSearchResults(results) {
// Add results HTML to placeholder
$resultsPlaceholder.html(results);
}
/**
* Add results content to item template
* @param {String} html
* @param {object} item
* @return {String} Populated HTML
*/
function populateResultContent(html, item) {
html = injectContent(html, item.title, '##Title##');
html = injectContent(html, item.link, '##Url##');
if (item.excerpt) {
html = injectContent(html, item.excerpt, '##Excerpt##');
} else {
html = injectContent(html, '', '##Excerpt##');
}
if (item.date) {
html = injectContent(html, item.date, '##Date##');
} else {
html = injectContent(html, '', '##Date##');
}
return html;
}
/**
* Populates results string
* @param {String} count
* @return null
*/
function populateResultsString(count) {
if (typeof window.history.replaceState == 'function') {
history.replaceState({}, '', '?q='+encodeURIComponent(q));
}
$foundTerm.text(q);
$foundCount.text(count);
$foundContainer.show();
}
/* ==========================================================================
Helper functions
========================================================================== */
/**
* Gets query string parameter - taken from http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
* @param {String} name
* @return {String} parameter value
*/
function getParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
/**
* Injects content into template using placeholder
* @param {String} originalContent
* @param {String} injection
* @param {String} placeholder
* @return {String} injected content
*/
function injectContent(originalContent, injection, placeholder) {
var regex = new RegExp(placeholder, 'g');
return originalContent.replace(regex, injection);
}