'; } $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'); }); // 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