';
}
function initSearch() {
var $searchComponent = document.getElementById('search');
if ($searchComponent === null) {
return;
}
var $searchInput = $searchComponent.querySelector('input');
var $searchResults = $searchComponent.querySelector('.results-container');
var $searchResultsItems = $searchComponent.querySelector('.results');
var MAX_ITEMS = 10;
var options = {
bool: 'AND',
fields: {
title: { boost: 2 },
body: { boost: 1 },
}
};
var currentTerm = '';
var index = elasticlunr.Index.load(window.searchIndex);
$searchInput.addEventListener("keyup", debounce(function () {
var term = $searchInput.value.trim();
if (term === currentTerm || !index) {
return;
}
$searchResults.style.display = term === '' ? 'none' : 'block';
$searchResultsItems.innerHTML = '';
if (term === '') {
return;
}
var results = index.search(term, options);
if (results.length === 0) {
$searchResults.style.display = 'none';
return;
}
currentTerm = term;
for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) {
var item = document.createElement('li');
item.innerHTML = formatSearchResultItem(results[i], term.split(' '));
$searchResultsItems.appendChild(item);
}
}, 150));
}
if (document.readyState === 'complete' ||
(document.readyState !== 'loading' && !document.documentElement.doScroll)
) {
initSearch();
} else {
document.addEventListener('DOMContentLoaded', initSearch);
}