123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
"use strict";
modulum('FolderWidgetBase', ['WidgetGroupBase'],
function(context, cls) {
cls.FolderWidgetBase = context.oo.Class(cls.WidgetGroupBase, function($super) {
return {
__name: "FolderWidgetBase",
__virtual: true,
__dataContentPlaceholderSelector: ".containerElement",
_currentPage: null,
_onLayoutUnbindHandler: null,
isMinified: false,
_isLateRendering: false,
_initElement: function() {
this._ignoreLayout = true;
$super._initElement.call(this);
},
_initLayout: function() {
if (!this.isMinified && this._layoutEngine) {
this._onLayoutUnbindHandler = this._layoutEngine.onLayoutApplied(this.removePagesFromDom.bind(this));
}
},
destroy: function() {
if (this._onLayoutUnbindHandler) {
this._onLayoutUnbindHandler();
this._onLayoutUnbindHandler = null;
}
$super.destroy.call(this);
},
addChildWidget: function(widget, options) {
if (!widget.isInstanceOf(cls.PageWidget)) {
throw "Only PageWidgets can be added in FolderWidgetBase";
}
options = options || {};
options.noDOMInsert = options.noDOMInsert || this.isMinified;
$super.addChildWidget.call(this, widget, options);
var pageWidget = widget;
if (this.isMinified) {
this.getContainerElement().appendChild(pageWidget.getReplacer());
}
if (this._children.length === 1) {
this.setCurrentPage(pageWidget, false);
}
},
removeChildWidget: function(widget) {
var pageWidget = widget;
var nextCurrentIndex = -1;
if (pageWidget === this.getCurrentPage()) {
this._currentPage = null;
nextCurrentIndex = this._children.indexOf(pageWidget);
if (nextCurrentIndex >= this._children.length - 1) {
nextCurrentIndex = this._children.length - 2;
}
}
if (pageWidget.getTitleWidget()) {
pageWidget.getTitleWidget().getElement().remove();
}
pageWidget.getElement().remove();
$super.removeChildWidget.call(this, pageWidget);
if (this._children.length && nextCurrentIndex !== -1) {
this.setCurrentPage(this._children[nextCurrentIndex], false);
}
},
addPagesInDom: function() {
if (!this._isLateRendering) {
for (var i = 0; i < this.getChildren().length; i++) {
var page = this.getChildren()[i];
if (page !== this.getCurrentPage() && !page.hasChildWebComponent()) {
page.addPageInDom();
}
}
if (!this._onLayoutUnbindHandler) {
this._onLayoutUnbindHandler = this._layoutEngine.onLayoutApplied(this.removePagesFromDom.bind(this));
}
this.isMinified = false;
}
},
removePagesFromDom: function() {
if (!this._isLateRendering) {
for (var i = 0; i < this.getChildren().length; i++) {
var page = this.getChildren()[i];
if (page !== this.getCurrentPage() && !page.hasChildWebComponent()) {
page.removePageFromDom();
}
}
this.isMinified = true;
}
if (this._onLayoutUnbindHandler) {
this._onLayoutUnbindHandler();
this._onLayoutUnbindHandler = null;
}
},
onTitleClick: function(page) {
if (this.setCurrentPage(page, false)) {
this.emit(context.constants.widgetEvents.requestFocus);
}
},
getCurrentPage: function() {
return this._currentPage;
},
setCurrentPage: function(page, executeAction) {
if (this._currentPage !== page ) {
for (var i = 0; i < this._children.length; ++i) {
var child = this._children[i];
child.getTitleWidget().setCurrent(child === page);
}
if (this._currentPage) {
if (this.isMinified && !this._currentPage.hasChildWebComponent()) {
this._currentPage.removePageFromDom();
}
this._currentPage.removeClass("currentPage");
this._currentPage.disable();
}
this._currentPage = page;
if (!this._currentPage.getLayoutEngine().needMeasure()) {
this._currentPage.addClass("currentPage");
}
if (this.isMinified && !this._currentPage.hasChildWebComponent()) {
this._currentPage.addPageInDom();
}
this._currentPage.activate();
if (page) {
this.getLayoutEngine().invalidateAllocatedSpace();
}
this.emit(context.constants.widgetEvents.change, page, executeAction);
return true;
}
return false;
},
updateCurrentPage: function() {
var focusedWidget = this.getUserInterfaceWidget().getVMFocusedWidget();
var focusedWidgetIsPage = Boolean(focusedWidget) && focusedWidget instanceof cls.PageWidget;
if (focusedWidgetIsPage && !focusedWidget.isHidden()) {
this.setCurrentPage(focusedWidget);
} else {
var firstVisiblePage = null;
if (this._children) {
for (var i = 0; i < this._children.length; i++) {
var page = this._children[i];
if (!page.isHidden()) {
if (!firstVisiblePage) {
firstVisiblePage = page;
}
if (focusedWidget && !focusedWidgetIsPage && focusedWidget.isChildOf(page)) {
this.setCurrentPage(page);
firstVisiblePage = null;
break;
}
}
}
}
if (firstVisiblePage) {
this.setCurrentPage(firstVisiblePage, false);
}
}
},
getPageCount: function() {
return this._children.length;
},
getVisiblePageCount: function() {
var count = 0;
for (var i = 0; i < this._children.length; i++) {
var page = this._children[i];
if (!page.isHidden()) {
count++;
}
}
return count;
},
setLateRendering: function(lateRendering) {
this._isLateRendering = lateRendering;
if (this._isLateRendering) {
this.isMinified = true;
}
}
};
});
});