2021-04-09 15:28:23 -04:00
|
|
|
import * as params from '@params';
|
|
|
|
|
2022-08-20 04:50:51 -04:00
|
|
|
let fuse; // holds our search engine
|
|
|
|
let resList = document.getElementById('searchResults');
|
|
|
|
let sInput = document.getElementById('searchInput');
|
|
|
|
let first, last, current_elem = null
|
|
|
|
let 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 () {
|
2022-08-20 04:50:51 -04:00
|
|
|
let xhr = new XMLHttpRequest();
|
2020-12-06 07:49:50 -05:00
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
if (xhr.status === 200) {
|
2022-08-20 04:50:51 -04:00
|
|
|
let data = JSON.parse(xhr.responseText);
|
2020-12-06 07:49:50 -05:00
|
|
|
if (data) {
|
|
|
|
// fuse.js options; check fuse.js website for details
|
2022-08-20 04:50:51 -04:00
|
|
|
let options = {
|
2020-12-06 07:49:50 -05:00
|
|
|
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-10-30 04:03:15 -04:00
|
|
|
if (params.fuseOpts) {
|
|
|
|
options = {
|
2022-09-02 12:02:32 -04:00
|
|
|
isCaseSensitive: params.fuseOpts.iscasesensitive ?? false,
|
|
|
|
includeScore: params.fuseOpts.includescore ?? false,
|
|
|
|
includeMatches: params.fuseOpts.includematches ?? false,
|
|
|
|
minMatchCharLength: params.fuseOpts.minmatchcharlength ?? 1,
|
|
|
|
shouldSort: params.fuseOpts.shouldsort ?? true,
|
|
|
|
findAllMatches: params.fuseOpts.findallmatches ?? false,
|
|
|
|
keys: params.fuseOpts.keys ?? ['title', 'permalink', 'summary', 'content'],
|
|
|
|
location: params.fuseOpts.location ?? 0,
|
|
|
|
threshold: params.fuseOpts.threshold ?? 0.4,
|
|
|
|
distance: params.fuseOpts.distance ?? 100,
|
|
|
|
ignoreLocation: params.fuseOpts.ignorelocation ?? true
|
2021-10-30 04:03:15 -04:00
|
|
|
}
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
2021-04-09 15:29:24 -04:00
|
|
|
function activeToggle(ae) {
|
|
|
|
document.querySelectorAll('.focus').forEach(function (element) {
|
|
|
|
// rm focus class
|
|
|
|
element.classList.remove("focus")
|
|
|
|
});
|
|
|
|
if (ae) {
|
|
|
|
ae.focus()
|
|
|
|
document.activeElement = current_elem = ae;
|
|
|
|
ae.parentElement.classList.add("focus")
|
|
|
|
} else {
|
|
|
|
document.activeElement.parentElement.classList.add("focus")
|
|
|
|
}
|
2021-02-07 11:04:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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-04-09 15:28:40 -04:00
|
|
|
if (fuse) {
|
2023-08-12 03:47:24 -04:00
|
|
|
let results;
|
|
|
|
if (params.fuseOpts) {
|
|
|
|
results = fuse.search(this.value.trim(), {limit: params.fuseOpts.limit}); // the actual query being run using fuse.js along with options
|
|
|
|
} else {
|
|
|
|
results = fuse.search(this.value.trim()); // the actual query being run using fuse.js
|
|
|
|
}
|
2021-04-09 15:28:40 -04:00
|
|
|
if (results.length !== 0) {
|
|
|
|
// build our html if result exists
|
|
|
|
let resultSet = ''; // our results bucket
|
2020-12-06 07:49:50 -05:00
|
|
|
|
2021-04-09 15:28:40 -04:00
|
|
|
for (let item in results) {
|
|
|
|
resultSet += `<li class="post-entry"><header class="entry-header">${results[item].item.title} »</header>` +
|
|
|
|
`<a href="${results[item].item.permalink}" aria-label="${results[item].item.title}"></a></li>`
|
|
|
|
}
|
2020-12-06 07:49:50 -05:00
|
|
|
|
2021-04-09 15:28:40 -04:00
|
|
|
resList.innerHTML = resultSet;
|
|
|
|
resultsAvailable = true;
|
|
|
|
first = resList.firstChild;
|
|
|
|
last = resList.lastChild;
|
|
|
|
} else {
|
|
|
|
resultsAvailable = false;
|
|
|
|
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;
|
2022-08-20 04:50:51 -04:00
|
|
|
let ae = document.activeElement;
|
2021-04-09 15:29:24 -04:00
|
|
|
|
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) {
|
2022-08-20 04:50:51 -04:00
|
|
|
let 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
|
|
|
}
|
2021-04-09 15:29:24 -04:00
|
|
|
} else if (current_elem) ae = current_elem;
|
2021-01-16 02:56:55 -05:00
|
|
|
|
2021-09-04 06:14:22 -04:00
|
|
|
if (key === "Escape") {
|
|
|
|
reset()
|
|
|
|
} else if (!resultsAvailable || !inbox) {
|
|
|
|
return
|
|
|
|
} else if (key === "ArrowDown") {
|
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-04-09 15:29:24 -04:00
|
|
|
activeToggle(resList.firstChild.lastChild);
|
2021-09-04 06:14:22 -04:00
|
|
|
} else if (ae.parentElement != last) {
|
2020-12-18 01:15:10 -05:00
|
|
|
// if the currently focused element's parent is last, do nothing
|
|
|
|
// otherwise select the next search result
|
2021-04-09 15:29:24 -04:00
|
|
|
activeToggle(ae.parentElement.nextSibling.lastChild);
|
2020-12-18 01:15:10 -05:00
|
|
|
}
|
2021-09-04 06:14:22 -04:00
|
|
|
} else if (key === "ArrowUp") {
|
2020-12-18 01:15:10 -05:00
|
|
|
e.preventDefault();
|
2021-09-04 06:14:22 -04:00
|
|
|
if (ae.parentElement == first) {
|
2020-12-18 01:15:10 -05:00
|
|
|
// if the currently focused element is first item, go to input box
|
2021-04-09 15:29:24 -04:00
|
|
|
activeToggle(sInput);
|
2021-09-04 06:14:22 -04:00
|
|
|
} else if (ae != sInput) {
|
|
|
|
// if the currently focused element is input box, do nothing
|
2020-12-18 01:15:10 -05:00
|
|
|
// otherwise select the previous search result
|
2021-04-09 15:29:24 -04:00
|
|
|
activeToggle(ae.parentElement.previousSibling.lastChild);
|
2020-12-18 01:15:10 -05:00
|
|
|
}
|
2021-09-04 06:14:22 -04:00
|
|
|
} else if (key === "ArrowRight") {
|
2020-12-18 01:15:10 -05:00
|
|
|
ae.click(); // click on active link
|
|
|
|
}
|
|
|
|
}
|