Search now shows nothing if nothing was found
This commit is contained in:
parent
2defe8a4bd
commit
1759b09aec
30
static/dist/js/searchsuggestions.js
vendored
30
static/dist/js/searchsuggestions.js
vendored
|
|
@ -1,38 +1,38 @@
|
||||||
const searchinput = document.querySelector("#search");
|
const searchinput = document.querySelector("#search");
|
||||||
const searchsuggestions = document.querySelector("#searchdropdown");
|
const searchsuggestions = document.querySelector("#searchdropdown");
|
||||||
|
const li = (c) => document.createElement("li").appendChild(c);
|
||||||
searchinput.addEventListener("input", (e) => {
|
searchinput.addEventListener("input", (e) => {
|
||||||
q(e.target.value);
|
q(e.target.value).then((d) => {
|
||||||
|
if (d.length < 1) searchsuggestions.replaceChildren(nothing());
|
||||||
|
else searchsuggestions.replaceChildren(...suggestions(d));
|
||||||
|
})
|
||||||
});
|
});
|
||||||
async function q(s) {
|
async function q(s) {
|
||||||
if (s.length < 1) {clearSuggestions(searchsuggestions)}
|
if (s.length > 0) {
|
||||||
else {
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/api/search/?q=" + s);
|
const response = await fetch("/api/search/?q=" + s);
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
setSuggestions(searchsuggestions, result);
|
return result;
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
console.log("Error: " + error);
|
console.log("Error: " + error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
function clearSuggestions(o) {
|
function nothing() {
|
||||||
const span = document.createElement("span");
|
const span = document.createElement("span");
|
||||||
span.setAttribute("class", "dropdown-item-text");
|
span.setAttribute("class", "dropdown-item-text");
|
||||||
span.insertAdjacentText("afterbegin", "Nothing");
|
span.insertAdjacentText("afterbegin", "Nothing");
|
||||||
const li = document.createElement("li");
|
return li(span);
|
||||||
li.appendChild(span);
|
|
||||||
o.replaceChildren(li);
|
|
||||||
}
|
}
|
||||||
function setSuggestions(o, s) {
|
function suggestions(d) {
|
||||||
const newSuggestions = [];
|
const items = [];
|
||||||
for (const vid of s) {
|
for (const vid of d) {
|
||||||
const a = document.createElement("a");
|
const a = document.createElement("a");
|
||||||
a.setAttribute("class", "dropdown-item")
|
a.setAttribute("class", "dropdown-item")
|
||||||
a.setAttribute("href", "/view/" + vid.id);
|
a.setAttribute("href", "/view/" + vid.id);
|
||||||
a.insertAdjacentText("afterbegin", vid.name + " " + vid.id);
|
a.insertAdjacentText("afterbegin", vid.name + " " + vid.id);
|
||||||
const li = document.createElement("li");
|
items.push(li(a));
|
||||||
li.appendChild(a);
|
|
||||||
newSuggestions.push(li);
|
|
||||||
}
|
}
|
||||||
o.replaceChildren(...newSuggestions);
|
return items;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user