diff options
author | tdro <tdro@noreply.example.com> | 2023-09-22 10:54:18 -0400 |
---|---|---|
committer | tdro <tdro@noreply.example.com> | 2023-09-22 10:54:18 -0400 |
commit | ff11653854935a55e1a8b8bd51922da36513ec8f (patch) | |
tree | c3eb5ce174ccd034f95d7bd75ea18d451c62abb5 /static/js | |
parent | e4838ba04302f62ce0a497657eab154c1248d16d (diff) | |
download | canory-ff11653854935a55e1a8b8bd51922da36513ec8f.tar.gz canory-ff11653854935a55e1a8b8bd51922da36513ec8f.tar.bz2 canory-ff11653854935a55e1a8b8bd51922da36513ec8f.zip |
static/js/fixedsearch: Minor adjustments/refactor
Set fetch callback and check active element
Diffstat (limited to 'static/js')
-rw-r--r-- | static/js/fixedsearch.ts | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/static/js/fixedsearch.ts b/static/js/fixedsearch.ts index b42a620..fb53b6b 100644 --- a/static/js/fixedsearch.ts +++ b/static/js/fixedsearch.ts @@ -35,7 +35,7 @@ document.dispatchEvent(new KeyboardEvent("keydown", { "key": keyname })); } - submit.addEventListener("click", function (event) { + submit.addEventListener("click", function () { first(dropdown).focus(); press("ArrowDown"); }); @@ -110,7 +110,7 @@ }); document.addEventListener("click", function (event) { - if (!form.contains(event.target)) { + if (!form.contains(event.target) && !(document.activeElement === query)) { dropdown.setAttribute("hidden", ""); container.removeAttribute("data-focus"); } @@ -130,20 +130,22 @@ local.items = local.items.concat(remote.items); } if (paginated) { - fetchJson(remote.next_url, local); + fetchJson(remote.next_url, function (request) { + appendItemsTo(local, JSON.parse(request.responseText)); + }); } data = local; } - function fetchJson(url, store) { - const httpRequest = new XMLHttpRequest(); - httpRequest.onreadystatechange = function () { - if (httpRequest.readyState === 4 && httpRequest.status === 200) { - appendItemsTo(store, JSON.parse(httpRequest.responseText)); + function fetchJson(url, callback) { + const http = new XMLHttpRequest(); + http.onreadystatechange = function () { + if (http.readyState === 4 && http.status === 200 && callback) { + callback(http); } }; - httpRequest.open("GET", url); - httpRequest.send(); + http.open("GET", url); + http.send(); } /* Load script based on https://stackoverflow.com/a/55451823 */ @@ -172,7 +174,10 @@ loadScript(window.location.origin + "/js/fuzzysort.js") .then(function () { firstRun = false; - fetchJson("/index.json", {}); + + fetchJson("/index.json", function (request) { + appendItemsTo({}, JSON.parse(request.responseText)); + }); const options = { key: ["title"] }; @@ -204,7 +209,7 @@ if (results.length === 0 && term.length >= 0) { let separator = "—"; if (term.length === 0) separator = ""; - items = '<li><a href="javascript: void(0)" tabindex="0">'.concat( + items = '<li><a tabindex="0">'.concat( escape(term), " ", ).concat(separator, " No Results Found</a></li>"); |