feat(ui): add search filter to devices, zones and presets modals
Client-side filter that hides non-matching rows as you type, persists across list re-renders, and clears when the modal is reopened or on Esc. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a5a7fc575f
commit
3a7723c586
3 files changed
+101
No files matched your search
@@ -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');
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
@@ -72,6 +72,7 @@
|
||||
<input type="text" id="new-zone-name" placeholder="Zone name">
|
||||
<button class="btn btn-primary" id="create-zone-btn">Create</button>
|
||||
</div>
|
||||
<input type="search" id="zones-search" class="list-search-input" placeholder="Search zones…" autocomplete="off">
|
||||
<div id="zones-list-modal" class="profiles-list"></div>
|
||||
<div class="modal-actions">
|
||||
<button class="btn btn-secondary" id="zones-close-btn">Close</button>
|
||||
@@ -126,6 +127,7 @@
|
||||
<div id="devices-modal" class="modal">
|
||||
<div class="modal-content">
|
||||
<h2>Devices</h2>
|
||||
<input type="search" id="devices-search" class="list-search-input" placeholder="Search devices…" autocomplete="off">
|
||||
<div id="devices-list-modal" class="profiles-list"></div>
|
||||
<div class="modal-actions">
|
||||
<button type="button" class="btn btn-secondary" id="devices-close-btn">Close</button>
|
||||
@@ -292,6 +294,7 @@
|
||||
<button class="btn btn-primary" id="preset-add-btn">Add</button>
|
||||
<button class="btn btn-danger" id="preset-clear-device-btn">Clear Device Presets</button>
|
||||
</div>
|
||||
<input type="search" id="presets-search" class="list-search-input" placeholder="Search presets…" autocomplete="off">
|
||||
<div id="presets-list" class="profiles-list"></div>
|
||||
<div class="modal-actions">
|
||||
<button class="btn btn-secondary" id="presets-close-btn">Close</button>
|
||||
@@ -667,5 +670,6 @@
|
||||
<script src="/static/devices.js"></script>
|
||||
<script src="/static/outputs.js"></script>
|
||||
<script src="/static/sequences.js"></script>
|
||||
<script src="/static/list_search.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user