(function ($) { // ---- kis debounce, hogy ne fusson 8000x ---- function debounce(fn, wait) { var t; return function () { clearTimeout(t); var args = arguments; t = setTimeout(function () { fn.apply(null, args); }, wait); }; } function initLayeredNavDropdowns(root) { var $root = root ? $(root) : $(document); // Woo layered nav dropdown formok $root.find('form.woocommerce-widget-layered-nav-dropdown').each(function () { var $form = $(this); var $select = $form.find('select.woocommerce-widget-layered-nav-dropdown').first(); if (!$select.length) return; // change -> submit (Woo/YITH ezt normálisan AJAX-ként kezeli, ha a plugin úgy van) $select.off('change.taFix').on('change.taFix', function () { var val = $(this).val() || ''; var $hidden = $form.find('input[name^="filter_"]').first(); if ($hidden.length) $hidden.val(val); // Ne natív submit legyen (ami reloadol), hanem a Woo/YITH handler kapja el: $form.triggerHandler('submit'); }); // selectWoo preferált, select2 fallback if ($.fn.selectWoo && !$select.hasClass('select2-hidden-accessible')) { var placeholder = $select.find('option:first').text() || ''; $select.selectWoo({ placeholder: placeholder, width: '100%', minimumResultsForSearch: Infinity, allowClear: true }); } else if ($.fn.select2 && !$select.hasClass('select2-hidden-accessible')) { $select.select2({ width: '100%', minimumResultsForSearch: Infinity }); } }); } function tryReinitYithPresetUI() { // Ha placeholderben ragadt, akkor a YITH UI init nem futott le if (!$('.ta-yith-filters .yith-wcan-filter.filter-placeholder').length) return; // 1) próbáljuk a YITH saját initjét meghívni, ha létezik var candidates = [ window.yith_wcan, window.YITH_WCAN, window.yith_wcan_frontend, window.yithWcanFrontend ].filter(Boolean); for (var i = 0; i < candidates.length; i++) { var obj = candidates[i]; if (obj && typeof obj.init === 'function') { try { obj.init(); } catch (e) {} } if (obj && obj.frontend && typeof obj.frontend.init === 'function') { try { obj.frontend.init(); } catch (e) {} } } // 2) „soft” eventek (hátha a te verziód ezekre figyel) $(document.body).trigger('yith-wcan-init'); $(document.body).trigger('yith-wcan-ajax-filtered'); $(document.body).trigger('yith-wcan-ajax-success'); $(document.body).trigger('yith-wcan-ajax-reset'); } var fullReinit = debounce(function (mutRoot) { initLayeredNavDropdowns(mutRoot || document); tryReinitYithPresetUI(); }, 120); // első betöltés $(function () { fullReinit(document); // Figyeljük a filter blokkot és a terméklistát var target = document.querySelector('.ta-yith-filters') || document.body; var obs = new MutationObserver(function (mutations) { // ha bármi DOM csere történik a filter környékén, futtatjuk fullReinit(target); }); obs.observe(target, { childList: true, subtree: true }); // plusz: ha a terméklista frissül máshol var products = document.querySelector('.products') || document.querySelector('.shop-container') || null; if (products) { var obs2 = new MutationObserver(function () { fullReinit(document); }); obs2.observe(products, { childList: true, subtree: true }); } }); })(jQuery);