|
| 1 | +// assets/js/products.js |
| 2 | +document.addEventListener("DOMContentLoaded", () => { |
| 3 | + const listEl = document.getElementById("productList"); |
| 4 | + const emptyEl = document.getElementById("productEmptyMessage"); |
| 5 | + const searchInput = document.getElementById("productSearch"); |
| 6 | + const typeSelect = document.getElementById("productTypeFilter"); |
| 7 | + |
| 8 | + if (!listEl) return; |
| 9 | + |
| 10 | + let allProducts = []; |
| 11 | + |
| 12 | + async function loadProducts() { |
| 13 | + try { |
| 14 | + const res = await fetch("assets/data/products.json"); |
| 15 | + if (!res.ok) throw new Error(`HTTP ${res.status}`); |
| 16 | + allProducts = await res.json(); |
| 17 | + render(); |
| 18 | + } catch (err) { |
| 19 | + console.error("Failed to load products:", err); |
| 20 | + if (emptyEl) { |
| 21 | + emptyEl.textContent = "プロダクト一覧の読み込みに失敗しました。"; |
| 22 | + emptyEl.style.display = "block"; |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + function render() { |
| 28 | + const keyword = (searchInput?.value || "").trim().toLowerCase(); |
| 29 | + const typeFilter = typeSelect?.value || ""; |
| 30 | + |
| 31 | + const filtered = allProducts.filter((p) => { |
| 32 | + // 種類フィルタ |
| 33 | + if (typeFilter && p.type !== typeFilter) return false; |
| 34 | + |
| 35 | + // キーワードフィルタ |
| 36 | + if (keyword) { |
| 37 | + const haystack = [ |
| 38 | + p.title, |
| 39 | + p.description, |
| 40 | + p.type || "", |
| 41 | + ...(p.platform || []), |
| 42 | + ...(p.tags || []), |
| 43 | + ] |
| 44 | + .join(" ") |
| 45 | + .toLowerCase(); |
| 46 | + if (!haystack.includes(keyword)) return false; |
| 47 | + } |
| 48 | + |
| 49 | + return true; |
| 50 | + }); |
| 51 | + |
| 52 | + listEl.innerHTML = ""; |
| 53 | + |
| 54 | + if (filtered.length === 0) { |
| 55 | + if (emptyEl) emptyEl.style.display = "block"; |
| 56 | + return; |
| 57 | + } else if (emptyEl) { |
| 58 | + emptyEl.style.display = "none"; |
| 59 | + } |
| 60 | + |
| 61 | + filtered.forEach((p) => { |
| 62 | + const card = document.createElement("article"); |
| 63 | + card.className = "card card--clickable product-card"; |
| 64 | + |
| 65 | + // サムネ |
| 66 | + if (p.thumbnail) { |
| 67 | + const thumb = document.createElement("div"); |
| 68 | + thumb.className = "card__thumb"; |
| 69 | + const img = document.createElement("img"); |
| 70 | + img.src = p.thumbnail; |
| 71 | + img.alt = p.title; |
| 72 | + thumb.appendChild(img); |
| 73 | + card.appendChild(thumb); |
| 74 | + } |
| 75 | + |
| 76 | + const body = document.createElement("div"); |
| 77 | + body.className = "card__body"; |
| 78 | + |
| 79 | + const titleRow = document.createElement("div"); |
| 80 | + titleRow.className = "card__title-row"; |
| 81 | + |
| 82 | + const title = document.createElement("h3"); |
| 83 | + title.className = "card__title"; |
| 84 | + title.textContent = p.title; |
| 85 | + |
| 86 | + if (p.type) { |
| 87 | + const pill = document.createElement("span"); |
| 88 | + pill.className = "pill pill--accent"; |
| 89 | + pill.textContent = p.type; |
| 90 | + titleRow.appendChild(pill); |
| 91 | + } |
| 92 | + |
| 93 | + titleRow.appendChild(title); |
| 94 | + body.appendChild(titleRow); |
| 95 | + |
| 96 | + if (p.platform && p.platform.length > 0) { |
| 97 | + const plat = document.createElement("p"); |
| 98 | + plat.className = "card__meta"; |
| 99 | + plat.textContent = `Platform: ${p.platform.join(", ")}`; |
| 100 | + body.appendChild(plat); |
| 101 | + } |
| 102 | + |
| 103 | + if (p.description) { |
| 104 | + const desc = document.createElement("p"); |
| 105 | + desc.className = "card__description"; |
| 106 | + desc.textContent = p.description; |
| 107 | + body.appendChild(desc); |
| 108 | + } |
| 109 | + |
| 110 | + // タグ |
| 111 | + if (p.tags && p.tags.length > 0) { |
| 112 | + const tagRow = document.createElement("div"); |
| 113 | + tagRow.className = "card__tags"; |
| 114 | + p.tags.forEach((t) => { |
| 115 | + const tag = document.createElement("span"); |
| 116 | + tag.className = "tag"; |
| 117 | + tag.textContent = t; |
| 118 | + tagRow.appendChild(tag); |
| 119 | + }); |
| 120 | + body.appendChild(tagRow); |
| 121 | + } |
| 122 | + |
| 123 | + // リンク群 |
| 124 | + const linkRow = document.createElement("div"); |
| 125 | + linkRow.className = "card__actions"; |
| 126 | + |
| 127 | + if (p.storeLinks && p.storeLinks.length > 0) { |
| 128 | + p.storeLinks.forEach((link) => { |
| 129 | + const a = document.createElement("a"); |
| 130 | + a.href = link.url; |
| 131 | + a.target = "_blank"; |
| 132 | + a.rel = "noopener noreferrer"; |
| 133 | + a.className = "btn btn--sm btn--outline"; |
| 134 | + a.textContent = link.label || "Store"; |
| 135 | + linkRow.appendChild(a); |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + if (p.downloadLinks && p.downloadLinks.length > 0) { |
| 140 | + p.downloadLinks.forEach((link) => { |
| 141 | + const a = document.createElement("a"); |
| 142 | + a.href = link.url; |
| 143 | + a.className = "btn btn--sm btn--primary"; |
| 144 | + a.textContent = link.label || "Download"; |
| 145 | + linkRow.appendChild(a); |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + if (linkRow.children.length > 0) { |
| 150 | + body.appendChild(linkRow); |
| 151 | + } |
| 152 | + |
| 153 | + card.appendChild(body); |
| 154 | + listEl.appendChild(card); |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + if (searchInput) { |
| 159 | + searchInput.addEventListener("input", () => { |
| 160 | + render(); |
| 161 | + }); |
| 162 | + } |
| 163 | + if (typeSelect) { |
| 164 | + typeSelect.addEventListener("change", () => { |
| 165 | + render(); |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + loadProducts(); |
| 170 | +}); |
0 commit comments