123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670 |
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * @deprecated
- * @see lib/web/mage/menu.js
- */
- define([
- 'jquery',
- 'matchMedia',
- 'mage/template',
- 'mage/dropdowns',
- 'mage/terms'
- ], function ($, mediaCheck, mageTemplate) {
- 'use strict';
- $.widget('mage.navigationMenu', {
- options: {
- itemsContainer: '> ul',
- topLevel: 'li.level0',
- topLevelSubmenu: '> .submenu',
- topLevelHoverClass: 'hover',
- expandedTopLevel: '.more',
- hoverInTimeout: 300,
- hoverOutTimeout: 500,
- submenuAnimationSpeed: 200,
- collapsable: true,
- collapsableDropdownTemplate:
- '<script type="text/x-magento-template">' +
- '<li class="level0 level-top more parent">' +
- '<div class="submenu">' +
- '<ul><%= elems %></ul>' +
- '</div>' +
- '</li>' +
- '</script>'
- },
- /** @inheritdoc */
- _create: function () {
- this.itemsContainer = $(this.options.itemsContainer, this.element);
- this.topLevel = $(this.options.topLevel, this.element);
- this.topLevelSubmenu = $(this.options.topLevelSubmenu, this.topLevel);
- this._bind();
- },
- /**
- * @private
- */
- _init: function () {
- if (this.options.collapsable) {
- setTimeout($.proxy(function () {
- this._checkToCollapseOrExpand();
- }, this), 100);
- }
- },
- /**
- * @private
- */
- _bind: function () {
- this._on({
- /**
- * @param {jQuery.Event} e
- */
- 'mouseenter > ul > li.level0': function (e) {
- if (!this.entered) { // fix IE bug with 'mouseenter' event
- this.timeoutId && clearTimeout(this.timeoutId);
- this.timeoutId = setTimeout($.proxy(function () {
- this._openSubmenu(e);
- }, this), this.options.hoverInTimeout);
- this.entered = true;
- }
- },
- /**
- * @param {jQuery.Event} e
- */
- 'mouseleave > ul > li.level0': function (e) {
- this.entered = null;
- this.timeoutId && clearTimeout(this.timeoutId);
- this.timeoutId = setTimeout($.proxy(function () {
- this._closeSubmenu(e.currentTarget);
- }, this), this.options.hoverOutTimeout);
- },
- /**
- * @param {jQuert.Event} e
- */
- 'click': function (e) {
- e.stopPropagation();
- }
- });
- $(document)
- .on('click.hideMenu', $.proxy(function () {
- var isOpened = this.topLevel.filter(function () {
- return $(this).data('opened');
- });
- if (isOpened) {
- this._closeSubmenu(null, false);
- }
- }, this));
- $(window)
- .on('resize', $.proxy(function () {
- this.timeoutOnResize && clearTimeout(this.timeoutOnResize);
- this.timeoutOnResize = setTimeout($.proxy(function () {
- if (this.options.collapsable) {
- if ($(this.options.expandedTopLevel, this.element).length) {
- this._expandMenu();
- }
- this._checkToCollapseOrExpand();
- }
- }, this), 300);
- }, this));
- },
- /**
- * @param {jQuery.Event} e
- * @private
- */
- _openSubmenu: function (e) {
- var menuItem = e.currentTarget;
- if (!$(menuItem).data('opened')) {
- this._closeSubmenu(menuItem, true, true);
- $(this.options.topLevelSubmenu, menuItem)
- .slideDown(this.options.submenuAnimationSpeed, $.proxy(function () {
- $(menuItem).addClass(this.options.topLevelHoverClass);
- $(menuItem).data('opened', true);
- }, this));
- } else if ($(e.target).closest(this.options.topLevel)) {
- $(e.target)
- .addClass(this.options.topLevelHoverClass)
- .siblings(this.options.topLevel)
- .removeClass(this.options.topLevelHoverClass);
- }
- },
- /**
- * @param {*} menuItem
- * @param {*} excludeCurrent
- * @param {*} fast
- * @private
- */
- _closeSubmenu: function (menuItem, excludeCurrent, fast) {
- var topLevel = $(this.options.topLevel, this.element),
- activeSubmenu = $(this.options.topLevelSubmenu, menuItem || null);
- $(this.options.topLevelSubmenu, topLevel)
- .filter(function () {
- return excludeCurrent ? $(this).not(activeSubmenu) : true;
- })
- .slideUp(fast ? 0 : this.options.submenuAnimationSpeed);
- topLevel
- .removeClass(this.options.topLevelHoverClass)
- .data('opened', false);
- },
- /**
- * @private
- */
- _checkToCollapseOrExpand: function () {
- var navWidth, totalWidth, startCollapseIndex;
- if ($('html').hasClass('lt-640') || $('html').hasClass('w-640')) {
- return;
- }
- navWidth = this.itemsContainer.width();
- totalWidth = 0;
- startCollapseIndex = 0;
- $.each($(this.options.topLevel, this.element), function (index, item) {
- totalWidth += $(item).outerWidth(true);
- if (totalWidth > navWidth && !startCollapseIndex) {
- startCollapseIndex = index - 2;
- }
- });
- this[startCollapseIndex ? '_collapseMenu' : '_expandMenu'](startCollapseIndex);
- },
- /**
- * @param {*} startCollapseIndex
- * @private
- */
- _collapseMenu: function (startCollapseIndex) {
- this.elemsToCollapse = this.topLevel.filter(function (index) {
- return index > startCollapseIndex;
- });
- this.elemsToCollapseClone = $('<div></div>').append(this.elemsToCollapse.clone()).html();
- this.collapsableDropdown = $(
- mageTemplate(
- this.options.collapsableDropdownTemplate,
- {
- elems: this.elemsToCollapseClone
- }
- )
- );
- this.itemsContainer.append(this.collapsableDropdown);
- this.elemsToCollapse.detach();
- },
- /**
- * @private
- */
- _expandMenu: function () {
- this.elemsToCollapse && this.elemsToCollapse.appendTo(this.itemsContainer);
- this.collapsableDropdown && this.collapsableDropdown.remove();
- },
- /**
- * @private
- */
- _destroy: function () {
- this._expandMenu();
- }
- });
- /*
- * Provides "Continium" effect for submenu
- * */
- $.widget('mage.navigationMenu', $.mage.navigationMenu, {
- options: {
- parentLevel: '> ul > li.level0',
- submenuAnimationSpeed: 150,
- submenuContiniumEffect: false
- },
- /**
- * @private
- */
- _init: function () {
- this._super();
- this._applySubmenuStyles();
- },
- /**
- * @private
- */
- _applySubmenuStyles: function () {
- $(this.options.topLevelSubmenu, $(this.options.topLevel, this.element))
- .removeAttr('style');
- $(this.options.topLevelSubmenu, $(this.options.parentLevel, this.element))
- .css({
- display: 'block',
- height: 0,
- overflow: 'hidden'
- });
- },
- /**
- * @param {jQuery.Event} e
- * @private
- */
- _openSubmenu: function (e) {
- var menuItem = e.currentTarget,
- submenu = $(this.options.topLevelSubmenu, menuItem),
- openedItems = $(this.options.topLevel, this.element).filter(function () {
- return $(this).data('opened');
- });
- if (submenu.length) {
- this.heightToAnimate = $(this.options.itemsContainer, submenu).outerHeight(true);
- if (openedItems.length) {
- this._closeSubmenu(menuItem, true, this.heightToAnimate, $.proxy(function () {
- submenu.css({
- height: 'auto'
- });
- $(menuItem)
- .addClass(this.options.topLevelHoverClass);
- }, this), e);
- } else {
- submenu.animate({
- height: this.heightToAnimate
- }, this.options.submenuAnimationSpeed, $.proxy(function () {
- $(menuItem)
- .addClass(this.options.topLevelHoverClass);
- }, this));
- }
- $(menuItem)
- .data('opened', true);
- } else {
- this._closeSubmenu(menuItem);
- }
- },
- /**
- * @param {*} menuItem
- * @param {*} excludeCurrent
- * @param {*} heightToAnimate
- * @param {Function} callback
- * @private
- */
- _closeSubmenu: function (menuItem, excludeCurrent, heightToAnimate, callback) {
- var topLevel = $(this.options.topLevel, this.itemsContainer),
- prevOpenedItem, prevOpenedSubmenu;
- if (!excludeCurrent) {
- $(this.options.topLevelSubmenu, $(this.options.parentLevel, this.element))
- .animate({
- height: 0
- });
- topLevel
- .data('opened', false)
- .removeClass(this.options.topLevelHoverClass);
- } else {
- prevOpenedItem = topLevel.filter(function () {
- return $(this).data('opened');
- });
- prevOpenedSubmenu = $(this.options.topLevelSubmenu, prevOpenedItem);
- prevOpenedSubmenu.animate({
- height: heightToAnimate
- }, this.options.submenuAnimationSpeed, 'linear', function () {
- $(this).css({
- height: 0
- });
- callback && callback();
- });
- prevOpenedItem
- .data('opened', false)
- .removeClass(this.options.topLevelHoverClass);
- }
- },
- /**
- * @private
- */
- _collapseMenu: function () {
- this._superApply(arguments);
- this._applySubmenuStyles();
- }
- });
- // Responsive menu
- $.widget('mage.navigationMenu', $.mage.navigationMenu, {
- options: {
- responsive: false,
- origNavPlaceholder: '.page-header',
- mainContainer: 'body',
- pageWrapper: '.page-wrapper',
- openedMenuClass: 'opened',
- toggleActionPlaceholder: '.block-search',
- itemWithSubmenu: 'li.parent',
- titleWithSubmenu: 'li.parent > a',
- submenu: 'li.parent > .submenu',
- toggleActionTemplate:
- '<script type="text/x-magento-template">' +
- '<span data-action="toggle-nav" class="action toggle nav">Toggle Nav</span>' +
- '</script>',
- submenuActionsTemplate:
- '<script type="text/x-magento-template">' +
- '<li class="action all">' +
- '<a href="<%= categoryURL %>"><span>All <%= category %></span></a>' +
- '</li>' +
- '</script>',
- navigationSectionsWrapperTemplate:
- '<script type="text/x-magento-template">' +
- '<dl class="navigation-tabs" data-sections="tabs">' +
- '</dl>' +
- '</script>',
- navigationItemWrapperTemplate:
- '<script type="text/x-magento-template">' +
- '<dt class="item title <% if (active) { %>active<% } %>" data-section="title">' +
- '<a class="switch" data-toggle="switch" href="#TODO"><%= title %></a>' +
- '</dt>' +
- '<dd class="item content <% if (active) { %>active<%}%>" data-section="content">' +
- '</dd>' +
- '</script>'
- },
- /**
- * @private
- */
- _init: function () {
- this._super();
- this.mainContainer = $(this.options.mainContainer);
- this.pageWrapper = $(this.options.pageWrapper);
- this.toggleAction = $(mageTemplate(this.options.toggleActionTemplate, {}));
- if (this.options.responsive) {
- mediaCheck({
- media: '(min-width: 768px)',
- entry: $.proxy(function () {
- this._toggleDesktopMode();
- }, this),
- exit: $.proxy(function () {
- this._toggleMobileMode();
- }, this)
- });
- }
- },
- /**
- * @private
- */
- _bind: function () {
- this._super();
- this._bindDocumentEvents();
- },
- /**
- * @private
- */
- _bindDocumentEvents: function () {
- if (!this.eventsBound) {
- $(document)
- .on('click.toggleMenu', '.action.toggle.nav', $.proxy(function (e) {
- if ($(this.element).data('opened')) {
- this._hideMenu();
- } else {
- this._showMenu();
- }
- e.stopPropagation();
- this.mobileNav.scrollTop(0);
- this._fixedBackLink();
- }, this))
- .on('click.hideMenu', this.options.pageWrapper, $.proxy(function () {
- if ($(this.element).data('opened')) {
- this._hideMenu();
- this.mobileNav.scrollTop(0);
- this._fixedBackLink();
- }
- }, this))
- .on('click.showSubmenu', this.options.titleWithSubmenu, $.proxy(function (e) {
- this._showSubmenu(e);
- e.preventDefault();
- this.mobileNav.scrollTop(0);
- this._fixedBackLink();
- }, this))
- .on('click.hideSubmenu', '.action.back', $.proxy(function (e) {
- this._hideSubmenu(e);
- this.mobileNav.scrollTop(0);
- this._fixedBackLink();
- }, this));
- this.eventsBound = true;
- }
- },
- /**
- * @private
- */
- _showMenu: function () {
- $(this.element).data('opened', true);
- this.mainContainer.add('html').addClass(this.options.openedMenuClass);
- },
- /**
- * @private
- */
- _hideMenu: function () {
- $(this.element).data('opened', false);
- this.mainContainer.add('html').removeClass(this.options.openedMenuClass);
- },
- /**
- * @param {jQuery.Event} e
- * @private
- */
- _showSubmenu: function (e) {
- var submenu;
- $(e.currentTarget).addClass('action back');
- submenu = $(e.currentTarget).siblings('.submenu');
- submenu.addClass('opened');
- },
- /**
- * @param {jQuery.Event} e
- * @private
- */
- _hideSubmenu: function (e) {
- var submenuSelector = '.submenu',
- submenu = $(e.currentTarget).next(submenuSelector);
- $(e.currentTarget).removeClass('action back');
- submenu.removeClass('opened');
- },
- /**
- * @private
- */
- _renderSubmenuActions: function () {
- $.each(
- $(this.options.itemWithSubmenu),
- $.proxy(function (index, item) {
- var actions = $(mageTemplate(
- this.options.submenuActionsTemplate,
- {
- category: $('> a > span', item).text(),
- categoryURL: $('> a', item).attr('href')
- }
- )),
- submenu = $('> .submenu', item),
- items = $('> ul', submenu);
- items.prepend(actions);
- }, this)
- );
- },
- /**
- * @private
- */
- _toggleMobileMode: function () {
- this._expandMenu();
- $(this.options.topLevelSubmenu, $(this.options.topLevel, this.element))
- .removeAttr('style');
- this.toggleAction.insertBefore(this.options.toggleActionPlaceholder);
- this.mobileNav = $(this.element).detach().clone();
- this.mainContainer.prepend(this.mobileNav);
- this.mobileNav.find('> ul').addClass('nav');
- this._insertExtraItems();
- this._wrapItemsInSections();
- this.mobileNav.scroll($.proxy(function () {
- this._fixedBackLink();
- }, this));
- this._renderSubmenuActions();
- this._bindDocumentEvents();
- },
- /**
- * @private
- */
- _toggleDesktopMode: function () {
- this.mobileNav && this.mobileNav.remove();
- this.toggleAction.detach();
- $(this.element).insertAfter(this.options.origNavPlaceholder);
- $(document)
- .off('click.toggleMenu', '.action.toggle.nav')
- .off('click.hideMenu', this.options.pageWrapper)
- .off('click.showSubmenu', this.options.titleWithSubmenu)
- .off('click.hideSubmenu', '.action.back');
- this.eventsBound = false;
- this._applySubmenuStyles();
- },
- /**
- * @private
- */
- _insertExtraItems: function () {
- var settings, footerSettings, account;
- if ($('.header.panel .switcher').length) {
- settings = $('.header.panel .switcher')
- .clone()
- .addClass('settings');
- this.mobileNav.prepend(settings);
- }
- if ($('.footer .switcher').length) {
- footerSettings = $('.footer .switcher')
- .clone()
- .addClass('settings');
- this.mobileNav.prepend(footerSettings);
- }
- if ($('.header.panel .header.links li').length) {
- account = $('.header.panel > .header.links')
- .clone()
- .addClass('account');
- this.mobileNav.prepend(account);
- }
- },
- /**
- * @private
- */
- _wrapItemsInSections: function () {
- var account = $('> .account', this.mobileNav),
- settings = $('> .settings', this.mobileNav),
- nav = $('> .nav', this.mobileNav),
- navigationSectionsWrapper = $(mageTemplate(this.options.navigationSectionsWrapperTemplate, {})),
- navigationItemWrapper;
- this.mobileNav.append(navigationSectionsWrapper);
- if (nav.length) {
- navigationItemWrapper = $(mageTemplate(this.options.navigationItemWrapperTemplate, {
- title: 'Menu'
- }));
- navigationSectionsWrapper.append(navigationItemWrapper);
- navigationItemWrapper.eq(1).append(nav);
- }
- if (account.length) {
- navigationItemWrapper = $(mageTemplate(this.options.navigationItemWrapperTemplate, {
- title: 'Account'
- }));
- navigationSectionsWrapper.append(navigationItemWrapper);
- navigationItemWrapper.eq(1).append(account);
- }
- if (settings.length) {
- navigationItemWrapper = $(
- mageTemplate(this.options.navigationItemWrapperTemplate, {
- title: 'Settings'
- })
- );
- navigationSectionsWrapper.append(navigationItemWrapper);
- navigationItemWrapper.eq(1).append(settings);
- }
- navigationSectionsWrapper.addClass(
- 'navigation-tabs-' + navigationSectionsWrapper.find('[data-section="title"]').length
- );
- navigationSectionsWrapper.terms();
- },
- /**
- * @private
- */
- _fixedBackLink: function () {
- var linksBack = this.mobileNav.find('.submenu .action.back'),
- linkBack = this.mobileNav.find('.submenu.opened > ul > .action.back').last(),
- subMenu, navOffset, linkBackHeight;
- linksBack.removeClass('fixed');
- if (linkBack.length) {
- subMenu = linkBack.parent();
- navOffset = this.mobileNav.find('.nav').position().top;
- linkBackHeight = linkBack.height();
- if (navOffset <= 0) {
- linkBack.addClass('fixed');
- subMenu.css({
- paddingTop: linkBackHeight
- });
- } else {
- linkBack.removeClass('fixed');
- subMenu.css({
- paddingTop: 0
- });
- }
- }
- }
- });
- return $.mage.navigationMenu;
- });
|