function toggleSidebar() { document.getElementById('sidebar').classList.toggle('open'); } function showLayer(layerId) { document.querySelectorAll('.layer').forEach(layer => { layer.classList.remove('active'); }); document.getElementById('layer-' + layerId).classList.add('active'); document.body.className = ''; document.body.classList.add('theme-' + layerId); if (layerId !== 'home') { fetchData(layerId); } toggleSidebar(); } const planetData = {}; const planetCols = {}; function wireSearch(planetId) { const layer = document.getElementById('layer-' + planetId); if (!layer) return; const input = layer.querySelector('.search-bar input'); if (!input) return; const fresh = input.cloneNode(true); input.parentNode.replaceChild(fresh, input); fresh.addEventListener('input', () => { filterTable(planetId, fresh.value.trim()); }); } function filterTable(planetId, query) { const tbody = document.getElementById('tbody-' + planetId); if (!tbody) return; const rows = Array.from(tbody.querySelectorAll('tr')); const cols = planetCols[planetId] || []; const data = planetData[planetId] || []; const titleIndices = cols.reduce((acc, col, i) => { const c = col.toLowerCase(); if (c === 'nom' || c === 'name' || c.includes('title') || c.includes('titre')) { acc.push(i); } return acc; }, []); const searchIndices = titleIndices.length > 0 ? titleIndices : cols.reduce((acc, col, i) => { const c = col.toLowerCase(); if (!c.includes('url') && !c.includes('desc') && !c.includes('expl') && c !== 'image' && c !== 'img') { acc.push(i); } return acc; }, []); const tokens = query.toLowerCase().split(/\s+/).filter(Boolean); const tableContainer = tbody.closest('.table-container'); const showMoreBtn = tableContainer ? tableContainer.querySelector('.show-more-btn') : null; if (tokens.length === 0) { rows.forEach((tr, index) => { tr.style.display = ''; if (data.length > 10) { if (index >= 10) tr.classList.add('hidden-row'); else tr.classList.remove('hidden-row'); } }); if (showMoreBtn) showMoreBtn.style.display = ''; return; } if (showMoreBtn) showMoreBtn.style.display = 'none'; rows.forEach((tr, index) => { const rowObj = data[index]; if (!rowObj) { tr.style.display = 'none'; return; } const haystack = searchIndices .map(i => String(rowObj[cols[i]] || '').toLowerCase()) .join(' '); const matches = tokens.every(token => haystack.includes(token)); tr.classList.remove('hidden-row'); tr.style.display = matches ? '' : 'none'; }); } async function fetchData(planetId) { const tbody = document.getElementById('tbody-' + planetId); if (!tbody) return; if (tbody.getAttribute('data-loaded') === 'true') { wireSearch(planetId); return; } tbody.innerHTML = '