mirror of
https://github.com/NotAShelf/nvf.git
synced 2025-09-06 10:21:31 +00:00
docs: add search widget to options page
Some checks are pending
Check for typos in the source tree / check-typos (push) Waiting to run
Some checks are pending
Check for typos in the source tree / check-typos (push) Waiting to run
This commit is contained in:
parent
fca55e1db1
commit
37dc96575d
4 changed files with 62 additions and 2 deletions
40
docs/static/script/search.js
vendored
Normal file
40
docs/static/script/search.js
vendored
Normal file
|
@ -0,0 +1,40 @@
|
|||
document.addEventListener("DOMContentLoaded", () => {
|
||||
if (!window.location.pathname.endsWith("options.html")) return;
|
||||
|
||||
const searchBar = document.createDocumentFragment();
|
||||
const searchDiv = document.createElement("div");
|
||||
searchDiv.id = "search-bar";
|
||||
searchDiv.innerHTML = `
|
||||
<input type="text" id="search-input" placeholder="Search options by ID..." />
|
||||
<div id="search-results"></div>
|
||||
`;
|
||||
searchBar.appendChild(searchDiv);
|
||||
document.body.prepend(searchDiv);
|
||||
|
||||
const dtElements = Array.from(document.querySelectorAll("dt"));
|
||||
const ddElements = Array.from(document.querySelectorAll("dd"));
|
||||
const dtOptionIds = dtElements.map(
|
||||
(dt) => dt.querySelector("a")?.id.toLowerCase() || "",
|
||||
);
|
||||
|
||||
if (dtElements.length === 0 || ddElements.length === 0) {
|
||||
console.warn("Something went wrong, page may be loaded incorrectly.");
|
||||
return;
|
||||
}
|
||||
|
||||
let debounceTimeout;
|
||||
document.getElementById("search-input").addEventListener("input", (event) => {
|
||||
clearTimeout(debounceTimeout);
|
||||
debounceTimeout = setTimeout(() => {
|
||||
const query = event.target.value.toLowerCase();
|
||||
dtElements.forEach((dt, index) => {
|
||||
const isMatch = dtOptionIds[index].includes(query);
|
||||
|
||||
if (dt.classList.contains("hidden") !== !isMatch) {
|
||||
dt.classList.toggle("hidden", !isMatch);
|
||||
ddElements[index]?.classList.toggle("hidden", !isMatch);
|
||||
}
|
||||
});
|
||||
}, 200);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue