diff options
author | tdro <tdro@noreply.example.com> | 2022-11-02 11:08:22 -0400 |
---|---|---|
committer | tdro <tdro@noreply.example.com> | 2022-11-02 11:08:22 -0400 |
commit | bf1926903403a77767efb8bbbcde6b32c8929179 (patch) | |
tree | 73e9d52d9784142f598d7d85b4f736550819ee71 /assets | |
parent | bec107a46771da25778a92d7e881f157dc91c035 (diff) | |
download | canory-bf1926903403a77767efb8bbbcde6b32c8929179.tar.gz canory-bf1926903403a77767efb8bbbcde6b32c8929179.tar.bz2 canory-bf1926903403a77767efb8bbbcde6b32c8929179.zip |
themes/default/layouts/_default/index.json: Paginate
Load feed in chunks on the client side.
Diffstat (limited to 'assets')
-rw-r--r-- | assets/js/index.js | 45 |
1 files changed, 30 insertions, 15 deletions
diff --git a/assets/js/index.js b/assets/js/index.js index 5a4fb39..7deaee1 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -392,11 +392,27 @@ container.removeAttribute("data-focus"); } }); - function fetchJson(url, callback) { + let data1 = {}; + 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) { + fetchJson(remote.next_url, local); + } + data1 = local; + } + function fetchJson(url, store) { const httpRequest = new XMLHttpRequest(); httpRequest.onreadystatechange = function() { if (httpRequest.readyState === 4 && httpRequest.status === 200) { - if (callback) callback(JSON.parse(httpRequest.responseText)); + appendItemsTo(store, JSON.parse(httpRequest.responseText)); } }; httpRequest.open("GET", url); @@ -420,20 +436,19 @@ if (firstRun) { loadScript(window.location.origin + "/js/fuzzysort.js").then(()=>{ firstRun = false; - fetchJson("/index.json", function(data) { - const options = { - key: [ - "title" - ] - }; - query.addEventListener("keyup", function() { - search(query.value, data.items, options); - }); - query.addEventListener("focusin", function() { - search(query.value, data.items, options); - }); - search(query.value, data.items, options); + fetchJson("/index.json", {}); + const options = { + key: [ + "title" + ] + }; + query.addEventListener("keyup", function() { + search(query.value, data1.items, options); + }); + query.addEventListener("focusin", function() { + search(query.value, data1.items, options); }); + search(query.value, data1.items, options); }).catch((error)=>{ console.log("Error failed to load fuzzy sort: " + error); }); |