62 lines
2.0 KiB
JavaScript
62 lines
2.0 KiB
JavaScript
// Search
|
|
|
|
function search(req, dataset, key) {
|
|
const re = new RegExp(req, "i");
|
|
let res = [];
|
|
for (let data of dataset) {
|
|
if (re.test(key(data))) {
|
|
res.push(data)
|
|
}
|
|
}
|
|
return res;
|
|
};
|
|
|
|
window.addEventListener("load", async function (ev) {
|
|
let data_req = await fetch("/index.json")
|
|
let data = await data_req.json()
|
|
let key = (data) => (data.path + " " + data.title + " " + data.path + " " + data.tags);
|
|
document.getElementById("searchbox").addEventListener("input",
|
|
function (ev /*: InputEvent*/) {
|
|
let res = document.getElementById('results');
|
|
if (ev.target.value == "") {
|
|
res.innerHTML = "";
|
|
} else {
|
|
const vals = search(ev.target.value, data, key);
|
|
res.innerHTML =
|
|
'<ul class="sresult">' +
|
|
vals.map((it) =>
|
|
`<li class="sresult"><a href="/${it.path}.html">${it.title}</a></li>`
|
|
).reduce((a, b) => a + b) + '</ul>';
|
|
}
|
|
}
|
|
);
|
|
});
|
|
|
|
function initDictionary() {
|
|
window.addEventListener("load", function (_) {
|
|
document.getElementById("searchdict").addEventListener("input", (e) => {
|
|
let dictbox = document.getElementById("dictbox");
|
|
if (!dictbox) return;
|
|
let entries = dictbox.childNodes;
|
|
let req = e.target.value;
|
|
for (let entry of entries) {
|
|
if (req == "") {
|
|
entry.style.setProperty("display", "block");
|
|
} else {
|
|
console.log(`looking for ${req}`);
|
|
console.log(entry);
|
|
let re = new RegExp(req, "i");
|
|
if (re.test(entry.dataset["dictL"]) ||
|
|
re.test(entry.dataset["dictT"]) ||
|
|
re.test(entry.dataset["dictD"])) {
|
|
entry.style.setProperty("display", "block");
|
|
} else {
|
|
entry.style.setProperty("display", "none");
|
|
}
|
|
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|