diff --git a/_dev/js/theme/components/TopMenu.js b/_dev/js/theme/components/TopMenu.js deleted file mode 100644 index afb9bdb2..00000000 --- a/_dev/js/theme/components/TopMenu.js +++ /dev/null @@ -1,30 +0,0 @@ -import $ from 'jquery'; - -export default class TopMenu { - constructor(el) { - this.$el = $(el); - } - - init() { - const self = this; - self.$el.hoverIntent({ - over: self.toggleClassSubMenu, - out: self.toggleClassSubMenu, - selector: ' > li', - timeout: 300, - }); - } - - toggleClassSubMenu() { - const $item = $(this); - let expanded = $item.attr('aria-expanded'); - - if (typeof expanded !== 'undefined') { - expanded = expanded.toLowerCase() === 'true'; - $item.toggleClass('main-menu__item--active').attr('aria-expanded', !expanded); - $('.main-menu__sub', $item) - .attr('aria-expanded', !expanded) - .attr('aria-hidden', expanded); - } - } -} diff --git a/_dev/js/theme/components/cart/block-cart.js b/_dev/js/theme/components/cart/block-cart.js deleted file mode 100644 index 7166f873..00000000 --- a/_dev/js/theme/components/cart/block-cart.js +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright since 2007 PrestaShop SA and Contributors - * PrestaShop is an International Registered Trademark & Property of PrestaShop SA - * - * NOTICE OF LICENSE - * - * This source file is subject to the Academic Free License 3.0 (AFL-3.0) - * that is bundled with this package in the file LICENSE.md. - * It is also available through the world-wide-web at this URL: - * https://opensource.org/licenses/AFL-3.0 - * If you did not receive a copy of the license and are unable to - * obtain it through the world-wide-web, please send an email - * to license@prestashop.com so we can send you a copy immediately. - * - * DISCLAIMER - * - * Do not edit or add to this file if you wish to upgrade PrestaShop to newer - * versions in the future. If you wish to customize PrestaShop for your - * needs please refer to https://devdocs.prestashop.com/ for more information. - * - * @author PrestaShop SA and Contributors - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) - */ -import prestashop from 'prestashop'; -import $ from 'jquery'; - -prestashop.blockcart = prestashop.blockcart || {}; - -prestashop.blockcart.showModal = (html) => { - function getBlockCartModal() { - return $('#blockcart-modal'); - } - - const $blockCartModal = getBlockCartModal(); - - if ($blockCartModal.length) { - $blockCartModal.hide(); - } - - $('body').append(html); - - getBlockCartModal() - .modal('show') - .on('hidden.bs.modal', (e) => { - $(e.currentTarget).remove(); - }); -}; diff --git a/_dev/js/theme/components/useTopMenu.js b/_dev/js/theme/components/useTopMenu.js new file mode 100644 index 00000000..0404509c --- /dev/null +++ b/_dev/js/theme/components/useTopMenu.js @@ -0,0 +1,46 @@ +import useEvent from './event/useEvent'; + +const useTopMenu = (selector) => { + const { on, off } = useEvent(); + const menuElement = document.querySelector(selector); + const DOM_SELECTORS = { + SUB_ELEMENTS: '.main-menu__item--top', + SUB_MENU: '.main-menu__sub', + }; + const ITEM_ACTIVE = 'main-menu__item--active'; + + + const toggleSubMenu = (e) => { + const { currentTarget } = e; + const { SUB_MENU } = DOM_SELECTORS; + const expanded = currentTarget.getAttribute('aria-expanded'); + const isExpanded = expanded && expanded.toLowerCase() === 'true'; + const subMenu = currentTarget.querySelector(SUB_MENU); + + currentTarget.classList.toggle(ITEM_ACTIVE); + + if (subMenu) { + currentTarget.setAttribute('aria-expanded', !isExpanded); + + subMenu.setAttribute('aria-hidden', isExpanded); + subMenu.setAttribute('aria-expanded', !isExpanded); + + } + } + + const init = () => { + debugger + const { SUB_ELEMENTS } = DOM_SELECTORS; + + off(menuElement, 'mouseenter', SUB_ELEMENTS, toggleSubMenu); + off(menuElement, 'mouseleave', SUB_ELEMENTS, toggleSubMenu); + on(menuElement, 'mouseenter', SUB_ELEMENTS, toggleSubMenu); + on(menuElement, 'mouseleave', SUB_ELEMENTS, toggleSubMenu); + } + + return { + init + } +} + +export default useTopMenu; diff --git a/_dev/js/theme/index.js b/_dev/js/theme/index.js index df950b45..e73daa7f 100644 --- a/_dev/js/theme/index.js +++ b/_dev/js/theme/index.js @@ -5,8 +5,6 @@ import EventEmitter from 'events'; import '@js/theme/core'; import '@js/theme/vendors/bootstrap/bootstrap-imports'; -import 'bootstrap-touchspin'; -import 'jquery-hoverintent'; import '@js/theme/components/dynamic-bootstrap-components'; import bsCustomFileInput from 'bs-custom-file-input'; import '@js/theme/components/sliders'; @@ -14,7 +12,7 @@ import '@js/theme/components/responsive'; import '@js/theme/components/customer'; import '@js/theme/components/quickview'; import '@js/theme/components/product'; -import '@js/theme/components/cart/block-cart'; +import useTopMenu from './components/useTopMenu'; /* eslint-enable */ import prestashop from 'prestashop'; @@ -26,7 +24,6 @@ for (const i in EventEmitter.prototype) { } import usePasswordPolicy from '@js/theme/components/password/usePasswordPolicy'; import Form from '@js/theme/components/form'; -import TopMenu from '@js/theme/components/TopMenu'; import PageLazyLoad from '@js/theme/components/Lazyload'; import PageLoader from '@js/theme/components/PageLoader'; import useStickyElement from '@js/theme/components/useStickyElement'; @@ -61,14 +58,14 @@ function initStickyHeader() { } $(() => { + const { init: initTopMenu } = useTopMenu('.js-main-menu'); + initStickyHeader(); accLinksTriggerActive(); Form.init(); bsCustomFileInput.init(); - const topMenu = new TopMenu('#_desktop_top_menu .js-main-menu'); usePasswordPolicy('.field-password-policy'); - - topMenu.init(); + initTopMenu(); $('.js-select-link').on('change', ({ target }) => { window.location.href = $(target).val();