// Client-side search filter for modal lists (devices, zones, presets). // Hides .profiles-row children whose text doesn't match; re-applies when the list re-renders. function rowSearchText(row) { const parts = []; row.querySelectorAll('*').forEach((el) => { if (el.closest('button, .btn')) return; if (el.children.length === 0 && el.textContent) parts.push(el.textContent); }); if (row.dataset.deviceId) parts.push(row.dataset.deviceId); if (row.dataset.deviceAddress) parts.push(row.dataset.deviceAddress); return parts.join(' ').toLowerCase(); } function applyListSearch(list, query) { const terms = query.trim().toLowerCase().split(/\s+/).filter(Boolean); let shown = 0; let total = 0; list.querySelectorAll(':scope > .profiles-row').forEach((row) => { total += 1; const text = rowSearchText(row); const match = terms.every((t) => text.includes(t)); row.classList.toggle('list-search-hidden', !match); if (match) shown += 1; }); let empty = list.querySelector(':scope > .list-search-empty'); if (total > 0 && shown === 0 && terms.length) { if (!empty) { empty = document.createElement('p'); empty.className = 'muted-text list-search-empty'; empty.textContent = 'No matches.'; list.appendChild(empty); } } else if (empty) { empty.remove(); } } function initListSearch(inputId, listId, modalId) { const input = document.getElementById(inputId); const list = document.getElementById(listId); if (!input || !list) return; let applying = false; const run = () => { applying = true; applyListSearch(list, input.value); observer.takeRecords(); applying = false; }; const observer = new MutationObserver(() => { if (!applying) run(); }); observer.observe(list, { childList: true }); input.addEventListener('input', run); input.addEventListener('keydown', (e) => { if (e.key === 'Escape' && input.value) { e.stopPropagation(); input.value = ''; run(); } }); // Clear the search each time the modal is opened. const modal = modalId && document.getElementById(modalId); if (modal) { new MutationObserver(() => { if (modal.classList.contains('active') && input.value) { input.value = ''; run(); } }).observe(modal, { attributes: true, attributeFilter: ['class'] }); } } document.addEventListener('DOMContentLoaded', () => { initListSearch('devices-search', 'devices-list-modal', 'devices-modal'); initListSearch('zones-search', 'zones-list-modal', 'zones-modal'); initListSearch('presets-search', 'presets-list', 'presets-modal'); });