// Projects Page JavaScript document.addEventListener("DOMContentLoaded", function () { const pills = document.querySelectorAll("[data-filter]"); const grid = document.querySelector("#projectsGrid"); let projectsData = []; // Load projects data async function loadProjects() { try { const response = await fetch("/data/projects.json"); projectsData = await response.json(); renderProjects("All"); } catch (error) { console.error("Error loading projects:", error); grid.innerHTML = '

Error loading projects. Please try again later.

'; } } // Render projects based on category function renderProjects(category = "All") { const filteredProjects = category === "All" ? projectsData : projectsData.filter((project) => project.category === category); grid.classList.add("fade-out"); setTimeout(() => { grid.innerHTML = filteredProjects .map( (project) => `
${project.category}

${project.title}

${project.location}

View Project
` ) .join(""); grid.classList.remove("fade-out"); grid.classList.add("fade-in"); setTimeout(() => { grid.classList.remove("fade-in"); }, 180); }, 120); } // Handle filter pill clicks pills.forEach((pill) => { pill.addEventListener("click", (e) => { e.preventDefault(); const category = pill.dataset.filter; // Update pill states pills.forEach((p) => { p.setAttribute("aria-pressed", p === pill ? "true" : "false"); }); // Update URL const url = new URL(location); if (category === "All") { url.searchParams.delete("category"); } else { url.searchParams.set("category", category); } history.pushState({}, "", url); // Render filtered projects renderProjects(category); }); }); // Handle browser back/forward window.addEventListener("popstate", () => { const category = new URL(location).searchParams.get("category") || "All"; const activePill = document.querySelector(`[data-filter="${category}"]`); if (activePill) { pills.forEach((p) => { p.setAttribute("aria-pressed", p === activePill ? "true" : "false"); }); renderProjects(category); } }); // Initialize with URL category if present const initialCategory = new URL(location).searchParams.get("category") || "All"; const initialPill = document.querySelector( `[data-filter="${initialCategory}"]` ); if (initialPill) { pills.forEach((p) => { p.setAttribute("aria-pressed", p === initialPill ? "true" : "false"); }); } // Load projects on page load loadProjects(); }); // Keyboard navigation for filter pills document.addEventListener("keydown", function (e) { const activePill = document.querySelector('.pill[aria-pressed="true"]'); const pills = Array.from(document.querySelectorAll(".pill")); const currentIndex = pills.indexOf(activePill); if (e.key === "ArrowRight" || e.key === "ArrowDown") { e.preventDefault(); const nextIndex = (currentIndex + 1) % pills.length; pills[nextIndex].click(); pills[nextIndex].focus(); } else if (e.key === "ArrowLeft" || e.key === "ArrowUp") { e.preventDefault(); const prevIndex = currentIndex === 0 ? pills.length - 1 : currentIndex - 1; pills[prevIndex].click(); pills[prevIndex].focus(); } });