thefoldwithin-earth/public/app.js

167 lines
5.7 KiB
JavaScript
Raw Normal View History

2025-11-08 14:39:26 -06:00
const els = {
body: document.body,
2025-11-08 15:26:01 -06:00
menuBtn: document.getElementById("menuBtn"),
2025-11-08 15:48:53 -06:00
primaryNav: document.getElementById("primaryNav"),
2025-11-08 15:26:01 -06:00
sectionSelect: document.getElementById("sectionSelect"),
sortSelect: document.getElementById("sortSelect"),
searchBox: document.getElementById("searchBox"),
postList: document.getElementById("postList"),
viewer: document.getElementById("viewer"),
content: document.getElementById("content")
2025-11-08 14:55:16 -06:00
};
2025-11-08 15:35:37 -06:00
const staticPages = new Set(["about", "contact", "legal"]);
2025-11-08 15:48:53 -06:00
const sectionIcons = { // Optional: Add icons per section (e.g., 'essays': '✍️')
essays: '✍️',
fieldnotes: '📓',
pinned: '📌'
};
2025-11-08 15:35:37 -06:00
2025-11-08 15:26:01 -06:00
let indexData = null;
2025-11-08 15:35:37 -06:00
let sidebarOpen = false;
2025-11-08 14:39:26 -06:00
2025-11-08 15:35:37 -06:00
async function init() {
try {
indexData = await (await fetch("index.json")).json();
2025-11-08 15:48:53 -06:00
populateNav();
2025-11-08 15:35:37 -06:00
populateSections();
wireUI();
renderList();
handleHash();
window.addEventListener("hashchange", handleHash);
} catch (e) {
2025-11-08 15:48:53 -06:00
els.viewer.innerHTML = "<h1>Error Loading Site</h1><p>Failed to load index data. Please refresh or check connection.</p>";
2025-11-08 15:35:37 -06:00
}
2025-11-08 14:55:16 -06:00
}
2025-11-08 15:48:53 -06:00
function populateNav() {
els.primaryNav.innerHTML = '<a href="#/">Home</a>';
staticPages.forEach(p => {
els.primaryNav.innerHTML += `<a href="#/${p}/">${p.charAt(0).toUpperCase() + p.slice(1)}</a>`;
});
indexData.sections.forEach(s => {
els.primaryNav.innerHTML += `<a href="#/${s}/">${s.charAt(0).toUpperCase() + s.slice(1)}</a>`;
});
}
2025-11-08 15:35:37 -06:00
function populateSections() {
2025-11-08 15:26:01 -06:00
els.sectionSelect.innerHTML = "";
2025-11-08 15:35:37 -06:00
indexData.sections.forEach(s => {
2025-11-08 15:48:53 -06:00
const icon = sectionIcons[s] ? `${sectionIcons[s]} ` : '';
2025-11-08 15:26:01 -06:00
const opt = document.createElement("option");
2025-11-08 15:35:37 -06:00
opt.value = s;
2025-11-08 15:48:53 -06:00
opt.textContent = `${icon}${s}`;
2025-11-08 15:26:01 -06:00
els.sectionSelect.appendChild(opt);
2025-11-08 14:39:26 -06:00
});
}
2025-11-08 14:17:27 -06:00
2025-11-08 15:35:37 -06:00
function wireUI() {
els.menuBtn.addEventListener("click", () => {
sidebarOpen = !sidebarOpen;
document.body.classList.toggle("sidebar-open", sidebarOpen);
2025-11-08 14:39:26 -06:00
});
2025-11-08 15:35:37 -06:00
els.sectionSelect.addEventListener("change", renderList);
els.sortSelect.addEventListener("change", renderList);
els.searchBox.addEventListener("input", renderList);
els.content.addEventListener("click", () => {
if (window.matchMedia("(max-width:1024px)").matches && document.body.classList.contains("sidebar-open")) {
2025-11-08 15:26:01 -06:00
document.body.classList.remove("sidebar-open");
2025-11-08 15:35:37 -06:00
sidebarOpen = false;
2025-11-08 12:12:47 -06:00
}
2025-11-08 15:26:01 -06:00
});
2025-11-08 14:39:26 -06:00
}
2025-11-08 14:17:27 -06:00
2025-11-08 15:35:37 -06:00
function renderList() {
2025-11-08 15:26:01 -06:00
const section = els.sectionSelect.value;
const sort = els.sortSelect.value;
const query = els.searchBox.value.toLowerCase();
2025-11-08 12:59:27 -06:00
2025-11-08 15:35:37 -06:00
let posts = indexData.flat.filter(p => p.path.split('/')[0] === section);
if (query) posts = posts.filter(p => p.title.toLowerCase().includes(query));
posts.sort((a, b) => sort === "newest" ? b.mtime - a.mtime : a.mtime - b.mtime);
2025-11-08 14:55:16 -06:00
2025-11-08 15:26:01 -06:00
els.postList.innerHTML = "";
2025-11-08 15:35:37 -06:00
for (const p of posts) {
2025-11-08 15:26:01 -06:00
const li = document.createElement("li");
2025-11-08 15:35:37 -06:00
const pin = p.pinned ? "&#9733; " : "";
li.innerHTML = `<a href="#/${p.path}">${pin}${p.title}</a><br><small>${new Date(p.mtime).toISOString().split("T")[0]}</small>`;
2025-11-08 15:26:01 -06:00
els.postList.appendChild(li);
2025-11-08 14:17:27 -06:00
}
2025-11-08 15:26:01 -06:00
}
2025-11-08 13:06:29 -06:00
2025-11-08 15:35:37 -06:00
async function handleHash() {
2025-11-08 15:48:53 -06:00
els.viewer.innerHTML = "";
2025-11-08 15:35:37 -06:00
const rel = location.hash.replace(/^#\//, "");
2025-11-08 15:26:01 -06:00
if (!rel) return renderDefault();
2025-11-08 15:35:37 -06:00
if (rel.endsWith('/')) {
const section = rel.replace(/\/$/, '');
if (staticPages.has(section)) {
2025-11-08 15:48:53 -06:00
renderIframe(`${section}/index.html`);
2025-11-08 15:35:37 -06:00
} else if (indexData.sections.includes(section)) {
els.sectionSelect.value = section;
renderList();
const sectionPosts = indexData.flat.filter(p => p.path.split('/')[0] === section);
if (sectionPosts.length === 0) {
2025-11-08 15:48:53 -06:00
els.viewer.innerHTML = `<h1>${section.charAt(0).toUpperCase() + section.slice(1)}</h1><p>No content in this section yet. Check back soon!</p>`;
2025-11-08 15:35:37 -06:00
} else {
const latest = sectionPosts.sort((a, b) => b.mtime - a.mtime)[0];
2025-11-08 15:48:53 -06:00
location.hash = '#/' + latest.path;
2025-11-08 15:35:37 -06:00
}
} else {
2025-11-08 15:48:53 -06:00
els.viewer.innerHTML = '<h1>404: Section Not Found</h1><p>The requested section does not exist.</p>';
2025-11-08 15:35:37 -06:00
}
} else {
const file = indexData.flat.find(f => f.path === rel);
if (!file) {
2025-11-08 15:48:53 -06:00
els.viewer.innerHTML = '<h1>404: File Not Found</h1><p>The requested file could not be located.</p>';
2025-11-08 15:35:37 -06:00
return;
}
try {
2025-11-08 15:48:53 -06:00
if (file.ext === ".md") {
await renderMarkdown(file.path);
} else {
renderIframe(file.path);
}
2025-11-08 15:35:37 -06:00
} catch (e) {
2025-11-08 15:48:53 -06:00
els.viewer.innerHTML = '<h1>Error Loading Content</h1><p>Unable to load the file. It may be corrupted or inaccessible.</p>';
2025-11-08 15:35:37 -06:00
}
}
2025-11-08 14:39:26 -06:00
}
2025-11-08 15:35:37 -06:00
async function renderMarkdown(rel) {
2025-11-08 15:48:53 -06:00
const src = await fetch(rel).then(r => { if (!r.ok) throw new Error('Fetch failed'); return r.text(); });
2025-11-08 15:26:01 -06:00
const html = marked.parse(src);
els.viewer.innerHTML = `<article>${html}</article>`;
2025-11-08 14:39:26 -06:00
}
2025-11-08 15:48:53 -06:00
function renderIframe(rel) {
2025-11-08 14:39:26 -06:00
const iframe = document.createElement("iframe");
2025-11-08 15:35:37 -06:00
iframe.setAttribute("sandbox", "allow-same-origin allow-scripts allow-forms");
2025-11-08 14:39:26 -06:00
iframe.loading = "eager";
2025-11-08 15:26:01 -06:00
iframe.src = "/" + rel;
2025-11-08 14:39:26 -06:00
els.viewer.appendChild(iframe);
2025-11-08 15:35:37 -06:00
iframe.addEventListener("load", () => {
2025-11-08 15:48:53 -06:00
if (rel.endsWith('.pdf')) return;
2025-11-08 15:35:37 -06:00
try {
2025-11-08 15:06:28 -06:00
const d = iframe.contentDocument || iframe.contentWindow.document;
const s = d.createElement("style");
s.textContent = `
2025-11-08 15:26:01 -06:00
html,body{margin:0;padding:0;background:transparent;color:#e6e3d7;font:16px/1.6 Inter,ui-sans-serif;}
main,article,section{max-width:720px;margin:auto;padding:2rem;}
2025-11-08 15:06:28 -06:00
`;
d.head.appendChild(s);
2025-11-08 15:35:37 -06:00
} catch {}
2025-11-08 14:39:26 -06:00
});
}
2025-11-08 15:35:37 -06:00
function renderDefault() {
const latest = [...indexData.flat].sort((a, b) => b.mtime - a.mtime)[0];
2025-11-08 15:48:53 -06:00
if (latest) {
location.hash = "#/" + latest.path;
} else {
els.viewer.innerHTML = '<h1>Welcome</h1><p>No content yet. Add files to sections and redeploy!</p>';
}
2025-11-08 15:26:01 -06:00
}
init();