@syncfusion/ej2-grids
Version:
Feature-rich JavaScript datagrid (datatable) control with built-in support for editing, filtering, grouping, paging, sorting, and exporting to Excel.
135 lines (134 loc) • 5.82 kB
JavaScript
import { remove, extend, getValue } from '@syncfusion/ej2-base';
import { isNullOrUndefined, addClass } from '@syncfusion/ej2-base';
import { CellType } from '../base/enum';
import { ValueFormatter } from '../services/value-formatter';
import { uiUpdate, initialEnd, dataReady, modelChanged, refreshAggregates, refreshFooterRenderer, groupAggregates, destroy } from '../base/constant';
import { FooterRenderer } from '../renderer/footer-renderer';
import { SummaryCellRenderer } from '../renderer/summary-cell-renderer';
import { GroupSummaryModelGenerator, CaptionSummaryModelGenerator } from '../services/summary-model-generator';
import * as literals from '../base/string-literals';
/**
* Summary Action controller.
*/
var Aggregate = /** @class */ (function () {
function Aggregate(parent, locator) {
this.parent = parent;
this.locator = locator;
this.addEventListener();
}
Aggregate.prototype.getModuleName = function () {
return 'aggregate';
};
Aggregate.prototype.initiateRender = function () {
var _this = this;
var cellFac = this.locator.getService('cellRendererFactory');
var instance = new SummaryCellRenderer(this.parent, this.locator);
var type = [CellType.Summary, CellType.CaptionSummary, CellType.GroupSummary];
for (var i = 0; i < type.length; i++) {
cellFac.addCellRenderer(type[parseInt(i.toString(), 10)], instance);
}
this.footerRenderer = new FooterRenderer(this.parent, this.locator);
this.footerRenderer.renderPanel();
this.footerRenderer.renderTable();
var footerContent = this.footerRenderer.getPanel();
if (this.parent.element.scrollHeight >= this.parent.getHeight(this.parent.height)
&& footerContent) {
addClass([footerContent], ['e-footerpadding']);
}
this.locator.register('footerRenderer', this.footerRenderer);
var fn = function () {
_this.prepareSummaryInfo();
_this.parent.off(dataReady, fn);
};
this.parent.on(dataReady, fn, this);
this.parent.on(dataReady, this.footerRenderer.refresh, this.footerRenderer);
};
/**
* @returns {void}
* @hidden
*/
Aggregate.prototype.prepareSummaryInfo = function () {
var _this = this;
summaryIterator(this.parent.aggregates, function (column) {
var cFormat = getValue('customFormat', column);
if (!isNullOrUndefined(cFormat)) {
column.setPropertiesSilent({ format: cFormat });
}
if (typeof (column.format) === 'object') {
var valueFormatter = new ValueFormatter();
column.setFormatter(valueFormatter.getFormatFunction(extend({}, column.format)));
}
else if (typeof (column.format) === 'string') {
var fmtr = _this.locator.getService('valueFormatter');
column.setFormatter(fmtr.getFormatFunction({ format: column.format }));
}
column.setPropertiesSilent({ columnName: column.columnName || column.field });
});
};
Aggregate.prototype.onPropertyChanged = function (e) {
if (e.module !== this.getModuleName()) {
return;
}
if (isNullOrUndefined(this.footerRenderer)) {
this.initiateRender();
}
this.prepareSummaryInfo();
this.footerRenderer.refresh();
var cModel = new CaptionSummaryModelGenerator(this.parent);
var gModel = new GroupSummaryModelGenerator(this.parent);
if (gModel.getData().length !== 0 || !cModel.isEmpty()) {
this.parent.notify(modelChanged, {});
}
};
Aggregate.prototype.addEventListener = function () {
if (this.parent.isDestroyed) {
return;
}
this.parent.on(initialEnd, this.initiateRender, this);
this.parent.on(uiUpdate, this.onPropertyChanged, this);
this.parent.on(refreshAggregates, this.refresh, this);
this.parent.on(destroy, this.destroy, this);
};
Aggregate.prototype.removeEventListener = function () {
if (this.parent.isDestroyed) {
return;
}
this.footerRenderer.removeEventListener();
this.parent.off(initialEnd, this.initiateRender);
this.parent.off(dataReady, this.footerRenderer.refresh);
this.parent.off(uiUpdate, this.onPropertyChanged);
this.parent.off(refreshAggregates, this.refresh);
this.parent.off(destroy, this.destroy);
if (this.parent.element.querySelector('.' + literals.gridFooter)) {
remove(this.parent.element.querySelector('.' + literals.gridFooter));
}
};
Aggregate.prototype.destroy = function () {
this.removeEventListener();
};
Aggregate.prototype.refresh = function (data, element) {
var editedData = data instanceof Array ? data : [data];
this.parent.notify(refreshFooterRenderer, editedData);
if (element) {
editedData.row = element;
}
if (this.parent.groupSettings.columns.length > 0) {
this.parent.notify(groupAggregates, editedData);
}
};
return Aggregate;
}());
export { Aggregate };
/**
* @param {AggregateRowModel[]} aggregates - specifies the AggregateRowModel
* @param {Function} callback - specifies the Function
* @returns {void}
* @private
*/
export function summaryIterator(aggregates, callback) {
for (var i = 0; i < aggregates.length; i++) {
for (var j = 0; j < aggregates[parseInt(i.toString(), 10)].columns.length; j++) {
callback(aggregates[parseInt(i.toString(), 10)].columns[parseInt(j.toString(), 10)], aggregates[parseInt(i.toString(), 10)]);
}
}
}