diff --git a/assets/js/index.js b/assets/js/index.js index 084313a..9dfc8e3 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -1,4 +1,5 @@ import './lib/dark-mode'; import './lib/fitvids'; import './lib/infinite-scroll'; -import './lib/nav-collapse'; \ No newline at end of file +import './lib/nav-collapse'; +import './lib/search'; \ No newline at end of file diff --git a/assets/js/lib/nav-collapse.js b/assets/js/lib/nav-collapse.js index a39649e..386eafe 100644 --- a/assets/js/lib/nav-collapse.js +++ b/assets/js/lib/nav-collapse.js @@ -5,9 +5,10 @@ function toggleMenu() { } document.querySelector("#menushow").addEventListener("click", toggleMenu); -document.querySelectorAll(".menu__item").forEach(menu => +document.querySelectorAll(".menu__nav-item").forEach(menu => menu.addEventListener("click", toggleMenu) ); +document.getElementById("darkMode").addEventListener("click", toggleMenu); var nav = document.querySelector('.site-header'); var feed = document.querySelector('main'); diff --git a/assets/js/lib/search.js b/assets/js/lib/search.js new file mode 100644 index 0000000..c879b28 --- /dev/null +++ b/assets/js/lib/search.js @@ -0,0 +1,37 @@ +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(); + var result = search.result.map(x => { + return ` +
+ `; + }).join(""); + + document.getElementById("search-results").innerHTML = result; +} + +let searchTimeout; + +if (window.yurikoSearchEndpoint != undefined) { + var el = document.getElementById("search-query"); + el.addEventListener("input", function (e) { + if (searchTimeout) window.clearTimeout(searchTimeout); + var val = el.value; + if (val !== "") { + document.getElementById("nav-menu").classList.add("menu__item--hidden") + searchTimeout = window.setTimeout(() => searchPosts(val), 2000); + } else { + document.getElementById("nav-menu").classList.remove("menu__item--hidden") + + document.getElementById("search-results").innerHTML = ``; + } + }); +} \ No newline at end of file diff --git a/partials/navigation.hbs b/partials/navigation.hbs index 3654424..b1944a8 100644 --- a/partials/navigation.hbs +++ b/partials/navigation.hbs @@ -1,14 +1,22 @@