AvalonDocumentationEngine/assets/system/searchNodes.js

73 lines
3.1 KiB
JavaScript
Raw Normal View History

2023-01-05 04:18:04 +00:00
var page = document.getElementById("page");
var search = document.getElementById("searchbar");
var overinput = document.querySelector(".overinput");
var clearinput = document.querySelector(".clearinput");
2023-01-05 20:58:03 +00:00
var bodyele = document.getElementById("body");
2023-01-05 04:18:04 +00:00
var searchNodes = [...document.getElementById("searchNodes").children];
var searchNodesCont = document.getElementById("searchNodes");
var sbheader = document.getElementById("sbheader");
function onsearch () {
2023-01-05 20:58:03 +00:00
search.value = search.value.replace(/^Redirects to /i, ">> " );
search.value = search.value.replace(/^Redirects /i, ">> ");
search.value = search.value.replace(/^\>\> to /i, ">> " );
2023-01-05 04:18:04 +00:00
if (sessionStorage.getItem("searchvalue") === "()" && search.value === ")")
search.value = "";
if (sessionStorage.getItem("searchvalue") === "()" && search.value === "(")
search.value = "";
if (search.value === "(" || search.value.startsWith("Tagged ")) {
search.value = "()";
search.setSelectionRange(1, 1);
search.focus();
}
if (search.value != "") {
searchNodesCont.classList.add("isSearching");
sbheader.classList.add("isSearching");
} else {
searchNodesCont.classList.remove("isSearching");
sbheader.classList.remove("isSearching");
searchNodes.forEach(child => child.style.display = "")
}
2023-01-05 20:58:03 +00:00
var value = search.value.replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/\\/g, "");
if (search.value.startsWith(">>")) {
2023-01-05 04:18:04 +00:00
overinput.style.display = "inline-block";
2023-01-05 20:58:03 +00:00
overinput.innerHTML = "Redirects " + (search.value.length > 3 && search.value !== ">> " ? search.value.replace(/^\>\>/, "to ") : "");
2023-01-05 04:18:04 +00:00
searchNodes.forEach(child => {
child.style.display = "none";
var name = child.querySelector(".linkname");
if (name.innerHTML.toLowerCase().includes(value.toLowerCase()))
child.style.display = "";
});
} else if (/^\(/.test(search.value) && /\)$/.test(search.value)) {
overinput.style.display = "inline-block";
overinput.innerHTML = "Tagged " + (search.value !== "()" ? search.value.replace(/^\(/, "with ").replace(/\)$/, "") : "");
var valstripped = search.value.replace(/^\(/, "").replace(/\)$/, "").toLowerCase();
searchNodes.forEach(child => {
child.style.display = "none";
var tags = child.querySelectorAll(".tagLabel");
tags.forEach(tag => {
if (tag.getAttribute("title").toLowerCase().includes(valstripped))
child.style.display = "";
else if (tag.innerHTML.toLowerCase().includes(valstripped))
child.style.display = "";
})
});
} else {
overinput.style.display = "none";
searchNodes.forEach(child => {
child.style.display = "none";
var name = child.querySelector(".linkname");
if (name.innerHTML.toLowerCase().includes(value.toLowerCase()))
child.style.display = "";
});
}
sessionStorage.setItem("searchvalue", search.value);
};
search.value = sessionStorage.getItem("searchvalue");
2023-01-05 20:58:03 +00:00
search.addEventListener('input', onsearch);
overinput.addEventListener("click", () => {search.focus();});
clearinput.addEventListener("click", () => {search.value = "";onsearch();});
onsearch();