@omnigrid/core 0.1.24 → 0.1.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/grid.d.ts +99 -2
- package/dist/grid.js +336 -10
- package/dist/groupingPipeline.d.ts +49 -0
- package/dist/groupingPipeline.js +211 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +1 -0
- package/dist/pipeline.d.ts +68 -0
- package/dist/pipeline.js +62 -0
- package/dist/types.d.ts +240 -38
- package/package.json +41 -41
package/dist/grid.d.ts
CHANGED
|
@@ -2,7 +2,9 @@ import type { FilterCondition, FilterModel } from "./filtering";
|
|
|
2
2
|
import type { UIPopupAnchor, UIPopupDescriptor } from "./ui";
|
|
3
3
|
import { IconRegistry } from "./icons";
|
|
4
4
|
import { SlotManager } from "./slots";
|
|
5
|
-
import
|
|
5
|
+
import { type StateUpdater } from "./store";
|
|
6
|
+
import { DataPipeline } from "./pipeline";
|
|
7
|
+
import type { AggregationFunc, ColumnLeafDef, DataProcessor, GridApi, GridEvents, GridOptions, GridPlugin, GridState, RowClickEvent, RowHeightUpdateOptions, RowHoverEvent, RowHierarchy, RowId, RowSpanLayout, ScrollPosition, SlotMount, SlotName, TransactionResult, TreeRowNode, RowTransaction, ViewportData, ViewportState } from "./types";
|
|
6
8
|
import type { OverlayOptions, OverlayState } from "./overlays";
|
|
7
9
|
export declare class Grid<T> implements GridApi<T> {
|
|
8
10
|
private readonly store;
|
|
@@ -11,12 +13,20 @@ export declare class Grid<T> implements GridApi<T> {
|
|
|
11
13
|
private readonly cleanupPlugins;
|
|
12
14
|
private readonly pluginRegistrations;
|
|
13
15
|
private readonly dataProcessors;
|
|
16
|
+
private rowHierarchy?;
|
|
14
17
|
private readonly resolveRowId;
|
|
15
18
|
readonly slots: SlotManager<T>;
|
|
16
19
|
readonly icons: IconRegistry<T>;
|
|
20
|
+
readonly pipeline: DataPipeline<T>;
|
|
21
|
+
readonly methods: Record<string, unknown>;
|
|
17
22
|
private processedDataCache;
|
|
18
23
|
/** Output of the built-in filter stage; sits in front of every data processor. */
|
|
19
24
|
private filteredDataCache;
|
|
25
|
+
/** Flattened grouped rows (TreeRowNode[]), invalidated on structural/group changes. */
|
|
26
|
+
private groupedRowsCache;
|
|
27
|
+
private groupedRowsRevision;
|
|
28
|
+
/** Column id -> aggregation function map, registered by aggregation plugins. */
|
|
29
|
+
private aggregationFuncs;
|
|
20
30
|
private filterModel;
|
|
21
31
|
private filterMenuProviders;
|
|
22
32
|
private readonly popups;
|
|
@@ -54,6 +64,30 @@ export declare class Grid<T> implements GridApi<T> {
|
|
|
54
64
|
/** Returns the calculated width for a visible column, including off-screen columns. */
|
|
55
65
|
getColumnWidth(columnId: string): number | undefined;
|
|
56
66
|
getState(): GridState<T>;
|
|
67
|
+
/** Returns the row hierarchy capability registered by the active row-model plugin. @api */
|
|
68
|
+
getRowHierarchy(): RowHierarchy<T> | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* Registers the row hierarchy capability for composite row models.
|
|
71
|
+
* @param hierarchy - The active row hierarchy implementation.
|
|
72
|
+
* @returns Cleanup callback to unregister the provider.
|
|
73
|
+
* @api
|
|
74
|
+
*/
|
|
75
|
+
registerRowHierarchy(hierarchy: RowHierarchy<T>): () => void;
|
|
76
|
+
/**
|
|
77
|
+
* Updates the grid state via partial patch or functional updater.
|
|
78
|
+
* Accessible by plugins to update their contributed state slices.
|
|
79
|
+
* @param updater - Partial state patch or reducer function.
|
|
80
|
+
* @api
|
|
81
|
+
*/
|
|
82
|
+
setState(updater: StateUpdater<GridState<T>>): void;
|
|
83
|
+
/**
|
|
84
|
+
* Registers methods dynamically contributed by plugins.
|
|
85
|
+
* These methods are attached both to `api.methods` and directly to `api`.
|
|
86
|
+
* @param methods - Methods to register.
|
|
87
|
+
* @returns Cleanup callback to unregister the methods.
|
|
88
|
+
* @api
|
|
89
|
+
*/
|
|
90
|
+
registerMethods(methods: Record<string, unknown>): () => void;
|
|
57
91
|
/** Returns the current ephemeral scroll position (not part of Store state). */
|
|
58
92
|
getScrollPosition(): ScrollPosition;
|
|
59
93
|
/**
|
|
@@ -62,6 +96,14 @@ export declare class Grid<T> implements GridApi<T> {
|
|
|
62
96
|
* @api
|
|
63
97
|
*/
|
|
64
98
|
scrollTo(position: Partial<ScrollPosition>): void;
|
|
99
|
+
/**
|
|
100
|
+
* Returns the flat array of visible leaf columns.
|
|
101
|
+
*
|
|
102
|
+
* A column is visible when it is not explicitly `hidden`. In `'single'` group
|
|
103
|
+
* column mode with active grouping, all grouped columns except the primary
|
|
104
|
+
* one are hidden.
|
|
105
|
+
*/
|
|
106
|
+
private getVisibleLeafColumns;
|
|
65
107
|
getViewportData(): ViewportData<T>;
|
|
66
108
|
/**
|
|
67
109
|
* Expands the shared scrollable column window so `colSpan` origins stay
|
|
@@ -117,7 +159,62 @@ export declare class Grid<T> implements GridApi<T> {
|
|
|
117
159
|
*/
|
|
118
160
|
resolveColSpan(data: T, index: number, column: ColumnLeafDef<T>, columnIndex: number, columnCount: number): number;
|
|
119
161
|
getProcessedData(): T[];
|
|
120
|
-
|
|
162
|
+
/**
|
|
163
|
+
* Returns the active dataset consumed by the virtualizer and DOM pool.
|
|
164
|
+
* When grouping or a custom row model is active, returns the transformed rows;
|
|
165
|
+
* otherwise returns the processed data rows.
|
|
166
|
+
* @api
|
|
167
|
+
*/
|
|
168
|
+
getRows(): unknown[];
|
|
169
|
+
/**
|
|
170
|
+
* Returns the flattened `TreeRowNode[]` consumed by the virtualizer when
|
|
171
|
+
* grouping is active. Rebuilds the tree + aggregates only when the
|
|
172
|
+
* structural revision changes (data / columns / groupBy / expanded state).
|
|
173
|
+
*
|
|
174
|
+
* @returns The flattened group/data rows, empty when grouping is disabled.
|
|
175
|
+
* @api
|
|
176
|
+
*/
|
|
177
|
+
getGroupedRows(): TreeRowNode<T>[];
|
|
178
|
+
/**
|
|
179
|
+
* Sets the grouping fields. Replaces any existing grouping and resets the
|
|
180
|
+
* expanded state to all-expanded. Scroll position is reset to the top.
|
|
181
|
+
*
|
|
182
|
+
* @param fields - Grouping fields in nesting order, or empty/null to disable grouping.
|
|
183
|
+
* @api
|
|
184
|
+
*/
|
|
185
|
+
setGroupBy(fields: string[] | null | undefined, expandedGroups?: Record<string, boolean>): void;
|
|
186
|
+
/**
|
|
187
|
+
* Toggles the expanded state of a single group.
|
|
188
|
+
*
|
|
189
|
+
* @param groupId - Stable group id (the `id` field of a `GroupRowNode`).
|
|
190
|
+
* @api
|
|
191
|
+
*/
|
|
192
|
+
toggleGroup(groupId: string): void;
|
|
193
|
+
/** Expands every group. @api */
|
|
194
|
+
expandAll(): void;
|
|
195
|
+
/** Collapses every group. @api */
|
|
196
|
+
collapseAll(): void;
|
|
197
|
+
/** Replaces the expanded-state map for groups. @api */
|
|
198
|
+
setExpandedGroups(expandedGroups: Record<string, boolean>): void;
|
|
199
|
+
/**
|
|
200
|
+
* Registers column id -> aggregation function mappings. Called by
|
|
201
|
+
* aggregation plugins (e.g. `@omnigrid/aggregation-plugin`). Replaces any
|
|
202
|
+
* previously registered functions.
|
|
203
|
+
* @param funcs - Column id to aggregation function map.
|
|
204
|
+
* @returns Unregister function.
|
|
205
|
+
* @api
|
|
206
|
+
*/
|
|
207
|
+
registerAggregationFuncs(funcs: Record<string, AggregationFunc<T>>): () => void;
|
|
208
|
+
/** Configures the group column UI mode and header. @api */
|
|
209
|
+
setGroupColumnConfig(config: {
|
|
210
|
+
mode: 'single' | 'perLevel';
|
|
211
|
+
header?: string;
|
|
212
|
+
showChildCount?: boolean;
|
|
213
|
+
groupRowRenderer?: any;
|
|
214
|
+
}): void;
|
|
215
|
+
/** Sets or clears the custom group row renderer. @api */
|
|
216
|
+
setGroupRowRenderer(): void;
|
|
217
|
+
getFilteredData(): T[];
|
|
121
218
|
private static cloneFilterModel;
|
|
122
219
|
setFilterModel(model: FilterModel): void;
|
|
123
220
|
getFilterModel(): FilterModel;
|
package/dist/grid.js
CHANGED
|
@@ -6,6 +6,8 @@ import { evaluateAutoOverlay } from "./overlays";
|
|
|
6
6
|
import { IconRegistry } from "./icons";
|
|
7
7
|
import { SlotManager } from "./slots";
|
|
8
8
|
import { Store } from "./store";
|
|
9
|
+
import { DataPipeline } from "./pipeline";
|
|
10
|
+
import { buildGroupTree, computeAggregations, flattenTree } from "./groupingPipeline";
|
|
9
11
|
import { Virtualizer } from "./virtualizer";
|
|
10
12
|
export class Grid {
|
|
11
13
|
constructor(options) {
|
|
@@ -14,9 +16,15 @@ export class Grid {
|
|
|
14
16
|
this.pluginRegistrations = new Map();
|
|
15
17
|
this.dataProcessors = new Set();
|
|
16
18
|
this.icons = new IconRegistry();
|
|
19
|
+
this.methods = {};
|
|
17
20
|
this.processedDataCache = null;
|
|
18
21
|
/** Output of the built-in filter stage; sits in front of every data processor. */
|
|
19
22
|
this.filteredDataCache = null;
|
|
23
|
+
/** Flattened grouped rows (TreeRowNode[]), invalidated on structural/group changes. */
|
|
24
|
+
this.groupedRowsCache = null;
|
|
25
|
+
this.groupedRowsRevision = -1;
|
|
26
|
+
/** Column id -> aggregation function map, registered by aggregation plugins. */
|
|
27
|
+
this.aggregationFuncs = new Map();
|
|
20
28
|
this.filterModel = {};
|
|
21
29
|
this.filterMenuProviders = 0;
|
|
22
30
|
this.popups = new PopupManager((popup) => this.events.emit("popupChange", popup));
|
|
@@ -44,6 +52,12 @@ export class Grid {
|
|
|
44
52
|
columns: applyWrapText(options.columns, options.wrapText),
|
|
45
53
|
viewport: { width: 0, height: 0, scrollTop: 0, scrollLeft: 0 },
|
|
46
54
|
rowHeight: options.rowHeight ?? 32,
|
|
55
|
+
groupBy: [],
|
|
56
|
+
expandedGroups: {},
|
|
57
|
+
groupColumnMode: 'single',
|
|
58
|
+
groupColumnHeader: 'Group',
|
|
59
|
+
showChildCount: true,
|
|
60
|
+
groupRowRenderer: undefined,
|
|
47
61
|
rowOverscan: options.rowOverscan ?? 5,
|
|
48
62
|
columnOverscan: options.columnOverscan ?? 2,
|
|
49
63
|
flashedCells: new Map(),
|
|
@@ -84,6 +98,13 @@ export class Grid {
|
|
|
84
98
|
this.events.emit("slotsChange", { slot, mounts });
|
|
85
99
|
},
|
|
86
100
|
});
|
|
101
|
+
this.pipeline = new DataPipeline(() => {
|
|
102
|
+
this.processedDataCache = null;
|
|
103
|
+
this.groupedRowsCache = null;
|
|
104
|
+
this.groupedRowsRevision = -1;
|
|
105
|
+
if (!this.destroyed)
|
|
106
|
+
this.recomputeOverlay();
|
|
107
|
+
});
|
|
87
108
|
this.store.subscribe(() => this.events.emit("stateChange", this.store.getState()));
|
|
88
109
|
options.plugins?.forEach((plugin) => this.registerPlugin(plugin));
|
|
89
110
|
this.recomputeOverlay();
|
|
@@ -91,7 +112,7 @@ export class Grid {
|
|
|
91
112
|
/** Returns the calculated width for a visible column, including off-screen columns. */
|
|
92
113
|
getColumnWidth(columnId) {
|
|
93
114
|
const state = this.getState();
|
|
94
|
-
const visibleColumns =
|
|
115
|
+
const visibleColumns = this.getVisibleLeafColumns();
|
|
95
116
|
if (!visibleColumns.some((column) => column.id === columnId))
|
|
96
117
|
return undefined;
|
|
97
118
|
const pinnedLeft = visibleColumns.filter((column) => column.pinned === "left");
|
|
@@ -115,6 +136,65 @@ export class Grid {
|
|
|
115
136
|
getState() {
|
|
116
137
|
return this.store.getState();
|
|
117
138
|
}
|
|
139
|
+
/** Returns the row hierarchy capability registered by the active row-model plugin. @api */
|
|
140
|
+
getRowHierarchy() {
|
|
141
|
+
return this.rowHierarchy;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Registers the row hierarchy capability for composite row models.
|
|
145
|
+
* @param hierarchy - The active row hierarchy implementation.
|
|
146
|
+
* @returns Cleanup callback to unregister the provider.
|
|
147
|
+
* @api
|
|
148
|
+
*/
|
|
149
|
+
registerRowHierarchy(hierarchy) {
|
|
150
|
+
this.assertActive();
|
|
151
|
+
if (this.rowHierarchy && this.rowHierarchy !== hierarchy) {
|
|
152
|
+
throw new Error("A row hierarchy provider is already registered for this grid");
|
|
153
|
+
}
|
|
154
|
+
this.rowHierarchy = hierarchy;
|
|
155
|
+
return () => {
|
|
156
|
+
if (this.rowHierarchy === hierarchy)
|
|
157
|
+
this.rowHierarchy = undefined;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Updates the grid state via partial patch or functional updater.
|
|
162
|
+
* Accessible by plugins to update their contributed state slices.
|
|
163
|
+
* @param updater - Partial state patch or reducer function.
|
|
164
|
+
* @api
|
|
165
|
+
*/
|
|
166
|
+
setState(updater) {
|
|
167
|
+
this.assertActive();
|
|
168
|
+
this.filteredDataCache = null;
|
|
169
|
+
this.processedDataCache = null;
|
|
170
|
+
this.groupedRowsCache = null;
|
|
171
|
+
this.groupedRowsRevision = -1;
|
|
172
|
+
this.revision += 1;
|
|
173
|
+
this.store.setState(updater);
|
|
174
|
+
this.recomputeOverlay();
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Registers methods dynamically contributed by plugins.
|
|
178
|
+
* These methods are attached both to `api.methods` and directly to `api`.
|
|
179
|
+
* @param methods - Methods to register.
|
|
180
|
+
* @returns Cleanup callback to unregister the methods.
|
|
181
|
+
* @api
|
|
182
|
+
*/
|
|
183
|
+
registerMethods(methods) {
|
|
184
|
+
this.assertActive();
|
|
185
|
+
const registeredKeys = [];
|
|
186
|
+
for (const [key, value] of Object.entries(methods)) {
|
|
187
|
+
this.methods[key] = value;
|
|
188
|
+
this[key] = value;
|
|
189
|
+
registeredKeys.push(key);
|
|
190
|
+
}
|
|
191
|
+
return () => {
|
|
192
|
+
for (const key of registeredKeys) {
|
|
193
|
+
delete this.methods[key];
|
|
194
|
+
delete this[key];
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
}
|
|
118
198
|
/** Returns the current ephemeral scroll position (not part of Store state). */
|
|
119
199
|
getScrollPosition() {
|
|
120
200
|
return this.scrollPosition;
|
|
@@ -132,6 +212,28 @@ export class Grid {
|
|
|
132
212
|
this.scrollPosition = nextPosition;
|
|
133
213
|
this.events.emit("scrollTo", nextPosition);
|
|
134
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* Returns the flat array of visible leaf columns.
|
|
217
|
+
*
|
|
218
|
+
* A column is visible when it is not explicitly `hidden`. In `'single'` group
|
|
219
|
+
* column mode with active grouping, all grouped columns except the primary
|
|
220
|
+
* one are hidden.
|
|
221
|
+
*/
|
|
222
|
+
getVisibleLeafColumns() {
|
|
223
|
+
const state = this.getState();
|
|
224
|
+
return flattenColumns(state.columns).filter((column) => {
|
|
225
|
+
if (column.hidden)
|
|
226
|
+
return false;
|
|
227
|
+
if (state.groupBy.length > 0 && state.groupColumnMode === "single") {
|
|
228
|
+
const isGrouped = state.groupBy.some((field) => column.id === field || column.field === field);
|
|
229
|
+
if (isGrouped) {
|
|
230
|
+
const primary = state.groupBy[0];
|
|
231
|
+
return column.id === primary || column.field === primary;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return true;
|
|
235
|
+
});
|
|
236
|
+
}
|
|
135
237
|
getViewportData() {
|
|
136
238
|
const state = this.getState();
|
|
137
239
|
const processedData = this.getProcessedData();
|
|
@@ -141,8 +243,8 @@ export class Grid {
|
|
|
141
243
|
// array of leaf columns. The hierarchy lives in `state.columns`;
|
|
142
244
|
// groups are projected onto the visible leaf order in `columnGroups`
|
|
143
245
|
// so adapters can render stacked group header rows.
|
|
144
|
-
const visibleColumns =
|
|
145
|
-
const columnGroups = getColumnGroups(state.columns, (leaf) =>
|
|
246
|
+
const visibleColumns = this.getVisibleLeafColumns();
|
|
247
|
+
const columnGroups = getColumnGroups(state.columns, (leaf) => visibleColumns.includes(leaf));
|
|
146
248
|
// Pre-compute group layout metadata for adapters:
|
|
147
249
|
// - `headerRowCount`: number of group header rows above the leaf row
|
|
148
250
|
// - `columnGroupDepth`: maps each leaf column's flat visible index to
|
|
@@ -191,15 +293,17 @@ export class Grid {
|
|
|
191
293
|
: { start: 0, end: 0 };
|
|
192
294
|
const columnRange = Grid.expandColumnRangeForSpans(scrollable, baseColumnRange);
|
|
193
295
|
const hasVariable = this.virtualizer.hasVariableHeights;
|
|
194
|
-
const
|
|
296
|
+
const grouped = this.getGroupedRows();
|
|
297
|
+
const rowCount = grouped.length > 0 ? grouped.length : processedData.length;
|
|
298
|
+
const rowRange = this.virtualizer.getRowRange(rowCount, this.scrollPosition.scrollTop, state.viewport.height, hasVariable);
|
|
195
299
|
// The rows that actually intersect the viewport (no overscan): the
|
|
196
300
|
// adapter binds them eagerly so a recycled node never shows the data
|
|
197
301
|
// of a different row.
|
|
198
|
-
const visibleRowRange = this.virtualizer.getVisibleRowRange(
|
|
199
|
-
const rowOffsets = hasVariable
|
|
302
|
+
const visibleRowRange = this.virtualizer.getVisibleRowRange(rowCount, this.scrollPosition.scrollTop, state.viewport.height);
|
|
303
|
+
const rowOffsets = hasVariable && grouped.length === 0
|
|
200
304
|
? Array.from({ length: rowRange.end - rowRange.start }, (_, i) => this.virtualizer.getRowTop(rowRange.start + i, processedData.length))
|
|
201
305
|
: undefined;
|
|
202
|
-
const rowHeights = hasVariable
|
|
306
|
+
const rowHeights = hasVariable && grouped.length === 0
|
|
203
307
|
? Array.from({ length: rowRange.end - rowRange.start }, (_, i) => this.virtualizer.getRowHeight(rowRange.start + i))
|
|
204
308
|
: undefined;
|
|
205
309
|
// Offset composition:
|
|
@@ -230,12 +334,41 @@ export class Grid {
|
|
|
230
334
|
};
|
|
231
335
|
});
|
|
232
336
|
return {
|
|
233
|
-
rows: processedData.slice(rowRange.start, rowRange.end).map((
|
|
337
|
+
rows: (grouped.length > 0 ? grouped : processedData).slice(rowRange.start, rowRange.end).map((row, index) => {
|
|
234
338
|
const rowIndex = rowRange.start + index;
|
|
339
|
+
if (grouped.length > 0) {
|
|
340
|
+
const node = row;
|
|
341
|
+
if (node.type === 'group') {
|
|
342
|
+
return {
|
|
343
|
+
id: node.id,
|
|
344
|
+
data: undefined,
|
|
345
|
+
index: rowIndex,
|
|
346
|
+
level: node.level,
|
|
347
|
+
type: 'group',
|
|
348
|
+
group: node,
|
|
349
|
+
offset: rowIndex * state.rowHeight,
|
|
350
|
+
height: state.rowHeight,
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
return {
|
|
354
|
+
id: node.id,
|
|
355
|
+
data: node.data,
|
|
356
|
+
index: rowIndex,
|
|
357
|
+
level: node.level,
|
|
358
|
+
type: 'leaf',
|
|
359
|
+
group: node.parent,
|
|
360
|
+
offset: rowIndex * state.rowHeight,
|
|
361
|
+
height: state.rowHeight,
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
const data = row;
|
|
235
365
|
return {
|
|
236
366
|
id: this.getRowId(data, rowIndex),
|
|
237
367
|
data,
|
|
238
368
|
index: rowIndex,
|
|
369
|
+
level: 0,
|
|
370
|
+
type: 'leaf',
|
|
371
|
+
group: undefined,
|
|
239
372
|
offset: rowOffsets ? rowOffsets[index] : rowIndex * state.rowHeight,
|
|
240
373
|
height: rowHeights ? rowHeights[index] : state.rowHeight,
|
|
241
374
|
};
|
|
@@ -258,7 +391,7 @@ export class Grid {
|
|
|
258
391
|
rowOffsets,
|
|
259
392
|
rowHeights,
|
|
260
393
|
totalWidth,
|
|
261
|
-
totalHeight: this.virtualizer.getTotalHeight(
|
|
394
|
+
totalHeight: this.virtualizer.getTotalHeight(rowCount),
|
|
262
395
|
};
|
|
263
396
|
}
|
|
264
397
|
/**
|
|
@@ -344,10 +477,183 @@ export class Grid {
|
|
|
344
477
|
// optimization for DOM Pool operation with large row counts.
|
|
345
478
|
if (this.processedDataCache !== null)
|
|
346
479
|
return this.processedDataCache;
|
|
347
|
-
const
|
|
480
|
+
const state = this.getState();
|
|
481
|
+
const filtered = this.getFilteredData();
|
|
482
|
+
const legacyProcessed = [...this.dataProcessors].reduce((data, processor) => processor(data), filtered);
|
|
483
|
+
const result = this.pipeline.process(legacyProcessed, state);
|
|
348
484
|
this.processedDataCache = result;
|
|
349
485
|
return result;
|
|
350
486
|
}
|
|
487
|
+
/**
|
|
488
|
+
* Returns the active dataset consumed by the virtualizer and DOM pool.
|
|
489
|
+
* When grouping or a custom row model is active, returns the transformed rows;
|
|
490
|
+
* otherwise returns the processed data rows.
|
|
491
|
+
* @api
|
|
492
|
+
*/
|
|
493
|
+
getRows() {
|
|
494
|
+
const customRows = typeof this.getGroupedRows === "function" ? this.getGroupedRows() : undefined;
|
|
495
|
+
return customRows && customRows.length > 0 ? customRows : this.getProcessedData();
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Returns the flattened `TreeRowNode[]` consumed by the virtualizer when
|
|
499
|
+
* grouping is active. Rebuilds the tree + aggregates only when the
|
|
500
|
+
* structural revision changes (data / columns / groupBy / expanded state).
|
|
501
|
+
*
|
|
502
|
+
* @returns The flattened group/data rows, empty when grouping is disabled.
|
|
503
|
+
* @api
|
|
504
|
+
*/
|
|
505
|
+
getGroupedRows() {
|
|
506
|
+
const state = this.getState();
|
|
507
|
+
if (!state.groupBy || state.groupBy.length === 0) {
|
|
508
|
+
this.groupedRowsCache = null;
|
|
509
|
+
this.groupedRowsRevision = -1;
|
|
510
|
+
return [];
|
|
511
|
+
}
|
|
512
|
+
const revision = this.getRevision();
|
|
513
|
+
if (this.groupedRowsCache !== null && this.groupedRowsRevision === revision) {
|
|
514
|
+
return this.groupedRowsCache;
|
|
515
|
+
}
|
|
516
|
+
const processed = this.getProcessedData();
|
|
517
|
+
const root = buildGroupTree(processed, state.groupBy, state.expandedGroups, this.resolveRowId);
|
|
518
|
+
if (!root)
|
|
519
|
+
return [];
|
|
520
|
+
const aggregations = Object.fromEntries(this.aggregationFuncs);
|
|
521
|
+
computeAggregations(root, aggregations);
|
|
522
|
+
const flat = flattenTree(root, state.expandedGroups);
|
|
523
|
+
this.groupedRowsCache = flat;
|
|
524
|
+
this.groupedRowsRevision = revision;
|
|
525
|
+
return flat;
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Sets the grouping fields. Replaces any existing grouping and resets the
|
|
529
|
+
* expanded state to all-expanded. Scroll position is reset to the top.
|
|
530
|
+
*
|
|
531
|
+
* @param fields - Grouping fields in nesting order, or empty/null to disable grouping.
|
|
532
|
+
* @api
|
|
533
|
+
*/
|
|
534
|
+
setGroupBy(fields, expandedGroups) {
|
|
535
|
+
this.assertActive();
|
|
536
|
+
const next = fields ?? [];
|
|
537
|
+
const state = this.getState();
|
|
538
|
+
const changed = next.length !== state.groupBy.length ||
|
|
539
|
+
next.some((field, index) => field !== state.groupBy[index]) ||
|
|
540
|
+
JSON.stringify(expandedGroups ?? {}) !== JSON.stringify(state.expandedGroups);
|
|
541
|
+
if (!changed)
|
|
542
|
+
return;
|
|
543
|
+
this.groupedRowsCache = null;
|
|
544
|
+
this.groupedRowsRevision = -1;
|
|
545
|
+
this.revision += 1;
|
|
546
|
+
this.store.setState({ groupBy: next, expandedGroups: expandedGroups ?? {} });
|
|
547
|
+
this.scrollTo({ scrollTop: 0 });
|
|
548
|
+
this.recomputeOverlay();
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Toggles the expanded state of a single group.
|
|
552
|
+
*
|
|
553
|
+
* @param groupId - Stable group id (the `id` field of a `GroupRowNode`).
|
|
554
|
+
* @api
|
|
555
|
+
*/
|
|
556
|
+
toggleGroup(groupId) {
|
|
557
|
+
this.assertActive();
|
|
558
|
+
const state = this.getState();
|
|
559
|
+
// `undefined` means expanded, so toggling an unmarked group collapses it.
|
|
560
|
+
const next = { ...state.expandedGroups };
|
|
561
|
+
next[groupId] = state.expandedGroups[groupId] === false;
|
|
562
|
+
this.groupedRowsCache = null;
|
|
563
|
+
this.revision += 1;
|
|
564
|
+
this.store.setState({ expandedGroups: next });
|
|
565
|
+
}
|
|
566
|
+
/** Expands every group. @api */
|
|
567
|
+
expandAll() {
|
|
568
|
+
this.assertActive();
|
|
569
|
+
const state = this.getState();
|
|
570
|
+
const next = { ...state.expandedGroups };
|
|
571
|
+
this.getGroupedRows()
|
|
572
|
+
.filter((row) => row.type === 'group')
|
|
573
|
+
.forEach((group) => {
|
|
574
|
+
next[group.id] = true;
|
|
575
|
+
});
|
|
576
|
+
this.groupedRowsCache = null;
|
|
577
|
+
this.revision += 1;
|
|
578
|
+
this.store.setState({ expandedGroups: next });
|
|
579
|
+
}
|
|
580
|
+
/** Collapses every group. @api */
|
|
581
|
+
collapseAll() {
|
|
582
|
+
this.assertActive();
|
|
583
|
+
const state = this.getState();
|
|
584
|
+
const next = { ...state.expandedGroups };
|
|
585
|
+
this.getGroupedRows()
|
|
586
|
+
.filter((row) => row.type === 'group')
|
|
587
|
+
.forEach((group) => {
|
|
588
|
+
next[group.id] = false;
|
|
589
|
+
});
|
|
590
|
+
this.groupedRowsCache = null;
|
|
591
|
+
this.revision += 1;
|
|
592
|
+
this.store.setState({ expandedGroups: next });
|
|
593
|
+
}
|
|
594
|
+
/** Replaces the expanded-state map for groups. @api */
|
|
595
|
+
setExpandedGroups(expandedGroups) {
|
|
596
|
+
this.assertActive();
|
|
597
|
+
const state = this.getState();
|
|
598
|
+
const next = { ...expandedGroups };
|
|
599
|
+
const changed = JSON.stringify(next) !== JSON.stringify(state.expandedGroups);
|
|
600
|
+
if (!changed)
|
|
601
|
+
return;
|
|
602
|
+
this.groupedRowsCache = null;
|
|
603
|
+
this.revision += 1;
|
|
604
|
+
this.store.setState({ expandedGroups: next });
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Registers column id -> aggregation function mappings. Called by
|
|
608
|
+
* aggregation plugins (e.g. `@omnigrid/aggregation-plugin`). Replaces any
|
|
609
|
+
* previously registered functions.
|
|
610
|
+
* @param funcs - Column id to aggregation function map.
|
|
611
|
+
* @returns Unregister function.
|
|
612
|
+
* @api
|
|
613
|
+
*/
|
|
614
|
+
registerAggregationFuncs(funcs) {
|
|
615
|
+
this.assertActive();
|
|
616
|
+
for (const [key, func] of Object.entries(funcs)) {
|
|
617
|
+
this.aggregationFuncs.set(key, func);
|
|
618
|
+
}
|
|
619
|
+
this.groupedRowsCache = null;
|
|
620
|
+
this.groupedRowsRevision = -1;
|
|
621
|
+
const registeredKeys = Object.keys(funcs);
|
|
622
|
+
const unregister = () => {
|
|
623
|
+
for (const key of registeredKeys) {
|
|
624
|
+
this.aggregationFuncs.delete(key);
|
|
625
|
+
}
|
|
626
|
+
this.groupedRowsCache = null;
|
|
627
|
+
this.groupedRowsRevision = -1;
|
|
628
|
+
this.revision += 1;
|
|
629
|
+
};
|
|
630
|
+
return unregister;
|
|
631
|
+
}
|
|
632
|
+
/** Configures the group column UI mode and header. @api */
|
|
633
|
+
setGroupColumnConfig(config) {
|
|
634
|
+
this.assertActive();
|
|
635
|
+
const state = this.getState();
|
|
636
|
+
const nextMode = config.mode ?? state.groupColumnMode;
|
|
637
|
+
const nextHeader = config.header ?? state.groupColumnHeader;
|
|
638
|
+
const nextShowChildCount = config.showChildCount ?? state.showChildCount;
|
|
639
|
+
const nextGroupRowRenderer = config.groupRowRenderer === null ? undefined : (config.groupRowRenderer ?? state.groupRowRenderer);
|
|
640
|
+
if (state.groupColumnMode === nextMode &&
|
|
641
|
+
state.groupColumnHeader === nextHeader &&
|
|
642
|
+
state.showChildCount === nextShowChildCount &&
|
|
643
|
+
state.groupRowRenderer === nextGroupRowRenderer)
|
|
644
|
+
return;
|
|
645
|
+
this.revision += 1;
|
|
646
|
+
this.store.setState({
|
|
647
|
+
groupColumnMode: nextMode,
|
|
648
|
+
groupColumnHeader: nextHeader,
|
|
649
|
+
showChildCount: nextShowChildCount,
|
|
650
|
+
groupRowRenderer: nextGroupRowRenderer,
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
/** Sets or clears the custom group row renderer. @api */
|
|
654
|
+
setGroupRowRenderer() {
|
|
655
|
+
// Group row rendering is handled by plugins/adapters.
|
|
656
|
+
}
|
|
351
657
|
getFilteredData() {
|
|
352
658
|
if (this.filteredDataCache !== null)
|
|
353
659
|
return this.filteredDataCache;
|
|
@@ -377,6 +683,8 @@ export class Grid {
|
|
|
377
683
|
this.filterModel = next;
|
|
378
684
|
this.filteredDataCache = null;
|
|
379
685
|
this.processedDataCache = null;
|
|
686
|
+
this.groupedRowsCache = null;
|
|
687
|
+
this.groupedRowsRevision = -1;
|
|
380
688
|
this.revision += 1;
|
|
381
689
|
this.scrollTo({ scrollTop: 0 });
|
|
382
690
|
this.store.setState({});
|
|
@@ -421,6 +729,8 @@ export class Grid {
|
|
|
421
729
|
return;
|
|
422
730
|
this.filteredDataCache = null;
|
|
423
731
|
this.processedDataCache = null;
|
|
732
|
+
this.groupedRowsCache = null;
|
|
733
|
+
this.groupedRowsRevision = -1;
|
|
424
734
|
this.revision += 1;
|
|
425
735
|
this.store.setState({ quickFilter: nextQuery });
|
|
426
736
|
this.scrollTo({ scrollTop: 0 });
|
|
@@ -552,6 +862,8 @@ export class Grid {
|
|
|
552
862
|
const nextData = data ?? [];
|
|
553
863
|
this.filteredDataCache = null;
|
|
554
864
|
this.processedDataCache = null;
|
|
865
|
+
this.groupedRowsCache = null;
|
|
866
|
+
this.groupedRowsRevision = -1;
|
|
555
867
|
this.virtualizer.resetAllRowHeights();
|
|
556
868
|
this.revision += 1;
|
|
557
869
|
this.store.setState({ data: nextData });
|
|
@@ -574,6 +886,8 @@ export class Grid {
|
|
|
574
886
|
this.hasData = true;
|
|
575
887
|
this.filteredDataCache = null;
|
|
576
888
|
this.processedDataCache = null;
|
|
889
|
+
this.groupedRowsCache = null;
|
|
890
|
+
this.groupedRowsRevision = -1;
|
|
577
891
|
this.revision += 1;
|
|
578
892
|
this.store.setState({ data: nextData });
|
|
579
893
|
this.events.emit("dataChange", nextData);
|
|
@@ -701,6 +1015,8 @@ export class Grid {
|
|
|
701
1015
|
}
|
|
702
1016
|
this.filteredDataCache = null;
|
|
703
1017
|
this.processedDataCache = null;
|
|
1018
|
+
this.groupedRowsCache = null;
|
|
1019
|
+
this.groupedRowsRevision = -1;
|
|
704
1020
|
this.revision += 1;
|
|
705
1021
|
this.store.setState({ data: nextData });
|
|
706
1022
|
this.events.emit("dataChange", nextData);
|
|
@@ -766,6 +1082,8 @@ export class Grid {
|
|
|
766
1082
|
this.assertActive();
|
|
767
1083
|
this.filteredDataCache = null;
|
|
768
1084
|
this.processedDataCache = null;
|
|
1085
|
+
this.groupedRowsCache = null;
|
|
1086
|
+
this.groupedRowsRevision = -1;
|
|
769
1087
|
this.revision += 1;
|
|
770
1088
|
this.store.setState({ columns: applyWrapText(columns, this.wrapTextAll) });
|
|
771
1089
|
this.recomputeOverlay();
|
|
@@ -774,6 +1092,8 @@ export class Grid {
|
|
|
774
1092
|
this.assertActive();
|
|
775
1093
|
this.dataProcessors.add(processor);
|
|
776
1094
|
this.processedDataCache = null;
|
|
1095
|
+
this.groupedRowsCache = null;
|
|
1096
|
+
this.groupedRowsRevision = -1;
|
|
777
1097
|
this.recomputeOverlay();
|
|
778
1098
|
const unregister = () => {
|
|
779
1099
|
this.dataProcessors.delete(processor);
|
|
@@ -931,6 +1251,12 @@ export class Grid {
|
|
|
931
1251
|
this.cleanupPlugins.forEach((cleanup) => cleanup());
|
|
932
1252
|
this.cleanupPlugins.clear();
|
|
933
1253
|
this.dataProcessors.clear();
|
|
1254
|
+
this.pipeline.clear();
|
|
1255
|
+
this.rowHierarchy = undefined;
|
|
1256
|
+
for (const key of Object.keys(this.methods)) {
|
|
1257
|
+
delete this.methods[key];
|
|
1258
|
+
delete this[key];
|
|
1259
|
+
}
|
|
934
1260
|
this.popups.close();
|
|
935
1261
|
this.filteredDataCache = null;
|
|
936
1262
|
this.processedDataCache = null;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { AggregationFunc, DataRowNode, GroupRowNode, RowId, TreeRowNode } from "./types";
|
|
2
|
+
/** Internal mutable node used while building the group tree. */
|
|
3
|
+
interface BuildGroupNode<T> extends GroupRowNode<T> {
|
|
4
|
+
/** Leaf rows grouped directly under this node. */
|
|
5
|
+
leaves: DataRowNode<T>[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Builds a group tree from flat data.
|
|
9
|
+
*
|
|
10
|
+
* Rows are inserted level by level according to `groupBy`. The result is a
|
|
11
|
+
* rooted tree whose root is a synthetic group (level -1). Use
|
|
12
|
+
* `flattenTree` to obtain the linear `TreeRowNode[]` consumed by the
|
|
13
|
+
* virtualizer.
|
|
14
|
+
*
|
|
15
|
+
* @param data - Filtered/sorted flat row data.
|
|
16
|
+
* @param groupBy - Grouping fields in nesting order.
|
|
17
|
+
* @param expandedGroups - Initial expanded state keyed by group id.
|
|
18
|
+
* @param getRowId - Stable row id resolver.
|
|
19
|
+
* @returns The root group node, or `null` when `groupBy` is empty.
|
|
20
|
+
* @api
|
|
21
|
+
*/
|
|
22
|
+
export declare function buildGroupTree<T>(data: T[], groupBy: string[], expandedGroups: Record<string, boolean>, getRowId: (row: T, index: number) => RowId): BuildGroupNode<T> | null;
|
|
23
|
+
/**
|
|
24
|
+
* Flattens a group tree into the linear array consumed by the virtualizer.
|
|
25
|
+
*
|
|
26
|
+
* Only expanded groups contribute their descendants to the output; a collapsed
|
|
27
|
+
* group appears as a single row. Group `expanded` is re-derived from
|
|
28
|
+
* `expandedGroups` on every call, so toggling a group only requires re-flatten
|
|
29
|
+
* (no tree rebuild).
|
|
30
|
+
*
|
|
31
|
+
* @param root - Root group node from `buildGroupTree`.
|
|
32
|
+
* @param expandedGroups - Current expanded state keyed by group id.
|
|
33
|
+
* @returns The flattened `TreeRowNode[]` in pre-order.
|
|
34
|
+
* @api
|
|
35
|
+
*/
|
|
36
|
+
export declare function flattenTree<T>(root: BuildGroupNode<T> | null, expandedGroups: Record<string, boolean>): TreeRowNode<T>[];
|
|
37
|
+
/**
|
|
38
|
+
* Computes per-column aggregates for every group in the tree (bottom-up).
|
|
39
|
+
*
|
|
40
|
+
* Aggregates cover all descendant leaf rows. Results are written into each
|
|
41
|
+
* group node's `aggregations` map and `childrenCount`.
|
|
42
|
+
*
|
|
43
|
+
* @param root - Root group node.
|
|
44
|
+
* @param aggregations - Column id -> aggregation function map, provided by an
|
|
45
|
+
* aggregation plugin (e.g. `@omnigrid/aggregation-plugin`).
|
|
46
|
+
* @api
|
|
47
|
+
*/
|
|
48
|
+
export declare function computeAggregations<T>(root: BuildGroupNode<T> | null, aggregations: Record<string, AggregationFunc<T>>): void;
|
|
49
|
+
export {};
|