@syncfusion/ej2-pivotview
Version:
The pivot grid, or pivot table, is used to visualize large sets of relational data in a cross-tabular format, similar to an Excel pivot table.
921 lines • 128 kB
JavaScript
import { Dialog, Tooltip } from '@syncfusion/ej2-popups';
import { Droppable, createElement, extend, remove, addClass, closest, getInstance, select, SanitizeHtmlHelper } from '@syncfusion/ej2-base';
import { prepend, append, KeyboardEvents, removeClass, isNullOrUndefined } from '@syncfusion/ej2-base';
import { Button, RadioButton, CheckBox } from '@syncfusion/ej2-buttons';
import { MaskedTextBox } from '@syncfusion/ej2-inputs';
import * as cls from '../../common/base/css-constant';
import { TreeView } from '@syncfusion/ej2-navigations';
import { ContextMenu as Menu } from '@syncfusion/ej2-navigations';
import * as events from '../../common/base/constant';
import { Accordion } from '@syncfusion/ej2-navigations';
import { DropDownList } from '@syncfusion/ej2-dropdowns';
import { PivotUtil } from '../../base/util';
/**
* Module to render Calculated Field Dialog
*/
var COUNT = 'Count';
var AVG = 'Avg';
var MEDIAN = 'Median';
var MIN = 'Min';
var MAX = 'Max';
var SUM = 'Sum';
var DISTINCTCOUNT = 'DistinctCount';
var PRODUCT = 'Product';
var STDEV = 'SampleStDev';
var STDEVP = 'PopulationStDev';
var VAR = 'SampleVar';
var VARP = 'PopulationVar';
var CALC = 'CalculatedField';
var AGRTYPE = 'AggregateType';
/** @hidden */
var CalculatedField = /** @class */ (function () {
/**
* Constructor for calculatedfield module.
*
* @param {PivotView | PivotFieldList} parent - It represent the parent.
*/
function CalculatedField(parent) {
/** @hidden */
this.isFormula = false;
/** @hidden */
this.isRequireUpdate = false;
this.formatTypes = ['Standard', 'Currency', 'Percent', 'Custom', 'None'];
this.parent = parent;
this.existingReport = null;
this.parent.calculatedFieldModule = this;
this.removeEventListener();
this.addEventListener();
this.parentID = this.parent.element.id;
this.treeObj = null;
this.droppable = null;
this.newFields = null;
this.isFieldExist = true;
this.formulaText = null;
this.fieldText = null;
this.formatText = null;
this.formatType = null;
this.fieldType = null;
this.parentHierarchy = null;
this.isEdit = false;
this.currentFieldName = null;
}
/**
* To get module name.
*
* @returns {string} - It returns the Module name.
*/
CalculatedField.prototype.getModuleName = function () {
return 'calculatedField';
};
CalculatedField.prototype.keyActionHandler = function (e) {
var node = e.currentTarget.querySelector('.e-node-focus');
if (node) {
switch (e.action) {
case 'moveRight':
case 'shiftE':
if (this.parent.dataType === 'pivot') {
this.displayMenu(node);
}
break;
case 'delete':
if (node.tagName === 'LI' && !node.querySelector('.e-list-icon.e-format') && !node.querySelector('.e-list-icon').classList.contains('.e-icons') && (node.querySelector('.' + cls.GRID_REMOVE) &&
node.querySelector('.' + cls.GRID_REMOVE).classList.contains('e-list-icon'))) {
this.createConfirmDialog(this.parent.localeObj.getConstant('alert'), this.parent.localeObj.getConstant('removeCalculatedField'), {}, true, node);
}
break;
case 'enter':
{
var field = node.getAttribute('data-field');
var dialog = getInstance(select('#' + this.parentID + 'calculateddialog', document), Dialog);
var type = node.getAttribute('data-type');
var dropField = select('#' + this.parentID + 'droppable', dialog.element);
if (this.parent.dataType === 'pivot') {
if (dropField.value === '') {
if (type === CALC) {
dropField.value = node.getAttribute('data-uid');
}
else {
dropField.value = '"' + type + '(' + field + ')' + '"';
}
}
else if (dropField.value !== '') {
if (type === CALC) {
dropField.value = dropField.value + node.getAttribute('data-uid');
}
else {
dropField.value = dropField.value + '"' + type + '(' + field + ')' + '"';
}
}
}
else {
if (this.parent.olapEngineModule && this.parent.olapEngineModule.fieldList[field] &&
this.parent.olapEngineModule.fieldList[field].isCalculatedField) {
field = this.parent.olapEngineModule.fieldList[field].tag;
}
if (dropField.value === '') {
dropField.value = field;
}
else if (dropField.value !== '') {
dropField.value = dropField.value + field;
}
}
}
break;
}
}
};
/**
* Trigger while click treeview icon.
*
* @param {NodeClickEventArgs} e - Click event argument.
* @returns {void}
*/
CalculatedField.prototype.fieldClickHandler = function (e) {
var node = closest(e.event.target, 'li');
if (e.event.target.classList.contains(cls.FORMAT) ||
e.event.target.classList.contains(cls.CALC_EDIT) ||
e.event.target.classList.contains(cls.CALC_EDITED) ||
e.event.target.classList.contains(cls.GRID_REMOVE)) {
if (!this.parent.isAdaptive) {
this.displayMenu(node, e.node, e.event.target);
}
else if (this.parent.dataType === 'olap' && this.parent.isAdaptive) {
if (node.tagName === 'LI' && node.querySelector('.e-list-edit-icon').classList.contains(cls.CALC_EDIT) && e.event.target.classList.contains(cls.CALC_EDIT)) {
this.isEdit = true;
this.currentFieldName = node.getAttribute('data-field');
this.fieldText = node.getAttribute('data-caption');
this.formulaText = node.getAttribute('data-formula');
this.formatType = node.getAttribute('data-formatString');
this.formatText = this.formatType === 'Custom' ? node.getAttribute('data-customformatstring') : null;
this.fieldType = node.getAttribute('data-membertype');
this.parentHierarchy = this.fieldType === 'Dimension' ? node.getAttribute('data-hierarchy') : null;
addClass([node.querySelector('.e-list-edit-icon')], cls.CALC_EDITED);
removeClass([node.querySelector('.e-list-edit-icon')], cls.CALC_EDIT);
this.renderMobileLayout(this.parent.dialogRenderer.adaptiveElement);
}
else if (node.tagName === 'LI' && node.querySelector('.e-list-edit-icon').classList.contains(cls.CALC_EDITED) && e.event.target.classList.contains(cls.CALC_EDITED)) {
this.isEdit = false;
this.fieldText = this.formatText = this.formulaText = this.currentFieldName = null;
this.parentHierarchy = this.fieldType = this.formatType = null;
addClass([node.querySelector('.e-list-edit-icon')], cls.CALC_EDIT);
removeClass([node.querySelector('.e-list-edit-icon')], cls.CALC_EDITED);
}
else if (node.tagName === 'LI' && node.querySelector('.' + cls.GRID_REMOVE).classList.contains('e-icons') && e.event.target.classList.contains(cls.GRID_REMOVE)) {
this.createConfirmDialog(this.parent.localeObj.getConstant('alert'), this.parent.localeObj.getConstant('removeCalculatedField'), {}, true, e.node);
}
}
}
};
/**
* Trigger while click treeview icon.
*
* @param {AccordionClickArgs} e - Click event argument.
* @returns {void}
*/
CalculatedField.prototype.accordionClickHandler = function (e) {
if (e.item && e.item.iconCss.indexOf('e-list-icon') !== -1 &&
closest(e.originalEvent.target, '.e-acrdn-header-icon')) {
var node = closest(e.originalEvent.target, '.e-acrdn-header').querySelector('.' + cls.CALCCHECK);
var fieldName = node.getAttribute('data-field');
var captionName = node.getAttribute('data-caption');
var formatObj = PivotUtil.getFieldByName(fieldName, this.parent.dataSourceSettings.formatSettings);
var optionElement = closest(e.originalEvent.target, '.e-acrdn-header-icon');
if (formatObj) {
var pivotFormat = this.getFormat(formatObj.format);
var formatString = (pivotFormat ? this.formatTypes.indexOf(pivotFormat) > -1 ?
formatObj.format : 'Custom' : 'None');
this.formatType = formatString;
}
if (optionElement.querySelector('.' + cls.CALC_EDIT) && e.originalEvent.target.classList.contains(cls.CALC_EDIT)) {
this.isEdit = true;
this.currentFieldName = fieldName;
this.fieldText = captionName ? captionName : fieldName;
this.formulaText = this.parent.engineModule.fieldList[fieldName].formula;
this.formatText = formatObj ? formatObj.format : '';
addClass([optionElement.querySelector('.e-list-icon')], cls.CALC_EDITED);
removeClass([optionElement.querySelector('.e-list-icon')], cls.CALC_EDIT);
this.renderMobileLayout(this.parent.dialogRenderer.adaptiveElement);
}
else if (optionElement.querySelector('.' + cls.CALC_EDITED) &&
e.originalEvent.target.classList.contains(cls.CALC_EDITED)) {
this.isEdit = false;
this.fieldText = this.formatText = this.formulaText = this.currentFieldName = null;
addClass([optionElement.querySelector('.e-list-icon')], cls.CALC_EDIT);
removeClass([optionElement.querySelector('.e-list-icon')], cls.CALC_EDITED);
}
else if (optionElement.querySelector('.' + cls.GRID_REMOVE) &&
e.originalEvent.target.classList.contains(cls.GRID_REMOVE)) {
this.createConfirmDialog(this.parent.localeObj.getConstant('alert'), this.parent.localeObj.getConstant('removeCalculatedField'), {}, true, node);
}
}
};
CalculatedField.prototype.accordionCreated = function () {
var accordion = getInstance(select('#' + this.parentID + 'accordDiv', document), Accordion);
var allElement = accordion.element.querySelectorAll('.e-acrdn-item');
for (var i = 0; i < allElement.length; i++) {
if (allElement[i].querySelector('.' + cls.CALC_EDIT) || allElement[i].querySelector('.' + cls.CALC_EDITED)) {
var element = createElement('span', {
className: 'e-list-icon ' + cls.GRID_REMOVE + ' e-icons'
});
append([element], allElement[i].querySelector('.e-acrdn-header-icon'));
addClass([allElement[i]], cls.SELECT_CLASS);
}
}
};
CalculatedField.prototype.clearFormula = function () {
if (this.treeObj && this.treeObj.element.querySelector('li')) {
removeClass(this.treeObj.element.querySelectorAll('li'), 'e-active');
this.displayMenu(this.treeObj.element.querySelector('li'));
}
};
/**
* To display context menu.
*
* @param {HTMLElement} node - It contains the value of node.
* @param {HTMLElement} treeNode - It contains the value of tree Node.
* @param {HTMLElement} target - It represent the target.
* @returns {void}
*/
CalculatedField.prototype.displayMenu = function (node, treeNode, target) {
var edit = target ? target.classList.contains(cls.CALC_EDIT) : true;
var edited = target ? target.classList.contains(cls.CALC_EDITED) : true;
var dialog = getInstance(select('#' + this.parentID + 'calculateddialog', document), Dialog);
var inputObj = getInstance(select('#' + this.parentID + 'ddlelement', document), MaskedTextBox);
try {
if (this.parent.dataType === 'pivot' && node.querySelector('.e-list-icon.e-format') &&
node.querySelector('.e-list-icon.e-format').classList.contains(cls.ICON) &&
!node.querySelector('.e-list-icon').classList.contains(cls.CALC_EDITED) &&
!node.querySelector('.e-list-icon').classList.contains(cls.GRID_REMOVE) &&
!node.querySelector('.e-list-icon').classList.contains(cls.CALC_EDIT) && node.tagName === 'LI') {
var menuObj = select('#' + this.parentID + 'CalcContextmenu', document) ?
getInstance(select('#' + this.parentID + 'CalcContextmenu', document), Menu) : null;
if (menuObj && !menuObj.isDestroyed) {
menuObj.destroy();
}
this.curMenu = node.querySelector('.' + cls.LIST_TEXT_CLASS);
this.openContextMenu(node);
}
else if (node.tagName === 'LI' && (node.querySelector('.' + cls.CALC_EDIT) &&
node.querySelector('.' + cls.CALC_EDIT).classList.contains('e-list-icon') && edit ||
(this.parent.dataType === 'olap' && node.getAttribute('data-type') === CALC && node.classList.contains('e-active') && ((target && !target.classList.contains(cls.GRID_REMOVE)) || !target)))) {
this.isEdit = true;
var fieldName = node.getAttribute('data-field');
var caption = node.getAttribute('data-caption');
this.currentFieldName = fieldName;
inputObj.value = caption;
inputObj.dataBind();
var formatString = node.getAttribute('data-formatString');
var dialogElement = dialog.element;
var ddlFormatTypes = getInstance(select('#' + this.parentID + 'Format_Div', dialogElement), DropDownList);
var customFormat = getInstance(select('#' + this.parentID + 'Custom_Format_Element', dialogElement), MaskedTextBox);
var customFormatString = node.getAttribute('data-customformatstring');
if (this.parent.dataType === 'olap') {
var memberType = node.getAttribute('data-membertype');
var parentHierarchy = node.getAttribute('data-hierarchy');
var expression = node.getAttribute('data-formula');
var fieldTitle = select('#' + this.parentID + '_' + 'FieldNameTitle', dialogElement);
var memberTypeDrop = getInstance(select('#' + this.parentID + 'Member_Type_Div', dialogElement), DropDownList);
var hierarchyDrop = getInstance(select('#' + this.parentID + 'Hierarchy_List_Div', dialogElement), DropDownList);
fieldTitle.innerText = this.parent.localeObj.getConstant('caption');
select('#' + this.parentID + 'droppable', document).value = expression;
memberTypeDrop.readonly = true;
memberTypeDrop.value = memberType;
memberTypeDrop.dataBind();
if (memberType === 'Dimension') {
hierarchyDrop.value = parentHierarchy;
}
}
else {
addClass(this.treeObj.element.querySelectorAll('.' + cls.CALC_EDITED), cls.CALC_EDIT);
removeClass(this.treeObj.element.querySelectorAll('.' + cls.CALC_EDITED), cls.CALC_EDITED);
addClass([node.querySelector('.e-list-icon')], cls.CALC_EDITED);
removeClass([node.querySelector('.e-list-icon')], cls.CALC_EDIT);
node.querySelector('.' + cls.CALC_EDITED).setAttribute('title', this.parent.localeObj.getConstant('clearCalculatedField'));
select('#' + this.parentID + 'droppable', document).value = node.getAttribute('data-uid');
}
if (formatString !== '') {
ddlFormatTypes.value = formatString;
ddlFormatTypes.dataBind();
}
customFormat.value = customFormatString;
customFormat.dataBind();
}
else if (node.tagName === 'LI' && (node.querySelector('.' + cls.CALC_EDITED) &&
node.querySelector('.' + cls.CALC_EDITED).classList.contains('e-list-icon') && edited ||
(this.parent.dataType === 'olap' && !node.classList.contains('e-active')))) {
this.isEdit = false;
inputObj.value = '';
inputObj.dataBind();
var dialogElement = dialog.element;
var customFormat = getInstance(select('#' + this.parentID + 'Custom_Format_Element', dialogElement), MaskedTextBox);
customFormat.value = '';
customFormat.dataBind();
if (this.parent.dataType === 'olap') {
var hierarchyDrop = getInstance(select('#' + this.parentID + 'Hierarchy_List_Div', dialogElement), DropDownList);
var ddlFormatTypes = getInstance(select('#' + this.parentID + 'Format_Div', dialogElement), DropDownList);
var memberTypeDrop = getInstance(select('#' + this.parentID + 'Member_Type_Div', dialogElement), DropDownList);
var fieldTitle = select('#' + this.parentID + '_' + 'FieldNameTitle', dialogElement);
fieldTitle.innerText = this.parent.localeObj.getConstant('fieldTitle');
hierarchyDrop.index = 0;
hierarchyDrop.dataBind();
ddlFormatTypes.index = 0;
ddlFormatTypes.dataBind();
memberTypeDrop.index = 0;
memberTypeDrop.readonly = false;
memberTypeDrop.dataBind();
}
else {
addClass(this.treeObj.element.querySelectorAll('.' + cls.CALC_EDITED), cls.CALC_EDIT);
removeClass(this.treeObj.element.querySelectorAll('.' + cls.CALC_EDITED), cls.CALC_EDITED);
node.querySelector('.' + cls.CALC_EDIT).setAttribute('title', this.parent.localeObj.getConstant('edit'));
}
select('#' + this.parentID + 'droppable', document).value = '';
}
else if (node.tagName === 'LI' && (node.querySelector('.' + cls.GRID_REMOVE) &&
node.querySelector('.' + cls.GRID_REMOVE).classList.contains('e-list-icon')) && !edit && !edited) {
this.parent.actionObj.actionName = events.removeField;
if (this.parent.actionBeginMethod()) {
return;
}
var dropField = select('#' + this.parentID + 'droppable', document);
var field = {
name: this.isEdit ? this.currentFieldName : inputObj.value,
caption: inputObj.value,
formula: dropField.value
};
this.createConfirmDialog(this.parent.localeObj.getConstant('alert'), this.parent.localeObj.getConstant('removeCalculatedField'), field, true, treeNode);
}
}
catch (execption) {
this.parent.actionFailureMethod(execption);
}
};
CalculatedField.prototype.removeCalcField = function (node) {
var dataSourceSettings = this.parent.dataSourceSettings;
var fieldName = node.getAttribute('data-field');
var calcfields = dataSourceSettings.calculatedFieldSettings;
var engineModule;
if (this.parent.dataType === 'pivot') {
if (!this.parent.isAdaptive) {
this.treeObj.removeNodes([node]);
}
else {
var index = parseInt(node.getAttribute('id').split(this.parentID + '_')[1], 10);
if (typeof index === 'number') {
var accordion = getInstance(select('#' + this.parentID + 'accordDiv', document), Accordion);
accordion.hideItem(index);
}
}
}
for (var i = 0; i < calcfields.length; i++) {
if (calcfields[i] && calcfields[i].name === fieldName) {
calcfields.splice(i, 1);
break;
}
}
if (this.parent.dataType === 'olap') {
engineModule = this.parent.olapEngineModule;
var fields_1 = engineModule.fieldListData ?
engineModule.fieldListData : [];
for (var _i = 0, _a = Object.keys(fields_1); _i < _a.length; _i++) {
var item = _a[_i];
if (fields_1[parseInt(item, 10)].name === fieldName) {
var index = parseInt(item, 10);
if (typeof (index) === 'number') {
fields_1.splice(index, 1);
break;
}
}
}
var parentID = this.treeObj.getNode(node).parentID;
this.treeObj.removeNodes([node]);
if (calcfields.length <= 0) {
this.treeObj.removeNodes([parentID]);
}
}
else {
engineModule = this.parent.engineModule;
}
if (engineModule.fields) {
for (var i = 0; i < engineModule.fields.length; i++) {
if (engineModule.fields[i] === fieldName) {
engineModule.fields.splice(i, 1);
break;
}
}
}
if (engineModule.savedFieldList && engineModule.savedFieldList[fieldName]) {
delete engineModule.savedFieldList[fieldName];
}
if (engineModule.fieldList && engineModule.fieldList[fieldName]) {
delete engineModule.fieldList[fieldName];
}
var formatFields = dataSourceSettings.formatSettings;
for (var i = 0; i < formatFields.length; i++) {
if (formatFields[i] && formatFields[i].name === fieldName) {
formatFields.splice(i, 1);
break;
}
}
var fields = [dataSourceSettings.values, dataSourceSettings.rows, dataSourceSettings.columns, dataSourceSettings.filters];
for (var i = 0, n = fields.length; i < n; i++) {
for (var j = 0, length_1 = fields[i].length; j < length_1; j++) {
if (fields[i][j].name === fieldName) {
fields[i].splice(j, 1);
break;
}
}
}
if (this.isEdit && this.currentFieldName === fieldName) {
var inputObj = getInstance(select('#' + this.parentID + 'ddlelement', document), MaskedTextBox);
this.isEdit = false;
inputObj.value = '';
this.currentFieldName = this.formatText = this.fieldText = this.formatType = null;
this.formulaText = this.fieldType = this.parentHierarchy = null;
}
if ((!isNullOrUndefined(this.parent.isDeferLayoutUpdate) &&
!this.parent.isDeferLayoutUpdate) || (this.parent.pivotGridModule &&
!this.parent.pivotGridModule.pivotDeferLayoutUpdate) || this.parent.getModuleName() !== 'pivotfieldlist') {
this.parent.updateDataSource();
}
this.closeErrorDialog();
};
/**
* To set position for context menu.
*
* @param {HTMLElement} node - It contains the value of node.
* @returns {void}
*/
CalculatedField.prototype.openContextMenu = function (node) {
var _this = this;
var fieldName = node.getAttribute('data-field');
var type = this.parent.engineModule.fieldList[fieldName].type !== 'number' ? 'string' : 'number';
var validSummaryTypes = (type === 'string' ? this.getValidSummaryType().slice(0, 2) : this.getValidSummaryType());
var eventArgs = {
cancel: false, fieldName: fieldName,
aggregateTypes: this.getMenuItems(type).slice()
};
var control = this.parent.getModuleName() === 'pivotfieldlist' && this.parent.isPopupView ?
this.parent.pivotGridModule : this.parent;
control.trigger(events.aggregateMenuOpen, eventArgs, function (observedArgs) {
if (!observedArgs.cancel) {
var duplicateTypes = [];
var items = [];
for (var _i = 0, _a = observedArgs.aggregateTypes; _i < _a.length; _i++) {
var option = _a[_i];
if (validSummaryTypes.indexOf(option) > -1 && duplicateTypes.indexOf(option) === -1) {
duplicateTypes.push(option);
items.push({
id: _this.parent.element.id + 'Calc_' + option,
text: _this.parent.localeObj.getConstant(option)
});
}
}
_this.createMenu(items, node);
var pos = node.getBoundingClientRect();
var offset = window.scrollY || document.documentElement.scrollTop;
var menuObj = getInstance(select('#' + _this.parentID + 'CalcContextmenu', document), Menu);
if (_this.parent.enableRtl) {
menuObj.open(pos.top + offset, pos.left - 100);
}
else {
menuObj.open(pos.top + offset, pos.left + 150);
}
}
});
};
/**
* Triggers while select menu.
*
* @param {MenuEventArgs} menu - It represent the menu.
* @returns {void}
*/
CalculatedField.prototype.selectContextMenu = function (menu) {
if (menu.element.textContent !== null) {
var field = closest(this.curMenu, '.e-list-item').getAttribute('data-caption');
closest(this.curMenu, '.e-list-item').setAttribute('data-type', menu.element.id.split('_').pop());
this.curMenu.textContent = field + ' (' + menu.element.textContent + ')';
addClass([this.curMenu.parentElement.parentElement], ['e-node-focus', 'e-hover']);
this.curMenu.parentElement.parentElement.setAttribute('tabindex', '0');
this.curMenu.parentElement.parentElement.focus();
}
};
/**
* To create context menu.
*
* @param {MenuItemModel[]} menuItems - It represent the menuItems.
* @param {HTMLElement} node - It represent the node data.
* @returns {void}
*/
CalculatedField.prototype.createMenu = function (menuItems, node) {
var _this = this;
var menuOptions = {
cssClass: this.parentID + 'calculatedmenu' + (this.parent.cssClass ? (' ' + this.parent.cssClass) : ''),
items: menuItems,
enableRtl: this.parent.enableRtl,
locale: this.parent.locale,
// beforeOpen: this.beforeMenuOpen.bind(this),
select: this.selectContextMenu.bind(this),
onClose: function () {
_this.treeObj.element.focus();
addClass([node], ['e-hover', 'e-node-focus']);
}
};
var contextMenu;
if (select('#' + this.parentID + 'CalcContextmenu', document)) {
contextMenu = select('#' + this.parentID + 'CalcContextmenu', document);
}
else {
contextMenu = createElement('ul', {
id: this.parentID + 'CalcContextmenu'
});
}
var dialog = getInstance(select('#' + this.parentID + 'calculateddialog', document), Dialog);
dialog.element.appendChild(contextMenu);
var menuObj = new Menu(menuOptions);
menuObj.isStringTemplate = true;
menuObj.appendTo(contextMenu);
};
/**
* Triggers while click OK button.
*
* @returns {void}
*/
CalculatedField.prototype.applyFormula = function () {
var _this = this;
var currentObj = this;
var isExist = false;
removeClass([document.getElementById(this.parentID + 'ddlelement')], cls.EMPTY_FIELD);
var inputObj = getInstance(select('#' + this.parentID + 'ddlelement', document), MaskedTextBox);
this.newFields =
extend([], this.parent.dataSourceSettings.calculatedFieldSettings, null, true);
var eventArgs = {
fieldName: this.isEdit ? this.currentFieldName : inputObj.value,
calculatedField: this.getCalculatedFieldInfo(),
calculatedFieldSettings: PivotUtil.cloneCalculatedFieldSettings(this.parent.dataSourceSettings.calculatedFieldSettings),
cancel: false
};
var control = this.parent.getModuleName() === 'pivotfieldlist' &&
this.parent.isPopupView ? this.parent.pivotGridModule : this.parent;
control.trigger(events.calculatedFieldCreate, eventArgs, function (observedArgs) {
if (!observedArgs.cancel) {
var calcInfo = observedArgs.calculatedField;
if (!_this.isEdit) {
if (currentObj.parent.dataType === 'olap') {
var field = calcInfo.name;
if (currentObj.parent.olapEngineModule.fieldList[field] &&
currentObj.parent.olapEngineModule.fieldList[field].type !== 'CalculatedField') {
isExist = true;
}
}
else {
for (var _i = 0, _a = Object.keys(currentObj.parent.engineModule.fieldList); _i < _a.length; _i++) {
var key = _a[_i];
if (calcInfo.name && calcInfo.name === key &&
currentObj.parent.engineModule.fieldList[key].aggregateType !== 'CalculatedField') {
isExist = true;
}
}
}
}
if (isExist) {
currentObj.parent.pivotCommon.errorDialog.createErrorDialog(currentObj.parent.localeObj.getConstant('error'), currentObj.parent.localeObj.getConstant('fieldExist'));
return;
}
_this.existingReport = extend({}, _this.parent.dataSourceSettings, null, true);
var report = _this.parent.dataSourceSettings;
if (!isNullOrUndefined(calcInfo.name) && calcInfo.name !== '' &&
!isNullOrUndefined(calcInfo.caption) && calcInfo.caption !== '' && calcInfo.formula && calcInfo.formula !== '') {
var field = void 0;
if (_this.parent.dataType === 'olap') {
field = {
name: calcInfo.name,
formula: calcInfo.formula,
formatString: calcInfo.formatString
};
if (!isNullOrUndefined(calcInfo.hierarchyUniqueName)) {
field.hierarchyUniqueName = calcInfo.hierarchyUniqueName;
}
_this.isFieldExist = false;
if (!_this.isEdit) {
for (var i = 0; i < report.calculatedFieldSettings.length; i++) {
if (report.calculatedFieldSettings[i].name === field.name) {
_this.createConfirmDialog(currentObj.parent.localeObj.getConstant('alert'), currentObj.parent.localeObj.getConstant('confirmText'), calcInfo);
return;
}
}
}
else {
for (var i = 0; i < report.calculatedFieldSettings.length; i++) {
if (report.calculatedFieldSettings[i].name === field.name && _this.isEdit) {
report.calculatedFieldSettings[i].hierarchyUniqueName = calcInfo.hierarchyUniqueName;
_this.parent.olapEngineModule.fieldList[field.name].caption = calcInfo.caption;
report.calculatedFieldSettings[i].formatString = field.formatString;
report.calculatedFieldSettings[i].formula = field.formula;
field = report.calculatedFieldSettings[i];
_this.updateFormatSettings(report, field.name, calcInfo.formatString);
_this.parent.olapEngineModule.getFormattedFields(report.formatSettings);
if (_this.parent.olapEngineModule.formatFields[field.name]) {
if (_this.parent.olapEngineModule.formatFields[field.name].format) {
_this.parent.olapEngineModule.formatFields[field.name].format =
_this.getFormat(field.formatString);
}
else {
delete _this.parent.olapEngineModule.formatFields[field.name];
}
}
_this.isFieldExist = true;
break;
}
}
var axisFields = [report.rows, report.columns, report.values, report.filters];
var isFieldExist = false;
for (var _b = 0, axisFields_1 = axisFields; _b < axisFields_1.length; _b++) {
var fields = axisFields_1[_b];
for (var _c = 0, fields_2 = fields; _c < fields_2.length; _c++) {
var item = fields_2[_c];
if (item.isCalculatedField && field.name !== null &&
item.name === field.name && _this.isEdit) {
item.caption = calcInfo.caption;
_this.isFieldExist = true;
isFieldExist = true;
break;
}
}
if (isFieldExist) {
break;
}
}
}
if (!_this.isFieldExist) {
report.calculatedFieldSettings.push(field);
_this.updateFormatSettings(report, field.name, calcInfo.formatString);
_this.parent.olapEngineModule.getFormattedFields(report.formatSettings);
if (_this.parent.olapEngineModule.formatFields[field.name]) {
if (_this.parent.olapEngineModule.formatFields[field.name].format) {
_this.parent.olapEngineModule.formatFields[field.name].format = _this.getFormat(field.formatString);
}
else {
delete _this.parent.olapEngineModule.formatFields[field.name];
}
}
}
_this.parent.lastCalcFieldInfo = field;
}
else {
field = {
name: calcInfo.name,
caption: calcInfo.caption,
type: 'CalculatedField'
};
var cField = {
name: calcInfo.name,
formula: calcInfo.formula
};
if (!isNullOrUndefined(calcInfo.formatString)) {
cField.formatString = calcInfo.formatString;
}
_this.isFieldExist = true;
if (!_this.isEdit) {
for (var i = 0; i < report.values.length; i++) {
if (report.values[i].type === CALC && report.values[i].name === field.name) {
for (var j = 0; j < report.calculatedFieldSettings.length; j++) {
if (report.calculatedFieldSettings[j].name === field.name) {
_this.createConfirmDialog(currentObj.parent.localeObj.getConstant('alert'), currentObj.parent.localeObj.getConstant('confirmText'), calcInfo);
return;
}
}
_this.isFieldExist = false;
}
}
}
else {
for (var i = 0; i < report.values.length; i++) {
if (report.values[i].type === CALC && field.name !== null &&
report.values[i].name === field.name && _this.isEdit) {
for (var j = 0; j < report.calculatedFieldSettings.length; j++) {
if (report.calculatedFieldSettings[j].name === field.name) {
report.values.splice(i, 1);
report.values.splice(i, 0, field);
_this.currentFormula = report.calculatedFieldSettings[j].formula;
report.calculatedFieldSettings.splice(j, 1);
report.calculatedFieldSettings.splice(j, 0, cField);
_this.updateFormatSettings(report, field.name, calcInfo.formatString);
_this.isFieldExist = false;
}
}
}
}
}
if (_this.isFieldExist) {
report.values.push(field);
report.calculatedFieldSettings.push(cField);
_this.updateFormatSettings(report, field.name, calcInfo.formatString);
}
_this.parent.lastCalcFieldInfo = cField;
}
_this.addFormula(report, field.name);
}
else {
if (isNullOrUndefined(calcInfo.name) || calcInfo.name === '' ||
isNullOrUndefined(calcInfo.caption) || calcInfo.caption === '') {
var inputObj_1 = getInstance(select('#' + _this.parentID + 'ddlelement', document), MaskedTextBox);
inputObj_1.value = '';
addClass([document.getElementById(_this.parentID + 'ddlelement')], cls.EMPTY_FIELD);
document.getElementById(_this.parentID + 'ddlelement').focus();
}
else {
_this.triggerActionCompleteEvent();
}
}
}
else {
_this.endDialog();
_this.parent.lastCalcFieldInfo = {};
_this.isFormula = false;
}
});
};
CalculatedField.prototype.getCalculatedFieldInfo = function () {
var element = select('#' + this.parentID + 'calculateddialog', document);
var dialog = element ? getInstance(element, Dialog) : undefined;
var inputObj = getInstance(select('#' + this.parentID + 'ddlelement', document), MaskedTextBox);
var dropField = select('#' + this.parentID + 'droppable', document);
var dialogElement = this.parent.isAdaptive ?
this.parent.dialogRenderer.adaptiveElement.element : dialog.element;
var customFormat = getInstance(select('#' + this.parentID + 'Custom_Format_Element', dialogElement), MaskedTextBox);
var field = {
name: this.isEdit ? this.currentFieldName : inputObj.value,
caption: inputObj.value,
formula: dropField.value
};
var ddlFormatTypes = getInstance(select('#' + this.parentID + 'Format_Div', dialogElement), DropDownList);
field.formatString = (ddlFormatTypes.value === 'Custom' ? customFormat.value : (ddlFormatTypes.value === 'None' ? null : ddlFormatTypes.value));
if (this.parent.dataType === 'olap') {
var memberTypeDrop = getInstance(select('#' + this.parentID + 'Member_Type_Div', dialogElement), DropDownList);
var hierarchyDrop = getInstance(select('#' + this.parentID + 'Hierarchy_List_Div', dialogElement), DropDownList);
if (memberTypeDrop.value === 'Dimension') {
field.hierarchyUniqueName = hierarchyDrop.value;
}
}
return field;
};
CalculatedField.prototype.updateFormatSettings = function (report, fieldName, formatString) {
var newFormat = { name: fieldName, format: formatString, useGrouping: true };
var isFormatExist = false;
for (var i = 0; i < report.formatSettings.length; i++) {
if (report.formatSettings[i].name === fieldName) {
if (formatString === 'undefined' || formatString === undefined || formatString === '') {
report.formatSettings.splice(i, 1);
isFormatExist = true;
break;
}
else {
var formatObj = report.formatSettings[i].properties ?
report.formatSettings[i].properties : report.formatSettings[i];
formatObj.format = formatString;
report.formatSettings.splice(i, 1, formatObj);
isFormatExist = true;
break;
}
}
}
if (!isFormatExist && formatString !== '' && !isNullOrUndefined(formatString)) {
report.formatSettings.push(newFormat);
}
};
CalculatedField.prototype.addFormula = function (report, field) {
this.isFormula = true;
this.field = field;
this.parent.setProperties({ dataSourceSettings: report }, true);
if (this.parent.getModuleName() === 'pivotfieldlist' && (this.parent.isDeferLayoutUpdate ||
(this.parent.pivotGridModule && this.parent.pivotGridModule.pivotDeferLayoutUpdate))) {
this.parent.isRequiredUpdate = false;
}
try {
var actionInfo = {
calculatedFieldInfo: this.parent.lastCalcFieldInfo
};
this.parent.actionObj.actionInfo = actionInfo;
var actionName = (this.parent.actionObj.actionName === events.editCalculatedField) ?
events.calculatedFieldEdited : events.calculatedFieldApplied;
this.parent.actionObj.actionName = actionName;
this.parent.updateDataSource(false);
var pivot = (this.parent.getModuleName() === 'pivotfieldlist' && this.parent.pivotGridModule) ?
this.parent.pivotGridModule : this.parent;
if (pivot && pivot.dataSourceSettings.mode !== 'Server') {
this.endDialog();
}
else {
this.isRequireUpdate = true;
}
if (this.parent.getModuleName() === 'pivotfieldlist' &&
this.parent.renderMode === 'Fixed' && this.parent.isDeferLayoutUpdate) {
this.parent.pivotChange = true;
}
}
catch (exception) {
this.showError();
}
};
/**
*
* @returns {void}
* @hidden */
CalculatedField.prototype.endDialog = function () {
this.isEdit = false;
var element = select('#' + this.parentID + 'calculateddialog', document);
var dialog = element ? getInstance(element, Dialog) : undefined;
if (dialog) {
dialog.close();
}
else {
var inputObj = getInstance(select('#' + this.parentID + 'ddlelement', document), MaskedTextBox);
inputObj.value = '';
this.currentFieldName = this.formatText = this.fieldText = this.formatType = null;
this.formulaText = this.fieldType = this.parentHierarchy = null;
var dialogElement = this.parent.isAdaptive ?
this.parent.dialogRenderer.parentElement : dialog.element;
this.parent.dialogRenderer.parentElement.querySelector('.' + cls.CALCINPUT).value = '';
select('#' + this.parentID + 'droppable', this.parent.dialogRenderer.parentElement).value = '';
select('#' + this.parentID + 'Custom_Format_Element', this.parent.dialogRenderer.parentElement).value = '';
if (this.parent.dataType === 'olap') {
var memberTypeDrop = getInstance(select('#' + this.parentID + 'Member_Type_Div', dialogElement), DropDownList);
var hierarchyDrop = getInstance(select('#' + this.parentID + 'Hierarchy_List_Div', dialogElement), DropDownList);
memberTypeDrop.index = 0;
memberTypeDrop.readonly = false;
memberTypeDrop.dataBind();
hierarchyDrop.index = 0;
hierarchyDrop.enabled = false;
hierarchyDrop.dataBind();
}
var customFormat = getInstance(select('#' + this.parentID + 'Custom_Format_Element', dialogElement), MaskedTextBox);
var ddlFormatTypes = getInstance(select('#' + this.parentID + 'Format_Div', dialogElement), DropDownList);
ddlFormatTypes.index = this.parent.dataType === 'olap' ? 0 : 4;
ddlFormatTypes.dataBind();
customFormat.enabled = false;
customFormat.dataBind();
}
};
/**
*
* @returns {void}
* @hidden */
CalculatedField.prototype.showError = function () {
if (this.parent.engineModule.fieldList[this.field]) {
delete this.parent.engineModule.fieldList[this.field];
}
this.parent.setProperties({ dataSourceSettings: this.existingReport }, true);
if (this.isEdit) {
var calcFields = this.parent.dataSourceSettings.calculatedFieldSettings;
for (var i = 0; calcFields && i < calcFields.length; i++) {
if (calcFields[i].name === this.field) {
calcFields[i].formula = this.currentFormula;
break;
}
}
}
else if (this.parent.engineModule.fields) {
for (var i = 0; i < this.parent.engineModule.fields.length; i++) {
if (this.parent.engineModule.fields[i] === this.field) {
this.parent.engineModule.fields.splice(i, 1);
break;
}
}
}
this.parent.lastCalcFieldInfo = {};
this.triggerActionCompleteEvent();
this.parent.updateDataSource(false);
this.isFormula = false;
};
/**
* To get treeview data
*
* @param {PivotView | PivotFieldList} parent - It represent the parent.
* @returns {any} - Field List Data.
*/
CalculatedField.prototype.getFieldListData = function (parent) {
var fields = [];
if (this.parent.dataType === 'olap') {
fields =
PivotUtil.getClonedData(parent.olapEngineModule.fieldListData ?
parent.olapEngineModule.fieldListData : []);
for (var _i = 0, _a = fields; _i < _a.length; _i++) {
var item = _a[_i];
if (item.spriteCssClass &&
(item.spriteCssClass.indexOf('e-attributeCDB-icon') > -1 ||
item.spriteCssClass.indexOf('e-level-members') > -1)) {
item.hasChildren = true;
}
else if (item.spriteCssClass &&
(item.spriteCssClass.indexOf('e-namedSetCDB-icon') > -1)) {
item.hasChildren = false;
}
else if (item.spriteCssClass &&
(item.spriteCssClass.indexOf('e-calcMemberGroupCDB') > -1)) {
item.expanded = this.isEdit;
}
}
}
else {
for (var _b = 0, _c = (parent.engineModule.field