'; } $productsGrid.html(html).addClass('loaded'); }, error: () => $productsGrid.html('
Error loading products.
') }); } function loadChildCategoriesByIds(childIds, $container, $menuItem) { if ($container.hasClass('loaded') || !childIds) return; // Separate static boxes into "first" (data-static-order="first") and "last" (no attribute) var firstStaticBoxes = $container.find('.category-box.static[data-static-order="first"]').clone(); var lastStaticBoxes = $container.find('.category-box.static:not([data-static-order="first"])').clone(); $container.html('
No subcategories found.
'; } $container.html(html).addClass('loaded'); // NEW: If this sidebar item has a custom URL, override the
`; if (filtered.length > 0) { filtered.forEach(brand => { html += `
No brands found.
'; } $brandList.html(html).addClass('loaded'); }, error: () => $brandList.html('
Error loading brands.
') }); } function initDesktopMegaMenu() { // Sidebar hover logic $('.main-menu .menu-item.has-mega > a').on('mouseenter', function () { const $parent = $(this).closest('li'); const $firstSidebarItem = $parent.find('.mega-sidebar-menu li').first(); if ($firstSidebarItem.length) { const catId = $firstSidebarItem.data('cat-id'); const $targetContent = $parent.find('.category-content[data-cat-id="' + catId + '"]'); $('.category-content').hide(); $targetContent.show(); loadFeaturedProducts(catId, $targetContent); loadChildCategoriesByIds($firstSidebarItem.data('cat-child-ids'), $targetContent.find('.category-children'), $firstSidebarItem); } }); $('.mega-sidebar-menu li').on('mouseenter', function () { const $this = $(this); const catId = $this.data('cat-id'); const $targetContent = $('.category-content[data-cat-id="' + catId + '"]'); $this.addClass('active').siblings().removeClass('active'); $('.category-content').hide(); $targetContent.fadeIn(200); loadFeaturedProducts(catId, $targetContent); loadChildCategoriesByIds($this.data('cat-child-ids'), $targetContent.find('.category-children'), $this); }); $('.category-content').hide().first().show(); // Mega narrow: load categories on hover $('.main-menu .menu-item.has-mega-narrow').on('mouseenter', function () { const $this = $(this); const childIds = $this.data('cat-child-ids'); const $container = $this.find('.category-children'); loadChildCategoriesByIds(childIds, $container, $this); }); // Load brands on hover $('.main-menu .menu-item.has-brands').on('mouseenter', function () { loadTopBrands($(this)); }); } function initMobileMegaMenu() { $('#main-menu').removeClass('open'); $('.mega-sidebar-menu li, .category-content').removeClass('active'); // Hamburger toggle $('.mobile-menu-toggle').on('click', function () { $('#main-menu').toggleClass('open'); $(this).toggleClass('open'); setMobileMenuLock($('#main-menu').hasClass('open')); }); // Lock page scroll while the menu is open; the menu list scrolls itself const menuList = document.querySelector('#main-menu .menu-level-0'); let menuTouchY = 0; function sizeMobileMenu() { if (!menuList || !$('#main-menu').hasClass('open')) return; const vh = window.visualViewport ? window.visualViewport.height : window.innerHeight; const top = Math.max(0, menuList.getBoundingClientRect().top); menuList.style.maxHeight = Math.max(150, Math.floor(vh - top)) + 'px'; } function setMobileMenuLock(on) { if (on) { // Bring the menu bar to the top of the screen so the menu gets the full height const bar = document.querySelector('.header-menu') || document.getElementById('main-menu'); const barTop = bar.getBoundingClientRect().top; if (barTop > 0) window.scrollBy(0, barTop); } document.documentElement.classList.toggle('mobile-menu-lock', on); if (on) { sizeMobileMenu(); } else if (menuList) { menuList.style.maxHeight = ''; } } $(window).on('scroll', sizeMobileMenu); if (window.visualViewport) window.visualViewport.addEventListener('resize', sizeMobileMenu); // Older iOS Safari ignores overflow:hidden, so block touch scrolls that would move the page document.addEventListener('touchstart', function (e) { menuTouchY = e.touches[0].clientY; }, { passive: true }); document.addEventListener('touchmove', function (e) { if (!document.documentElement.classList.contains('mobile-menu-lock') || !e.cancelable) return; if (!menuList || !menuList.contains(e.target)) { e.preventDefault(); return; } const dy = e.touches[0].clientY - menuTouchY; menuTouchY = e.touches[0].clientY; const atTop = menuList.scrollTop <= 0; const atBottom = menuList.scrollTop + menuList.clientHeight >= menuList.scrollHeight - 1; if ((dy > 0 && atTop) || (dy < 0 && atBottom)) e.preventDefault(); }, { passive: false }); // Top-level toggle $('.main-menu > ul > li > a').on('click', function (e) { const $parent = $(this).closest('li'); // ✅ Skip: has-brands has its own dedicated handler below if ($parent.hasClass('has-brands')) return; if ($parent.hasClass('has-mega') || $parent.hasClass('has-mega-narrow')) { e.preventDefault(); $parent.toggleClass('open').siblings().removeClass('open'); } }); // Handle simple dropdowns (.has-dropdown) $('.main-menu > ul > li.has-dropdown > a').on('click', function (e) { e.preventDefault(); const $parent = $(this).closest('li'); $parent.toggleClass('open').siblings().removeClass('open'); }); // Inject category-content below each sidebar item $('.mega-sidebar-menu li').each(function () { const catId = $(this).data('cat-id'); const $content = $('.category-content[data-cat-id="' + catId + '"]'); if ($content.length) { $(this).after($content); } }); // Sidebar accordion toggle $('.mega-sidebar-menu li > a').on('click', function (e) { e.preventDefault(); const $li = $(this).parent(); const catId = $li.data('cat-id'); const $targetContent = $('.category-content[data-cat-id="' + catId + '"]'); $li.toggleClass('active'); $targetContent.stop(true, true).slideToggle(); if ($li.hasClass('active')) { loadFeaturedProducts(catId, $targetContent); loadChildCategoriesByIds($li.data('cat-child-ids'), $targetContent.find('.category-children'), $li); } }); // Mega narrow: load categories on tap $('.main-menu .menu-item.has-mega-narrow > a').on('click', function () { const $parent = $(this).closest('li'); const childIds = $parent.data('cat-child-ids'); const $container = $parent.find('.category-children'); loadChildCategoriesByIds(childIds, $container, $parent); }); // Load brands on tap (mobile) $('.main-menu .menu-item.has-brands > a').on('click', function (e) { e.preventDefault(); const $parent = $(this).closest('li.menu-item'); // Toggle open class on this