Add callback to reuse fetched nav items

Fixes #15.
This commit is contained in:
Florine W. Dekker 2021-05-03 19:20:54 +02:00
parent 70bbc76aa2
commit db7ab52818
Signed by: FWDekker
GPG Key ID: 78B3EAF58145AF25
2 changed files with 6 additions and 3 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@fwdekker/template",
"version": "2.3.1",
"version": "2.4.0",
"description": "The base template for pages on fwdekker.com.",
"author": "Felix W. Dekker",
"license": "MIT",

View File

@ -47,9 +47,10 @@ const doAfterLoad = function(fun) {
* Fetches entries asynchronously from the website's API.
*
* @param [highlightPath] {String} the path to highlight together with its parents
* @param [cb] {Function} the callback to execute on the fetched entries, to prevent the need to re-fetch elsewhere
* @returns {HTMLElement} a base navigation element that will eventually be filled with contents
*/
const nav = function(highlightPath = "") {
const nav = function(highlightPath = "", cb = undefined) {
const base = stringToHtml(
`<ul><li><a href="https://fwdekker.com/">` +
`<div class="logo"><img class="logo" src="https://fwdekker.com/favicon.png" alt="FWDekker" /></div>` +
@ -61,7 +62,9 @@ const nav = function(highlightPath = "") {
fetch("https://fwdekker.com/api/nav/")
.then(it => it.json())
.then(json => {
json.entries.forEach(entry => base.appendChild(stringToHtml(unpackEntry(entry, "/", highlightPath), "li")))
if (cb !== undefined) cb(json);
json.entries.forEach(entry => base.appendChild(stringToHtml(unpackEntry(entry, "/", highlightPath), "li")));
})
.catch(e => {
console.error("Failed to fetch navigation elements", e);