1
1
/* eslint-env browser */
2
- /* global jQuery, lunr, Handlebars, prettyPrint */
2
+ /* global jQuery, Handlebars, prettyPrint */
3
3
/*!
4
4
5
5
Documentation middleware.
@@ -228,45 +228,6 @@ Created by Zach Supalla.
228
228
Docs.resultsAdded = 0;
229
229
230
230
Docs.buildSearch = function() {
231
- if (typeof lunr === 'undefined') {
232
- return;
233
- }
234
- // Make Particle.function searchable with function only
235
- lunr.tokenizer.separator = /[\s\-.]+/;
236
- lunr.Pipeline.registerFunction(Docs._removeEmptyTokens, 'removeEmptyTokens');
237
-
238
- $.getJSON('/search-index.json', function(data) {
239
- var store = data.store;
240
- var idx = lunr.Index.load(data.index);
241
- $('input.search-box').keyup(function() {
242
- var searchQuery = this.value;
243
- var specifier="";
244
- if (searchQuery.indexOf(":")!=-1) { // if the searchQuery has a specifier
245
- specifier = searchQuery.split(":")[0];
246
- searchQuery = searchQuery.split(":")[1];
247
- }
248
- Docs.emptyResults();
249
- if (searchQuery === '' || searchQuery.length < 3) {
250
- $('.search-results').hide();
251
- } else {
252
- $('.search-results').show();
253
- var results = idx.search(searchQuery);
254
-
255
- // filter by section
256
- var sectionResults = Docs.filterSearchBySection(results,specifier);
257
-
258
- // define urls to prioritize
259
- // var urlArray = ["faq/particle-devices/led-troubleshooting/photon/#blinking-green"];
260
- // Docs.boostSearchByUrl(sectionResults,urlArray);
261
-
262
- // filter by device
263
- var topFilteredResults = Docs.filterSearchByDevice(sectionResults,specifier);
264
-
265
- // build search
266
- Docs.buildSearchResults(topFilteredResults, store);
267
- }
268
- });
269
- });
270
231
271
232
$('body').click(function() {
272
233
$('.search-results').hide();
@@ -288,121 +249,6 @@ Created by Zach Supalla.
288
249
return stringToTitleCase;
289
250
};
290
251
291
- Docs.boostSearchByUrl = function(results,urlArray) {
292
- var resultsBoost=[];
293
- // if the results you get contain a particular URL, push this URL to the top of the pile.
294
- for (x=0; x<results .length; x++) {
295
- // check to see if there is any overlap between the two arrays
296
- var currentResult = results[x];
297
- var currentUrl = String(currentResult.ref);
298
- for (y = 0; y<urlArray.length; y++) {
299
- if (currentUrl.indexOf(urlArray[y])!=-1) {
300
- // splice
301
- results.splice(x-1,x);
302
- // unshift
303
- results.unshift(currentResult);
304
- }
305
- }
306
- }
307
- return resultsBoost;
308
- }
309
-
310
- Docs.filterSearchBySection = function(results,specifier) {
311
- var pathSearch = window.location.pathname.split(" /" ).filter(function(n){return n!=" " });
312
- var sectionSearch = pathSearch[0];
313
-
314
- var sectionFilteredResults = [];
315
- var allSections = [" guide" ," tutorials" ," faq" ," reference" ," support" ];
316
-
317
- if (specifier.length>0) { // if the searchQuery has a specifier
318
- if (allSections.indexOf(specifier)!=-1) { // if your specifier identifies one of the sections
319
- // narrow by that section
320
- for (x=0; x<results .length; x++) {
321
- var params = String(results[x].ref);
322
- if (params.indexOf(specifier)!=-1) { // if the specifier is in this ref, then include it
323
- sectionFilteredResults.push(results[x]);
324
- }
325
- }
326
- }
327
- else { // if specifier is " all" or anything else, no need to narrow by section
328
- sectionFilteredResults = results;
329
- }
330
- }
331
- // else { // if there is no specifier, then narrow by current section
332
- // for (x = 0; x<results.length; x++) {
333
- // var params = String(results[x].ref);
334
- // if (params.indexOf(sectionSearch)!=-1) { // if the this section is in this ref, then include it
335
- // sectionFilteredResults.push(results[x]);
336
- // }
337
- // }
338
- // }
339
-
340
- else { // if there is no specifier, then search all
341
- sectionFilteredResults = results;
342
- }
343
-
344
- return sectionFilteredResults;
345
- }
346
-
347
- Docs.filterSearchByDevice = function(results,specifier) {
348
- var pathSearch = window.location.pathname.split(" /" ).filter(function(n){return n!=" " });
349
- var deviceSearch = pathSearch[pathSearch.length - 1];
350
-
351
- var allDevices = [" photon" ," core" ," electron" ," argon" ," boron" ," xenon" ];
352
-
353
- if (specifier.length>0) {
354
- if (allDevices.indexOf(specifier)!=-1) { // the specifier is one of the devices
355
- deviceSearch=specifier;
356
- }
357
- }
358
-
359
- var topFilteredResults = [];
360
-
361
- if (allDevices.indexOf(deviceSearch)!=-1) { // this section has device sensitivity
362
- for (x=0; x<results .length; x++) {
363
- var params = String(results[x].ref);
364
- var paramDeviceCount = 0;
365
- for (y = 0; y<allDevices.length; y++) {
366
- if (params.indexOf(allDevices[y])>=0) {
367
- paramDeviceCount++;
368
- }
369
- }
370
- if (paramDeviceCount==0) { // does it have one of the allDevices in it?
371
- topFilteredResults.push(results[x]);
372
- }
373
- else { // check to see if it includes the deviceSearch term
374
- if (String(results[x].ref).indexOf(deviceSearch) >= 0) {
375
- topFilteredResults.push(results[x]);
376
- }
377
- }
378
- }
379
- }
380
- else { // this section does not have device sensitivity, do not filter by device
381
- // probably should filter by some "default" device, whatever is what people most likely want to see
382
- topFilteredResults=results;
383
- }
384
- return topFilteredResults;
385
- }
386
-
387
- Docs.buildSearchResults = function(results, store) {
388
- var fiveResults = results.slice(0,5);
389
-
390
- var niceResults = fiveResults.map(function(r) {
391
- var resultInfo = store[r.ref];
392
- var nr = {};
393
- nr.link = r.ref;
394
- nr.title = resultInfo.title;
395
- nr.device = resultInfo.device;
396
- nr.collection = Docs.titleize(resultInfo.collection);
397
- nr.pageTitle = resultInfo.pageTitle;
398
- nr.collectionClass = resultInfo.collection;
399
- return nr;
400
- });
401
-
402
- var html = Handlebars.templates.search({results: niceResults});
403
- $('.search-results').append(html);
404
- };
405
-
406
252
Docs.toggleShowing = function() {
407
253
$('span.popupLink, span.footnoteLink').on('click', function() {
408
254
$(this).toggleClass('showing');
0 commit comments