- Chappelihof 39, 8863 Buttikon(SZ)

SHOP
Filter
Clear All

document.addEventListener('DOMContentLoaded', function () { /** * Normalizes a string by converting it to lowercase, * removing accents, and stripping numbers and extra spaces. * This ensures that "Blau (157)" and "blau" are treated as the same tag. * @param {string} txt The input string to normalize. * @returns {string} The normalized string. */ function normalizeLabel(txt) { if (!txt) return ''; let s = txt.replace(/\(\s*\d+\s*\)/, '').trim().replace(/\s+/g, ' '); s = s.toLocaleLowerCase('de'); s = s.normalize('NFD').replace(/[\u0300-\u036f]/g, ''); s = s.replace(/ß/g, 'ss'); return s; } /** * Finds the tags filter section and removes any duplicate list items. * This function is smart enough to be re-run whenever new items are added * to the list, ensuring that no duplicates are ever present. */ function removeDuplicateTags() { const section = document.querySelector('.wopb-filter-product_tag'); if (!section) { return; } const list = section.querySelector('.wopb-filter-check-list'); if (!list) { return; } const seenTags = new Set(); const items = Array.from(list.querySelectorAll('.wopb-filter-item')); items.forEach(item => { const input = item.querySelector('input.wopb-filter-tax-term-input'); const raw = (input?.dataset?.label || item.querySelector('label span')?.textContent || '').trim(); const normalizedTag = normalizeLabel(raw); if (seenTags.has(normalizedTag)) { item.remove(); } else { seenTags.add(normalizedTag); } }); } /** * Handles the visibility for a specific filter section. * Clicks "Show More" for categories and hides buttons for tags. */ function handleFilterSection(section) { if (section.classList.contains('wopb-filter-product_cat')) { const showMoreButton = section.querySelector('.wopb-filter-show-more'); if (showMoreButton) { showMoreButton.click(); setTimeout(() => { showMoreButton.remove(); const showLessButton = section.querySelector('.wopb-filter-show-less'); if (showLessButton) { showLessButton.remove(); } }, 500); } } else if (section.classList.contains('wopb-filter-product_tag')) { const showMoreButton = section.querySelector('.wopb-filter-show-more'); const showLessButton = section.querySelector('.wopb-filter-show-less'); if (showMoreButton) { showMoreButton.remove(); } if (showLessButton) { showLessButton.remove(); } removeDuplicateTags(); } // 🔥 FIX for mobile checkbox not showing checked when clicking on label text section.querySelectorAll('label').forEach(label => { label.addEventListener('click', function (e) { const input = this.querySelector('input[type="checkbox"]'); if (input) { // Prevent double-triggering e.preventDefault(); input.checked = !input.checked; // Trigger change event so the filter logic still works input.dispatchEvent(new Event('change', { bubbles: true })); } }); }); } // Use a MutationObserver to handle dynamically loaded content const observer = new MutationObserver((mutations) => { mutations.forEach((mutation) => { mutation.addedNodes.forEach((node) => { if (node.nodeType === 1) { if (node.classList.contains('wopb-filter-section')) { handleFilterSection(node); } else { node.querySelectorAll('.wopb-filter-section').forEach(handleFilterSection); } } }); }); }); const form = document.querySelector('form'); if (form) { observer.observe(form, { childList: true, subtree: true }); } // Initial call document.querySelectorAll('.wopb-filter-section').forEach(handleFilterSection); });






























