|
| 1 | +document.addEventListener("DOMContentLoaded", function () { |
| 2 | + let timelineItems = document.querySelectorAll(".timeline-item"); |
| 3 | + const filterBtns = document.querySelectorAll(".filter-btn"); |
| 4 | + |
| 5 | + // Sort by date in-place |
| 6 | + timelineItems = Array.from(timelineItems).sort((a, b) => { |
| 7 | + const aDateStr = a.querySelector("time")?.getAttribute("datetime"); |
| 8 | + const bDateStr = b.querySelector("time")?.getAttribute("datetime"); |
| 9 | + |
| 10 | + const aDate = aDateStr ? new Date(aDateStr) : new Date("1970-01-01"); |
| 11 | + const bDate = bDateStr ? new Date(bDateStr) : new Date("1970-01-01"); |
| 12 | + |
| 13 | + return bDate - aDate; |
| 14 | + }); |
| 15 | + |
| 16 | + // For some reason, this doesn't quite work |
| 17 | + const timeline = document.querySelector(".timeline"); |
| 18 | + timeline.innerHTML = ""; |
| 19 | + |
| 20 | + let lastYear; |
| 21 | + // Append each item to the timeline |
| 22 | + timelineItems.forEach((item) => { |
| 23 | + const dateStr = item.querySelector("time")?.getAttribute("datetime"); |
| 24 | + const thisYear = dateStr ? new Date(dateStr).getFullYear() : null; |
| 25 | + |
| 26 | + // Add year separator if the year has changed |
| 27 | + if (thisYear && thisYear !== lastYear) { |
| 28 | + const yearSeparator = document.createElement("h2"); |
| 29 | + yearSeparator.textContent = thisYear.toString(); |
| 30 | + yearSeparator.setAttribute("data-type", thisYear.toString()); |
| 31 | + yearSeparator.classList.add("year-separator", "text-2xl", "px-3", "pb-2", "pt-3"); |
| 32 | + timeline.appendChild(yearSeparator); |
| 33 | + |
| 34 | + lastYear = thisYear; |
| 35 | + } |
| 36 | + |
| 37 | + timeline.appendChild(item) |
| 38 | + }); |
| 39 | + |
| 40 | + |
| 41 | + // Add event listeners to filter buttons |
| 42 | + filterBtns.forEach((btn) => { |
| 43 | + btn.addEventListener("click", function () { |
| 44 | + const type = this.getAttribute("data-type"); |
| 45 | + const yearVisibilityCount = {}; |
| 46 | + |
| 47 | + // Re-set active filter button |
| 48 | + filterBtns.forEach((b) => |
| 49 | + b.classList.remove("bg-blue-500", "text-white") |
| 50 | + ); |
| 51 | + this.classList.add("bg-blue-500", "text-white"); |
| 52 | + |
| 53 | + // Show/hide timeline items |
| 54 | + timelineItems.forEach((item) => { |
| 55 | + const dateStr = item.querySelector("time")?.getAttribute("datetime"); |
| 56 | + const thisYear = dateStr ? new Date(dateStr).getFullYear() : null; |
| 57 | + |
| 58 | + // Make sure year is in the count object |
| 59 | + yearVisibilityCount[thisYear] = yearVisibilityCount[thisYear] || 0; |
| 60 | + |
| 61 | + if (type === "all" || item.getAttribute("data-type") === type) { |
| 62 | + item.classList.remove("hidden"); |
| 63 | + |
| 64 | + // Count visible items for each year |
| 65 | + if (thisYear) { |
| 66 | + yearVisibilityCount[thisYear] += 1; |
| 67 | + } |
| 68 | + } else { |
| 69 | + item.classList.add("hidden"); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + // Show/hide year separators for each element |
| 74 | + for (const separator of document.querySelectorAll(".year-separator")) { |
| 75 | + const year = separator.getAttribute("data-type"); |
| 76 | + if (type === "all" || yearVisibilityCount[year] > 0) { |
| 77 | + separator.classList.remove("hidden"); |
| 78 | + } else { |
| 79 | + separator.classList.add("hidden"); |
| 80 | + } |
| 81 | + } |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + // Find <a> tags with empty hrefs |
| 86 | + // Change them to span tags |
| 87 | + const emptyLinks = document.querySelectorAll("a[href='']"); |
| 88 | + emptyLinks.forEach((link) => { |
| 89 | + const span = document.createElement("span"); |
| 90 | + span.innerHTML = link.innerHTML; |
| 91 | + link.parentNode.replaceChild(span, link); |
| 92 | + }); |
| 93 | +}); |
0 commit comments