|
| 1 | +/** |
| 2 | + * Product comparison — loads search-index.json and builds a capability × product |
| 3 | + * comparison table for up to 3 selected products. |
| 4 | + */ |
| 5 | +(function () { |
| 6 | + 'use strict'; |
| 7 | + |
| 8 | + var container = document.getElementById('comparisonResult'); |
| 9 | + var exportPanel = document.getElementById('compareExport'); |
| 10 | + if (!container) return; // not on compare page |
| 11 | + |
| 12 | + var index = null; |
| 13 | + var selectedProducts = []; |
| 14 | + |
| 15 | + // Read URL state |
| 16 | + function readURL() { |
| 17 | + var params = new URLSearchParams(window.location.search); |
| 18 | + var ids = (params.get('products') || '').split(',').filter(Boolean); |
| 19 | + ids.forEach(function (id) { |
| 20 | + var cb = document.querySelector('.product-checkbox input[value="' + id + '"]'); |
| 21 | + if (cb) cb.checked = true; |
| 22 | + }); |
| 23 | + return ids; |
| 24 | + } |
| 25 | + |
| 26 | + function updateURL() { |
| 27 | + var params = new URLSearchParams(window.location.search); |
| 28 | + if (selectedProducts.length) { |
| 29 | + params.set('products', selectedProducts.join(',')); |
| 30 | + } else { |
| 31 | + params.delete('products'); |
| 32 | + } |
| 33 | + var qs = params.toString(); |
| 34 | + var url = window.location.pathname + (qs ? '?' + qs : ''); |
| 35 | + history.replaceState(null, '', url); |
| 36 | + } |
| 37 | + |
| 38 | + function loadIndex(cb) { |
| 39 | + if (index) return cb(); |
| 40 | + var xhr = new XMLHttpRequest(); |
| 41 | + xhr.open('GET', 'assets/data.json'); |
| 42 | + xhr.onload = function () { |
| 43 | + if (xhr.status === 200) { |
| 44 | + try { index = JSON.parse(xhr.responseText); } catch (e) { index = { capabilities: [], implementations: [], products: [] }; } |
| 45 | + } |
| 46 | + cb(); |
| 47 | + }; |
| 48 | + xhr.onerror = function () { cb(); }; |
| 49 | + xhr.send(); |
| 50 | + } |
| 51 | + |
| 52 | + function getSelectedProducts() { |
| 53 | + var checkboxes = document.querySelectorAll('.product-checkbox input:checked'); |
| 54 | + var ids = []; |
| 55 | + for (var i = 0; i < checkboxes.length; i++) ids.push(checkboxes[i].value); |
| 56 | + return ids; |
| 57 | + } |
| 58 | + |
| 59 | + // Enforce max 3 selection |
| 60 | + function enforceMax() { |
| 61 | + var all = document.querySelectorAll('.product-checkbox input'); |
| 62 | + var checked = document.querySelectorAll('.product-checkbox input:checked'); |
| 63 | + for (var i = 0; i < all.length; i++) { |
| 64 | + all[i].disabled = (checked.length >= 3 && !all[i].checked); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + window.updateComparison = function () { |
| 69 | + enforceMax(); |
| 70 | + selectedProducts = getSelectedProducts(); |
| 71 | + updateURL(); |
| 72 | + |
| 73 | + if (selectedProducts.length < 2) { |
| 74 | + container.innerHTML = '<p class="compare-hint">Select at least 2 products to compare.</p>'; |
| 75 | + if (exportPanel) exportPanel.hidden = true; |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + loadIndex(function () { |
| 80 | + renderComparison(); |
| 81 | + }); |
| 82 | + }; |
| 83 | + |
| 84 | + function renderComparison() { |
| 85 | + if (!index) return; |
| 86 | + |
| 87 | + // Build lookup: product → capability → implementation |
| 88 | + var implMap = {}; // { productId: { capId: impl } } |
| 89 | + selectedProducts.forEach(function (pid) { implMap[pid] = {}; }); |
| 90 | + |
| 91 | + index.implementations.forEach(function (impl) { |
| 92 | + if (selectedProducts.indexOf(impl.product) === -1) return; |
| 93 | + (impl.capabilities || []).forEach(function (capId) { |
| 94 | + if (!implMap[impl.product][capId]) { |
| 95 | + implMap[impl.product][capId] = impl; |
| 96 | + } |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + // Filter capabilities to those relevant for at least one selected product |
| 101 | + var relevantCaps = index.capabilities.filter(function (cap) { |
| 102 | + for (var i = 0; i < selectedProducts.length; i++) { |
| 103 | + if (implMap[selectedProducts[i]][cap.id]) return true; |
| 104 | + } |
| 105 | + return false; |
| 106 | + }); |
| 107 | + |
| 108 | + // Group capabilities by group |
| 109 | + var groups = {}; |
| 110 | + var groupOrder = []; |
| 111 | + relevantCaps.forEach(function (cap) { |
| 112 | + var g = cap.group || 'other'; |
| 113 | + if (!groups[g]) { groups[g] = []; groupOrder.push(g); } |
| 114 | + groups[g].push(cap); |
| 115 | + }); |
| 116 | + |
| 117 | + // Product name lookup |
| 118 | + var prodNames = {}; |
| 119 | + index.products.forEach(function (p) { prodNames[p.id] = p.name; }); |
| 120 | + |
| 121 | + // Build table |
| 122 | + var html = '<table class="compare-table"><thead><tr><th>Capability</th>'; |
| 123 | + selectedProducts.forEach(function (pid) { |
| 124 | + html += '<th>' + escapeHtml(prodNames[pid] || pid) + '</th>'; |
| 125 | + }); |
| 126 | + html += '</tr></thead><tbody>'; |
| 127 | + |
| 128 | + groupOrder.forEach(function (group) { |
| 129 | + html += '<tr class="compare-group-row"><td colspan="' + (selectedProducts.length + 1) + '">' + escapeHtml(formatGroup(group)) + '</td></tr>'; |
| 130 | + groups[group].forEach(function (cap) { |
| 131 | + html += '<tr><td class="compare-cap-name">' + escapeHtml(cap.name) + '</td>'; |
| 132 | + selectedProducts.forEach(function (pid) { |
| 133 | + var impl = implMap[pid][cap.id]; |
| 134 | + if (impl) { |
| 135 | + var badge = gatingBadge(impl.gating); |
| 136 | + html += '<td class="compare-cell compare-yes">' + escapeHtml(impl.name) + ' ' + badge + '</td>'; |
| 137 | + } else { |
| 138 | + html += '<td class="compare-cell compare-no">—</td>'; |
| 139 | + } |
| 140 | + }); |
| 141 | + html += '</tr>'; |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + html += '</tbody></table>'; |
| 146 | + |
| 147 | + // Summary |
| 148 | + var total = index.capabilities.length; |
| 149 | + var summaryParts = selectedProducts.map(function (pid) { |
| 150 | + var count = 0; |
| 151 | + index.capabilities.forEach(function (cap) { if (implMap[pid][cap.id]) count++; }); |
| 152 | + return escapeHtml(prodNames[pid] || pid) + ': ' + count + '/' + total; |
| 153 | + }); |
| 154 | + html = '<p class="compare-summary">' + summaryParts.join(' • ') + '</p>' + html; |
| 155 | + |
| 156 | + container.innerHTML = html; |
| 157 | + if (exportPanel) exportPanel.hidden = false; |
| 158 | + } |
| 159 | + |
| 160 | + function gatingBadge(gating) { |
| 161 | + if (!gating) return ''; |
| 162 | + var cls = 'badge-gating'; |
| 163 | + if (gating === 'free') cls += ' badge-free'; |
| 164 | + else if (gating === 'paid') cls += ' badge-paid'; |
| 165 | + return '<span class="' + cls + '">' + escapeHtml(gating) + '</span>'; |
| 166 | + } |
| 167 | + |
| 168 | + function formatGroup(g) { |
| 169 | + return g.replace(/-/g, ' ').replace(/\b\w/g, function (c) { return c.toUpperCase(); }); |
| 170 | + } |
| 171 | + |
| 172 | + function escapeHtml(s) { |
| 173 | + var d = document.createElement('div'); |
| 174 | + d.appendChild(document.createTextNode(s || '')); |
| 175 | + return d.innerHTML; |
| 176 | + } |
| 177 | + |
| 178 | + // Expose data for export.js |
| 179 | + window._compareData = function () { |
| 180 | + if (!index || selectedProducts.length < 2) return null; |
| 181 | + var prodNames = {}; |
| 182 | + index.products.forEach(function (p) { prodNames[p.id] = p.name; }); |
| 183 | + var implMap = {}; |
| 184 | + selectedProducts.forEach(function (pid) { implMap[pid] = {}; }); |
| 185 | + index.implementations.forEach(function (impl) { |
| 186 | + if (selectedProducts.indexOf(impl.product) === -1) return; |
| 187 | + (impl.capabilities || []).forEach(function (capId) { |
| 188 | + if (!implMap[impl.product][capId]) implMap[impl.product][capId] = impl; |
| 189 | + }); |
| 190 | + }); |
| 191 | + return { capabilities: index.capabilities, selectedProducts: selectedProducts, prodNames: prodNames, implMap: implMap }; |
| 192 | + }; |
| 193 | + |
| 194 | + // Init from URL |
| 195 | + var initial = readURL(); |
| 196 | + if (initial.length >= 2) { |
| 197 | + selectedProducts = initial; |
| 198 | + loadIndex(function () { |
| 199 | + enforceMax(); |
| 200 | + renderComparison(); |
| 201 | + }); |
| 202 | + } |
| 203 | +})(); |
0 commit comments