✏️ 正在编辑: menu.js
路径:
/home/h359620/public_html/wp-content/themes/datis/inc/elementor/widgets/datis_menu/js/menu.js
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
/* ========================= * MOBILE MENU + TABS * =======================*/ (function () { /** * این تابع روی هر ویجت منو بهصورت جداگانه اعمال میشود * @param {HTMLElement} rootEl عنصر nav.datis-menu-mobile */ function initOneMobileMenu(rootEl) { if (!rootEl) return; // wrapper خود ویجت (برای پیدا کردن burger همان ویجت) const widgetWrapper = rootEl.closest('.elementor-widget') || document; const burger = widgetWrapper.querySelector('.datis-burger'); const overlay = rootEl.querySelector('.datis-overlay'); const closeBtn = rootEl.querySelector('.datis-close'); const lists = rootEl.querySelectorAll('.menu-list'); if (!burger) return; // اگر دکمهای نداریم، کاری نکن function toggleDrawer(open) { const isOpen = (open !== undefined) ? open : !rootEl.classList.contains('is-open'); rootEl.classList.toggle('is-open', isOpen); burger.setAttribute('aria-expanded', isOpen ? 'true' : 'false'); document.documentElement.classList.toggle('datis-no-scroll', isOpen); } // رویدادهای باز/بستن burger.addEventListener('click', function () { toggleDrawer(); }); overlay && overlay.addEventListener('click', function () { toggleDrawer(false); }); closeBtn && closeBtn.addEventListener('click', function () { toggleDrawer(false); }); // آکاردئون زیرمنو / مگامنو برای هر لیست داخل همین منو lists.forEach(function (list) { if (!list) return; list.addEventListener('click', function (e) { const trigger = e.target.closest('.menu-mega-trigger, .menu-item-has-children > a'); if (!trigger) return; const li = trigger.closest('li'); if (!li) return; const expanded = li.getAttribute('aria-expanded') === 'true'; // بستن بقیه آیتمهای باز همین لیست list.querySelectorAll('li[aria-expanded="true"]').forEach(function (x) { if (x !== li) x.setAttribute('aria-expanded', 'false'); }); li.setAttribute('aria-expanded', expanded ? 'false' : 'true'); if (li.classList.contains('menu-item-has-children') || li.classList.contains('datis-mega-menu')) { e.preventDefault(); } }); // تنظیم ARIA اولیه list.querySelectorAll('li.menu-item-has-children, li.datis-mega-menu').forEach(function (li) { if (!li.hasAttribute('aria-expanded')) li.setAttribute('aria-expanded', 'false'); li.setAttribute('aria-haspopup', 'true'); }); }); // تبهای موبایل (اگر وجود داشته باشند) const tabs = rootEl.querySelectorAll('.datis-menu-mobile-tab'); const panels = rootEl.querySelectorAll('.datis-menu-mobile-panel'); if (tabs.length && panels.length) { tabs.forEach(function (tab) { tab.addEventListener('click', function () { const target = tab.getAttribute('data-tab'); // فعال/غیرفعال کردن تبها tabs.forEach(function (t) { const isActive = (t === tab); t.classList.toggle('is-active', isActive); t.setAttribute('aria-selected', isActive ? 'true' : 'false'); }); // سوئیچ پنلها panels.forEach(function (panel) { const match = panel.getAttribute('data-panel') === target; panel.classList.toggle('is-active', match); }); // بستن آکاردئونها در پنلهای غیرفعال panels.forEach(function (panel) { if (!panel.classList.contains('is-active')) { panel.querySelectorAll('li[aria-expanded="true"]').forEach(function (li) { li.setAttribute('aria-expanded', 'false'); }); } }); }); }); } // ESC → بستن document.addEventListener('keydown', function (e) { if (e.key === 'Escape' && rootEl.classList.contains('is-open')) { toggleDrawer(false); } }); // وقتی ویوپورت رفت روی دسکتاپ، منو رو ببند const mq = window.matchMedia('(min-width: 992px)'); mq.addEventListener('change', function (ev) { if (ev.matches) toggleDrawer(false); }); } function initAllMenusInDocument() { document.querySelectorAll('.datis-menu-mobile').forEach(function (rootEl) { initOneMobileMenu(rootEl); }); } // لود اولیه صفحه if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initAllMenusInDocument); } else { initAllMenusInDocument(); } // وقتی المنتور ویجت رو Ajax میاره (مثلاً در ادیتور) if (window.elementorFrontend && window.elementorFrontend.hooks) { window.elementorFrontend.hooks.addAction( 'frontend/element_ready/datis_menu.default', function ($scope) { try { const rootEl = $scope[0].querySelector('.datis-menu-mobile'); if (rootEl) initOneMobileMenu(rootEl); } catch (e) { } } ); } })(); /* ========================= * MEGA MENU POSITION (DESKTOP) * =======================*/ (function () { const ROOT = document.querySelector('.e-con-inner'); // والد مرجع const items = document.querySelectorAll('.datis-menu-desktop .menu-list > li.datis-mega-menu'); if (!ROOT || !items.length) return; const isRTL = getComputedStyle(document.documentElement).direction === 'rtl'; function measureWidthShown(el) { const prevDisplay = el.style.display; const prevVis = el.style.visibility; const needTemp = getComputedStyle(el).display === 'none'; if (needTemp) { el.style.visibility = 'hidden'; el.style.display = 'block'; } const w = el.getBoundingClientRect().width; if (needTemp) { el.style.display = prevDisplay; el.style.visibility = prevVis; } return w; } function place(mm, li) { const pr = ROOT.getBoundingClientRect(); const ir = li.getBoundingClientRect(); const vw = window.innerWidth; mm.style.top = (pr.bottom + 85) + 'px'; const centerX = (ir.left + ir.right) / 2; const mmW = measureWidthShown(mm); if (!isRTL) { let desiredLeft = centerX - mmW / 2; const minLeft = pr.left; const maxLeft = pr.right - mmW; const finalLeft = Math.max(minLeft, Math.min(desiredLeft, maxLeft)); mm.style.left = finalLeft + 'px'; mm.style.right = ''; } else { let desiredRight = (vw - centerX) - mmW / 2; const minRight = vw - pr.right; const maxRight = vw - pr.left - mmW; const finalRight = Math.max(minRight, Math.min(desiredRight, maxRight)); mm.style.right = (finalRight - 20) + 'px'; mm.style.left = ''; } } function bind(li) { const mm = li.querySelector(':scope > .datis-megamenu-wrapper'); if (!mm) return; const onEnter = function () { place(mm, li); }; li.addEventListener('mouseenter', onEnter); window.addEventListener('resize', onEnter, { passive: true }); } items.forEach(bind); })(); (function () { const ROOT = document.querySelector('.e-con-inner'); // والد مرجع const items = document.querySelectorAll('.datis-menu-desktop .menu-list > li.datis-mega-menu'); if (!ROOT || !items.length) return; const isRTL = getComputedStyle(document.documentElement).direction === 'rtl'; function measureWidthShown(el) { // اگر hidden است، موقتاً نمایش بده تا اندازهگیری کنیم const prevDisplay = el.style.display; const prevVis = el.style.visibility; const needTemp = getComputedStyle(el).display === 'none'; if (needTemp) { el.style.visibility = 'hidden'; el.style.display = 'block'; } const w = el.getBoundingClientRect().width; if (needTemp) { el.style.display = prevDisplay; el.style.visibility = prevVis; } return w; } function place(mm, li) { const pr = ROOT.getBoundingClientRect(); // کانتینر مرجع const ir = li.getBoundingClientRect(); // خود آیتم والد const vw = window.innerWidth; /* === TOP: دقیقاً زیر آیتم === */ const gap = 8; // فاصله دلخواه بین آیتم و مگامنو mm.style.top = 40 + 'px'; /* === HORIZONTAL === */ const centerX = (ir.left + ir.right) / 2; const mmW = measureWidthShown(mm); if (isRTL) { let desiredLeft = centerX - mmW / 2; const minLeft = pr.left; const maxLeft = pr.right - mmW; mm.style.left = 0 + 'px'; mm.style.right = ''; } else { let desiredRight = (vw - centerX) - mmW / 2; const minRight = vw - pr.right; const maxRight = vw - pr.left - mmW; mm.style.right = Math.max(minRight, Math.min(desiredRight, maxRight)) + 'px'; mm.style.left = ''; } } function bind(li) { const mm = li.querySelector(':scope > .datis-megamenu-wrapper'); if (!mm) return; const onEnter = () => place(mm, li); li.addEventListener('mouseenter', onEnter); window.addEventListener('resize', onEnter, { passive: true }); } items.forEach(bind); })();
💾 保存文件
← 返回文件管理器