From 219ffd9dd76638810271cc0733ff7732d873eac8 Mon Sep 17 00:00:00 2001 From: tdro Date: Mon, 5 Feb 2024 02:09:53 -0500 Subject: static/js/fixedsearch: Warm this up a bit Not a database --- assets/js/index.js | 54 +++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'assets') diff --git a/assets/js/index.js b/assets/js/index.js index 9f17871..e80d794 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -401,7 +401,9 @@ const dropdown = document.getElementById("search-results"); const container = document.getElementById("search-frame"); function first(element) { - return element.firstChild.nextElementSibling.firstChild.nextElementSibling; + if (element.firstChild.nextElementSibling) { + return element.firstChild.nextElementSibling.firstChild.nextElementSibling; + } } function last(element) { return element.lastElementChild.firstChild.nextElementSibling; @@ -499,24 +501,6 @@ container.removeAttribute("data-focus"); } }); - let data = {}; - function isEmpty(obj) { - return Object.keys(obj).length === 0; - } - function appendItemsTo(local, remote) { - const paginated = Object.keys(remote).includes("next_url"); - if (isEmpty(local)) { - local = remote; - } else { - local.items = local.items.concat(remote.items); - } - if (paginated) { - fetch(remote.next_url, function(request) { - appendItemsTo(local, JSON.parse(request.responseText)); - }); - } - data = local; - } function fetch(url, callback) { const http = new XMLHttpRequest(); http.onreadystatechange = function() { @@ -538,20 +522,38 @@ script.src = url; }); } + let data = {}; let boot = true; + const options = { + key: [ + "title" + ] + }; + function isEmpty(obj) { + return Object.keys(obj).length === 0; + } + function appendItemsTo(local, remote) { + const paginated = Object.keys(remote).includes("next_url"); + if (isEmpty(local)) { + local = remote; + } else { + local.items = local.items.concat(remote.items); + } + if (paginated) { + fetch(remote.next_url, function(request) { + appendItemsTo(local, JSON.parse(request.responseText)); + }); + } else search(query.value, data.items, options); + data = local; + } function initialize() { if (boot) { script(window.location.origin + "/js/fuzzysort.js").then(function() { fetch("/index.json", function(request) { appendItemsTo({}, JSON.parse(request.responseText)); - search(query.value, data.items, options); + search("", data.items, options); boot = false; }); - const options = { - key: [ - "title" - ] - }; [ "keyup", "focusin" @@ -566,8 +568,6 @@ }); }).catch(function(error) { console.error("ERROR: Failed to load fuzzy search", error); - boot = true; - initialize(); }); } } -- cgit v1.2.3