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 --- assets/js/index.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'assets/js') diff --git a/assets/js/index.js b/assets/js/index.js index 9895067..72e6736 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -385,7 +385,7 @@ "key": keyname })); } - submit.addEventListener("click", function(event) { + submit.addEventListener("click", function() { first(dropdown).focus(); press("ArrowDown"); }); @@ -444,7 +444,7 @@ scrolls++; }); 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"); } @@ -461,19 +461,21 @@ 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(); } function loadScript(url) { return new Promise(function(resolve, reject) { @@ -493,7 +495,9 @@ if (firstRun) { 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" @@ -522,7 +526,7 @@ if (results.length === 0 && term.length >= 0) { let separator = "—"; if (term.length === 0) separator = ""; - items = '
  • '.concat(escape(term), " ").concat(separator, " No Results Found
  • "); + items = '
  • '.concat(escape(term), " ").concat(separator, " No Results Found
  • "); dropdown.removeAttribute("hidden"); container.setAttribute("data-focus", ""); } else { -- cgit v1.2.3