From 21e8950146f69bf5e263b97cadd457a455453562 Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 13 Sep 2021 23:55:02 -0400 Subject: [PATCH] Remove preset pages; force users to look pages up; fix bugs --- index.html | 2 +- scripts/{fuzzynav.js => fuzzy.js} | 49 +++++++++++++++++++------------ 2 files changed, 32 insertions(+), 19 deletions(-) rename scripts/{fuzzynav.js => fuzzy.js} (56%) diff --git a/index.html b/index.html index c81e23f..b71731f 100644 --- a/index.html +++ b/index.html @@ -81,7 +81,7 @@ Hosted on GitHub © 2021 Paul W. - + \ No newline at end of file diff --git a/scripts/fuzzynav.js b/scripts/fuzzy.js similarity index 56% rename from scripts/fuzzynav.js rename to scripts/fuzzy.js index 30fa696..ac81762 100644 --- a/scripts/fuzzynav.js +++ b/scripts/fuzzy.js @@ -1,40 +1,53 @@ (function () { var client = new XMLHttpRequest(); client.open("GET", "/pages.json"); - client.onreadystatechange = function () { - initFuzzy(client.responseText); + client.onreadystatechange = () => { + if (client.readyState === 4) + fuzzyInit(client.responseText); } client.send(); })(); -function initFuzzy(text) { - if (text == "") +function fuzzyInit(pagesFileName) { + if (pagesFileName == "") return; var pages; try { - pages = JSON.parse(text); + pages = JSON.parse(pagesFileName); } catch (e) { + console.error(e); + document.body.innerHTML = e.message; return; } pages.sort(); - let output = ""; - - for (const [name, rlink] of pages) { - output += `
${name}
`; - } - - document.querySelector("#results").innerHTML = output; - document.querySelector("#search") .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 = []; for (const [i, [title, page]] of pages.entries()) { - ret = hotFuzz(title, document.querySelector("#search").value); - if (ret == false) + ret = fuzzySearch(title, document.querySelector("#search").value); + if (ret === null) continue; 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, ""); search = search.toLowerCase(); let tokens = list.split(''); @@ -67,11 +80,11 @@ function hotFuzz(list, input) { tokens[i] = `${ch}`; pc++; if (search.length < pc) - return false; + return null; } } if (search.length != pc) - return false; + return null; return {first: tokens.join(''), second: (score / search.length)}; } \ No newline at end of file