diff --git a/src/static/list_search.js b/src/static/list_search.js new file mode 100644 index 0000000..8a12217 --- /dev/null +++ b/src/static/list_search.js @@ -0,0 +1,80 @@ +// 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'); +}); diff --git a/src/static/style.css b/src/static/style.css index 70679be..ee867e7 100644 --- a/src/static/style.css +++ b/src/static/style.css @@ -1454,3 +1454,20 @@ body.preset-ui-run .edit-mode-only { margin-top: 0.75rem; color: #ff6b6b; } + +.list-search-input { + width: 100%; + margin-top: 1rem; + padding: 0.5rem; + background-color: #3a3a3a; + border: 1px solid #4a4a4a; + border-radius: 4px; + color: white; + box-sizing: border-box; +} +.list-search-input + .profiles-list { + margin-top: 0.5rem; +} +.profiles-row.list-search-hidden { + display: none !important; +} diff --git a/src/templates/index.html b/src/templates/index.html index 9390ab1..3009914 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -72,6 +72,7 @@ +