const menu = document.querySelector("#menu"); const caption = document.querySelector("#caption"); const titleEl = document.querySelector("#toy-title"); const blurbEl = document.querySelector("#toy-blurb"); const sourceBtn = document.querySelector("#source-btn"); const viewport = document.querySelector("#viewport"); const stage = document.querySelector("#stage"); const splash = document.querySelector("#splash"); const frame = document.querySelector("#frame"); const sourceEl = document.querySelector("#source"); let toys = []; let current = null; let sourceOpen = false; let sourceText = null; let sourceGen = 0; function splashCopy(missing) { if (missing) return "No such toy."; if (!toys.length) return "No toys yet."; return "Pick a toy."; } function markSelected(id) { for (const link of menu.querySelectorAll("a")) { const on = link.dataset.id === id; if (on) link.setAttribute("aria-current", "page"); else link.removeAttribute("aria-current"); } } function showSplash(missing) { current = null; sourceOpen = false; sourceText = null; sourceGen++; splash.hidden = false; splash.textContent = splashCopy(missing); frame.hidden = true; frame.removeAttribute("src"); sourceEl.hidden = true; sourceEl.textContent = ""; viewport.classList.add("is-splash"); stage.classList.remove("has-source"); titleEl.textContent = ""; blurbEl.textContent = ""; sourceBtn.hidden = true; sourceBtn.setAttribute("aria-pressed", "false"); markSelected(null); } function renderMenu() { menu.replaceChildren(); for (const toy of toys) { const link = document.createElement("a"); link.href = "#" + encodeURIComponent(toy.id); link.dataset.id = toy.id; link.textContent = toy.title; if (toy.blurb) link.title = toy.blurb; menu.append(link); } } function openToy(toy) { current = toy; sourceOpen = false; sourceText = null; sourceGen++; splash.hidden = true; frame.hidden = false; frame.title = toy.title; frame.src = "toys/" + encodeURIComponent(toy.id) + "/index.html"; sourceEl.hidden = true; sourceEl.textContent = ""; viewport.classList.remove("is-splash"); stage.classList.remove("has-source"); titleEl.textContent = toy.title; blurbEl.textContent = toy.blurb || ""; sourceBtn.hidden = !toy.source; sourceBtn.setAttribute("aria-pressed", "false"); markSelected(toy.id); } function selectFromHash() { const id = decodeURIComponent(location.hash.replace(/^#/, "")); if (!id) { showSplash(false); return; } const toy = toys.find((item) => item.id === id); if (!toy) { showSplash(true); return; } openToy(toy); } sourceBtn.addEventListener("click", async () => { if (!current || !current.source) return; sourceOpen = !sourceOpen; sourceBtn.setAttribute("aria-pressed", String(sourceOpen)); if (!sourceOpen) { sourceEl.hidden = true; stage.classList.remove("has-source"); return; } const gen = sourceGen; const toy = current; stage.classList.add("has-source"); sourceEl.hidden = false; if (sourceText == null) { sourceEl.textContent = "Loading…"; const url = "toys/" + encodeURIComponent(toy.id) + "/" + toy.source; let text; try { const res = await fetch(url); text = res.ok ? await res.text() : "Source missing."; } catch { text = "Source missing."; } if (gen !== sourceGen) return; sourceText = text; } if (!sourceOpen) return; sourceEl.textContent = sourceText; }); window.addEventListener("hashchange", selectFromHash); fetch("toys.json") .then((res) => { if (!res.ok) throw new Error("toys.json"); return res.json(); }) .then((list) => { toys = Array.isArray(list) ? list : []; renderMenu(); selectFromHash(); }) .catch(() => { splash.hidden = false; splash.textContent = "Could not load the toy list."; });