123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
"use strict";
modulum('TopMenuGroupWidget', ['WidgetGroupBase', 'WidgetFactory'],
function(context, cls) {
cls.TopMenuGroupWidget = context.oo.Class(cls.WidgetGroupBase, function($super) {
return {
__name: 'TopMenuGroupWidget',
_image: null,
_dropDown: null,
_textElement: null,
_mainContainerWidget: null,
_isSubMenu: null,
_initContainerElement: function() {
$super._initContainerElement.call(this);
this._mainContainerWidget = window.document.getElementsByClassName("gbc_MainContainerWidget")[0];
this._textElement = this._element.getElementsByTagName('span')[0];
this._image = cls.WidgetFactory.createWidget('ImageWidget', this.getBuildParameters());
this._image.setAutoScale(true);
this._element.prependChild(this._image.getElement());
this._responsiveMenuWidget = this.getParentWidget() && this.getParentWidget()._responsiveMenuWidget;
this._dropDown = cls.WidgetFactory.createWidget('ChoiceDropDown', this.getBuildParameters());
this._dropDown.setParentWidget(this);
this._dropDown.setBackgroundColor(this.getBackgroundColor());
this._dropDown.onOpen(function() {
if (!this.isSubMenu()) {
this.addClass("current");
this.getParentWidget().addClass("open");
}
}.bind(this));
this._dropDown.onClose(function() {
if (!this.isSubMenu()) {
if (this.getElement()) {
this.removeClass("current");
}
if (this.getParentWidget()) {
this.getParentWidget().removeClass("open");
}
}
}.bind(this));
if (!window.isMobile()) {
this._element.on('mouseover.TopMenuGroupWidget', this._displayTopMenuDropDown.bind(this));
} else {
this._dropDown.shouldClose = function(targetElement) {
return !targetElement.isElementOrChildOf(this.getParentWidget().getElement());
}.bind(this);
}
},
setTopMenuWidget: function(tmWidget) {
this._topMenuWidget = tmWidget;
},
destroy: function() {
if (!window.isMobile()) {
this._element.off('mouseover.TopMenuGroupWidget');
}
if (this._image) {
this._image.destroy();
this._image = null;
}
$super.destroy.call(this);
this._dropDown.destroy();
this._dropDown = null;
},
managePriorityKeyDown: function(keyString, domKeyEvent, repeat) {
if (!this.isEnabled()) {
return false;
}
var keyProcessed = false;
if (this.isVisible()) {
var currentChild = this._dropDown.getCurrentChildren();
var parent = this.getParentWidget();
keyProcessed = true;
switch (keyString) {
case "esc":
this._dropDown.hide();
break;
case this.getStart():
if (!this.isSubMenu()) {
var previousMenu = parent.getPreviousMenu(this);
if (previousMenu) {
this.removeClass("current");
if (previousMenu.getName().endsWith("TopMenuGroupWidget")) {
previousMenu._displayTopMenuDropDown(null);
}
}
} else {
this._dropDown.remove();
}
break;
case this.getEnd():
if (currentChild && currentChild.getName().endsWith("TopMenuGroupWidget")) {
currentChild._displayTopMenuDropDown(null);
} else {
if (!this.isSubMenu()) {
var nextMenu = parent.getNextMenu(this);
if (nextMenu) {
this.removeClass("current");
if (nextMenu.getName().endsWith("TopMenuGroupWidget")) {
nextMenu._displayTopMenuDropDown(null);
}
}
}
}
break;
case "enter":
case "return":
if (currentChild) {
if (currentChild.getName().endsWith("TopMenuGroupWidget")) {
currentChild._displayTopMenuDropDown(null);
} else if (currentChild.getName().endsWith("TopMenuCommandWidget")) {
if (currentChild.getAUIWidget()) {
currentChild.getAUIWidget().emit(context.constants.widgetEvents.click);
} else {
currentChild.emit(context.constants.widgetEvents.click);
}
}
}
break;
default:
keyProcessed = false;
}
}
if (keyProcessed) {
return true;
} else {
return this._dropDown.managePriorityKeyDown(keyString, domKeyEvent, repeat);
}
},
manageMouseClick: function(domEvent) {
if (!this.isEnabled()) {
return true;
}
if (this.isInSidebar()) {
var topmenu = this._topMenuWidget;
var sideBarTopmenu = this._topMenuWidget._responsiveMenuWidget;
sideBarTopmenu.displayNext(this);
return false;
}
if (!this.isSubMenu()) {
this._dropDown.toggle();
return false;
} else if (window.isMobile()) {
this._displayTopMenuDropDown();
}
return true;
},
_displayTopMenuDropDown: function(event) {
if (!this.isEnabled()) {
return;
}
if (!this.isInSidebar()) {
this._dropDown.getChildren().slice().forEach((child) => {
this._dropDown.adoptChildWidget(child);
});
}
if (cls.DropDownWidget.hasAnyVisible()) {
var element = event ? event.target : this.getElement();
var lastDropDown = cls.DropDownWidget.getActiveDropDown();
if (lastDropDown.getParentWidget() !== this) {
if (!lastDropDown.getElement().contains(element)) {
lastDropDown.remove();
}
if (this.isSubMenu()) {
var parentRect = this.getElement().getBoundingClientRect();
this._dropDown.x = parentRect.right;
this._dropDown.y = parentRect.top;
}
this._dropDown.show(this.isSubMenu());
}
}
},
isSubMenu: function() {
if (this._isSubMenu === null && this.getParentWidget()) {
this._isSubMenu = !this.getParentWidget().getName().endsWith("TopMenuWidget");
}
return this._isSubMenu;
},
isInSidebar: function() {
return this._topMenuWidget && this._topMenuWidget.isRenderedInSidebar();
},
addChildWidget: function(widget, options) {
this._dropDown.adoptChildWidget(widget, options);
},
removeChildWidget: function(widget) {
this._dropDown.removeChildWidget(widget);
},
getChildren: function() {
return this._dropDown.getChildren();
},
setText: function(text) {
this._setTextContent(text, "_textElement");
},
getText: function() {
return this._textElement.textContent;
},
setImage: function(image) {
this._image.setSrc(image);
this._image.toggleClass("hidden", !this.isSubMenu() && !image);
},
getImage: function() {
if (this._image) {
return this._image.getSrc();
}
return null;
},
setBackgroundColor: function(color) {
if (this._dropDown) {
this._dropDown.setBackgroundColor(color);
}
},
setColor: function(color) {
$super.setColor.call(this, color);
if (this._dropDown) {
this._dropDown.setColor(color);
}
},
hasFocus: function() {
return true;
},
getDropDown: function() {
return this._dropDown;
}
};
});
cls.WidgetFactory.registerBuilder('TopMenuGroup', cls.TopMenuGroupWidget);
});