UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

185 lines (184 loc) • 6.87 kB
/** * DevExtreme (esm/__internal/grids/data_grid/grouping/extenders/grouping_data_controller.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/ */ import { Deferred, when } from "../../../../../core/utils/deferred"; import { countRowsBefore } from "../../../../grids/grid_core/data_controller/utils/row_changes"; import { getGroupColumnIndices, isGroupNode, isGroupRow, isSameContinuationState, isSameExpandedState } from "../utils"; export const groupingDataControllerExtender = Base => class extends Base { init() { super.init(); this.createAction("onRowExpanding"); this.createAction("onRowExpanded"); this.createAction("onRowCollapsing"); this.createAction("onRowCollapsed") } _beforeProcessItems(items) { const baseItems = super._beforeProcessItems(items); const groupColumns = this._columnsController.getGroupColumns(); if (!baseItems.length || !groupColumns.length) { return baseItems } return this.processGroupItems(baseItems, groupColumns.length) } _processItem(dataItem, options) { if (isGroupRow(dataItem)) { const processedGroupItem = this.processGroupItem(dataItem, options); options.dataIndex = 0; return processedGroupItem } return super._processItem(dataItem, options) } processGroupItem(groupItem, options) { return groupItem } getDefaultProcessGroupItemsOptions() { const scrollingMode = this.option("scrolling.mode"); return { collectContinuationItems: "virtual" !== scrollingMode && "infinite" !== scrollingMode, resultItems: [], path: [], values: [] } } processGroupItems(items, groupsCount, parentOptions) { const groupedColumns = this._columnsController.getGroupColumns(); const column = groupedColumns[groupedColumns.length - groupsCount]; const options = parentOptions ?? this.getDefaultProcessGroupItemsOptions(); const { resultItems: resultItems } = options; if (options.data) { if (options.collectContinuationItems || !options.data.isContinuation) { resultItems.push({ rowType: "group", data: options.data, groupIndex: options.path.length - 1, isExpanded: !!options.data.items, key: options.path.slice(), values: options.values.slice() }) } } if (!items) { return resultItems } if (0 === groupsCount) { resultItems.push(...items); return resultItems } for (const item of items) { if (item && isGroupNode(item)) { options.data = item; options.path.push(item.key); options.values.push(null !== column && void 0 !== column && column.deserializeValue && !column.calculateDisplayValue ? column.deserializeValue(item.key) : item.key); this.processGroupItems(item.items, groupsCount - 1, options); options.data = void 0; options.path.pop(); options.values.pop() } else { resultItems.push(item) } } return resultItems } adjustInsertRowIndex(visibleRowIndex) { const groupRowCount = countRowsBefore(this.getVisibleRows(), visibleRowIndex, "group"); return super.adjustInsertRowIndex(visibleRowIndex) + groupRowCount } getChangedColumnIndices(oldItem, newItem, visibleRowIndex, isLiveUpdate) { if ("group" === oldItem.rowType && "group" === newItem.rowType) { return getGroupColumnIndices(oldItem, newItem) } return super.getChangedColumnIndices(oldItem, newItem, visibleRowIndex, isLiveUpdate) } isSameRowState(item1, item2) { if ("group" === item1.rowType && (!isSameExpandedState(item1, item2) || !isSameContinuationState(item1, item2))) { return false } return super.isSameRowState(item1, item2) } publicMethods() { return [...super.publicMethods(), "collapseAll", "expandAll", "isRowExpanded", "expandRow", "collapseRow"] } collapseAll(groupIndex) { const dataSourceAdapter = this.dataSourceController.getAdapter(); if (null !== dataSourceAdapter && void 0 !== dataSourceAdapter && dataSourceAdapter.collapseAll(groupIndex)) { dataSourceAdapter.pageIndex(0); dataSourceAdapter.reload() } } expandAll(groupIndex) { const dataSourceAdapter = this.dataSourceController.getAdapter(); if (null !== dataSourceAdapter && void 0 !== dataSourceAdapter && dataSourceAdapter.expandAll(groupIndex)) { dataSourceAdapter.pageIndex(0); dataSourceAdapter.reload() } } changeRowExpand(key) { const expanded = this.isRowExpanded(key); const args = { key: key, expanded: expanded }; this.executeAction(expanded ? "onRowCollapsing" : "onRowExpanding", args); if (!args.cancel) { return when(this.changeRowExpandCore(key)).done(() => { args.expanded = !expanded; this.executeAction(expanded ? "onRowCollapsed" : "onRowExpanded", args) }) } return Deferred().resolve() } changeRowExpandCore(key) { const dataSourceAdapter = this.dataSourceController.getAdapter(); const d = Deferred(); if (!dataSourceAdapter) { d.resolve() } else { when(dataSourceAdapter.changeRowExpand(key)).done(() => { this.load().done(d.resolve).fail(d.reject) }).fail(d.reject) } return d } isRowExpanded(key) { var _this$dataSourceContr; return !!(null !== (_this$dataSourceContr = this.dataSourceController.getAdapter()) && void 0 !== _this$dataSourceContr && _this$dataSourceContr.isRowExpanded(key)) } expandRow(key) { if (!this.isRowExpanded(key)) { return this.changeRowExpand(key) } return Deferred().resolve() } collapseRow(key) { if (this.isRowExpanded(key)) { return this.changeRowExpand(key) } return Deferred().resolve() } optionChanged(e) { if ("grouping" === e.name) { e.handled = true; this.reset(); return } super.optionChanged(e) } };