/** * Scrollable device/group list with a fixed add row (dropdown + button) below. * Used by group and zone edit modals. */ function prepareZoneDevicesPanel(containerEl) { if (!containerEl) return null; let listEl = containerEl.querySelector('.zone-devices-list'); let addSlot = containerEl.querySelector('.zone-devices-add-slot'); if (!listEl) { containerEl.innerHTML = ''; containerEl.classList.add('zone-devices-panel'); listEl = document.createElement('div'); listEl.className = 'zone-devices-list'; addSlot = document.createElement('div'); addSlot.className = 'zone-devices-add-slot'; containerEl.appendChild(listEl); containerEl.appendChild(addSlot); } else { listEl.innerHTML = ''; addSlot.innerHTML = ''; } return { listEl, addSlot }; } /** * Search field + scrollable filtered list for picking an item to add. */ function createSearchableAddPicker({ entries, excludeIds, labelFor, searchTextFor, onPick, actionFor, placeholder = 'Search…', emptyMessage = 'No matches.', noItemsMessage = 'Nothing to add.', }) { const wrap = document.createElement('div'); wrap.className = 'zone-device-add-picker'; const excluded = excludeIds || new Set(); const available = (entries || []).filter(([id]) => !excluded.has(id)); if (!available.length) { const empty = document.createElement('span'); empty.className = 'muted-text'; empty.textContent = noItemsMessage; wrap.appendChild(empty); return wrap; } const search = document.createElement('input'); search.type = 'search'; search.className = 'zone-device-add-search'; search.placeholder = placeholder; search.setAttribute('aria-label', placeholder); const results = document.createElement('div'); results.className = 'zone-device-add-results'; results.setAttribute('role', 'listbox'); const filterAvailable = (query) => { const q = String(query || '').trim().toLowerCase(); return available.filter(([id, item]) => { if (!q) return true; const text = searchTextFor(id, item); return String(text).toLowerCase().includes(q); }); }; const pickEntry = (id, item) => { onPick(id, item); search.value = ''; renderResults(''); }; const renderResults = (query) => { results.innerHTML = ''; const filtered = filterAvailable(query); if (!filtered.length) { const none = document.createElement('div'); none.className = 'zone-device-add-results-empty muted-text'; none.textContent = emptyMessage; results.appendChild(none); return; } filtered.forEach(([id, item]) => { const btn = document.createElement('button'); btn.type = 'button'; btn.className = 'zone-device-add-result'; btn.setAttribute('role', 'option'); btn.textContent = labelFor(id, item); btn.addEventListener('click', () => pickEntry(id, item)); if (!actionFor) { results.appendChild(btn); return; } const row = document.createElement('div'); row.className = 'zone-device-add-row'; row.setAttribute('role', 'none'); row.appendChild(btn); row.appendChild(actionFor(id, item)); results.appendChild(row); }); }; search.addEventListener('input', () => renderResults(search.value)); search.addEventListener('focus', () => renderResults(search.value)); search.addEventListener('keydown', (event) => { if (event.key !== 'Enter') return; const filtered = filterAvailable(search.value); if (filtered.length === 1) { event.preventDefault(); pickEntry(filtered[0][0], filtered[0][1]); } }); renderResults(''); wrap.appendChild(search); wrap.appendChild(results); return wrap; } if (typeof window !== 'undefined') { window.prepareZoneDevicesPanel = prepareZoneDevicesPanel; window.createSearchableAddPicker = createSearchableAddPicker; }