39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
const searchinput = document.querySelector("#search");
|
|
const searchsuggestions = document.querySelector("#searchdropdown");
|
|
searchinput.addEventListener("input", (e) => {
|
|
q(e.target.value);
|
|
});
|
|
async function q(s) {
|
|
if (s.length < 1) {clearSuggestions(searchsuggestions)}
|
|
else {
|
|
try {
|
|
const response = await fetch("/api/search/?q=" + s);
|
|
const result = await response.json();
|
|
setSuggestions(searchsuggestions, result);
|
|
} catch(error) {
|
|
console.log("Error: " + error);
|
|
}
|
|
}
|
|
}
|
|
function clearSuggestions(o) {
|
|
const span = document.createElement("span");
|
|
span.setAttribute("class", "dropdown-item-text");
|
|
span.insertAdjacentText("afterbegin", "Nothing");
|
|
const li = document.createElement("li");
|
|
li.appendChild(span);
|
|
o.replaceChildren(li);
|
|
}
|
|
function setSuggestions(o, s) {
|
|
const newSuggestions = [];
|
|
for (const vid of s) {
|
|
const a = document.createElement("a");
|
|
a.setAttribute("class", "dropdown-item")
|
|
a.setAttribute("href", "/view/" + vid.id);
|
|
a.insertAdjacentText("afterbegin", vid.name + " " + vid.id);
|
|
const li = document.createElement("li");
|
|
li.appendChild(a);
|
|
newSuggestions.push(li);
|
|
}
|
|
o.replaceChildren(...newSuggestions);
|
|
}
|