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 --- static/js/fixedsearch.ts | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) (limited to 'static') diff --git a/static/js/fixedsearch.ts b/static/js/fixedsearch.ts index 6d91dfe..537ddf2 100644 --- a/static/js/fixedsearch.ts +++ b/static/js/fixedsearch.ts @@ -13,8 +13,10 @@ const container = document.getElementById("search-frame"); // targets parent function first(element) { + if (element.firstChild.nextElementSibling) { return element.firstChild.nextElementSibling.firstChild .nextElementSibling; + } } function last(element) { @@ -138,27 +140,6 @@ } }); - 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 () { @@ -187,8 +168,28 @@ }); } + 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") @@ -196,11 +197,10 @@ 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"].forEach(function (event) { query.addEventListener(event, function () { @@ -211,7 +211,6 @@ }).catch(function (error) { console.error("ERROR: Failed to load fuzzy search", error); - boot = true; initialize(); }); } } -- cgit v1.2.3