|
| 1 | +function collectFamilies(data) { |
| 2 | + const familyCounts = {}; |
| 3 | + |
| 4 | + if (!data.projects || typeof data.projects !== "object") { |
| 5 | + console.error("Invalid projects data structure:", data.projects); |
| 6 | + return familyCounts; |
| 7 | + } |
| 8 | + |
| 9 | + const projects = Object.values(data.projects); |
| 10 | + |
| 11 | + for (const project of projects) { |
| 12 | + if (!project || typeof project !== "object") continue; |
| 13 | + |
| 14 | + const family = project.family; |
| 15 | + |
| 16 | + if (family) { |
| 17 | + familyCounts[family] = (familyCounts[family] || 0) + 1; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + return familyCounts; |
| 22 | +} |
| 23 | + |
| 24 | +function collectTags(data) { |
| 25 | + const tagCounts = {}; |
| 26 | + |
| 27 | + if (!data.projects || typeof data.projects !== "object") { |
| 28 | + console.error("Invalid projects data structure:", data.projects); |
| 29 | + return tagCounts; |
| 30 | + } |
| 31 | + |
| 32 | + const projects = Object.values(data.projects); |
| 33 | + |
| 34 | + for (const project of projects) { |
| 35 | + if (!project || typeof project !== "object") continue; |
| 36 | + |
| 37 | + const tags = project.tags; |
| 38 | + |
| 39 | + if (Array.isArray(tags)) { |
| 40 | + tags.forEach((tag) => { |
| 41 | + tagCounts[tag] = (tagCounts[tag] || 0) + 1; |
| 42 | + }); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return tagCounts; |
| 47 | +} |
| 48 | + |
| 49 | +function displayFamilies(familyCounts) { |
| 50 | + const familiesContainer = document.getElementById("product-families-list"); |
| 51 | + familiesContainer.innerHTML = ""; |
| 52 | + |
| 53 | + const sortedFamilies = Object.keys(familyCounts).sort(); |
| 54 | + |
| 55 | + sortedFamilies.forEach((family) => { |
| 56 | + const familyCount = familyCounts[family]; |
| 57 | + |
| 58 | + const familyRow = document.createElement("div"); |
| 59 | + familyRow.className = "family-row"; |
| 60 | + |
| 61 | + const checkbox = document.createElement("input"); |
| 62 | + checkbox.type = "checkbox"; |
| 63 | + checkbox.id = `family-${CSS.escape(family)}`; |
| 64 | + checkbox.addEventListener("change", handleFamilySelection); |
| 65 | + |
| 66 | + const familyName = document.createElement("span"); |
| 67 | + familyName.className = "family-name"; |
| 68 | + familyName.textContent = family; |
| 69 | + |
| 70 | + const familyCountElem = document.createElement("span"); |
| 71 | + familyCountElem.className = "family-count"; |
| 72 | + familyCountElem.textContent = `(${familyCount})`; |
| 73 | + |
| 74 | + familyRow.appendChild(checkbox); |
| 75 | + familyRow.appendChild(familyName); |
| 76 | + //familyRow.appendChild(familyCountElem); |
| 77 | + |
| 78 | + familiesContainer.appendChild(familyRow); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +function displayTags(tagCounts) { |
| 83 | + const tagsContainer = document.getElementById("product-tags-list"); |
| 84 | + tagsContainer.innerHTML = ""; |
| 85 | + |
| 86 | + const sortedTags = Object.keys(tagCounts).sort(); |
| 87 | + |
| 88 | + sortedTags.forEach((tag) => { |
| 89 | + const tagCount = tagCounts[tag]; |
| 90 | + |
| 91 | + const tagRow = document.createElement("div"); |
| 92 | + tagRow.className = "tag-row"; |
| 93 | + |
| 94 | + const checkbox = document.createElement("input"); |
| 95 | + checkbox.type = "checkbox"; |
| 96 | + checkbox.id = `tag-${CSS.escape(tag)}`; |
| 97 | + checkbox.addEventListener("change", handleTagSelection); |
| 98 | + |
| 99 | + const tagName = document.createElement("span"); |
| 100 | + tagName.className = "tag-name"; |
| 101 | + tagName.textContent = tag; |
| 102 | + |
| 103 | + const tagCountElem = document.createElement("span"); |
| 104 | + tagCountElem.className = "tag-count"; |
| 105 | + tagCountElem.textContent = `(${tagCount})`; |
| 106 | + |
| 107 | + tagRow.appendChild(checkbox); |
| 108 | + tagRow.appendChild(tagName); |
| 109 | + //tagRow.appendChild(tagCountElem); |
| 110 | + |
| 111 | + tagsContainer.appendChild(tagRow); |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +function handleFamilySelection() { |
| 116 | + const selectedFamilies = Array.from( |
| 117 | + document.querySelectorAll( |
| 118 | + '#product-families-list input[type="checkbox"]:checked', |
| 119 | + ), |
| 120 | + ).map((checkbox) => |
| 121 | + checkbox.id.replace("family-", "").replace("\\ ", "-").toLowerCase(), |
| 122 | + ); |
| 123 | + |
| 124 | + console.log("Selected families:", selectedFamilies); |
| 125 | + |
| 126 | + const projectCards = document.querySelectorAll(".project-card"); |
| 127 | + |
| 128 | + projectCards.forEach((card) => { |
| 129 | + const family = card.getAttribute("data-family").toLowerCase(); |
| 130 | + console.log("Family:", family); |
| 131 | + card.style.display = |
| 132 | + selectedFamilies.length === 0 || selectedFamilies.includes(family) |
| 133 | + ? "flex" |
| 134 | + : "none"; |
| 135 | + }); |
| 136 | +} |
| 137 | + |
| 138 | +function handleTagSelection() { |
| 139 | + const selectedTags = Array.from( |
| 140 | + document.querySelectorAll( |
| 141 | + '#product-tags-list input[type="checkbox"]:checked', |
| 142 | + ), |
| 143 | + ).map((checkbox) => |
| 144 | + checkbox.id.replace("tag-", "").replace("\\ ", "-").toLowerCase(), |
| 145 | + ); |
| 146 | + |
| 147 | + console.log("Selected tags:", selectedTags); |
| 148 | + |
| 149 | + const projectCards = document.querySelectorAll(".project-card"); |
| 150 | + |
| 151 | + projectCards.forEach((card) => { |
| 152 | + const rawTags = card.getAttribute("data-tags"); |
| 153 | + |
| 154 | + if (!rawTags) { |
| 155 | + card.style.display = "none"; |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + // Parse the data-tags string |
| 160 | + let cardTags = []; |
| 161 | + try { |
| 162 | + cardTags = JSON.parse(rawTags.replace(/'/g, '"')); |
| 163 | + } catch (error) { |
| 164 | + console.error("Error parsing data-tags:", rawTags, error); |
| 165 | + card.style.display = "none"; |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + const cardTagsLower = cardTags.map((tag) => tag.toLowerCase()); |
| 170 | + const hasMatchingTag = selectedTags.some((tag) => |
| 171 | + cardTagsLower.includes(tag), |
| 172 | + ); |
| 173 | + |
| 174 | + card.style.display = |
| 175 | + selectedTags.length === 0 || hasMatchingTag ? "flex" : "none"; |
| 176 | + }); |
| 177 | +} |
| 178 | + |
| 179 | +function initializeAllCards() { |
| 180 | + const articleElement = document.querySelector("article"); |
| 181 | + const projects = articleElement.querySelectorAll(".project-card"); |
| 182 | + projects.forEach((project) => { |
| 183 | + project.style.display = "flex"; |
| 184 | + }); |
| 185 | +} |
| 186 | + |
| 187 | +fetch("_static/projects.json") |
| 188 | + .then((response) => { |
| 189 | + if (!response.ok) { |
| 190 | + throw new Error(`HTTP error! Status: ${response.status}`); |
| 191 | + } |
| 192 | + return response.json(); |
| 193 | + }) |
| 194 | + .then((data) => { |
| 195 | + // Display families |
| 196 | + const familyCounts = collectFamilies(data); |
| 197 | + displayFamilies(familyCounts); |
| 198 | + |
| 199 | + // Display tags |
| 200 | + const tagCounts = collectTags(data); |
| 201 | + displayTags(tagCounts); |
| 202 | + |
| 203 | + // Render all cards |
| 204 | + initializeAllCards(); |
| 205 | + }) |
| 206 | + .catch((error) => { |
| 207 | + console.error("Error fetching the projects data:", error); |
| 208 | + }); |
0 commit comments