2021-04-09 15:28:23 -04:00
|
|
|
import * as params from '@params';
|
|
|
|
|
2020-12-06 07:49:50 -05:00
|
|
|
var fuse; // holds our search engine
|
2020-12-18 01:15:10 -05:00
|
|
|
var resList = document.getElementById('searchResults');
|
|
|
|
var sInput = document.getElementById('searchInput');
|
|
|
|
var first, last = null
|
|
|
|
var resultsAvailable = false;
|
2020-12-06 07:49:50 -05:00
|
|
|
|
2021-04-09 15:28:23 -04:00
|
|
|
// load our search index
|
|
|
|
window.onload = function () {
|
2020-12-06 07:49:50 -05:00
|
|
|
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 01:26:09 -05:00
|
|
|
ignoreLocation: true,
|
2020-12-06 07:49:50 -05:00
|
|
|
keys: [
|
|
|
|
'title',
|
|
|
|
'permalink',
|
|
|
|
'summary',
|
|
|
|
'content'
|
|
|
|
]
|
|
|
|
};
|
2021-04-09 15:28:23 -04:00
|
|
|
if (params.fuseOpts) options = params.fuseOpts;
|
2020-12-06 07:49:50 -05: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 01:15:10 -05: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() {
|
2021-02-07 11:04:34 -05:00
|
|
|
document.activeElement.parentElement.classList.toggle("focus")
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
resultsAvailable = false;
|
|
|
|
resList.innerHTML = sInput.value = ''; // clear inputbox and searchResults
|
|
|
|
sInput.focus(); // shift focus to input box
|
2020-12-18 01:15:10 -05:00
|
|
|
}
|
|
|
|
|
2020-12-06 07:49:50 -05:00
|
|
|
// execute search as each character is typed
|
2021-01-16 02:56:55 -05:00
|
|
|
sInput.onkeyup = function (e) {
|
2020-12-06 07:49:50 -05:00
|
|
|
// run a search query (for "term") every time a letter is typed
|
|
|
|
// in the search box
|
2021-01-30 01:24:34 -05:00
|
|
|
const results = fuse.search(this.value.trim()); // the actual query being run using fuse.js
|
2020-12-06 07:49:50 -05: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)
|
|
|
|
}
|
|
|
|
|
2021-02-07 11:04:34 -05:00
|
|
|
resList.innerHTML = resultSet;
|
2020-12-18 01:15:10 -05:00
|
|
|
resultsAvailable = true;
|
|
|
|
first = resList.firstChild;
|
|
|
|
last = resList.lastChild;
|
2020-12-06 09:14:36 -05:00
|
|
|
} else {
|
2020-12-18 01:15:10 -05:00
|
|
|
resultsAvailable = false;
|
2021-02-07 11:04:34 -05:00
|
|
|
resList.innerHTML = '';
|
2020-12-06 07:49:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-07 11:04:34 -05:00
|
|
|
sInput.addEventListener('search', function (e) {
|
|
|
|
// clicked on x
|
|
|
|
if (!this.value) reset()
|
|
|
|
})
|
|
|
|
|
2020-12-18 01:15:10 -05:00
|
|
|
// kb bindings
|
|
|
|
document.onkeydown = function (e) {
|
|
|
|
let key = e.key;
|
|
|
|
let ae = document.activeElement;
|
2020-12-18 10:55:34 -05:00
|
|
|
let inbox = document.getElementById("searchbox").contains(ae)
|
2020-12-18 01:15:10 -05:00
|
|
|
|
2021-01-16 02:56:55 -05:00
|
|
|
if (ae === sInput) {
|
2021-02-07 11:04:34 -05:00
|
|
|
var elements = document.getElementsByClassName('focus');
|
2021-01-16 02:56:55 -05:00
|
|
|
while (elements.length > 0) {
|
2021-02-07 11:04:34 -05:00
|
|
|
elements[0].classList.remove('focus');
|
2021-01-16 02:56:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 10:55:34 -05:00
|
|
|
if (key === "ArrowDown" && resultsAvailable && inbox) {
|
2020-12-18 01:15:10 -05:00
|
|
|
e.preventDefault();
|
|
|
|
if (ae == sInput) {
|
|
|
|
// if the currently focused element is the search input, focus the <a> of first <li>
|
2021-02-07 11:04:34 -05:00
|
|
|
activeToggle(); // rm focus class
|
2020-12-18 01:15:10 -05:00
|
|
|
resList.firstChild.lastChild.focus();
|
2021-02-07 11:04:34 -05:00
|
|
|
activeToggle(); // add focus class
|
2020-12-18 01:15:10 -05:00
|
|
|
} else if (ae.parentElement == last) {
|
|
|
|
// if the currently focused element's parent is last, do nothing
|
|
|
|
} else {
|
|
|
|
// otherwise select the next search result
|
2021-02-07 11:04:34 -05:00
|
|
|
activeToggle(); // rm focus class
|
2020-12-18 01:15:10 -05:00
|
|
|
ae.parentElement.nextSibling.lastChild.focus();
|
2021-02-07 11:04:34 -05:00
|
|
|
activeToggle(); // add focus class
|
2020-12-18 01:15:10 -05:00
|
|
|
}
|
2020-12-18 10:55:34 -05:00
|
|
|
} else if (key === "ArrowUp" && resultsAvailable && inbox) {
|
2020-12-18 01:15:10 -05: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
|
2021-02-07 11:04:34 -05:00
|
|
|
activeToggle(); // rm focus class
|
2020-12-18 01:15:10 -05:00
|
|
|
sInput.focus();
|
|
|
|
} else {
|
|
|
|
// otherwise select the previous search result
|
2021-02-07 11:04:34 -05:00
|
|
|
activeToggle(); // rm focus class
|
2020-12-18 01:15:10 -05:00
|
|
|
ae.parentElement.previousSibling.lastChild.focus();
|
2021-02-07 11:04:34 -05:00
|
|
|
activeToggle(); // add focus class
|
2020-12-18 01:15:10 -05:00
|
|
|
}
|
2020-12-18 10:55:34 -05:00
|
|
|
} else if (key === "ArrowRight" && resultsAvailable && inbox) {
|
2020-12-18 01:15:10 -05:00
|
|
|
ae.click(); // click on active link
|
|
|
|
} else if (key === "Escape") {
|
2021-02-07 11:04:34 -05:00
|
|
|
reset()
|
2020-12-18 01:15:10 -05:00
|
|
|
}
|
|
|
|
}
|