2020-12-06 13:49:50 +01:00
|
|
|
var fuse; // holds our search engine
|
2020-12-18 07:15:10 +01:00
|
|
|
var resList = document.getElementById('searchResults');
|
|
|
|
var sInput = document.getElementById('searchInput');
|
|
|
|
var first, last = null
|
|
|
|
var resultsAvailable = false;
|
2020-12-06 13:49:50 +01:00
|
|
|
|
|
|
|
// load our search index, only executed onload
|
|
|
|
function loadSearch() {
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
var data = JSON.parse(xhr.responseText);
|
|
|
|
if (data) {
|
|
|
|
// fuse.js options; check fuse.js website for details
|
|
|
|
var options = {
|
|
|
|
distance: 100,
|
|
|
|
threshold: 0.4,
|
2021-01-30 07:26:09 +01:00
|
|
|
ignoreLocation: true,
|
2020-12-06 13:49:50 +01:00
|
|
|
keys: [
|
|
|
|
'title',
|
|
|
|
'permalink',
|
|
|
|
'summary',
|
|
|
|
'content'
|
|
|
|
]
|
|
|
|
};
|
2020-12-15 14:25:01 +01:00
|
|
|
{{ if . }}options = {{ jsonify . }}{{ end }} // load custom options from .Site.Params.fuseOpts
|
2020-12-06 13:49:50 +01:00
|
|
|
fuse = new Fuse(data, options); // build the index from the json file
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log(xhr.responseText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.open('GET', "../index.json");
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
|
2020-12-18 07:15:10 +01:00
|
|
|
|
|
|
|
function itemGen(name, link) {
|
|
|
|
return `<li class="post-entry"><header class="entry-header">${name} »</header><a href="${link}" aria-label="${name}"></a></li>`
|
|
|
|
}
|
|
|
|
|
|
|
|
function activeToggle() {
|
|
|
|
document.activeElement.parentElement.classList.toggle("active")
|
|
|
|
}
|
|
|
|
|
2020-12-06 13:49:50 +01:00
|
|
|
// execute search as each character is typed
|
2021-01-16 08:56:55 +01:00
|
|
|
sInput.onkeyup = function (e) {
|
2020-12-06 13:49:50 +01:00
|
|
|
// run a search query (for "term") every time a letter is typed
|
|
|
|
// in the search box
|
2021-01-30 07:24:34 +01:00
|
|
|
const results = fuse.search(this.value.trim()); // the actual query being run using fuse.js
|
2020-12-06 13:49:50 +01:00
|
|
|
|
|
|
|
if (results.length !== 0) {
|
|
|
|
// build our html if result exists
|
|
|
|
let resultSet = ''; // our results bucket
|
|
|
|
|
|
|
|
for (let item in results) {
|
|
|
|
resultSet = resultSet + itemGen(results[item].item.title, results[item].item.permalink)
|
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById("searchResults").innerHTML = resultSet;
|
2020-12-18 07:15:10 +01:00
|
|
|
resultsAvailable = true;
|
|
|
|
first = resList.firstChild;
|
|
|
|
last = resList.lastChild;
|
2020-12-06 15:14:36 +01:00
|
|
|
} else {
|
2020-12-18 07:15:10 +01:00
|
|
|
resultsAvailable = false;
|
2020-12-06 15:14:36 +01:00
|
|
|
document.getElementById("searchResults").innerHTML = '';
|
2020-12-06 13:49:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 07:15:10 +01:00
|
|
|
// kb bindings
|
|
|
|
document.onkeydown = function (e) {
|
|
|
|
let key = e.key;
|
|
|
|
let ae = document.activeElement;
|
2020-12-18 16:55:34 +01:00
|
|
|
let inbox = document.getElementById("searchbox").contains(ae)
|
2020-12-18 07:15:10 +01:00
|
|
|
|
2021-01-16 08:56:55 +01:00
|
|
|
if (ae === sInput) {
|
|
|
|
var elements = document.getElementsByClassName('active');
|
|
|
|
while (elements.length > 0) {
|
|
|
|
elements[0].classList.remove('active');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 16:55:34 +01:00
|
|
|
if (key === "ArrowDown" && resultsAvailable && inbox) {
|
2020-12-18 07:15:10 +01:00
|
|
|
e.preventDefault();
|
|
|
|
if (ae == sInput) {
|
|
|
|
// if the currently focused element is the search input, focus the <a> of first <li>
|
|
|
|
activeToggle(); // rm active class
|
|
|
|
resList.firstChild.lastChild.focus();
|
|
|
|
activeToggle(); // add active class
|
|
|
|
} else if (ae.parentElement == last) {
|
|
|
|
// if the currently focused element's parent is last, do nothing
|
|
|
|
} else {
|
|
|
|
// otherwise select the next search result
|
|
|
|
activeToggle(); // rm active class
|
|
|
|
ae.parentElement.nextSibling.lastChild.focus();
|
|
|
|
activeToggle(); // add active class
|
|
|
|
}
|
2020-12-18 16:55:34 +01:00
|
|
|
} else if (key === "ArrowUp" && resultsAvailable && inbox) {
|
2020-12-18 07:15:10 +01:00
|
|
|
e.preventDefault();
|
|
|
|
if (ae == sInput) {
|
|
|
|
// if the currently focused element is input box, do nothing
|
|
|
|
} else if (ae.parentElement == first) {
|
|
|
|
// if the currently focused element is first item, go to input box
|
|
|
|
activeToggle(); // rm active class
|
|
|
|
sInput.focus();
|
|
|
|
} else {
|
|
|
|
// otherwise select the previous search result
|
|
|
|
activeToggle(); // rm active class
|
|
|
|
ae.parentElement.previousSibling.lastChild.focus();
|
|
|
|
activeToggle(); // add active class
|
|
|
|
}
|
2020-12-18 16:55:34 +01:00
|
|
|
} else if (key === "ArrowRight" && resultsAvailable && inbox) {
|
2020-12-18 07:15:10 +01:00
|
|
|
ae.click(); // click on active link
|
|
|
|
} else if (key === "Escape") {
|
|
|
|
resultsAvailable = false;
|
2021-01-16 08:56:55 +01:00
|
|
|
resList.innerHTML = sInput.value = ''; // clear inputbox and searchResults
|
2020-12-18 07:15:10 +01:00
|
|
|
sInput.focus(); // shift focus to input box
|
|
|
|
}
|
|
|
|
}
|