thefoldwithin-earth/public/app.js

290 lines
9.5 KiB
JavaScript
Raw Normal View History

2025-11-09 19:37:10 +00:00
const els = {
2025-11-09 19:58:05 +00:00
menuBtn: document.getElementById("menuBtn"),
primaryNav: document.getElementById("primaryNav"),
subNav: document.getElementById("subNav"),
sectionSelect: document.getElementById("sectionSelect"),
tagSelect: document.getElementById("tagSelect"),
sortSelect: document.getElementById("sortSelect"),
searchMode: document.getElementById("searchMode"),
searchBox: document.getElementById("searchBox"),
postList: document.getElementById("postList"),
viewer: document.getElementById("viewer"),
content: document.getElementById("content"),
toggleControls: document.getElementById("toggleControls"),
filterPanel: document.getElementById("filterPanel")
2025-11-09 19:37:10 +00:00
};
2025-11-09 19:58:05 +00:00
let indexData = null;
let sidebarOpen = false;
let currentParent = null;
2025-11-09 20:06:19 +00:00
let indexFiles = null; // Cached
2025-11-09 19:37:10 +00:00
async function init() {
try {
2025-11-09 19:58:05 +00:00
indexData = await (await fetch("index.json")).json();
2025-11-09 20:06:19 +00:00
indexFiles = indexData.flat.filter(f => f.isIndex); // Cache
2025-11-09 19:58:05 +00:00
populateNav();
populateSections();
populateTags();
wireUI();
renderList();
handleHash();
window.addEventListener("hashchange", handleHash);
2025-11-09 19:37:10 +00:00
} catch (e) {
2025-11-09 20:06:19 +00:00
els.viewer.innerHTML = "<h1>Error</h1><p>Failed to load site data.</p>";
2025-11-09 19:37:10 +00:00
}
2025-11-09 19:22:23 +00:00
}
2025-11-09 19:37:10 +00:00
function populateNav() {
2025-11-09 19:58:05 +00:00
els.primaryNav.innerHTML = '<a href="#/">Home</a>';
2025-11-09 19:37:10 +00:00
const navSections = [...new Set(
indexData.flat
.filter(f => f.isIndex && f.path.split("/").length > 1)
.map(f => f.path.split("/")[0])
)].sort();
navSections.forEach(s => {
els.primaryNav.innerHTML += `<a href="#/${s}/">${s.charAt(0).toUpperCase() + s.slice(1)}</a>`;
});
}
function populateSections() {
els.sectionSelect.innerHTML = '<option value="all">All Sections</option>';
indexData.sections.forEach(s => {
const opt = document.createElement("option");
opt.value = s; opt.textContent = s;
els.sectionSelect.appendChild(opt);
});
2025-11-09 20:06:19 +00:00
if (indexData.sections.includes("posts")) {
els.sectionSelect.value = "posts";
} else if (indexData.sections.length > 0) {
els.sectionSelect.value = indexData.sections[0];
}
2025-11-09 19:37:10 +00:00
}
function populateTags() {
indexData.tags.forEach(t => {
const opt = document.createElement("option");
opt.value = t; opt.textContent = t;
els.tagSelect.appendChild(opt);
});
}
2025-11-09 20:06:19 +00: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-09 19:37:10 +00:00
function wireUI() {
els.menuBtn.addEventListener("click", () => {
sidebarOpen = !sidebarOpen;
2025-11-09 19:58:05 +00:00
document.body.classList.toggle("sidebar-open", sidebarOpen);
2025-11-09 19:37:10 +00:00
});
els.toggleControls.addEventListener("click", () => {
const open = els.filterPanel.open;
els.filterPanel.open = !open;
2025-11-09 19:58:05 +00:00
els.toggleControls.textContent = open ? "Filters" : "Hide";
2025-11-09 19:37:10 +00:00
});
els.sectionSelect.addEventListener("change", () => {
renderList();
2025-11-09 19:58:05 +00:00
if (els.sectionSelect.value !== "all") loadDefaultForSection(els.sectionSelect.value);
2025-11-09 19:37:10 +00:00
});
[els.tagSelect, els.sortSelect, els.searchMode].forEach(el => el.addEventListener("change", renderList));
2025-11-09 19:58:05 +00:00
els.searchBox.addEventListener("input", renderList);
2025-11-09 19:37:10 +00:00
els.content.addEventListener("click", (e) => {
if (window.innerWidth < 1024 && document.body.classList.contains("sidebar-open")) {
if (!e.target.closest("#sidebar")) {
document.body.classList.remove("sidebar-open");
2025-11-09 19:58:05 +00:00
sidebarOpen = false;
2025-11-09 19:37:10 +00:00
}
}
});
}
function renderList() {
const section = els.sectionSelect.value;
const tags = Array.from(els.tagSelect.selectedOptions).map(o => o.value.toLowerCase());
const sort = els.sortSelect.value;
const mode = els.searchMode.value;
const query = els.searchBox.value.toLowerCase();
2025-11-09 19:58:05 +00:00
let posts = indexData.flat.filter(p => !p.isIndex);
2025-11-09 19:37:10 +00:00
if (section !== "all") posts = posts.filter(p => p.path.split('/')[0] === section);
2025-11-09 19:58:05 +00:00
if (tags.length) posts = posts.filter(p => tags.every(t => p.tags.includes(t)));
2025-11-09 19:37:10 +00:00
if (query) {
posts = posts.filter(p => {
const text = mode === "content" ? p.title + " " + p.excerpt : p.title;
2025-11-09 19:58:05 +00:00
return text.toLowerCase().includes(query);
2025-11-09 19:37:10 +00:00
});
}
2025-11-09 19:58:05 +00:00
posts.sort((a, b) => sort === "newest" ? b.mtime - a.mtime : a.mtime - b.mtime);
2025-11-09 19:37:10 +00:00
els.postList.innerHTML = posts.length ? "" : "<li>No posts found.</li>";
posts.forEach(p => {
const li = document.createElement("li");
const pin = p.isPinned ? "Star " : "";
2025-11-09 20:06:19 +00:00
const time = formatTimestamp(p.ctime);
2025-11-09 19:37:10 +00:00
li.innerHTML = `<a href="#/${p.path}">${pin}${p.title}</a><small>${time}</small>`;
els.postList.appendChild(li);
});
}
function loadDefaultForSection(section) {
const posts = indexData.flat.filter(p => p.path.split('/')[0] === section && !p.isIndex);
if (!posts.length) {
els.viewer.innerHTML = `<h1>${section}</h1><p>No content yet.</p>`;
return;
}
const pinned = posts.find(p => p.isPinned) || posts.sort((a,b) => b.mtime - a.mtime)[0];
location.hash = `#/${pinned.path}`;
}
2025-11-09 20:06:19 +00:00
// NESTED HORIZON: Deep-Aware Sub-Navigation
2025-11-09 19:37:10 +00:00
function renderSubNav(parent) {
const subnav = els.subNav;
subnav.innerHTML = "";
subnav.classList.remove("visible");
2025-11-09 19:58:05 +00:00
if (!parent || !indexData.hierarchies?.[parent]) return;
2025-11-09 19:37:10 +00:00
const subs = indexData.hierarchies[parent];
subs.forEach(child => {
const link = document.createElement("a");
link.href = `#/${parent}/${child}/`;
link.textContent = child.charAt(0).toUpperCase() + child.slice(1);
subnav.appendChild(link);
});
2025-11-09 20:06:19 +00:00
requestAnimationFrame(() => {
subnav.classList.add("visible");
});
2025-11-09 19:37:10 +00:00
}
async function handleHash() {
2025-11-09 19:58:05 +00:00
els.viewer.innerHTML = "";
2025-11-09 19:37:10 +00:00
const rel = location.hash.replace(/^#\//, "");
2025-11-09 20:06:19 +00:00
const parts = rel.split("/").filter(Boolean); // e.g., ["about", "Mark"]
// Determine current depth parent for subnav
2025-11-09 19:37:10 +00:00
const currentParentPath = parts.slice(0, -1).join("/") || parts[0] || null;
if (currentParentPath !== currentParent) {
currentParent = currentParentPath;
2025-11-09 19:58:05 +00:00
renderSubNav(currentParent);
2025-11-09 19:37:10 +00:00
}
2025-11-09 19:22:23 +00:00
2025-11-09 20:06:19 +00:00
// Sync sidebar section to top-level
2025-11-09 19:37:10 +00:00
const topSection = parts[0] || null;
if (topSection && indexData.sections.includes(topSection)) {
els.sectionSelect.value = topSection;
2025-11-09 19:58:05 +00:00
renderList();
2025-11-09 19:37:10 +00:00
}
2025-11-09 19:22:23 +00:00
2025-11-09 19:37:10 +00:00
if (!rel) return renderDefault();
2025-11-09 20:06:19 +00:00
// CASE: Trailing slash → render index at *current* level
2025-11-09 19:37:10 +00:00
if (rel.endsWith('/')) {
const currentPath = parts.join("/");
2025-11-09 20:06:19 +00:00
2025-11-09 19:37:10 +00:00
const indexFile = indexFiles.find(f => {
const dir = f.path.split("/").slice(0, -1).join("/");
2025-11-09 19:58:05 +00:00
return dir === currentPath;
2025-11-09 19:37:10 +00:00
});
if (indexFile) {
2025-11-09 20:06:19 +00:00
try {
if (indexFile.ext === ".md") {
const src = await fetch(indexFile.path).then(r => r.ok ? r.text() : "");
const html = marked.parse(src || `# ${currentPath.split("/").pop()}\n\nNo content yet.`);
els.viewer.innerHTML = `<article class="markdown">${html}</article>`;
2025-11-09 20:29:33 +00:00
} else if (indexFile.ext === ".html") {
// Use unified bounded renderer even for nested index.html
renderIframe(indexFile.path);
2025-11-09 20:06:19 +00:00
} else {
2025-11-09 20:29:33 +00:00
els.viewer.innerHTML = `<h1>${currentPath.split("/").pop()}</h1><p>Unsupported type.</p>`;
2025-11-09 20:06:19 +00:00
}
} catch (e) {
els.viewer.innerHTML = `<h1>${currentPath.split("/").pop()}</h1><p>No content yet.</p>`;
2025-11-09 19:37:10 +00:00
}
} else {
2025-11-09 20:06:19 +00:00
// No index → show children or fallback
if (topSection) {
els.sectionSelect.value = topSection;
renderList();
loadDefaultForSection(topSection);
} else {
els.viewer.innerHTML = `<h1>${currentPath.split("/").pop()}</h1><p>No content yet.</p>`;
}
2025-11-09 19:37:10 +00:00
}
2025-11-09 20:06:19 +00:00
}
// CASE: Direct file
else {
2025-11-09 19:37:10 +00:00
const file = indexData.flat.find(f => f.path === rel);
if (!file) {
2025-11-09 19:58:05 +00:00
els.viewer.innerHTML = "<h1>404</h1><p>Not found.</p>";
2025-11-09 19:37:10 +00:00
return;
}
2025-11-09 20:06:19 +00:00
file.ext === ".md" ? await renderMarkdown(file.path) : renderIframe(file.path);
2025-11-09 19:37:10 +00:00
}
2025-11-09 19:22:23 +00:00
}
2025-11-09 19:37:10 +00:00
async function renderMarkdown(rel) {
const src = await fetch(rel).then(r => r.ok ? r.text() : "");
els.viewer.innerHTML = `<article class="markdown">${marked.parse(src || "# Untitled")}</article>`;
}
2025-11-09 20:06:19 +00:00
function renderIframe(rel) {
2025-11-09 20:17:11 +00:00
const viewer = els.viewer;
const container = document.createElement("div");
container.className = "preview-wrapper";
const header = document.createElement("div");
header.className = "preview-header";
header.innerHTML = `<button class="popout-btn" data-src="${rel}">Open Full View ↗</button>`;
2025-11-09 20:06:19 +00:00
const iframe = document.createElement("iframe");
iframe.src = "/" + rel;
iframe.loading = "eager";
iframe.setAttribute("sandbox", "allow-same-origin allow-scripts allow-forms");
2025-11-09 20:17:11 +00:00
container.appendChild(header);
container.appendChild(iframe);
viewer.appendChild(container);
header.querySelector(".popout-btn").addEventListener("click", e => {
const url = e.target.dataset.src.startsWith("/") ? e.target.dataset.src : "/" + e.target.dataset.src;
window.open(url, "_blank", "noopener,noreferrer");
});
2025-11-09 20:06:19 +00:00
iframe.onload = () => {
try {
const doc = iframe.contentDocument;
const style = doc.createElement("style");
style.textContent = `
2025-11-09 20:17:11 +00:00
html,body{background:#0b0b0b;color:#e6e3d7;font-family:Inter,sans-serif;
margin:0;padding:2rem;}
2025-11-09 20:06:19 +00:00
*{max-width:720px;margin:auto;}
2025-11-09 20:17:11 +00:00
img,video,iframe{max-width:100%;height:auto;}
2025-11-09 20:06:19 +00:00
`;
doc.head.appendChild(style);
2025-11-09 20:17:11 +00:00
} catch {}
2025-11-09 20:06:19 +00:00
};
2025-11-08 23:29:53 -06:00
}
2025-11-09 19:37:10 +00:00
function renderDefault() {
2025-11-09 20:06:19 +00:00
const defaultSection = indexData.sections.includes("posts") ? "posts" : (indexData.sections[0] || null);
2025-11-09 19:37:10 +00:00
if (defaultSection) {
els.sectionSelect.value = defaultSection;
renderList();
loadDefaultForSection(defaultSection);
} else {
2025-11-09 20:06:19 +00:00
els.viewer.innerHTML = "<h1>Welcome</h1><p>Add content to begin.</p>";
2025-11-09 19:37:10 +00:00
}
2025-11-09 19:22:23 +00:00
}
2025-11-09 19:58:05 +00:00
init();