From 718e01a49147300ee963c37d90c5f6bbcaf04f48 Mon Sep 17 00:00:00 2001 From: Dariusz L Date: Sun, 19 Apr 2026 21:08:33 +0200 Subject: [PATCH] Improve MLFilterSwitch DOM handling Make MLFilterSwitch more robust by guarding against a missing filter button and caching the element reference before toggling active state. Use getAttribute('data-...') instead of dataset for consistent data access, and adjust child DIV indexing to account for a category header so active/inactive sections are selected correctly. Add comments and clearer variable names for maintainability. --- web/linker.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/web/linker.js b/web/linker.js index 2a3c673..3ca6f67 100644 --- a/web/linker.js +++ b/web/linker.js @@ -4090,17 +4090,22 @@ app.registerExtension({ // Global helper functions for inline onclick handlers window.MLFilterSwitch = function(filter) { - document.querySelectorAll('.ml-btn-filter').forEach(b => b.classList.remove('active')); - document.getElementById('filter-' + filter).classList.add('active'); + const filterBtn = document.getElementById('filter-' + filter); + if (!filterBtn) return; + // Update button states + document.querySelectorAll('.ml-btn-filter').forEach(b => b.classList.remove('active')); + filterBtn.classList.add('active'); + + // Filter model sections document.querySelectorAll('.ml-model-section').forEach(s => { - const hasActive = s.dataset.mlActive === 'true'; - const hasInactive = s.dataset.mlInactive === 'true'; + const hasActive = s.getAttribute('data-ml-active') === 'true'; + const hasInactive = s.getAttribute('data-ml-inactive') === 'true'; - // Get direct child divs (active and inactive sections) - const subSections = Array.from(s.children).filter(c => c.tagName === 'DIV'); - const activeSection = subSections[0]; - const inactiveSection = subSections[1]; + // Get child divs: category header, active section, inactive section + const childDivs = Array.from(s.children).filter(c => c.tagName === 'DIV'); + const activeSection = childDivs[1]; + const inactiveSection = childDivs[2]; if (filter === 'all') { s.style.display = 'block';