From ff11653854935a55e1a8b8bd51922da36513ec8f Mon Sep 17 00:00:00 2001 From: tdro Date: Fri, 22 Sep 2023 10:54:18 -0400 Subject: static/js/fixedsearch: Minor adjustments/refactor Set fetch callback and check active element --- static/js/fixedsearch.ts | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'static/js') 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 = '
  • '.concat( + items = '
  • '.concat( escape(term), " ", ).concat(separator, " No Results Found
  • "); -- cgit v1.2.3