1
0
mirror of https://github.com/Damillora/Yuika synced 2024-11-25 10:37:32 +00:00
Yuika/assets/js/lib/search.js

43 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-05-07 14:41:29 +00:00
async function searchPosts(query) {
const apiEndpoint = window.yurikoSearchEndpoint + "/api/article/search?"
const queryString = "q="
const pageString = "&page="
let page = 1
var data = await fetch(apiEndpoint + queryString + query + pageString + page)
var search = await data.json();
2021-07-10 17:56:01 +00:00
var result = "";
if (search.result) {
result = search.result.map(x => {
return `
2021-05-07 14:41:29 +00:00
<div class="menu__search-result">
<a href="${x.url}"><h2 class="menu__search-title">${x.title}</h2></a>
<p class="menu__search-desc">${x.excerpt}</p>
</div>
`;
2021-07-10 17:56:01 +00:00
}).join("");
}
2021-05-07 14:41:29 +00:00
2021-07-10 17:56:01 +00:00
document.getElementById("search-count").innerHTML = search.total + " results found";
2021-05-07 14:41:29 +00:00
document.getElementById("search-results").innerHTML = result;
}
if (window.yurikoSearchEndpoint != undefined) {
var el = document.getElementById("search-query");
el.addEventListener("input", function (e) {
var val = el.value;
if (val !== "") {
2021-07-10 17:56:01 +00:00
document.getElementById("site-header").classList.add("search-enabled")
document.getElementById("nav").classList.add("hidden")
2021-05-08 05:43:27 +00:00
searchPosts(val);
2021-05-07 14:41:29 +00:00
} else {
2021-07-10 17:56:01 +00:00
document.getElementById("site-header").classList.remove("search-enabled")
document.getElementById("nav").classList.remove("hidden")
2021-05-07 14:41:29 +00:00
2021-07-10 17:56:01 +00:00
document.getElementById("search-count").innerHTML = ``;
2021-05-07 14:41:29 +00:00
document.getElementById("search-results").innerHTML = ``;
}
});
2021-05-08 05:43:27 +00:00
}