Remove preset pages; force users to look pages up; fix bugs
This commit is contained in:
parent
4965af478a
commit
21e8950146
@ -81,7 +81,7 @@
|
|||||||
<a href="https://github.com/lambdapaul/www">Hosted on GitHub</a>
|
<a href="https://github.com/lambdapaul/www">Hosted on GitHub</a>
|
||||||
© 2021 Paul W.
|
© 2021 Paul W.
|
||||||
</footer>
|
</footer>
|
||||||
<script src="/scripts/fuzzynav.js"></script>
|
<script src="/scripts/fuzzy.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
@ -1,40 +1,53 @@
|
|||||||
(function () {
|
(function () {
|
||||||
var client = new XMLHttpRequest();
|
var client = new XMLHttpRequest();
|
||||||
client.open("GET", "/pages.json");
|
client.open("GET", "/pages.json");
|
||||||
client.onreadystatechange = function () {
|
client.onreadystatechange = () => {
|
||||||
initFuzzy(client.responseText);
|
if (client.readyState === 4)
|
||||||
|
fuzzyInit(client.responseText);
|
||||||
}
|
}
|
||||||
client.send();
|
client.send();
|
||||||
})();
|
})();
|
||||||
|
|
||||||
function initFuzzy(text) {
|
function fuzzyInit(pagesFileName) {
|
||||||
if (text == "")
|
if (pagesFileName == "")
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var pages;
|
var pages;
|
||||||
try {
|
try {
|
||||||
pages = JSON.parse(text);
|
pages = JSON.parse(pagesFileName);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
document.body.innerHTML = e.message;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pages.sort();
|
pages.sort();
|
||||||
|
|
||||||
let output = "";
|
|
||||||
|
|
||||||
for (const [name, rlink] of pages) {
|
|
||||||
output += `<a class="hyperlink" href="${rlink}"><div class="name">${name}</div><div class="link">${rlink}</div></a>`;
|
|
||||||
}
|
|
||||||
|
|
||||||
document.querySelector("#results").innerHTML = output;
|
|
||||||
|
|
||||||
document.querySelector("#search")
|
document.querySelector("#search")
|
||||||
.addEventListener("keyup", (e) => {
|
.addEventListener("keyup", (e) => {
|
||||||
|
switch (e.code) {
|
||||||
|
case "Enter":
|
||||||
|
if (document.querySelector("#results").childNodes[0].href === undefined)
|
||||||
|
return;
|
||||||
|
window.location = document.querySelector("#results").childNodes[0].href;
|
||||||
|
break;
|
||||||
|
case "ArrowDown":
|
||||||
|
console.log("S");
|
||||||
|
break;
|
||||||
|
case "ArrowUp":
|
||||||
|
console.log("W");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (document.querySelector("#search").value === ""){
|
||||||
|
document.querySelector("#results").innerHTML = "";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let results = [];
|
let results = [];
|
||||||
for (const [i, [title, page]] of pages.entries()) {
|
for (const [i, [title, page]] of pages.entries()) {
|
||||||
ret = hotFuzz(title, document.querySelector("#search").value);
|
ret = fuzzySearch(title, document.querySelector("#search").value);
|
||||||
if (ret == false)
|
if (ret === null)
|
||||||
continue;
|
continue;
|
||||||
results.push([ret, page]);
|
results.push([ret, page]);
|
||||||
}
|
}
|
||||||
@ -54,7 +67,7 @@ function initFuzzy(text) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function hotFuzz(list, input) {
|
function fuzzySearch(list, input) {
|
||||||
let search = input.replace(/\s/g, "");
|
let search = input.replace(/\s/g, "");
|
||||||
search = search.toLowerCase();
|
search = search.toLowerCase();
|
||||||
let tokens = list.split('');
|
let tokens = list.split('');
|
||||||
@ -67,11 +80,11 @@ function hotFuzz(list, input) {
|
|||||||
tokens[i] = `<span class="highlight">${ch}</span>`;
|
tokens[i] = `<span class="highlight">${ch}</span>`;
|
||||||
pc++;
|
pc++;
|
||||||
if (search.length < pc)
|
if (search.length < pc)
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (search.length != pc)
|
if (search.length != pc)
|
||||||
return false;
|
return null;
|
||||||
|
|
||||||
return {first: tokens.join(''), second: (score / search.length)};
|
return {first: tokens.join(''), second: (score / search.length)};
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user