-
-
Save slashdotdash/a9441baea14228ab84be to your computer and use it in GitHub Desktop.
Initialise lunr.js search on demand when focusing on the search input field.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
// execute the given function only once | |
function once(fn) { | |
var done = false; | |
return function () { | |
return done ? void 0 : ((done = true), fn.apply(this, arguments)) | |
}; | |
} | |
// load a .js script on demand, executing the on loaded function after downloaded and parsed | |
var demand = function(src, onLoaded) { | |
var onLoadOnce = once(onLoaded); | |
var domscript = document.createElement('script'); | |
domscript.src = src; | |
domscript.onload = onLoadOnce; | |
domscript.onreadystatechange = function() { | |
if ('loaded' === domscript.readyState) { | |
onLoadOnce(); | |
} | |
}; | |
document.getElementsByTagName('head')[0].appendChild(domscript); | |
}; | |
// add an event handler to the focus of the given element input | |
var focused = function(id, onFocus) { | |
var elem = document.getElementById(id); | |
if (elem.addEventListener) { | |
elem.addEventListener('focus', onFocus, false); | |
} else if (elem.attachEvent) { | |
elem.attachEvent('onfocus', onFocus); | |
} | |
}; | |
// load the search .js file when the search query input gains focused (once) | |
var loadOnDemand = function(loaded) { | |
focused('search-query', once(function() { | |
demand('/js/search.min.js', loaded); | |
})); | |
}; | |
// initialize lunr.js search after loading dependencies on demand | |
loadOnDemand(function() { | |
(function($) { | |
$('#search-query').lunrSearch({ | |
index: '/search.json', | |
results: '#search-results', | |
entries: '.entries', | |
template: '#search-results-template' | |
}); | |
})(jQuery); | |
}); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){function e(a){var b=!1;return function(){return b?void 0:(b=!0,a.apply(this,arguments))}}var f=function(a,b){var c=e(b),d=document.createElement("script");d.src=a;d.onloadDone=!1;d.onload=c;d.onreadystatechange=function(){"loaded"===d.readyState&&!d.onloadDone&&c()};document.getElementsByTagName("head")[0].appendChild(d)},g=function(a,b){var c=document.getElementById(a);c.addEventListener?c.addEventListener("focus",b,!1):c.attachEvent&&c.attachEvent("onfocus",b)};(function(a){g("search-query",e(function(){f("/js/search.min.js",a)}))})(function(){jQuery("#search-query").lunrSearch({index:"/search.json",results:"#search-results",entries:".entries",template:"#search-results-template"})})})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment