123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
"use strict";
modulum('TableColumnTitleWidget', ['WidgetBase', 'WidgetFactory'],
function(context, cls) {
cls.TableColumnTitleWidget = context.oo.Class(cls.WidgetBase, function($super) {
return {
__name: "TableColumnTitleWidget",
_resizerDragX: null,
_contextMenu: null,
_autoAlignment: false,
_sortIconElement: null,
_sortGlyph: null,
constructor: function(opts) {
opts = (opts || {});
opts.inTable = true;
$super.constructor.call(this, opts);
},
_initLayout: function() {
},
destroy: function() {
if (this._contextMenu) {
this._contextMenu.destroyChildren();
this._contextMenu.destroy();
this._contextMenu = null;
}
$super.destroy.call(this);
},
manageMouseRightClick: function(domEvent) {
if (domEvent.shiftKey) {
return false;
}
domEvent.preventCancelableDefault();
this._buildContextMenu(domEvent);
return false;
},
showContextMenu: function(xPos) {
this._buildContextMenu(null, xPos);
},
_buildContextMenu: function(domEvent, xPos) {
if (this._contextMenu) {
this._contextMenu.destroyChildren();
this._contextMenu.destroy();
this._contextMenu = null;
}
var table = this.getTableWidget();
if (table) {
var opts = this.getBuildParameters();
opts.inTable = false;
opts.ignoreLayout = true;
this._contextMenu = cls.WidgetFactory.createWidget("ContextMenu", opts);
this._contextMenu.allowMultipleChoices(true);
this._contextMenu.setParentWidget(this);
this._contextMenu.setColor(table.getColor());
this._contextMenu.setBackgroundColor(table.getBackgroundColor());
this._contextMenu.onClose(function() {
this.afterDomMutator(function() {
if (this._contextMenu) {
this._contextMenu.destroyChildren();
this._contextMenu.destroy();
this._contextMenu = null;
}
}.bind(this));
}.bind(this), true);
if (domEvent && domEvent.touches && domEvent.touches[0]) {
this._contextMenu.x = domEvent.touches[0].clientX;
} else if (window.event) {
this._contextMenu.x = window.event.clientX;
} else {
this._contextMenu.x = xPos;
}
var columns = table.getColumns();
var tableColumn = this.getParentWidget();
var hideShowFunc = function(columnTitle) {
if (!this.isUnhidable()) {
this.emit(gbc.constants.widgetEvents.tableShowHideCol, "toggle");
}
if (columnTitle && columnTitle._contextMenu && columnTitle._contextMenu.isVisible()) {
var uiWidget = columnTitle.getUserInterfaceWidget();
if (uiWidget) {
uiWidget.getElement().domFocus();
}
}
};
var checkLast = function(checkWidget, columnTitle) {
var children = columnTitle.getContextMenu().getChildren();
var checkCount = children.filter(function(c) {
if (c._tc) {
c.setEnabled(!c._tc.isUnhidable());
if (c.isInstanceOf(cls.CheckBoxWidget) && c.getValue()) {
return c;
}
}
});
if (checkCount.length === 1) {
checkCount[0].setEnabled(false);
}
};
for (var i = 0; i < columns.length; i++) {
var tc = columns[i];
if (!tc.isAlwaysHidden() && !tc.isAlwaysVisible()) {
var check = cls.WidgetFactory.createWidget("CheckBox", opts);
check._tc = tc;
check.setEnabled(!tc.isUnhidable());
check.setText(tc.getTitleWidget().getText());
check.setValue(!tc.isHidden());
check.when(context.constants.widgetEvents.click, checkLast.bind(tc, check, this));
this._contextMenu.addChildWidget(check, {
clickCallback: hideShowFunc.bind(tc, this)
});
checkLast(check, this);
}
}
this._contextMenu.addSeparator();
var showAllColumnsLabel = cls.WidgetFactory.createWidget("Label", opts);
showAllColumnsLabel.setValue(i18next.t("gwc.contextMenu.showAllColumns"));
showAllColumnsLabel.addClass("gbc_showAllColumns_action");
this._contextMenu.addChildWidget(showAllColumnsLabel, {
clickCallback: function() {
var columns = this.getTableWidget().getColumns();
for (var i = 0; i < columns.length; i++) {
var tc = columns[i];
if (!tc.isAlwaysHidden() && !tc.isUnhidable()) {
tc.emit(gbc.constants.widgetEvents.tableShowHideCol, "show");
}
}
}.bind(this)
});
var hideOtherColumnsLabel = cls.WidgetFactory.createWidget("Label", opts);
hideOtherColumnsLabel.setValue(i18next.t("gwc.contextMenu.hideAllButSelected"));
hideOtherColumnsLabel.addClass("gbc_hideAllButSelected_action");
this._contextMenu.addChildWidget(hideOtherColumnsLabel, {
clickCallback: function() {
var columns = this.getTableWidget().getColumns();
for (var i = 0; i < columns.length; i++) {
var tc = columns[i];
if (!tc.isAlwaysHidden() && !tc.isUnhidable() && tc !== this.getParentWidget()) {
tc.emit(gbc.constants.widgetEvents.tableShowHideCol, "hide");
}
}
this.getParentWidget().emit(gbc.constants.widgetEvents.tableShowHideCol, "show");
}.bind(this)
});
var autoFitAllColumnsLabel = cls.WidgetFactory.createWidget("Label", opts);
autoFitAllColumnsLabel.setValue(i18next.t("gwc.contextMenu.autoFitAllColumns"));
autoFitAllColumnsLabel.addClass("gbc_autoFitAllColumns_action");
this._contextMenu.addChildWidget(autoFitAllColumnsLabel, {
clickCallback: function() {
this.getTableWidget().autoFitAllColumns();
}.bind(this)
});
var fitToViewAllColumnsLabel = cls.WidgetFactory.createWidget("Label", opts);
fitToViewAllColumnsLabel.setValue(i18next.t("gwc.contextMenu.fitToViewAllColumns"));
fitToViewAllColumnsLabel.addClass("gbc_fitToViewAllColumns_action");
this._contextMenu.addChildWidget(fitToViewAllColumnsLabel, {
clickCallback: function() {
this.getTableWidget().fitToViewAllColumns();
}.bind(this)
});
this._contextMenu.addSeparator();
var resetDefaultLabel = cls.WidgetFactory.createWidget("Label", opts);
resetDefaultLabel.setValue(i18next.t("gwc.contextMenu.restoreDefaultSettings"));
resetDefaultLabel.addClass("gbc_restoreColumnSort_action");
this._contextMenu.addChildWidget(resetDefaultLabel, {
clickCallback: function() {
table.emit(context.constants.widgetEvents.tableResetToDefault);
}.bind(this)
});
var resetLabel = cls.WidgetFactory.createWidget("Label", opts);
resetLabel.setValue(i18next.t("gwc.contextMenu.restoreColumnSort"));
resetLabel.addClass("gbc_restoreColumnSort_action");
this._contextMenu.addChildWidget(resetLabel, {
clickCallback: function() {
table.emit(context.constants.widgetEvents.tableHeaderSort, -1);
}.bind(this)
});
if (table.isFrozenTable()) {
this._contextMenu.addSeparator();
var leftFrozenLabel = cls.WidgetFactory.createWidget("Label", opts);
var rightFrozenLabel = cls.WidgetFactory.createWidget("Label", opts);
var unfreezeLabel = cls.WidgetFactory.createWidget("Label", opts);
var freezeIndex = 0;
var columnCount = 0;
leftFrozenLabel.setValue(i18next.t("gwc.contextMenu.freezeLeft"));
rightFrozenLabel.setValue(i18next.t("gwc.contextMenu.freezeRight"));
unfreezeLabel.setValue(i18next.t("gwc.contextMenu.unfreezeAll"));
leftFrozenLabel.addClass("gbc_freezeLeft_action");
rightFrozenLabel.addClass("gbc_freezeRight_action");
unfreezeLabel.addClass("gbc_unfreezeAll_action");
this._contextMenu.addChildWidget((this.isReversed() ? rightFrozenLabel : leftFrozenLabel), {
clickCallback: function() {
freezeIndex = tableColumn.getOrderedColumnIndex() + 1;
table.setLeftFrozenColumns(freezeIndex);
table.emit(gbc.constants.widgetEvents.tableLeftFrozen, freezeIndex);
}.bind(this)
});
this._contextMenu.addChildWidget((this.isReversed() ? leftFrozenLabel : rightFrozenLabel), {
clickCallback: function() {
columnCount = columns.length;
freezeIndex = tableColumn.getOrderedColumnIndex();
table.setRightFrozenColumns(columnCount - freezeIndex);
table.emit(gbc.constants.widgetEvents.tableRightFrozen, columnCount - freezeIndex);
}.bind(this)
});
this._contextMenu.addChildWidget(unfreezeLabel, {
clickCallback: function() {
table.setLeftFrozenColumns(0);
table.setRightFrozenColumns(0);
table.emit(gbc.constants.widgetEvents.tableLeftFrozen, 0);
table.emit(gbc.constants.widgetEvents.tableRightFrozen, 0);
}.bind(this)
});
}
this._element.domFocus(null, table.getElement());
this._contextMenu.show();
}
},
managePriorityKeyDown: function(keyString, domKeyEvent, repeat) {
var keyProcessed = false;
if (this._contextMenu.isVisible()) {
keyProcessed = this._contextMenu.managePriorityKeyDown(keyString, domKeyEvent, repeat);
}
return keyProcessed;
},
getResizer: function() {
return this._element.getElementsByClassName("resizer")[0];
},
getHeaderText: function() {
return this._element.getElementsByClassName("headerText")[0];
},
getContextMenu: function() {
return this._contextMenu;
},
getSortIconElement: function() {
return this._sortIconElement;
},
getTableWidget: function() {
return this._parentWidget._parentWidget;
},
manageMouseClick: function(domEvent) {
if (domEvent.target.hasClass("headerText")) {
this.getTableWidgetBase().emit(context.constants.widgetEvents.tableHeaderSort, this.getParentWidget().getColumnIndex());
}
return false;
},
onResizerDoubleClick: function() {
this.getParentWidget().autoSetWidth();
},
onResizerDragStart: function(evt) {
if (window.browserInfo.isFirefox) {
try {
evt.dataTransfer.setData('text/plain', '');
} catch (ex) {
console.error("evt.dataTransfer.setData('text/plain', ''); not supported");
}
}
if (this.getParentWidget().isSizable()) {
this.getTableWidget()._dndMode = "columnResizing";
this.getParentWidget().getElement().addClass("resizing");
this._resizerDragX = evt.clientX || evt.screenX;
} else {
evt.preventCancelableDefault();
}
},
onResizerDrag: function() {
if (this.getTableWidget()._dndMode === "columnResizing") {
if (!this._resizerDragX || !this.getTableWidget()._dndMouseDragX) {
return;
}
var size = this.getTableWidget()._dndMouseDragX - this._resizerDragX;
var initialWidth = this.getParentWidget().getWidth();
var newWidth = initialWidth + size;
if (this.isReversed()) {
newWidth = initialWidth - size;
}
if (newWidth > 30) {
this._resizerDragX = this.getTableWidget()._dndMouseDragX;
this.getParentWidget().setWidthFromUserInteraction(newWidth);
}
}
},
onResizerDragEnd: function(evt) {
var tc = this.getParentWidget();
tc._element.removeClass("resizing");
this._resizerDragX = null;
evt.preventCancelableDefault();
this.getTableWidget()._dndMode = null;
var width = tc.getWidth();
this.getParentWidget().setWidthFromUserInteraction(width);
},
onReorderingDragStart: function(evt) {
if (window.browserInfo.isFirefox) {
try {
evt.dataTransfer.setData('text/plain', '');
} catch (ex) {
console.error("evt.dataTransfer.setData('text/plain', ''); not supported");
}
}
if (this.getParentWidget().isMovable()) {
this.getTableWidget()._dndMode = "columnReordering";
this.getTableWidget()._dndDraggedColumnWidget = this.getParentWidget();
} else {
evt.preventCancelableDefault();
}
},
onReorderingDragEnd: function(evt) {
if (this.getTableWidget()._dndReorderingDragOverWidget !== null) {
this.getTableWidget()._dndReorderingDragOverWidget.getElement()
.removeClass("reordering_left").removeClass("reordering_right");
this.getTableWidget()._dndReorderingDragOverWidget = null;
}
var allNoDrop = this.getTableWidget().getElement().querySelectorAll(".noDrop");
for (var i = 0; i < allNoDrop.length; i++) {
allNoDrop[i].removeClass("noDrop");
}
this.getTableWidget()._noDrop = {
"left": false,
"right": false,
"center": false
};
this.getTableWidget()._dndMode = null;
this.getTableWidget()._dndDraggedColumnWidget = null;
},
onReorderingDrop: function() {
if (this.getTableWidget()._dndMode === "columnReordering") {
if (this.getTableWidget()._dndReorderingDragOverWidget &&
this.getTableWidget()._dndDraggedColumnWidget !== this.getTableWidget()._dndReorderingDragOverWidget) {
if (this.getTableWidget()._dndDraggedColumnWidget.getFrozenIndex() === this.getTableWidget()._dndReorderingDragOverWidget
.getFrozenIndex()) {
this.reorderColumns(
this.getTableWidget()._dndDraggedColumnWidget, this.getTableWidget()._dndReorderingDragOverWidget);
}
}
}
},
reorderColumns: function(draggedColumn, dropColumn) {
let tableWidget = this.getTableWidget();
tableWidget.resetOrderedColumns();
let orderedColumns = tableWidget.getOrderedColumns();
let newOrderedColumns = orderedColumns.slice();
let dragColIndex = newOrderedColumns.indexOf(draggedColumn);
let dropColIndex = newOrderedColumns.indexOf(dropColumn);
newOrderedColumns.removeAt(dragColIndex);
newOrderedColumns.insert(this.getTableWidget()._dndDraggedColumnWidget, dropColIndex);
for (let i = 0; i < newOrderedColumns.length; i++) {
let col = newOrderedColumns[i];
col.setOrder(i);
}
for (let i = 0; i < newOrderedColumns.length; i++) {
let col = newOrderedColumns[i];
col.emit(context.constants.widgetEvents.tableOrderColumn, i);
}
},
onReorderingDragOver: function(evt) {
if (this.getTableWidget()._dndMode === "columnReordering") {
var lastReorderingDragOverColumnWidget = this.getTableWidget()._dndReorderingDragOverWidget;
if (lastReorderingDragOverColumnWidget !== this.getParentWidget()) {
if (lastReorderingDragOverColumnWidget !== null) {
lastReorderingDragOverColumnWidget.getElement()
.removeClass("reordering_left").removeClass("reordering_right");
}
this.getTableWidget()._dndReorderingDragOverWidget = this.getParentWidget();
}
var overIndex = this.getParentWidget().getOrderedColumnIndex();
var startIndex = this.getTableWidget()._dndDraggedColumnWidget.getOrderedColumnIndex();
if (evt.currentTarget && evt.currentTarget.hasClass("gbc_TableLeftColumnsHeaders")) {
if (!this.getTableWidget()._noDrop.left) {
this.getTableWidget().getRightColumnsContainer().addClass("noDrop");
this.getTableWidget().getRightColumnsHeaders().addClass("noDrop");
this.getTableWidget().getColumnsContainer().addClass("noDrop");
this.getTableWidget().getColumnsHeaders().addClass("noDrop");
this.getTableWidget()._noDrop = {
"left": false,
"right": true,
"center": true
};
}
} else if (evt.currentTarget && evt.currentTarget.hasClass("gbc_TableRightColumnsHeaders")) {
if (!this.getTableWidget()._noDrop.right) {
this.getTableWidget().getLeftColumnsContainer().addClass("noDrop");
this.getTableWidget().getLeftColumnsHeaders().addClass("noDrop");
this.getTableWidget().getColumnsContainer().addClass("noDrop");
this.getTableWidget().getColumnsHeaders().addClass("noDrop");
this.getTableWidget()._noDrop = {
"left": true,
"right": false,
"center": true
};
}
} else if (evt.currentTarget && evt.currentTarget.hasClass("gbc_TableColumnsHeaders")) {
if (!this.getTableWidget()._noDrop.center) {
this.getTableWidget().getLeftColumnsContainer().addClass("noDrop");
this.getTableWidget().getLeftColumnsHeaders().addClass("noDrop");
this.getTableWidget().getRightColumnsContainer().addClass("noDrop");
this.getTableWidget().getRightColumnsHeaders().addClass("noDrop");
this.getTableWidget()._noDrop = {
"left": true,
"right": true,
"center": false
};
}
}
this.getParentWidget().getElement().addClass(overIndex >= startIndex ? "reordering_right" : "reordering_left");
}
},
onReorderingDragLeave: function() {
if (this.getTableWidget()._dndMode === "columnReordering") {
this.getParentWidget().getElement().removeClass("reordering_left").removeClass("reordering_right");
this.getTableWidget()._dndReorderingDragOverWidget = null;
}
},
setText: function(text) {
this._setTextContent(text, function() {
return this._element.getElementsByClassName("headerText")[0];
}.bind(this));
var tableWidget = this.getTableWidget();
if (tableWidget) {
tableWidget.synchronizeHeadersHeight();
}
},
getText: function() {
return this._element.getElementsByClassName("headerText")[0].textContent;
},
setWidth: function(width) {
this.setStyle({
"width": width + "px"
});
},
getWidthStyle: function() {
return this.getStyle("width");
},
setSortDecorator: function(sortType) {
var glyphClass = "hidden";
if (sortType === "asc") {
glyphClass = "caret-up";
}
if (sortType === "desc") {
glyphClass = "caret-down";
}
if (!this._sortIconElement && glyphClass !== "hidden") {
this._sortIconElement = document.createElement("span");
this._sortIconElement.addClass("sortIcon");
this._element.prependChild(this._sortIconElement);
}
if (this._sortIconElement) {
this._sortIconElement.removeClass(this._sortGlyph);
this._sortIconElement.addClass(glyphClass);
}
this._sortGlyph = glyphClass;
},
setOrder: function(index) {
this.setStyle({
"order": index
});
},
hasFocus: function() {
return true;
},
setTextAlign: function(alignment) {
if (alignment === "auto") {
this._autoAlignment = true;
} else {
this.setStyle(" .headerText", {
"text-align": alignment
});
}
},
isAutoTextAlignement: function() {
return this._autoAlignment;
},
isReversed: function() {
return this._parentWidget._parentWidget.isReversed();
},
shouldShowApplicationContextMenu: function() {
return false;
}
};
});
cls.WidgetFactory.registerBuilder('TableColumnTitle', cls.TableColumnTitleWidget);
});