thefoldwithin-earth/public/app.js

193 lines
6.4 KiB
JavaScript
Raw Normal View History

2025-11-08 14:39:26 -06:00
const els = {
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"),
2025-11-08 16:09:05 -06:00
tagSelect: document.getElementById("tagSelect"),
2025-11-08 15:26:01 -06:00
sortSelect: document.getElementById("sortSelect"),
2025-11-08 16:09:05 -06:00
searchMode: document.getElementById("searchMode"),
2025-11-08 15:26:01 -06:00
searchBox: document.getElementById("searchBox"),
postList: document.getElementById("postList"),
viewer: document.getElementById("viewer"),
2025-11-08 18:23:47 -06:00
content: document.getElementById("content"),
toggleControls: document.getElementById("toggleControls"),
filterPanel: document.getElementById("filterPanel")
2025-11-08 14:55:16 -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();
2025-11-08 16:09:05 -06:00
populateTags();
2025-11-08 15:35:37 -06:00
wireUI();
renderList();
handleHash();
window.addEventListener("hashchange", handleHash);
} catch (e) {
2025-11-08 18:23:47 -06:00
els.viewer.innerHTML = "<h1>Error</h1><p>Failed to load site data.</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>';
2025-11-08 19:17:07 -06:00
const navSections = [...new Set(
indexData.flat
.filter(f => f.isIndex)
.map(f => f.path.split("/")[0])
)].sort();
navSections.forEach(s => {
els.primaryNav.innerHTML += `<a href="#/${s}/">${s.charAt(0).toUpperCase() + s.slice(1)}</a>`;
2025-11-08 15:48:53 -06:00
});
}
2025-11-08 15:35:37 -06:00
function populateSections() {
2025-11-08 16:09:05 -06:00
els.sectionSelect.innerHTML = '<option value="all">All Sections</option>';
2025-11-08 18:10:27 -06:00
indexData.sections.forEach(s => {
2025-11-08 15:26:01 -06:00
const opt = document.createElement("option");
2025-11-08 18:23:47 -06:00
opt.value = s; opt.textContent = 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 16:09:05 -06:00
function populateTags() {
indexData.tags.forEach(t => {
const opt = document.createElement("option");
2025-11-08 18:23:47 -06:00
opt.value = t; opt.textContent = t;
2025-11-08 16:09:05 -06:00
els.tagSelect.appendChild(opt);
});
}
2025-11-08 19:10:20 -06:00
function formatTimestamp(ms) {
const d = new Date(ms);
return `${d.getFullYear()}-${String(d.getMonth()+1).padStart(2,'0')}-${String(d.getDate()).padStart(2,'0')} ${String(d.getHours()).padStart(2,'0')}:${String(d.getMinutes()).padStart(2,'0')}`;
}
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 18:23:47 -06:00
els.toggleControls.addEventListener("click", () => {
const open = els.filterPanel.open;
els.filterPanel.open = !open;
els.toggleControls.textContent = open ? "Filters" : "Hide";
});
2025-11-08 18:10:27 -06:00
els.sectionSelect.addEventListener("change", () => {
renderList();
if (els.sectionSelect.value !== "all") loadDefaultForSection(els.sectionSelect.value);
});
2025-11-08 18:23:47 -06:00
2025-11-08 18:10:27 -06:00
[els.tagSelect, els.sortSelect, els.searchMode].forEach(el => el.addEventListener("change", renderList));
els.searchBox.addEventListener("input", renderList);
2025-11-08 18:23:47 -06:00
2025-11-08 18:43:20 -06:00
els.content.addEventListener("click", (e) => {
2025-11-08 18:23:47 -06:00
if (window.innerWidth < 1024 && document.body.classList.contains("sidebar-open")) {
2025-11-08 18:43:20 -06:00
if (!e.target.closest("#sidebar")) {
document.body.classList.remove("sidebar-open");
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;
2025-11-08 16:09:05 -06:00
const tags = Array.from(els.tagSelect.selectedOptions).map(o => o.value.toLowerCase());
2025-11-08 15:26:01 -06:00
const sort = els.sortSelect.value;
2025-11-08 16:09:05 -06:00
const mode = els.searchMode.value;
2025-11-08 15:26:01 -06:00
const query = els.searchBox.value.toLowerCase();
2025-11-08 12:59:27 -06:00
2025-11-08 18:23:47 -06:00
let posts = indexData.flat.filter(p => !p.isIndex);
2025-11-08 16:09:05 -06:00
if (section !== "all") posts = posts.filter(p => p.path.split('/')[0] === section);
2025-11-08 18:23:47 -06:00
if (tags.length) posts = posts.filter(p => tags.every(t => p.tags.includes(t)));
2025-11-08 16:09:05 -06:00
if (query) {
posts = posts.filter(p => {
2025-11-08 18:23:47 -06:00
const text = mode === "content" ? p.title + " " + p.excerpt : p.title;
return text.toLowerCase().includes(query);
2025-11-08 16:09:05 -06:00
});
}
2025-11-08 15:35:37 -06:00
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 18:23:47 -06:00
els.postList.innerHTML = posts.length ? "" : "<li>No posts found.</li>";
posts.forEach(p => {
2025-11-08 15:26:01 -06:00
const li = document.createElement("li");
2025-11-08 18:23:47 -06:00
const pin = p.isPinned ? "Star " : "";
2025-11-08 19:10:20 -06:00
const time = formatTimestamp(p.ctime);
li.innerHTML = `<a href="#/${p.path}">${pin}${p.title}</a><small>${time}</small>`;
2025-11-08 15:26:01 -06:00
els.postList.appendChild(li);
2025-11-08 18:23:47 -06:00
});
2025-11-08 15:26:01 -06:00
}
2025-11-08 13:06:29 -06:00
2025-11-08 18:10:27 -06:00
function loadDefaultForSection(section) {
2025-11-08 18:23:47 -06:00
const posts = indexData.flat.filter(p => p.path.split('/')[0] === section && !p.isIndex);
2025-11-08 18:10:27 -06:00
if (!posts.length) {
2025-11-08 18:23:47 -06:00
els.viewer.innerHTML = `<h1>${section}</h1><p>No content yet.</p>`;
2025-11-08 18:10:27 -06:00
return;
}
2025-11-08 18:23:47 -06:00
const pinned = posts.find(p => p.isPinned) || posts.sort((a,b) => b.mtime - a.mtime)[0];
location.hash = `#/${pinned.path}`;
2025-11-08 18:10:27 -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('/')) {
2025-11-08 18:23:47 -06:00
const section = rel.slice(0, -1);
2025-11-08 19:10:20 -06:00
const indexFile = indexData.flat.find(f => f.path.startsWith(section + "/") && f.isIndex);
2025-11-08 18:10:27 -06:00
if (indexFile) {
2025-11-08 18:23:47 -06:00
indexFile.ext === ".md" ? await renderMarkdown(indexFile.path) : renderIframe(indexFile.path);
2025-11-08 15:35:37 -06:00
} else {
2025-11-08 18:10:27 -06:00
els.sectionSelect.value = section;
renderList();
loadDefaultForSection(section);
2025-11-08 15:35:37 -06:00
}
} else {
const file = indexData.flat.find(f => f.path === rel);
if (!file) {
2025-11-08 18:23:47 -06:00
els.viewer.innerHTML = "<h1>404</h1><p>Not found.</p>";
2025-11-08 15:35:37 -06:00
return;
}
2025-11-08 18:23:47 -06:00
file.ext === ".md" ? await renderMarkdown(file.path) : renderIframe(file.path);
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 18:23:47 -06:00
const src = await fetch(rel).then(r => r.ok ? r.text() : Promise.reject());
els.viewer.innerHTML = `<article class="markdown">${marked.parse(src)}</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:26:01 -06:00
iframe.src = "/" + rel;
2025-11-08 18:23:47 -06:00
iframe.loading = "eager";
iframe.setAttribute("sandbox", "allow-same-origin allow-scripts allow-forms");
2025-11-08 14:39:26 -06:00
els.viewer.appendChild(iframe);
2025-11-08 18:23:47 -06:00
iframe.onload = () => {
2025-11-08 15:35:37 -06:00
try {
2025-11-08 18:23:47 -06:00
const doc = iframe.contentDocument;
const style = doc.createElement("style");
style.textContent = `
html,body{background:#0b0b0b;color:#e6e3d7;font-family:Inter,sans-serif;margin:0;padding:2rem;}
*{max-width:720px;margin:auto;}
2025-11-08 18:43:20 -06:00
img, video, iframe {max-width:100%;height:auto;}
2025-11-08 15:06:28 -06:00
`;
2025-11-08 18:23:47 -06:00
doc.head.appendChild(style);
2025-11-08 19:01:40 -06:00
} catch (e) {}
2025-11-08 18:23:47 -06:00
};
2025-11-08 14:39:26 -06:00
}
2025-11-08 15:35:37 -06:00
function renderDefault() {
2025-11-08 18:23:47 -06:00
const latest = indexData.flat.filter(f => !f.isIndex).sort((a,b) => b.mtime - a.mtime)[0];
if (latest) location.hash = `#/${latest.path}`;
else els.viewer.innerHTML = "<h1>Welcome</h1><p>Add content to begin.</p>";
2025-11-08 15:26:01 -06:00
}
init();