devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
440 lines (438 loc) • 19.1 kB
JavaScript
/**
* DevExtreme (cjs/__internal/grids/data_grid/summary/m_summary.js)
* Version: 26.1.6
* Build date: Fri Oct 16 2026
*
* Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.summaryRowsViewExtender = exports.summaryEditingControllerExtender = exports.summaryDataSourceAdapterExtender = exports.renderSummaryCell = exports.FooterView = void 0;
var _query = _interopRequireDefault(require("../../../../common/data/query"));
var _store_helper = _interopRequireDefault(require("../../../../common/data/store_helper"));
var _utils = require("../../../../common/data/utils");
var _renderer = _interopRequireDefault(require("../../../../core/renderer"));
var _extend = require("../../../../core/utils/extend");
var _iterator = require("../../../../core/utils/iterator");
var _type = require("../../../../core/utils/type");
var _columns_view = require("../../../grids/grid_core/views/columns_view");
var _const = require("../../../grids/grid_core/views/const");
var _core = _interopRequireDefault(require("../core"));
var _m_aggregate_calculator = _interopRequireDefault(require("../m_aggregate_calculator"));
var _const2 = require("./const");
var _get_summary_options = require("./utils/get_summary_options");
function _interopRequireDefault(e) {
return e && e.__esModule ? e : {
default: e
}
}
const renderSummaryCell = function(cell, options, setAria) {
const $cell = (0, _renderer.default)(cell);
const {
column: column
} = options;
const {
summaryItems: summaryItems
} = options;
const $summaryItems = [];
if (!column.command && summaryItems) {
for (const summaryItem of summaryItems) {
const text = _core.default.getSummaryText(summaryItem, options.summaryTexts);
const $summaryItemElement = (0, _renderer.default)("<div>").css("textAlign", summaryItem.alignment || column.alignment).addClass(_const2.DATAGRID_SUMMARY_ITEM_CLASS).addClass(_const2.DATAGRID_TEXT_CONTENT_CLASS).addClass(summaryItem.cssClass).toggleClass(_const2.DATAGRID_GROUP_TEXT_CONTENT_CLASS, "group" === options.rowType).text(text);
setAria("label", `${column.caption??""} ${text??""}`, $summaryItemElement);
$summaryItems.push($summaryItemElement)
}
$cell.append($summaryItems)
}
};
exports.renderSummaryCell = renderSummaryCell;
const getSummaryCellOptions = function(that, options) {
const summaryTexts = that.option("summary.texts") || {};
return {
totalItem: options.row,
summaryItems: options.row.summaryCells[options.columnIndex],
summaryTexts: summaryTexts
}
};
const forEachGroup = function(groups, groupCount, callback, path) {
path = path || [];
for (let i = 0; i < groups.length; i++) {
path.push(groups[i].key);
if (1 === groupCount) {
callback(path, groups[i].items)
} else {
forEachGroup(groups[i].items, groupCount - 1, callback, path)
}
path.pop()
}
};
const applyAddedData = function(data, insertedData, groupLevel) {
if (groupLevel) {
return applyAddedData(data, insertedData.map(item => ({
items: [item]
}), groupLevel - 1))
}
return data.concat(insertedData)
};
const applyRemovedData = function(data, removedData, groupLevel) {
if (groupLevel) {
return data.map(data => {
const updatedData = {};
const updatedItems = applyRemovedData(data.items || [], removedData, groupLevel - 1);
Object.defineProperty(updatedData, "aggregates", {
get: () => data.aggregates,
set: value => {
data.aggregates = value
}
});
return (0, _extend.extend)(updatedData, data, {
items: updatedItems
})
})
}
return data.filter(data => removedData.indexOf(data) < 0)
};
const sortGroupsBySummaryCore = function(items, groups, sortByGroups) {
if (!items || !groups.length) {
return items
}
const group = groups[0];
const sorts = sortByGroups[0];
let query;
if (group && sorts && sorts.length) {
query = (0, _query.default)(items);
(0, _iterator.each)(sorts, function(index) {
if (0 === index) {
query = query.sortBy(this.selector, this.desc)
} else {
query = query.thenBy(this.selector, this.desc)
}
});
query.enumerate().done(sortedItems => {
items = sortedItems
})
}
groups = groups.slice(1);
sortByGroups = sortByGroups.slice(1);
if (groups.length && sortByGroups.length) {
(0, _iterator.each)(items, function() {
this.items = sortGroupsBySummaryCore(this.items, groups, sortByGroups)
})
}
return items
};
const sortGroupsBySummary = function(data, group, summary) {
const sortByGroups = summary && summary.sortByGroups && summary.sortByGroups();
if (sortByGroups && sortByGroups.length) {
return sortGroupsBySummaryCore(data, group, sortByGroups)
}
return data
};
class FooterView extends _columns_view.ColumnsView {
_getRows() {
return this._dataController.footerItems()
}
_getCellOptions(options) {
return (0, _extend.extend)(super._getCellOptions(options), getSummaryCellOptions(this, options))
}
_renderCellContent($cell, options) {
renderSummaryCell($cell, options, this.setAria.bind(this));
super._renderCellContent.apply(this, arguments)
}
_renderCore(change) {
let needUpdateScrollLeft = false;
const totalItem = this._dataController.footerItems()[0];
if (!change || !change.columnIndices) {
this.element().empty().addClass(_const2.DATAGRID_TOTAL_FOOTER_CLASS).toggleClass(_const2.DATAGRID_NOWRAP_CLASS, !this.option("wordWrapEnabled"));
needUpdateScrollLeft = true
}
if (totalItem && totalItem.summaryCells && totalItem.summaryCells.length) {
this._updateContent(this._renderTable({
change: change
}), change);
needUpdateScrollLeft && this.updateScrollLeftPosition()
}
return super._renderCore(change)
}
_updateContent($newTable, change) {
if (change && "update" === change.changeType && change.columnIndices) {
return this.waitAsyncTemplates().done(() => {
const $row = this.getTableElement().find(`.${_const.CLASSES.row}`);
const $newRow = $newTable.find(`.${_const.CLASSES.row}`);
this._updateCells($row, $newRow, change.columnIndices[0])
})
}
return super._updateContent.apply(this, arguments)
}
_rowClick(e) {
const item = this._dataController.footerItems()[e.rowIndex] || {};
this.executeAction("onRowClick", (0, _extend.extend)({}, e, item))
}
_columnOptionChanged(e) {
const {
optionNames: optionNames
} = e;
if (e.changeTypes.grouping) {
return
}
if (optionNames.width || optionNames.visibleWidth) {
super._columnOptionChanged(e)
}
}
_handleDataChanged(e) {
const {
changeType: changeType
} = e;
if ("update" === e.changeType && e.repaintChangesOnly) {
if (!e.totalColumnIndices) {
this.render()
} else if (e.totalColumnIndices.length) {
this.render(null, {
changeType: "update",
columnIndices: [e.totalColumnIndices]
})
}
} else if ("refresh" === changeType || "append" === changeType || "prepend" === changeType) {
this.render()
}
}
_createRow(row) {
const $row = super._createRow.apply(this, arguments);
if (row.rowType === _const2.DATAGRID_TOTAL_FOOTER_ROW_TYPE) {
$row.addClass(_const2.DATAGRID_FOOTER_ROW_CLASS);
$row.addClass(_const2.DATAGRID_CELL_DISABLED);
$row.attr("tabindex", 0)
}
return $row
}
getHeight() {
return this.getElementHeight()
}
isVisible() {
return !!this._dataController.footerItems().length
}
}
exports.FooterView = FooterView;
const summaryDataSourceAdapterExtender = Base => class extends Base {
init(dataSource) {
super.init(dataSource);
this.columnsController = this.getController("columns");
this.editingController = this.getController("editing");
this._totalAggregates = []
}
getSummary(remoteOperations) {
const summary = this.option("summary");
if (!summary) {
return
}
const result = (0, _get_summary_options.getSummaryOptions)({
summary: summary,
sortByGroupSummaryInfo: this.option("sortByGroupSummaryInfo"),
remoteOperations: remoteOperations ?? this.remoteOperations(),
getUpdatedItemData: data => this.editingController.getUpdatedData(data),
columnOption: id => this.columnsController.columnOption(id),
groupColumns: this.columnsController.getGroupColumns(),
component: this.component
});
return result
}
totalAggregates() {
return this._totalAggregates
}
isLastLevelGroupItemsPagingLocal() {
const summary = this.getSummary();
const sortByGroupsInfo = null === summary || void 0 === summary ? void 0 : summary.sortByGroups();
return null === sortByGroupsInfo || void 0 === sortByGroupsInfo ? void 0 : sortByGroupsInfo.length
}
sortLastLevelGroupItems(items, groups, paths) {
const groupedItems = _store_helper.default.multiLevelGroup((0, _query.default)(items), groups).toArray();
let result = [];
paths.forEach(path => {
forEachGroup(groupedItems, groups.length, (itemsPath, items) => {
if (path.toString() === itemsPath.toString()) {
result = result.concat(items)
}
})
});
return result
}
_customizeRemoteOperations(options) {
const summary = this.getSummary();
if (summary) {
if (options.remoteOperations.summary) {
if (!options.isCustomLoading || options.storeLoadOptions.isLoadingAll) {
if (options.storeLoadOptions.group) {
if (options.remoteOperations.grouping) {
options.storeLoadOptions.groupSummary = summary.groupAggregates
} else if (summary.groupAggregates.length) {
options.remoteOperations.paging = false
}
}
options.storeLoadOptions.totalSummary = summary.totalAggregates
}
} else if (summary.totalAggregates.length || summary.groupAggregates.length && options.storeLoadOptions.group) {
options.remoteOperations.paging = false
}
}
super._customizeRemoteOperations.apply(this, arguments);
const cachedExtra = options.cachedData.extra;
if (null !== cachedExtra && void 0 !== cachedExtra && cachedExtra.summary && !options.isCustomLoading) {
options.storeLoadOptions.totalSummary = void 0
}
}
calculateTotalAggregates(data, totalAggregates, groupAggregates, groupLevel) {
let dataWithEditingChanges = data;
if (this.option("summary.recalculateWhileEditing")) {
const insertedData = this.editingController.getInsertedData();
if (insertedData.length) {
dataWithEditingChanges = applyAddedData(dataWithEditingChanges, insertedData, groupLevel)
}
const removedData = this.editingController.getRemovedData();
if (removedData.length) {
dataWithEditingChanges = applyRemovedData(dataWithEditingChanges, removedData, groupLevel)
}
}
const calculator = new _m_aggregate_calculator.default({
data: dataWithEditingChanges,
totalAggregates: totalAggregates,
groupAggregates: groupAggregates,
groupLevel: groupLevel
});
calculator.calculate();
return calculator.totalAggregates()
}
customizeLoadResultHandlerCore(options) {
if (options.isCustomLoading && !options.storeLoadOptions.isLoadingAll) {
super.customizeLoadResultHandlerCore(options);
return
}
const groups = (0, _utils.normalizeSortingInfo)(options.storeLoadOptions.group ?? options.loadOptions.group ?? []);
const remoteOperations = options.remoteOperations ?? {};
const summary = this.getSummary(remoteOperations);
if (remoteOperations.summary) {
if (!remoteOperations.paging && groups.length && summary) {
if (!remoteOperations.grouping) {
this.calculateTotalAggregates(options.data, [], summary.groupAggregates, groups.length)
}
options.data = sortGroupsBySummary(options.data, groups, summary)
}
} else if (!remoteOperations.paging && summary) {
var _options$cachedData;
const operationTypes = options.operationTypes ?? {};
const hasOperations = Object.keys(operationTypes).some(type => operationTypes[type]);
if (!hasOperations || !(null !== (_options$cachedData = options.cachedData) && void 0 !== _options$cachedData && null !== (_options$cachedData = _options$cachedData.extra) && void 0 !== _options$cachedData && _options$cachedData.summary) || groups.length && summary.groupAggregates.length) {
const totalAggregates = this.calculateTotalAggregates(options.data, summary.totalAggregates, summary.groupAggregates, groups.length);
options.extra = (0, _type.isPlainObject)(options.extra) ? options.extra : {};
options.extra.summary = totalAggregates;
if (options.cachedData) {
options.cachedData.extra = options.extra
}
}
options.data = sortGroupsBySummary(options.data, groups, summary)
}
if (!options.isCustomLoading) {
var _options$extra;
this._totalAggregates = (null === (_options$extra = options.extra) || void 0 === _options$extra ? void 0 : _options$extra.summary) ?? this._totalAggregates
}
super.customizeLoadResultHandlerCore(options)
}
};
exports.summaryDataSourceAdapterExtender = summaryDataSourceAdapterExtender;
const summaryEditingControllerExtender = Base => class extends Base {
_refreshSummary() {
if (this.option("summary.recalculateWhileEditing") && !this.isSaving()) {
this._dataController.refresh({
load: true,
changesOnly: true
})
}
}
_addChange(params) {
const result = super._addChange.apply(this, arguments);
if (params.type) {
this._refreshSummary()
}
return result
}
_removeChange() {
const result = super._removeChange.apply(this, arguments);
this._refreshSummary();
return result
}
cancelEditData() {
const result = super.cancelEditData.apply(this, arguments);
this._refreshSummary();
return result
}
};
exports.summaryEditingControllerExtender = summaryEditingControllerExtender;
const summaryRowsViewExtender = Base => class extends Base {
_createRow(row) {
const $row = super._createRow.apply(this, arguments);
row && $row.addClass(row.rowType === _const2.DATAGRID_GROUP_FOOTER_ROW_TYPE ? _const2.DATAGRID_GROUP_FOOTER_CLASS : "");
return $row
}
_renderCells($row, options) {
super._renderCells.apply(this, arguments);
if ("group" === options.row.rowType && options.row.summaryCells && options.row.summaryCells.length) {
this._renderGroupSummaryCells($row, options)
}
}
_hasAlignByColumnSummaryItems(columnIndex, options) {
const column = options.columns[columnIndex];
const isGrouped = (0, _type.isDefined)(column.groupIndex);
const isVirtual = "virtual" === column.command;
const hasSummaryCells = !!options.row.summaryCells[columnIndex].length;
return !isGrouped && (isVirtual || hasSummaryCells)
}
_getAlignByColumnCellCount(groupCellColSpan, options) {
let alignByColumnCellCount = 0;
for (let i = 1; i < groupCellColSpan; i++) {
const columnIndex = options.row.summaryCells.length - i;
const hasAlignBySummary = this._hasAlignByColumnSummaryItems(columnIndex, options);
if (hasAlignBySummary) {
alignByColumnCellCount = i
}
}
return alignByColumnCellCount
}
_renderGroupSummaryCells($row, options) {
const $groupCell = $row.children().last();
const groupCellColSpan = Number($groupCell.attr("colSpan")) || 1;
const alignByColumnCellCount = this._getAlignByColumnCellCount(groupCellColSpan, options);
this._renderGroupSummaryCellsCore($groupCell, options, groupCellColSpan, alignByColumnCellCount)
}
_renderGroupSummaryCellsCore($groupCell, options, groupCellColSpan, alignByColumnCellCount) {
if (alignByColumnCellCount > 0) {
$groupCell.attr("colSpan", groupCellColSpan - alignByColumnCellCount);
for (let i = 0; i < alignByColumnCellCount; i++) {
const columnIndex = options.columns.length - alignByColumnCellCount + i;
this._renderCell($groupCell.parent(), (0, _extend.extend)({
column: options.columns[columnIndex],
columnIndex: this._getSummaryCellIndex(columnIndex, options.columns)
}, options))
}
}
}
_getSummaryCellIndex(columnIndex, columns) {
return columnIndex
}
_getCellTemplate(options) {
if (!options.column.command && !(0, _type.isDefined)(options.column.groupIndex) && options.summaryItems && options.summaryItems.length) {
return (cell, options) => renderSummaryCell(cell, options, this.setAria.bind(this))
}
return super._getCellTemplate(options)
}
_getCellOptions(options) {
const that = this;
const parameters = super._getCellOptions(options);
if (options.row.summaryCells) {
return (0, _extend.extend)(parameters, getSummaryCellOptions(that, options))
}
return parameters
}
};
exports.summaryRowsViewExtender = summaryRowsViewExtender;