// https://github.com/Pixabay/JavaScript-autoComplete/commit/eda3201210a86f7fcfb7f496f4b2ca419ff423e8
// MIT Licence
var autoComplete=function(){function e(e){function t(e,t){return e.classList?e.classList.contains(t):new RegExp("\\b"+t+"\\b").test(e.className)}function o(e,t,o){e.attachEvent?e.attachEvent("on"+t,o):e.addEventListener(t,o)}function s(e,t,o){e.detachEvent?e.detachEvent("on"+t,o):e.removeEventListener(t,o)}function n(e,s,n,l){o(l||document,s,function(o){for(var s,l=o.target||o.srcElement;l&&!(s=t(l,e));)l=l.parentElement;s&&n.call(l,o)})}if(document.querySelector){var l={selector:0,source:0,minChars:3,delay:150,offsetLeft:0,offsetTop:1,cache:1,menuClass:"",renderItem:function(e,t){t=t.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&");var o=new RegExp("("+t.split(" ").join("|")+")","gi");return'
'+e.replace(o,"$1")+"
"},onSelect:function(){}};for(var c in e)e.hasOwnProperty(c)&&(l[c]=e[c]);for(var a="object"==typeof l.selector?[l.selector]:document.querySelectorAll(l.selector),u=0;u0?i.sc.scrollTop=n+i.sc.suggestionHeight+s-i.sc.maxHeight:0>n&&(i.sc.scrollTop=n+s)}else i.sc.scrollTop=0},o(window,"resize",i.updateSC),document.body.appendChild(i.sc),n("autocomplete-suggestion","mouseleave",function(){var e=i.sc.querySelector(".autocomplete-suggestion.selected");e&&setTimeout(function(){e.className=e.className.replace("selected","")},20)},i.sc),n("autocomplete-suggestion","mouseover",function(){var e=i.sc.querySelector(".autocomplete-suggestion.selected");e&&(e.className=e.className.replace("selected","")),this.className+=" selected"},i.sc),n("autocomplete-suggestion","mousedown",function(e){if(t(this,"autocomplete-suggestion")){var o=this.getAttribute("data-val");i.value=o,l.onSelect(e,o,this),i.sc.style.display="none"}},i.sc),i.blurHandler=function(){try{var e=document.querySelector(".autocomplete-suggestions:hover")}catch(t){var e=0}e?i!==document.activeElement&&setTimeout(function(){i.focus()},20):(i.last_val=i.value,i.sc.style.display="none",setTimeout(function(){i.sc.style.display="none"},350))},o(i,"blur",i.blurHandler);var r=function(e){var t=i.value;if(i.cache[t]=e,e.length&&t.length>=l.minChars){for(var o="",s=0;st||t>40)&&13!=t&&27!=t){var o=i.value;if(o.length>=l.minChars){if(o!=i.last_val){if(i.last_val=o,clearTimeout(i.timer),l.cache){if(o in i.cache)return void r(i.cache[o]);for(var s=1;s"],
post_tags: [""],
fragment_size: 80,
no_match_size: 200,
fields: {
description: {number_of_fragments: 0},
og_description: {number_of_fragments: 0},
twitter_description: {number_of_fragments: 0},
title: {number_of_fragments: 0},
body: {number_of_fragments: 2}
}
},
stored_fields: ['id'],
_source: ['title', 'date']
};
var request = new XMLHttpRequest();
request.open('POST', url, true);
request.setRequestHeader('Content-Type', 'application/json');
request.send(JSON.stringify(q));
request.onload = function(e) {
if (request.status == 404) {
callback({
currentPage: 0,
resultsPerPage: resultsPerPage,
numPages: 0,
took: 0,
total: 0,
hits: []
});
}
else {
var searchFunction = function(resultsPerPage, page) {
search(term, callback, resultsPerPage, page);
};
var rawResults = JSON.parse(request.responseText);
var givenResults = rawResults.hits;
givenResults.currentPage = page;
givenResults.resultsPerPage = resultsPerPage;
givenResults.numPages = Math.trunc(givenResults.total / givenResults.resultsPerPage) + 1;
for (var i = 0; i < givenResults.hits.length; i++) {
var hit = givenResults.hits[i];
hit.url = hit._id;
var highlight = hit.highlight;
if (highlight.body) {
highlight.fragment = highlight.body;
}
if (highlight.description) {
highlight.body = highlight.description;
}
else if (highlight.og_description) {
highlight.body = highlight.og_description;
}
else if (highlight.twitter_description) {
highlight.body = highlight.twitter_description;
}
}
callback(givenResults, searchFunction);
}
};
}
}