|
| 1 | +import type { DeferredObj } from '@js/core/utils/deferred'; |
| 2 | +import { Deferred, when } from '@js/core/utils/deferred'; |
| 3 | +import type { Properties } from '@js/ui/data_grid'; |
| 4 | +import type { DataController } from '@ts/grids/grid_core/data_controller/data_controller'; |
| 5 | +import type { ItemProcessingOptions, ProcessedItem } from '@ts/grids/grid_core/data_controller/types'; |
| 6 | +import type { RawItemData } from '@ts/grids/grid_core/data_source_adapter/types'; |
| 7 | +import type { |
| 8 | + ModuleType, |
| 9 | + OptionChanged, |
| 10 | + OptionChangedFor, |
| 11 | + RowKey, |
| 12 | +} from '@ts/grids/grid_core/m_types'; |
| 13 | + |
| 14 | +import type { |
| 15 | + ChangeRowExpandArgs, GroupItem, ProcessGroupItemsOptions, |
| 16 | +} from '../types'; |
| 17 | +import { |
| 18 | + isGroupNode, isGroupRow, isSameContinuationState, isSameExpandedState, |
| 19 | +} from '../utils'; |
| 20 | + |
| 21 | +export const groupingDataControllerExtender = ( |
| 22 | + Base: ModuleType<DataController>, |
| 23 | +): ModuleType<DataController> => class GroupingDataControllerExtender extends Base { |
| 24 | + public init(): void { |
| 25 | + super.init(); |
| 26 | + |
| 27 | + this.createAction('onRowExpanding'); |
| 28 | + this.createAction('onRowExpanded'); |
| 29 | + this.createAction('onRowCollapsing'); |
| 30 | + this.createAction('onRowCollapsed'); |
| 31 | + } |
| 32 | + |
| 33 | + protected _beforeProcessItems(items: RawItemData[]): (RawItemData | GroupItem)[] { |
| 34 | + const baseItems = super._beforeProcessItems(items); |
| 35 | + |
| 36 | + const groupColumns = this._columnsController.getGroupColumns(); |
| 37 | + |
| 38 | + if (!baseItems.length || !groupColumns.length) { |
| 39 | + return baseItems; |
| 40 | + } |
| 41 | + |
| 42 | + return this.processGroupItems(baseItems, groupColumns.length); |
| 43 | + } |
| 44 | + |
| 45 | + protected _processItem( |
| 46 | + dataItem: RawItemData | GroupItem, |
| 47 | + options: ItemProcessingOptions, |
| 48 | + ): ProcessedItem { |
| 49 | + if (isGroupRow(dataItem)) { |
| 50 | + const processedGroupItem = this.processGroupItem(dataItem, options); |
| 51 | + options.dataIndex = 0; |
| 52 | + return processedGroupItem; |
| 53 | + } |
| 54 | + |
| 55 | + return super._processItem(dataItem, options); |
| 56 | + } |
| 57 | + |
| 58 | + protected processGroupItem( |
| 59 | + groupItem: GroupItem, |
| 60 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 61 | + options: ItemProcessingOptions, |
| 62 | + ): ProcessedItem { |
| 63 | + return groupItem; |
| 64 | + } |
| 65 | + |
| 66 | + private getDefaultProcessGroupItemsOptions(): ProcessGroupItemsOptions { |
| 67 | + const scrollingMode = this.option('scrolling.mode'); |
| 68 | + |
| 69 | + return { |
| 70 | + collectContinuationItems: scrollingMode !== 'virtual' && scrollingMode !== 'infinite', |
| 71 | + resultItems: [], |
| 72 | + path: [], |
| 73 | + values: [], |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + protected processGroupItems( |
| 78 | + items: RawItemData[] | null | undefined, |
| 79 | + groupsCount: number, |
| 80 | + parentOptions?: ProcessGroupItemsOptions, |
| 81 | + ): (RawItemData | GroupItem)[] { |
| 82 | + const groupedColumns = this._columnsController.getGroupColumns(); |
| 83 | + const column = groupedColumns[groupedColumns.length - groupsCount]; |
| 84 | + |
| 85 | + const options = parentOptions ?? this.getDefaultProcessGroupItemsOptions(); |
| 86 | + const { resultItems } = options; |
| 87 | + |
| 88 | + if (options.data) { |
| 89 | + if (options.collectContinuationItems || !options.data.isContinuation) { |
| 90 | + resultItems.push({ |
| 91 | + rowType: 'group', |
| 92 | + data: options.data, |
| 93 | + groupIndex: options.path.length - 1, |
| 94 | + isExpanded: !!options.data.items, |
| 95 | + key: options.path.slice(), |
| 96 | + values: options.values.slice(), |
| 97 | + }); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (!items) { |
| 102 | + return resultItems; |
| 103 | + } |
| 104 | + |
| 105 | + if (groupsCount === 0) { |
| 106 | + resultItems.push(...items); |
| 107 | + |
| 108 | + return resultItems; |
| 109 | + } |
| 110 | + |
| 111 | + for (const item of items) { |
| 112 | + if (item && isGroupNode(item)) { |
| 113 | + options.data = item; |
| 114 | + options.path.push(item.key); |
| 115 | + options.values.push( |
| 116 | + column?.deserializeValue && !column.calculateDisplayValue |
| 117 | + ? column.deserializeValue(item.key) |
| 118 | + : item.key, |
| 119 | + ); |
| 120 | + |
| 121 | + this.processGroupItems(item.items, groupsCount - 1, options); |
| 122 | + |
| 123 | + options.data = undefined; |
| 124 | + options.path.pop(); |
| 125 | + options.values.pop(); |
| 126 | + } else { |
| 127 | + resultItems.push(item); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return resultItems; |
| 132 | + } |
| 133 | + |
| 134 | + protected isSameRowState(item1: ProcessedItem, item2: ProcessedItem): boolean { |
| 135 | + if (item1.rowType === 'group' |
| 136 | + && (!isSameExpandedState(item1, item2) || !isSameContinuationState(item1, item2))) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
| 140 | + return super.isSameRowState(item1, item2); |
| 141 | + } |
| 142 | + |
| 143 | + public publicMethods(): string[] { |
| 144 | + const groupingPublicMethods = ['collapseAll', 'expandAll', 'isRowExpanded', 'expandRow', 'collapseRow']; |
| 145 | + |
| 146 | + return [...super.publicMethods(), ...groupingPublicMethods]; |
| 147 | + } |
| 148 | + |
| 149 | + private collapseAll(groupIndex: number): void { |
| 150 | + const dataSource = this._dataSource; |
| 151 | + if (dataSource?.collapseAll(groupIndex)) { |
| 152 | + dataSource.pageIndex(0); |
| 153 | + dataSource.reload(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private expandAll(groupIndex: number): void { |
| 158 | + const dataSource = this._dataSource; |
| 159 | + if (dataSource?.expandAll(groupIndex)) { |
| 160 | + dataSource.pageIndex(0); |
| 161 | + dataSource.reload(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private changeRowExpand(key: RowKey): DeferredObj<unknown> { |
| 166 | + const expanded = this.isRowExpanded(key); |
| 167 | + const args: ChangeRowExpandArgs = { |
| 168 | + key, |
| 169 | + expanded, |
| 170 | + }; |
| 171 | + |
| 172 | + this.executeAction(expanded ? 'onRowCollapsing' : 'onRowExpanding', args); |
| 173 | + |
| 174 | + if (!args.cancel) { |
| 175 | + return when(this.changeRowExpandCore(key)).done(() => { |
| 176 | + args.expanded = !expanded; |
| 177 | + this.executeAction(expanded ? 'onRowCollapsed' : 'onRowExpanded', args); |
| 178 | + }); |
| 179 | + } |
| 180 | + |
| 181 | + return Deferred().resolve(); |
| 182 | + } |
| 183 | + |
| 184 | + protected changeRowExpandCore(key: RowKey): DeferredObj<unknown> { |
| 185 | + const dataSource = this._dataSource; |
| 186 | + |
| 187 | + const d = Deferred(); |
| 188 | + if (!dataSource) { |
| 189 | + d.resolve(); |
| 190 | + } else { |
| 191 | + when(dataSource.changeRowExpand(key)).done(() => { |
| 192 | + // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 193 | + this.load().done(d.resolve).fail(d.reject); |
| 194 | + // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 195 | + }).fail(d.reject); |
| 196 | + } |
| 197 | + |
| 198 | + return d; |
| 199 | + } |
| 200 | + |
| 201 | + private isRowExpanded(key: RowKey): boolean { |
| 202 | + return !!this._dataSource?.isRowExpanded(key); |
| 203 | + } |
| 204 | + |
| 205 | + private expandRow(key: RowKey): DeferredObj<unknown> { |
| 206 | + if (!this.isRowExpanded(key)) { |
| 207 | + return this.changeRowExpand(key); |
| 208 | + } |
| 209 | + |
| 210 | + return Deferred().resolve(); |
| 211 | + } |
| 212 | + |
| 213 | + private collapseRow(key: RowKey): DeferredObj<unknown> { |
| 214 | + if (this.isRowExpanded(key)) { |
| 215 | + return this.changeRowExpand(key); |
| 216 | + } |
| 217 | + |
| 218 | + return Deferred().resolve(); |
| 219 | + } |
| 220 | + |
| 221 | + public optionChanged(e: OptionChanged | OptionChangedFor<Pick<Properties, 'grouping'>>): void { |
| 222 | + if (e.name === 'grouping') { |
| 223 | + e.handled = true; |
| 224 | + this.reset(); |
| 225 | + return; |
| 226 | + } |
| 227 | + |
| 228 | + super.optionChanged(e); |
| 229 | + } |
| 230 | +}; |
0 commit comments