init
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
define([
|
||||
'uiComponent',
|
||||
'ko'
|
||||
], function (Component, ko) {
|
||||
'use strict';
|
||||
|
||||
return Component.extend({
|
||||
defaults: {
|
||||
rawMenuData: []
|
||||
},
|
||||
|
||||
initialize: function () {
|
||||
this._super();
|
||||
|
||||
this.rawMenuData = this.rawMenuData || [];
|
||||
this.menuData = ko.observableArray(this.rawMenuData);
|
||||
|
||||
var firstId = this.rawMenuData.length ? this.rawMenuData[0].id : null;
|
||||
this.currentSecondaryId = ko.observable(firstId);
|
||||
|
||||
this.activeSecondaryCategory = ko.computed(function () {
|
||||
var currentId = this.currentSecondaryId(),
|
||||
items = this.menuData(),
|
||||
index;
|
||||
|
||||
for (index = 0; index < items.length; index++) {
|
||||
if (items[index].id === currentId) {
|
||||
return items[index];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}, this);
|
||||
},
|
||||
|
||||
switchSecondaryCategory: function (category, event) {
|
||||
var menuContainer,
|
||||
menuScrollTop,
|
||||
navigationContainer,
|
||||
navigationScrollTop,
|
||||
pageScrollTop,
|
||||
shouldRestoreScroll = event && event.type === 'click' &&
|
||||
window.matchMedia('(max-width: 992px)').matches;
|
||||
|
||||
if (shouldRestoreScroll) {
|
||||
menuContainer = event.currentTarget.closest('.esun-megamenu-inner');
|
||||
menuScrollTop = menuContainer ? menuContainer.scrollTop : 0;
|
||||
navigationContainer = event.currentTarget.closest('.nav-sections');
|
||||
navigationScrollTop = navigationContainer ? navigationContainer.scrollTop : 0;
|
||||
pageScrollTop = window.pageYOffset;
|
||||
}
|
||||
|
||||
if (category && typeof category.id !== 'undefined') {
|
||||
this.currentSecondaryId(category.id);
|
||||
}
|
||||
|
||||
if (shouldRestoreScroll) {
|
||||
window.requestAnimationFrame(function restoreScroll() {
|
||||
if (menuContainer) {
|
||||
menuContainer.scrollTop = menuScrollTop;
|
||||
}
|
||||
|
||||
if (navigationContainer) {
|
||||
navigationContainer.scrollTop = navigationScrollTop;
|
||||
}
|
||||
|
||||
window.scrollTo(0, pageScrollTop);
|
||||
|
||||
window.requestAnimationFrame(function () {
|
||||
if (menuContainer) {
|
||||
menuContainer.scrollTop = menuScrollTop;
|
||||
}
|
||||
|
||||
if (navigationContainer) {
|
||||
navigationContainer.scrollTop = navigationScrollTop;
|
||||
}
|
||||
|
||||
window.scrollTo(0, pageScrollTop);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,113 @@
|
||||
define([
|
||||
'jquery'
|
||||
], function ($) {
|
||||
'use strict';
|
||||
|
||||
return function (config, element) {
|
||||
var $navigation = $(element),
|
||||
mobileQuery = window.matchMedia(config.mediaBreakpoint || '(max-width: 992px)'),
|
||||
triggerSelector = '.esun-megamenu-trigger > .level-top';
|
||||
|
||||
function resetMenus() {
|
||||
$navigation
|
||||
.find('.esun-megamenu-trigger')
|
||||
.removeClass('is-open')
|
||||
.find('> .esun-megamenu-wrapper')
|
||||
.css('display', '')
|
||||
.end()
|
||||
.find('> .level-top')
|
||||
.removeClass('ui-state-active')
|
||||
.attr('aria-expanded', 'false')
|
||||
.end()
|
||||
.find('> .esun-megamenu-toggle')
|
||||
.attr('aria-expanded', 'false');
|
||||
}
|
||||
|
||||
function handleNavigationClick(event) {
|
||||
var $target = $(event.target),
|
||||
$item = $target.closest('.esun-megamenu-trigger'),
|
||||
$contentLink = $target.closest('a.esun-product-link, a.esun-view-all'),
|
||||
$trigger,
|
||||
$linkedToggle,
|
||||
willOpen;
|
||||
|
||||
if (mobileQuery.matches && $contentLink.length) {
|
||||
event.preventDefault();
|
||||
event.stopImmediatePropagation();
|
||||
window.location.assign($contentLink.get(0).href);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!mobileQuery.matches || !$item.length ||
|
||||
$target.closest('.esun-megamenu-wrapper').length) {
|
||||
return;
|
||||
}
|
||||
|
||||
$linkedToggle = $target.closest('.esun-megamenu-toggle');
|
||||
|
||||
if ($item.hasClass('esun-linked-megamenu-trigger') && !$linkedToggle.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
$trigger = $linkedToggle.length
|
||||
? $linkedToggle
|
||||
: $item.find('> .level-top');
|
||||
|
||||
if (!$trigger.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
event.stopImmediatePropagation();
|
||||
|
||||
willOpen = !$item.hasClass('is-open');
|
||||
resetMenus();
|
||||
|
||||
if (willOpen) {
|
||||
$item.addClass('is-open');
|
||||
$trigger.attr('aria-expanded', 'true');
|
||||
}
|
||||
}
|
||||
|
||||
$navigation
|
||||
.find(triggerSelector)
|
||||
.attr({
|
||||
'aria-expanded': 'false'
|
||||
});
|
||||
|
||||
$navigation
|
||||
.find('.esun-megamenu-toggle')
|
||||
.attr({
|
||||
'aria-expanded': 'false'
|
||||
});
|
||||
|
||||
$navigation
|
||||
.find('.esun-megamenu-wrapper')
|
||||
.on('click.esunMobileMegaMenu', function (event) {
|
||||
if (mobileQuery.matches) {
|
||||
event.stopPropagation();
|
||||
}
|
||||
});
|
||||
|
||||
$(document)
|
||||
.off('click.esunMobileMegaMenuReset', '[data-action="toggle-nav"]')
|
||||
.on('click.esunMobileMegaMenuReset', '[data-action="toggle-nav"]', resetMenus);
|
||||
|
||||
if (typeof mobileQuery.addEventListener === 'function') {
|
||||
mobileQuery.addEventListener('change', function (event) {
|
||||
if (!event.matches) {
|
||||
resetMenus();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
mobileQuery.addListener(function (event) {
|
||||
if (!event.matches) {
|
||||
resetMenus();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
element.addEventListener('click', handleNavigationClick, true);
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user