|
| 1 | +const METADATA_PATH = "./metadata/samples.index.metadata.json"; |
| 2 | +const BASE_PHASES = [ |
| 3 | + "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", |
| 4 | + "11", "12", "13", "14", "15", "16", "17", "18", "19" |
| 5 | +]; |
| 6 | +const PHASE_FALLBACK_DETAILS = { |
| 7 | + "16": { |
| 8 | + title: "Phase 16 - 3D Foundations", |
| 9 | + description: "3D execution track including camera, input, lighting, and runtime debug slices." |
| 10 | + }, |
| 11 | + "17": { |
| 12 | + title: "Phase 17 - Advanced 3D + Gameplay", |
| 13 | + description: "Extended rendering and gameplay-focused 3D samples with integrated debug surfaces." |
| 14 | + }, |
| 15 | + "18": { |
| 16 | + title: "Phase 18 - Runtime Hardening", |
| 17 | + description: "Runtime and boundary hardening slices that preserve engine/shared separation." |
| 18 | + }, |
| 19 | + "19": { |
| 20 | + title: "Phase 19 - Integration Validation", |
| 21 | + description: "System integration and lifecycle validation for stable execution flow." |
| 22 | + } |
| 23 | +}; |
| 24 | + |
| 25 | +export function normalize(value) { |
| 26 | + return typeof value === "string" ? value.trim() : ""; |
| 27 | +} |
| 28 | + |
| 29 | +export function dedupeSortedPhases(samples, phaseDetails) { |
| 30 | + const phaseSet = new Set(); |
| 31 | + for (const detail of phaseDetails) { |
| 32 | + const id = normalize(detail.phase); |
| 33 | + if (/^\d{2}$/.test(id)) { |
| 34 | + phaseSet.add(id); |
| 35 | + } |
| 36 | + } |
| 37 | + for (const sample of samples) { |
| 38 | + const id = normalize(sample.phase); |
| 39 | + if (/^\d{2}$/.test(id)) { |
| 40 | + phaseSet.add(id); |
| 41 | + } |
| 42 | + } |
| 43 | + return [...phaseSet].sort((a, b) => Number(a) - Number(b)); |
| 44 | +} |
| 45 | + |
| 46 | +export function buildPhaseRows(metadata) { |
| 47 | + const samples = Array.isArray(metadata?.samples) ? metadata.samples : []; |
| 48 | + const phaseDetails = Array.isArray(metadata?.phases) ? metadata.phases : []; |
| 49 | + const detailByPhase = new Map( |
| 50 | + phaseDetails |
| 51 | + .map((detail) => [normalize(detail.phase), detail]) |
| 52 | + .filter(([phase]) => /^\d{2}$/.test(phase)) |
| 53 | + ); |
| 54 | + const phases = dedupeSortedPhases(samples, phaseDetails); |
| 55 | + for (const basePhase of BASE_PHASES) { |
| 56 | + phases.push(basePhase); |
| 57 | + } |
| 58 | + const normalizedPhases = [...new Set(phases)].sort((a, b) => Number(a) - Number(b)); |
| 59 | + return normalizedPhases.map((phase) => { |
| 60 | + const sampleCount = samples.filter((entry) => normalize(entry.phase) === phase).length; |
| 61 | + const detail = detailByPhase.get(phase) || PHASE_FALLBACK_DETAILS[phase]; |
| 62 | + const title = normalize(detail?.title) || `Phase ${phase}`; |
| 63 | + const description = |
| 64 | + normalize(detail?.description) || "Phase content available in the sample lane."; |
| 65 | + return { |
| 66 | + phase, |
| 67 | + href: `./phase-${phase}/${phase}01/index.html`, |
| 68 | + title, |
| 69 | + description |
| 70 | + }; |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +export function renderPhaseCards(container, rows) { |
| 75 | + container.innerHTML = ""; |
| 76 | + for (const row of rows) { |
| 77 | + const card = document.createElement("a"); |
| 78 | + card.className = "card-link"; |
| 79 | + card.href = row.href; |
| 80 | + card.dataset.phase = row.phase; |
| 81 | + card.innerHTML = `<h3>${row.title}</h3><p>${row.description}</p>`; |
| 82 | + container.appendChild(card); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +export function filterPhaseRows(rows, query) { |
| 87 | + const normalizedQuery = normalize(query).toLowerCase(); |
| 88 | + if (!normalizedQuery) { |
| 89 | + return rows; |
| 90 | + } |
| 91 | + return rows.filter((row) => { |
| 92 | + const haystack = `${row.phase} ${row.title} ${row.description}`.toLowerCase(); |
| 93 | + return haystack.includes(normalizedQuery); |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +export function updateStatus(statusNode, shown, total, query) { |
| 98 | + const label = normalize(query); |
| 99 | + if (!label) { |
| 100 | + statusNode.textContent = `Showing all ${total} phases.`; |
| 101 | + return; |
| 102 | + } |
| 103 | + statusNode.textContent = `Showing ${shown} of ${total} phases for "${label}".`; |
| 104 | +} |
| 105 | + |
| 106 | +export async function initSamplesIndex() { |
| 107 | + const container = document.getElementById("samples-phase-list"); |
| 108 | + const input = document.getElementById("samples-phase-filter-input"); |
| 109 | + const statusNode = document.getElementById("samples-phase-filter-status"); |
| 110 | + if (!container || !input || !statusNode) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + try { |
| 115 | + const response = await fetch(METADATA_PATH, { cache: "no-store" }); |
| 116 | + if (!response.ok) { |
| 117 | + throw new Error(`Failed to load samples metadata (${response.status})`); |
| 118 | + } |
| 119 | + const metadata = await response.json(); |
| 120 | + const allRows = buildPhaseRows(metadata); |
| 121 | + const applyFilter = () => { |
| 122 | + const filteredRows = filterPhaseRows(allRows, input.value); |
| 123 | + renderPhaseCards(container, filteredRows); |
| 124 | + updateStatus(statusNode, filteredRows.length, allRows.length, input.value); |
| 125 | + }; |
| 126 | + input.addEventListener("input", applyFilter); |
| 127 | + applyFilter(); |
| 128 | + } catch (error) { |
| 129 | + container.innerHTML = ""; |
| 130 | + statusNode.textContent = "Unable to load phase list. Verify local server access to sample metadata."; |
| 131 | + console.error(error); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +if (typeof window !== "undefined" && typeof document !== "undefined") { |
| 136 | + initSamplesIndex(); |
| 137 | +} |
0 commit comments