Files
2026-09-15 10:35:56 +08:00

85 lines
2.8 KiB
JavaScript
Executable File

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);
});
});
}
}
});
});