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 /assets/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 'assets/js')
-rw-r--r-- | assets/js/index.js | 28 |
1 files changed, 16 insertions, 12 deletions
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 = '<li><a href="javascript: void(0)" tabindex="0">'.concat(escape(term), " ").concat(separator, " No Results Found</a></li>"); + items = '<li><a tabindex="0">'.concat(escape(term), " ").concat(separator, " No Results Found</a></li>"); dropdown.removeAttribute("hidden"); container.setAttribute("data-focus", ""); } else { |