website/themes/terminal-plus-minus/assets/js/search-post.js
2019-09-02 01:51:06 +08:00

56 lines
1.6 KiB
JavaScript

let search = {};
function getJson(url) {
return fetch(url).then(response => response.json()).catch(error => [])
}
function searchEvent(event) {
const { target } = event;
if (target.classList.contains("site__search-bar")) {
const searchResultsNode = target.parentNode.parentNode.querySelector(".site__search-bar-results");
if (!searchResultsNode) {
return;
}
while (searchResultsNode.firstElementChild) {searchResultsNode.removeChild(searchResultsNode.firstElementChild)}
const searchResults = search.search(target.value)
for (const searchResult of searchResults) {
const searchResultItemElement = document.createElement("a");
searchResultItemElement.classList.add("search-results__item");
searchResultItemElement.href = searchResult.url
searchResultItemElement.textContent = searchResult.name
searchResultsNode.appendChild(searchResultItemElement);
}
}
}
async function loadIndex(url, container) {
const json = await getJson(url);
const options = {
shouldSort: true,
threshold: 0.6,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: [
"name"
]
}
const fuse = new Fuse(json, options);
search = fuse;
const searchBars = document.querySelectorAll(".site__search");
for (const searchBar of searchBars) {
const searchBarNode = searchBar.querySelector(".site__search-bar.form__input");
searchBar.addEventListener("input", searchEvent);
}
}