@kivacore/ui-pro-shared 0.0.9 → 0.0.10
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/package.json +187 -21
- package/src/.kiva/config.json +27 -0
- package/src/components/avatar-group.tsx +222 -0
- package/src/components/kiva/bulk-update-dialog/bulk-update-dialog.tsx +240 -0
- package/src/components/kiva/bulk-update-dialog/index.tsx +6 -0
- package/src/components/kiva/bulk-update-dialog/types.ts +33 -0
- package/src/components/kiva/calendar/animations.ts +40 -0
- package/src/components/kiva/calendar/calendar-body.tsx +106 -0
- package/src/components/kiva/calendar/calendar-sidebar.tsx +103 -0
- package/src/components/kiva/calendar/calendar.tsx +243 -0
- package/src/components/kiva/calendar/constants.ts +12 -0
- package/src/components/kiva/calendar/contexts/calendar-context.tsx +696 -0
- package/src/components/kiva/calendar/contexts/dnd-context.tsx +173 -0
- package/src/components/kiva/calendar/dialogs/add-edit-event-dialog.tsx +287 -0
- package/src/components/kiva/calendar/dialogs/delete-event-dialog.tsx +73 -0
- package/src/components/kiva/calendar/dialogs/event-details-dialog.tsx +163 -0
- package/src/components/kiva/calendar/dialogs/events-list-dialog.tsx +122 -0
- package/src/components/kiva/calendar/dnd/draggable-event.tsx +66 -0
- package/src/components/kiva/calendar/dnd/droppable-area.tsx +53 -0
- package/src/components/kiva/calendar/dnd/resizable-event.tsx +190 -0
- package/src/components/kiva/calendar/header/calendar-header.tsx +84 -0
- package/src/components/kiva/calendar/header/date-navigator.tsx +188 -0
- package/src/components/kiva/calendar/header/filter.tsx +80 -0
- package/src/components/kiva/calendar/header/today-button.tsx +49 -0
- package/src/components/kiva/calendar/header/user-select.tsx +111 -0
- package/src/components/kiva/calendar/header/view-tabs.tsx +144 -0
- package/src/components/kiva/calendar/helpers.ts +617 -0
- package/src/components/kiva/calendar/hooks/use-disclosure.ts +19 -0
- package/src/components/kiva/calendar/hooks/use-local-storage.ts +40 -0
- package/src/components/kiva/calendar/hooks/use-media-query.ts +25 -0
- package/src/components/kiva/calendar/hooks.ts +5 -0
- package/src/components/kiva/calendar/index.tsx +27 -0
- package/src/components/kiva/calendar/interfaces.ts +35 -0
- package/src/components/kiva/calendar/schemas.ts +20 -0
- package/src/components/kiva/calendar/settings/settings.tsx +116 -0
- package/src/components/kiva/calendar/skeletons/calendar-header-skeleton.tsx +27 -0
- package/src/components/kiva/calendar/skeletons/calendar-skeleton.tsx +19 -0
- package/src/components/kiva/calendar/skeletons/day-view-skeleton.tsx +40 -0
- package/src/components/kiva/calendar/skeletons/month-view-skeleton.tsx +36 -0
- package/src/components/kiva/calendar/skeletons/week-view-skeleton.tsx +48 -0
- package/src/components/kiva/calendar/skeletons/year-view-skeleton.tsx +42 -0
- package/src/components/kiva/calendar/types.ts +25 -0
- package/src/components/kiva/calendar/views/agenda-view/agenda-events.tsx +150 -0
- package/src/components/kiva/calendar/views/month-view/calendar-month-view.tsx +82 -0
- package/src/components/kiva/calendar/views/month-view/day-cell.tsx +218 -0
- package/src/components/kiva/calendar/views/month-view/event-bullet.tsx +46 -0
- package/src/components/kiva/calendar/views/month-view/month-event-badge.tsx +154 -0
- package/src/components/kiva/calendar/views/timeline/calendar-timeline-view.tsx +100 -0
- package/src/components/kiva/calendar/views/timeline/lib/timeline-layout.ts +130 -0
- package/src/components/kiva/calendar/views/timeline/lib/timeline-model.ts +504 -0
- package/src/components/kiva/calendar/views/timeline/timeline-event-bar.tsx +68 -0
- package/src/components/kiva/calendar/views/timeline/timeline-grid-header.tsx +125 -0
- package/src/components/kiva/calendar/views/timeline/timeline-row.tsx +159 -0
- package/src/components/kiva/calendar/views/timeline/timeline-section.tsx +61 -0
- package/src/components/kiva/calendar/views/timeline/use-timeline-sections.ts +278 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-day-view.tsx +277 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-time-line.tsx +47 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-week-view.tsx +229 -0
- package/src/components/kiva/calendar/views/week-and-day-view/day-view-multi-day-events-row.tsx +79 -0
- package/src/components/kiva/calendar/views/week-and-day-view/event-block.tsx +116 -0
- package/src/components/kiva/calendar/views/week-and-day-view/render-grouped-events.tsx +54 -0
- package/src/components/kiva/calendar/views/week-and-day-view/week-view-multi-day-events-row.tsx +159 -0
- package/src/components/kiva/calendar/views/year-view/calendar-year-view.tsx +159 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-active-chips.tsx +137 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-clear.tsx +34 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-search.tsx +67 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-section.tsx +77 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-async-options.tsx +354 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-boolean.tsx +66 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-checkbox-list.tsx +137 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-date-range.tsx +204 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-numeric-range.tsx +162 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-options-dropdown.tsx +191 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-text.tsx +53 -0
- package/src/components/kiva/data-table-side-filters/core/query-translator.ts +367 -0
- package/src/components/kiva/data-table-side-filters/core/types.ts +81 -0
- package/src/components/kiva/data-table-side-filters/data-table-side-filters.tsx +483 -0
- package/src/components/kiva/data-table-side-filters/hooks/use-data-table-side-filters.tsx +160 -0
- package/src/components/kiva/data-table-side-filters/hooks/use-show-more.tsx +62 -0
- package/src/components/kiva/data-table-side-filters/index.tsx +44 -0
- package/src/components/kiva/data-table-side-filters/lib/render-mode.ts +62 -0
- package/src/components/kiva/date-time-picker.tsx +163 -0
- package/src/components/kiva/editable-record-detail.tsx +809 -0
- package/src/components/kiva/enum-option-editor/enum-option-editor.tsx +891 -0
- package/src/components/kiva/enum-option-editor/index.tsx +26 -0
- package/src/components/kiva/enum-option-editor/kiva-enum-option-editor.tsx +320 -0
- package/src/components/kiva/enum-option-editor/types.ts +90 -0
- package/src/components/kiva/kanban.tsx +1376 -0
- package/src/components/kiva/kiva-data-export-menu.tsx +543 -0
- package/src/components/kiva/map.tsx +1631 -0
- package/src/components/kiva/pivot-calendar/components/group-sidebar.tsx +95 -0
- package/src/components/kiva/pivot-calendar/components/pivot-calendar-toolbar.tsx +113 -0
- package/src/components/kiva/pivot-calendar/components/stats-cell.tsx +126 -0
- package/src/components/kiva/pivot-calendar/helpers.ts +374 -0
- package/src/components/kiva/pivot-calendar/hooks/use-pivot-calendar.ts +222 -0
- package/src/components/kiva/pivot-calendar/index.tsx +32 -0
- package/src/components/kiva/pivot-calendar/pivot-calendar.tsx +107 -0
- package/src/components/kiva/pivot-calendar/types.ts +154 -0
- package/src/components/kiva/pivot-calendar/views/days-of-month-view.tsx +37 -0
- package/src/components/kiva/pivot-calendar/views/days-of-week-view.tsx +37 -0
- package/src/components/kiva/pivot-calendar/views/month-calendar-view.tsx +154 -0
- package/src/components/kiva/pivot-calendar/views/months-of-year-view.tsx +40 -0
- package/src/components/kiva/pivot-calendar/views/pivot-grid-view.tsx +251 -0
- package/src/components/kiva/pivot-filter/index.tsx +9 -0
- package/src/components/kiva/pivot-filter/pivot-filter-item.tsx +159 -0
- package/src/components/kiva/pivot-filter/pivot-filter-settings.tsx +161 -0
- package/src/components/kiva/pivot-filter/pivot-filter.tsx +187 -0
- package/src/components/kiva/pivot-filter/types.ts +82 -0
- package/src/components/kiva/pivot-grid/hooks/use-pivot-grid.ts +683 -0
- package/src/components/kiva/pivot-grid/index.tsx +23 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-color-rules.ts +98 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-export.ts +265 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-model.ts +1110 -0
- package/src/components/kiva/pivot-grid/pivot-grid-demo-data.ts +200 -0
- package/src/components/kiva/pivot-grid/pivot-grid-export-menu.tsx +66 -0
- package/src/components/kiva/pivot-grid/pivot-grid-toolbar.tsx +85 -0
- package/src/components/kiva/pivot-grid/pivot-grid.tsx +601 -0
- package/src/components/kiva/pivot-grid/types.ts +195 -0
- package/src/components/kiva/place-autocomplete.tsx +484 -0
- package/src/components/kiva/record-delete-confirm-dialog/index.tsx +10 -0
- package/src/components/kiva/record-delete-confirm-dialog/record-delete-confirm-dialog.tsx +266 -0
- package/src/components/kiva/record-delete-confirm-dialog/types.ts +25 -0
- package/src/hooks/kiva/_internal/autonumber-setting.ts +112 -0
- package/src/hooks/kiva/_internal/form-layout-to-view.ts +665 -0
- package/src/hooks/kiva/_internal/kiva-hook-utils.ts +143 -0
- package/src/hooks/kiva/_internal/use-computed-field-evaluator.tsx +310 -0
- package/src/hooks/kiva/_internal/use-field-actions-evaluator.tsx +398 -0
- package/src/hooks/kiva/_internal/use-form-actions-evaluator.tsx +363 -0
- package/src/hooks/kiva/_internal/use-form-view-engine.tsx +2200 -0
- package/src/hooks/kiva/_internal/use-kiva-form-view-fields.tsx +190 -0
- package/src/hooks/kiva/_internal/use-kiva-kanban-cards.tsx +311 -0
- package/src/hooks/kiva/use-data-export.tsx +265 -0
- package/src/hooks/kiva/use-kiva-calendar.tsx +992 -0
- package/src/hooks/kiva/use-kiva-data-export.tsx +173 -0
- package/src/hooks/kiva/use-kiva-data-grid.tsx +4750 -0
- package/src/hooks/kiva/use-kiva-data-view-select.tsx +1273 -0
- package/src/hooks/kiva/use-kiva-form-view.tsx +1581 -0
- package/src/hooks/kiva/use-kiva-inventory.tsx +184 -0
- package/src/hooks/kiva/use-kiva-kanban.tsx +1750 -0
- package/src/hooks/kiva/use-kiva-map-view.tsx +2255 -0
- package/src/hooks/kiva/use-kiva-pivot-calendar.tsx +759 -0
- package/src/hooks/kiva/use-kiva-pivot-filter.tsx +1523 -0
- package/src/hooks/kiva/use-kiva-pivot-grid.tsx +543 -0
- package/src/hooks/kiva/use-kiva-tenant.tsx +313 -0
- package/src/hooks/kiva/use-parameter-persistence.ts +191 -0
|
@@ -0,0 +1,1110 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
import {
|
|
4
|
+
type PivotGridDimension,
|
|
5
|
+
type PivotGridHeaderPathSegment,
|
|
6
|
+
type PivotGridHeaderRow,
|
|
7
|
+
type PivotGridLeafColumn,
|
|
8
|
+
type PivotGridMeasure,
|
|
9
|
+
type PivotGridPinPosition,
|
|
10
|
+
type PivotGridRenderedCell,
|
|
11
|
+
type PivotGridRenderedRow
|
|
12
|
+
} from '../types';
|
|
13
|
+
|
|
14
|
+
interface PivotAxisValueNode<TData> {
|
|
15
|
+
id: string;
|
|
16
|
+
axis: 'row' | 'column';
|
|
17
|
+
depth: number;
|
|
18
|
+
value: unknown;
|
|
19
|
+
label: string;
|
|
20
|
+
pathKeys: Array<string>;
|
|
21
|
+
pathLabels: Array<string>;
|
|
22
|
+
pathNodeIds: Array<string>;
|
|
23
|
+
rawRowIndices: Array<number>;
|
|
24
|
+
rawRowIds: Array<string>;
|
|
25
|
+
children: Array<PivotAxisValueNode<TData>>;
|
|
26
|
+
childrenMap: Map<string, PivotAxisValueNode<TData>>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface PivotAxisBuildResult<TData> {
|
|
30
|
+
root: PivotAxisValueNode<TData>;
|
|
31
|
+
leafNodesByIndex: Array<PivotAxisValueNode<TData>>;
|
|
32
|
+
pathNodeIdsByIndex: Array<Array<string>>;
|
|
33
|
+
nodesById: Map<string, PivotAxisValueNode<TData>>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface PivotAggregateMeta {
|
|
37
|
+
id: string;
|
|
38
|
+
kind: 'leaf' | 'subtotal' | 'grand-total';
|
|
39
|
+
ownerNodeId?: string;
|
|
40
|
+
pathLabels: Array<string>;
|
|
41
|
+
rawRowIndices: Array<number>;
|
|
42
|
+
rawRowIds: Array<string>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface PivotAggregateCell {
|
|
46
|
+
rawRowIndices: Array<number>;
|
|
47
|
+
rawRowIds: Array<string>;
|
|
48
|
+
sum: number;
|
|
49
|
+
count: number;
|
|
50
|
+
min: number | null;
|
|
51
|
+
max: number | null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface PivotGridStructure<TData> {
|
|
55
|
+
data: Array<TData>;
|
|
56
|
+
rawRowIds: Array<string>;
|
|
57
|
+
rowDimensions: Array<PivotGridDimension<TData>>;
|
|
58
|
+
columnDimensions: Array<PivotGridDimension<TData>>;
|
|
59
|
+
measures: Array<PivotGridMeasure<TData>>;
|
|
60
|
+
rowTree: PivotAxisBuildResult<TData>;
|
|
61
|
+
columnTree: PivotAxisBuildResult<TData>;
|
|
62
|
+
rowLeafMetaByNodeId: Map<string, PivotAggregateMeta>;
|
|
63
|
+
rowSubtotalMetaByNodeId: Map<string, PivotAggregateMeta>;
|
|
64
|
+
rowGrandTotalMeta: PivotAggregateMeta;
|
|
65
|
+
columnLeafMetaByNodeId: Map<string, PivotAggregateMeta>;
|
|
66
|
+
columnSubtotalMetaByNodeId: Map<string, PivotAggregateMeta>;
|
|
67
|
+
columnGrandTotalMeta: PivotAggregateMeta;
|
|
68
|
+
allRowToggleIds: Array<string>;
|
|
69
|
+
allColumnToggleIds: Array<string>;
|
|
70
|
+
cellAccumulators: Map<string, PivotAggregateCell>;
|
|
71
|
+
headerDepth: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface PivotGridViewModel<TData> {
|
|
75
|
+
visibleRows: Array<PivotGridRenderedRow<TData>>;
|
|
76
|
+
visibleLeafColumns: Array<PivotGridLeafColumn<TData>>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const GRAND_TOTAL_LABEL = 'Grand Total';
|
|
80
|
+
const SUBTOTAL_LABEL = 'Subtotal';
|
|
81
|
+
|
|
82
|
+
function assertDimensionCount<TData>(
|
|
83
|
+
dimensions: Array<PivotGridDimension<TData>>,
|
|
84
|
+
axis: 'row' | 'column'
|
|
85
|
+
): void {
|
|
86
|
+
if (dimensions.length < 1 || dimensions.length > 3) {
|
|
87
|
+
throw new Error(
|
|
88
|
+
`PivotGrid requires between 1 and 3 ${axis} dimensions.`
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function serializePivotValue(value: unknown): string {
|
|
94
|
+
if (value === null || value === undefined || value === '') {
|
|
95
|
+
return '__empty__';
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (value instanceof Date) {
|
|
99
|
+
return `date:${value.toISOString()}`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
switch (typeof value) {
|
|
103
|
+
case 'string':
|
|
104
|
+
return `string:${value}`;
|
|
105
|
+
|
|
106
|
+
case 'number':
|
|
107
|
+
return `number:${value}`;
|
|
108
|
+
|
|
109
|
+
case 'boolean':
|
|
110
|
+
return `boolean:${value}`;
|
|
111
|
+
|
|
112
|
+
case 'bigint':
|
|
113
|
+
return `bigint:${value.toString()}`;
|
|
114
|
+
|
|
115
|
+
default:
|
|
116
|
+
try {
|
|
117
|
+
return `json:${JSON.stringify(value)}`;
|
|
118
|
+
} catch {
|
|
119
|
+
return `fallback:${String(value)}`;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function formatDimensionValue<TData>(
|
|
125
|
+
dimension: PivotGridDimension<TData>,
|
|
126
|
+
value: unknown
|
|
127
|
+
): string {
|
|
128
|
+
if (value === null || value === undefined || value === '') {
|
|
129
|
+
return dimension.emptyLabel ?? 'Empty';
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (dimension.formatValue) {
|
|
133
|
+
return dimension.formatValue(value);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (value instanceof Date) {
|
|
137
|
+
return value.toISOString().split('T')[0] ?? '';
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return String(value);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function createRootNode<TData>(
|
|
144
|
+
axis: 'row' | 'column'
|
|
145
|
+
): PivotAxisValueNode<TData> {
|
|
146
|
+
return {
|
|
147
|
+
id: `${axis}:root`,
|
|
148
|
+
axis,
|
|
149
|
+
depth: 0,
|
|
150
|
+
value: null,
|
|
151
|
+
label: '',
|
|
152
|
+
pathKeys: [],
|
|
153
|
+
pathLabels: [],
|
|
154
|
+
pathNodeIds: [],
|
|
155
|
+
rawRowIndices: [],
|
|
156
|
+
rawRowIds: [],
|
|
157
|
+
children: [],
|
|
158
|
+
childrenMap: new Map()
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function buildAxisTree<TData>(params: {
|
|
163
|
+
axis: 'row' | 'column';
|
|
164
|
+
data: Array<TData>;
|
|
165
|
+
dimensions: Array<PivotGridDimension<TData>>;
|
|
166
|
+
rawRowIds: Array<string>;
|
|
167
|
+
}): PivotAxisBuildResult<TData> {
|
|
168
|
+
const {
|
|
169
|
+
axis,
|
|
170
|
+
data,
|
|
171
|
+
dimensions,
|
|
172
|
+
rawRowIds
|
|
173
|
+
} = params;
|
|
174
|
+
|
|
175
|
+
const root = createRootNode<TData>(axis);
|
|
176
|
+
const leafNodesByIndex: Array<PivotAxisValueNode<TData>> = [];
|
|
177
|
+
const pathNodeIdsByIndex: Array<Array<string>> = [];
|
|
178
|
+
const nodesById = new Map<string, PivotAxisValueNode<TData>>([[root.id, root]]);
|
|
179
|
+
|
|
180
|
+
data.forEach((row, rowIndex) => {
|
|
181
|
+
const rawRowId = rawRowIds[rowIndex] ?? String(rowIndex);
|
|
182
|
+
let current = root;
|
|
183
|
+
const nextPathNodeIds: Array<string> = [];
|
|
184
|
+
|
|
185
|
+
current.rawRowIndices.push(rowIndex);
|
|
186
|
+
current.rawRowIds.push(rawRowId);
|
|
187
|
+
|
|
188
|
+
dimensions.forEach((dimension, dimensionIndex) => {
|
|
189
|
+
const rawValue = dimension.getValue(row);
|
|
190
|
+
const serializedValue = serializePivotValue(rawValue);
|
|
191
|
+
let child = current.childrenMap.get(serializedValue);
|
|
192
|
+
|
|
193
|
+
if (!child) {
|
|
194
|
+
const pathKeys = [...current.pathKeys, `${dimension.id}:${serializedValue}`];
|
|
195
|
+
const pathLabels = [...current.pathLabels, formatDimensionValue(dimension, rawValue)];
|
|
196
|
+
const childId = `${axis}:${pathKeys.join('|')}`;
|
|
197
|
+
|
|
198
|
+
child = {
|
|
199
|
+
id: childId,
|
|
200
|
+
axis,
|
|
201
|
+
depth: dimensionIndex + 1,
|
|
202
|
+
value: rawValue,
|
|
203
|
+
label: pathLabels[pathLabels.length - 1] ?? '',
|
|
204
|
+
pathKeys,
|
|
205
|
+
pathLabels,
|
|
206
|
+
pathNodeIds: [...current.pathNodeIds, childId],
|
|
207
|
+
rawRowIndices: [],
|
|
208
|
+
rawRowIds: [],
|
|
209
|
+
children: [],
|
|
210
|
+
childrenMap: new Map()
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
current.children.push(child);
|
|
214
|
+
current.childrenMap.set(serializedValue, child);
|
|
215
|
+
nodesById.set(childId, child);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
child.rawRowIndices.push(rowIndex);
|
|
219
|
+
child.rawRowIds.push(rawRowId);
|
|
220
|
+
|
|
221
|
+
current = child;
|
|
222
|
+
nextPathNodeIds.push(child.id);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
leafNodesByIndex[rowIndex] = current;
|
|
226
|
+
pathNodeIdsByIndex[rowIndex] = nextPathNodeIds;
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
sortAxisNode(root, dimensions);
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
root,
|
|
233
|
+
leafNodesByIndex,
|
|
234
|
+
pathNodeIdsByIndex,
|
|
235
|
+
nodesById
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function sortAxisNode<TData>(
|
|
240
|
+
node: PivotAxisValueNode<TData>,
|
|
241
|
+
dimensions: Array<PivotGridDimension<TData>>
|
|
242
|
+
): void {
|
|
243
|
+
const dimension = dimensions[node.depth];
|
|
244
|
+
|
|
245
|
+
if (dimension) {
|
|
246
|
+
node.children.sort((leftNode, rightNode) => {
|
|
247
|
+
if (dimension.sort) {
|
|
248
|
+
return dimension.sort(leftNode.value, rightNode.value);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return leftNode.label.localeCompare(
|
|
252
|
+
rightNode.label,
|
|
253
|
+
undefined,
|
|
254
|
+
{ numeric: true, sensitivity: 'base' }
|
|
255
|
+
);
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
node.children.forEach(child => sortAxisNode(child, dimensions));
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function createLeafAggregateId(axis: 'row' | 'column', nodeId: string): string {
|
|
263
|
+
return `${axis}:leaf:${nodeId}`;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function createSubtotalAggregateId(
|
|
267
|
+
axis: 'row' | 'column',
|
|
268
|
+
nodeId: string
|
|
269
|
+
): string {
|
|
270
|
+
return `${axis}:subtotal:${nodeId}`;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function createGrandTotalAggregateId(axis: 'row' | 'column'): string {
|
|
274
|
+
return `${axis}:grand-total`;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function collectAggregateMeta<TData>(params: {
|
|
278
|
+
axis: 'row' | 'column';
|
|
279
|
+
tree: PivotAxisBuildResult<TData>;
|
|
280
|
+
maxDepth: number;
|
|
281
|
+
}): {
|
|
282
|
+
leafMetaByNodeId: Map<string, PivotAggregateMeta>;
|
|
283
|
+
subtotalMetaByNodeId: Map<string, PivotAggregateMeta>;
|
|
284
|
+
grandTotalMeta: PivotAggregateMeta;
|
|
285
|
+
toggleIds: Array<string>;
|
|
286
|
+
} {
|
|
287
|
+
const {
|
|
288
|
+
axis,
|
|
289
|
+
tree,
|
|
290
|
+
maxDepth
|
|
291
|
+
} = params;
|
|
292
|
+
const leafMetaByNodeId = new Map<string, PivotAggregateMeta>();
|
|
293
|
+
const subtotalMetaByNodeId = new Map<string, PivotAggregateMeta>();
|
|
294
|
+
const toggleIds: Array<string> = [];
|
|
295
|
+
|
|
296
|
+
function walk(node: PivotAxisValueNode<TData>) {
|
|
297
|
+
toggleIds.push(node.id);
|
|
298
|
+
|
|
299
|
+
subtotalMetaByNodeId.set(node.id, {
|
|
300
|
+
id: createSubtotalAggregateId(axis, node.id),
|
|
301
|
+
kind: 'subtotal',
|
|
302
|
+
ownerNodeId: node.id,
|
|
303
|
+
pathLabels: [...node.pathLabels, SUBTOTAL_LABEL],
|
|
304
|
+
rawRowIndices: [...node.rawRowIndices],
|
|
305
|
+
rawRowIds: [...node.rawRowIds]
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
if (node.depth === maxDepth) {
|
|
309
|
+
leafMetaByNodeId.set(node.id, {
|
|
310
|
+
id: createLeafAggregateId(axis, node.id),
|
|
311
|
+
kind: 'leaf',
|
|
312
|
+
ownerNodeId: node.id,
|
|
313
|
+
pathLabels: [...node.pathLabels],
|
|
314
|
+
rawRowIndices: [...node.rawRowIndices],
|
|
315
|
+
rawRowIds: [...node.rawRowIds]
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
node.children.forEach(walk);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
tree.root.children.forEach(walk);
|
|
323
|
+
|
|
324
|
+
return {
|
|
325
|
+
leafMetaByNodeId,
|
|
326
|
+
subtotalMetaByNodeId,
|
|
327
|
+
grandTotalMeta: {
|
|
328
|
+
id: createGrandTotalAggregateId(axis),
|
|
329
|
+
kind: 'grand-total',
|
|
330
|
+
pathLabels: [GRAND_TOTAL_LABEL],
|
|
331
|
+
rawRowIndices: [...tree.root.rawRowIndices],
|
|
332
|
+
rawRowIds: [...tree.root.rawRowIds]
|
|
333
|
+
},
|
|
334
|
+
toggleIds
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function getMeasureNumericValue<TData>(
|
|
339
|
+
measure: PivotGridMeasure<TData>,
|
|
340
|
+
row: TData
|
|
341
|
+
): number | null {
|
|
342
|
+
if (measure.aggregate === 'count') {
|
|
343
|
+
return 1;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const rawValue = measure.getValue?.(row);
|
|
347
|
+
|
|
348
|
+
if (
|
|
349
|
+
rawValue === null
|
|
350
|
+
|| rawValue === undefined
|
|
351
|
+
|| (typeof rawValue === 'string' && rawValue === '')
|
|
352
|
+
) {
|
|
353
|
+
return null;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (typeof rawValue === 'number') {
|
|
357
|
+
return Number.isFinite(rawValue) ? rawValue : null;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const numericValue = Number(rawValue);
|
|
361
|
+
|
|
362
|
+
return Number.isFinite(numericValue) ? numericValue : null;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function getAccumulatorKey(rowId: string, columnId: string): string {
|
|
366
|
+
return `${rowId}__${columnId}`;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function getOrCreateCellAccumulator(
|
|
370
|
+
map: Map<string, PivotAggregateCell>,
|
|
371
|
+
rowId: string,
|
|
372
|
+
columnId: string
|
|
373
|
+
): PivotAggregateCell {
|
|
374
|
+
const key = getAccumulatorKey(rowId, columnId);
|
|
375
|
+
let accumulator = map.get(key);
|
|
376
|
+
|
|
377
|
+
if (!accumulator) {
|
|
378
|
+
accumulator = {
|
|
379
|
+
rawRowIndices: [],
|
|
380
|
+
rawRowIds: [],
|
|
381
|
+
sum: 0,
|
|
382
|
+
count: 0,
|
|
383
|
+
min: null,
|
|
384
|
+
max: null
|
|
385
|
+
};
|
|
386
|
+
map.set(key, accumulator);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
return accumulator;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function updateAccumulator(
|
|
393
|
+
accumulator: PivotAggregateCell,
|
|
394
|
+
aggregate: PivotGridMeasure<unknown>['aggregate'],
|
|
395
|
+
rawRowIndex: number,
|
|
396
|
+
rawRowId: string,
|
|
397
|
+
numericValue: number | null
|
|
398
|
+
): void {
|
|
399
|
+
accumulator.rawRowIndices.push(rawRowIndex);
|
|
400
|
+
accumulator.rawRowIds.push(rawRowId);
|
|
401
|
+
|
|
402
|
+
if (aggregate === 'count') {
|
|
403
|
+
accumulator.count += 1;
|
|
404
|
+
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
if (numericValue === null) {
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
accumulator.sum += numericValue;
|
|
413
|
+
accumulator.count += 1;
|
|
414
|
+
accumulator.min
|
|
415
|
+
= accumulator.min === null ? numericValue : Math.min(accumulator.min, numericValue);
|
|
416
|
+
accumulator.max
|
|
417
|
+
= accumulator.max === null ? numericValue : Math.max(accumulator.max, numericValue);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function measureAccumulatorValue<TData>(
|
|
421
|
+
measure: PivotGridMeasure<TData>,
|
|
422
|
+
accumulator: PivotAggregateCell | undefined
|
|
423
|
+
): number {
|
|
424
|
+
if (!accumulator) return 0;
|
|
425
|
+
|
|
426
|
+
switch (measure.aggregate) {
|
|
427
|
+
case 'count':
|
|
428
|
+
return accumulator.count;
|
|
429
|
+
|
|
430
|
+
case 'avg':
|
|
431
|
+
return accumulator.count > 0 ? accumulator.sum / accumulator.count : 0;
|
|
432
|
+
|
|
433
|
+
case 'min':
|
|
434
|
+
return accumulator.min ?? 0;
|
|
435
|
+
|
|
436
|
+
case 'max':
|
|
437
|
+
return accumulator.max ?? 0;
|
|
438
|
+
|
|
439
|
+
default:
|
|
440
|
+
return accumulator.sum;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function formatMeasureValue<TData>(
|
|
445
|
+
measure: PivotGridMeasure<TData>,
|
|
446
|
+
value: number
|
|
447
|
+
): string {
|
|
448
|
+
if (measure.formatValue) {
|
|
449
|
+
return measure.formatValue(value);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
if (measure.aggregate === 'count') {
|
|
453
|
+
return new Intl.NumberFormat().format(value);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
return new Intl.NumberFormat(undefined, {
|
|
457
|
+
maximumFractionDigits: Number.isInteger(value) ? 0 : 2
|
|
458
|
+
}).format(value);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
export function buildPivotGridStructure<TData>(params: {
|
|
462
|
+
data: Array<TData>;
|
|
463
|
+
rowDimensions: Array<PivotGridDimension<TData>>;
|
|
464
|
+
columnDimensions: Array<PivotGridDimension<TData>>;
|
|
465
|
+
measures: Array<PivotGridMeasure<TData>>;
|
|
466
|
+
getRowId?: (row: TData, index: number) => string;
|
|
467
|
+
}): PivotGridStructure<TData> {
|
|
468
|
+
const {
|
|
469
|
+
data,
|
|
470
|
+
rowDimensions,
|
|
471
|
+
columnDimensions,
|
|
472
|
+
measures,
|
|
473
|
+
getRowId
|
|
474
|
+
} = params;
|
|
475
|
+
|
|
476
|
+
assertDimensionCount(rowDimensions, 'row');
|
|
477
|
+
assertDimensionCount(columnDimensions, 'column');
|
|
478
|
+
|
|
479
|
+
if (measures.length === 0) {
|
|
480
|
+
throw new Error('PivotGrid requires at least one measure.');
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const rawRowIds = data.map((row, index) => getRowId?.(row, index) ?? String(index));
|
|
484
|
+
const rowTree = buildAxisTree({
|
|
485
|
+
axis: 'row',
|
|
486
|
+
data,
|
|
487
|
+
dimensions: rowDimensions,
|
|
488
|
+
rawRowIds
|
|
489
|
+
});
|
|
490
|
+
const columnTree = buildAxisTree({
|
|
491
|
+
axis: 'column',
|
|
492
|
+
data,
|
|
493
|
+
dimensions: columnDimensions,
|
|
494
|
+
rawRowIds
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
const rowMeta = collectAggregateMeta({
|
|
498
|
+
axis: 'row',
|
|
499
|
+
tree: rowTree,
|
|
500
|
+
maxDepth: rowDimensions.length
|
|
501
|
+
});
|
|
502
|
+
const columnMeta = collectAggregateMeta({
|
|
503
|
+
axis: 'column',
|
|
504
|
+
tree: columnTree,
|
|
505
|
+
maxDepth: columnDimensions.length
|
|
506
|
+
});
|
|
507
|
+
const cellAccumulators = new Map<string, PivotAggregateCell>();
|
|
508
|
+
|
|
509
|
+
data.forEach((row, rowIndex) => {
|
|
510
|
+
const rawRowId = rawRowIds[rowIndex] ?? String(rowIndex);
|
|
511
|
+
const rowLeafNode = rowTree.leafNodesByIndex[rowIndex];
|
|
512
|
+
const columnLeafNode = columnTree.leafNodesByIndex[rowIndex];
|
|
513
|
+
|
|
514
|
+
if (!rowLeafNode || !columnLeafNode) {
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
const rowAggregateIds = [createLeafAggregateId('row', rowLeafNode.id), ...(rowTree.pathNodeIdsByIndex[rowIndex] ?? []).map(nodeId => createSubtotalAggregateId('row', nodeId)), createGrandTotalAggregateId('row')];
|
|
519
|
+
const columnAggregateBases = [
|
|
520
|
+
{
|
|
521
|
+
id: createLeafAggregateId('column', columnLeafNode.id),
|
|
522
|
+
kind: 'leaf' as const,
|
|
523
|
+
ownerNodeId: columnLeafNode.id
|
|
524
|
+
},
|
|
525
|
+
...(columnTree.pathNodeIdsByIndex[rowIndex] ?? []).map(nodeId => ({
|
|
526
|
+
id: createSubtotalAggregateId('column', nodeId),
|
|
527
|
+
kind: 'subtotal' as const,
|
|
528
|
+
ownerNodeId: nodeId
|
|
529
|
+
})),
|
|
530
|
+
{
|
|
531
|
+
id: createGrandTotalAggregateId('column'),
|
|
532
|
+
kind: 'grand-total' as const,
|
|
533
|
+
ownerNodeId: undefined
|
|
534
|
+
}
|
|
535
|
+
];
|
|
536
|
+
|
|
537
|
+
measures.forEach((measure) => {
|
|
538
|
+
const measureValue = getMeasureNumericValue(measure, row);
|
|
539
|
+
|
|
540
|
+
columnAggregateBases.forEach((columnAggregate) => {
|
|
541
|
+
const columnId = `${columnAggregate.id}__${measure.id}`;
|
|
542
|
+
|
|
543
|
+
rowAggregateIds.forEach((rowAggregateId) => {
|
|
544
|
+
const accumulator = getOrCreateCellAccumulator(
|
|
545
|
+
cellAccumulators,
|
|
546
|
+
rowAggregateId,
|
|
547
|
+
columnId
|
|
548
|
+
);
|
|
549
|
+
|
|
550
|
+
updateAccumulator(
|
|
551
|
+
accumulator,
|
|
552
|
+
measure.aggregate,
|
|
553
|
+
rowIndex,
|
|
554
|
+
rawRowId,
|
|
555
|
+
measureValue
|
|
556
|
+
);
|
|
557
|
+
});
|
|
558
|
+
});
|
|
559
|
+
});
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
return {
|
|
563
|
+
data,
|
|
564
|
+
rawRowIds,
|
|
565
|
+
rowDimensions,
|
|
566
|
+
columnDimensions,
|
|
567
|
+
measures,
|
|
568
|
+
rowTree,
|
|
569
|
+
columnTree,
|
|
570
|
+
rowLeafMetaByNodeId: rowMeta.leafMetaByNodeId,
|
|
571
|
+
rowSubtotalMetaByNodeId: rowMeta.subtotalMetaByNodeId,
|
|
572
|
+
rowGrandTotalMeta: rowMeta.grandTotalMeta,
|
|
573
|
+
columnLeafMetaByNodeId: columnMeta.leafMetaByNodeId,
|
|
574
|
+
columnSubtotalMetaByNodeId: columnMeta.subtotalMetaByNodeId,
|
|
575
|
+
columnGrandTotalMeta: columnMeta.grandTotalMeta,
|
|
576
|
+
allRowToggleIds: rowMeta.toggleIds,
|
|
577
|
+
allColumnToggleIds: columnMeta.toggleIds,
|
|
578
|
+
cellAccumulators,
|
|
579
|
+
headerDepth: columnDimensions.length + (measures.length > 1 ? 1 : 0)
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function createRowHeaderCells<TData>(params: {
|
|
584
|
+
maxDepth: number;
|
|
585
|
+
kind: PivotGridRenderedRow<TData>['kind'];
|
|
586
|
+
node?: PivotAxisValueNode<TData>;
|
|
587
|
+
labelOverride?: string;
|
|
588
|
+
canToggle?: boolean;
|
|
589
|
+
isExpanded?: boolean;
|
|
590
|
+
rawRowCount: number;
|
|
591
|
+
}): PivotGridRenderedRow<TData>['headerCells'] {
|
|
592
|
+
const {
|
|
593
|
+
maxDepth,
|
|
594
|
+
kind,
|
|
595
|
+
node,
|
|
596
|
+
labelOverride,
|
|
597
|
+
canToggle = false,
|
|
598
|
+
isExpanded = false,
|
|
599
|
+
rawRowCount
|
|
600
|
+
} = params;
|
|
601
|
+
|
|
602
|
+
return Array.from({ length: maxDepth }, (_, index) => {
|
|
603
|
+
if (!node) {
|
|
604
|
+
return {
|
|
605
|
+
key: `grand-total:${index}`,
|
|
606
|
+
label: index === 0 ? GRAND_TOTAL_LABEL : '',
|
|
607
|
+
kind: index === 0 ? kind : 'blank',
|
|
608
|
+
depth: 0,
|
|
609
|
+
isCurrentDepth: index === 0,
|
|
610
|
+
canToggle: false,
|
|
611
|
+
isExpanded: false,
|
|
612
|
+
rawRowCount
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
if (index < node.depth - 1) {
|
|
617
|
+
return {
|
|
618
|
+
key: `${node.id}:ancestor:${index}`,
|
|
619
|
+
label: node.pathLabels[index] ?? '',
|
|
620
|
+
kind: 'blank',
|
|
621
|
+
depth: index + 1,
|
|
622
|
+
isCurrentDepth: false,
|
|
623
|
+
canToggle: false,
|
|
624
|
+
isExpanded: false,
|
|
625
|
+
rawRowCount
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
if (index === node.depth - 1) {
|
|
630
|
+
return {
|
|
631
|
+
key: `${node.id}:${kind}:${index}`,
|
|
632
|
+
label: labelOverride ?? node.label,
|
|
633
|
+
kind,
|
|
634
|
+
depth: node.depth,
|
|
635
|
+
isCurrentDepth: true,
|
|
636
|
+
canToggle,
|
|
637
|
+
isExpanded,
|
|
638
|
+
groupNodeId: canToggle ? node.id : undefined,
|
|
639
|
+
rawRowCount,
|
|
640
|
+
title: labelOverride ?? node.label
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
return {
|
|
645
|
+
key: `${node.id}:blank:${index}`,
|
|
646
|
+
label: '',
|
|
647
|
+
kind: 'blank',
|
|
648
|
+
depth: index + 1,
|
|
649
|
+
isCurrentDepth: false,
|
|
650
|
+
canToggle: false,
|
|
651
|
+
isExpanded: false,
|
|
652
|
+
rawRowCount
|
|
653
|
+
};
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
function buildVisibleRows<TData>(
|
|
658
|
+
structure: PivotGridStructure<TData>,
|
|
659
|
+
expandedRowIds: Record<string, boolean>
|
|
660
|
+
): Array<PivotGridRenderedRow<TData>> {
|
|
661
|
+
const visibleRows: Array<PivotGridRenderedRow<TData>> = [];
|
|
662
|
+
const maxDepth = structure.rowDimensions.length;
|
|
663
|
+
|
|
664
|
+
function walk(node: PivotAxisValueNode<TData>) {
|
|
665
|
+
const isExpanded = expandedRowIds[node.id] !== false;
|
|
666
|
+
const canToggle = node.children.length > 0;
|
|
667
|
+
|
|
668
|
+
if (node.depth < maxDepth) {
|
|
669
|
+
visibleRows.push({
|
|
670
|
+
id: `row:group:${node.id}`,
|
|
671
|
+
kind: 'group',
|
|
672
|
+
depth: node.depth,
|
|
673
|
+
pathLabels: [...node.pathLabels],
|
|
674
|
+
headerCells: createRowHeaderCells({
|
|
675
|
+
maxDepth,
|
|
676
|
+
kind: 'group',
|
|
677
|
+
node,
|
|
678
|
+
canToggle,
|
|
679
|
+
isExpanded,
|
|
680
|
+
rawRowCount: node.rawRowIndices.length
|
|
681
|
+
}),
|
|
682
|
+
rawRowIndices: [...node.rawRowIndices],
|
|
683
|
+
rawRowIds: [...node.rawRowIds],
|
|
684
|
+
rawRowCount: node.rawRowIndices.length,
|
|
685
|
+
cellMap: new Map()
|
|
686
|
+
});
|
|
687
|
+
|
|
688
|
+
if (isExpanded) {
|
|
689
|
+
node.children.forEach(walk);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
visibleRows.push({
|
|
693
|
+
id: createSubtotalAggregateId('row', node.id),
|
|
694
|
+
kind: 'subtotal',
|
|
695
|
+
depth: node.depth,
|
|
696
|
+
pathLabels: [...node.pathLabels, SUBTOTAL_LABEL],
|
|
697
|
+
headerCells: createRowHeaderCells({
|
|
698
|
+
maxDepth,
|
|
699
|
+
kind: 'subtotal',
|
|
700
|
+
node,
|
|
701
|
+
labelOverride: `${node.label} ${SUBTOTAL_LABEL}`,
|
|
702
|
+
rawRowCount: node.rawRowIndices.length
|
|
703
|
+
}),
|
|
704
|
+
rawRowIndices: [...node.rawRowIndices],
|
|
705
|
+
rawRowIds: [...node.rawRowIds],
|
|
706
|
+
rawRowCount: node.rawRowIndices.length,
|
|
707
|
+
cellMap: new Map()
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
visibleRows.push({
|
|
714
|
+
id: createLeafAggregateId('row', node.id),
|
|
715
|
+
kind: 'leaf',
|
|
716
|
+
depth: node.depth,
|
|
717
|
+
pathLabels: [...node.pathLabels],
|
|
718
|
+
headerCells: createRowHeaderCells({
|
|
719
|
+
maxDepth,
|
|
720
|
+
kind: 'leaf',
|
|
721
|
+
node,
|
|
722
|
+
rawRowCount: node.rawRowIndices.length
|
|
723
|
+
}),
|
|
724
|
+
rawRowIndices: [...node.rawRowIndices],
|
|
725
|
+
rawRowIds: [...node.rawRowIds],
|
|
726
|
+
rawRowCount: node.rawRowIndices.length,
|
|
727
|
+
cellMap: new Map()
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
visibleRows.push({
|
|
731
|
+
id: createSubtotalAggregateId('row', node.id),
|
|
732
|
+
kind: 'subtotal',
|
|
733
|
+
depth: node.depth,
|
|
734
|
+
pathLabels: [...node.pathLabels, SUBTOTAL_LABEL],
|
|
735
|
+
headerCells: createRowHeaderCells({
|
|
736
|
+
maxDepth,
|
|
737
|
+
kind: 'subtotal',
|
|
738
|
+
node,
|
|
739
|
+
labelOverride: `${node.label} ${SUBTOTAL_LABEL}`,
|
|
740
|
+
rawRowCount: node.rawRowIndices.length
|
|
741
|
+
}),
|
|
742
|
+
rawRowIndices: [...node.rawRowIndices],
|
|
743
|
+
rawRowIds: [...node.rawRowIds],
|
|
744
|
+
rawRowCount: node.rawRowIndices.length,
|
|
745
|
+
cellMap: new Map()
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
structure.rowTree.root.children.forEach(walk);
|
|
750
|
+
|
|
751
|
+
visibleRows.push({
|
|
752
|
+
id: structure.rowGrandTotalMeta.id,
|
|
753
|
+
kind: 'grand-total',
|
|
754
|
+
depth: 0,
|
|
755
|
+
pathLabels: [GRAND_TOTAL_LABEL],
|
|
756
|
+
headerCells: createRowHeaderCells({
|
|
757
|
+
maxDepth,
|
|
758
|
+
kind: 'grand-total',
|
|
759
|
+
rawRowCount: structure.rowGrandTotalMeta.rawRowIndices.length
|
|
760
|
+
}),
|
|
761
|
+
rawRowIndices: [...structure.rowGrandTotalMeta.rawRowIndices],
|
|
762
|
+
rawRowIds: [...structure.rowGrandTotalMeta.rawRowIds],
|
|
763
|
+
rawRowCount: structure.rowGrandTotalMeta.rawRowIndices.length,
|
|
764
|
+
cellMap: new Map()
|
|
765
|
+
});
|
|
766
|
+
|
|
767
|
+
return visibleRows;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
function createPathSegments<TData>(params: {
|
|
771
|
+
structure: PivotGridStructure<TData>;
|
|
772
|
+
node?: PivotAxisValueNode<TData>;
|
|
773
|
+
kind: PivotGridLeafColumn<TData>['kind'];
|
|
774
|
+
expandedColumnIds: Record<string, boolean>;
|
|
775
|
+
}): Array<PivotGridHeaderPathSegment | null> {
|
|
776
|
+
const {
|
|
777
|
+
structure,
|
|
778
|
+
node,
|
|
779
|
+
kind,
|
|
780
|
+
expandedColumnIds
|
|
781
|
+
} = params;
|
|
782
|
+
const segments: Array<PivotGridHeaderPathSegment | null> = Array.from({
|
|
783
|
+
length: structure.columnDimensions.length
|
|
784
|
+
}, () => null);
|
|
785
|
+
|
|
786
|
+
if (!node) {
|
|
787
|
+
segments[0] = {
|
|
788
|
+
key: 'column:grand-total',
|
|
789
|
+
label: GRAND_TOTAL_LABEL
|
|
790
|
+
};
|
|
791
|
+
|
|
792
|
+
return segments;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
if (kind === 'leaf') {
|
|
796
|
+
node.pathLabels.forEach((label, index) => {
|
|
797
|
+
const currentNodeId = node.pathNodeIds[index];
|
|
798
|
+
const currentNode = currentNodeId ? structure.columnTree.nodesById.get(currentNodeId) : undefined;
|
|
799
|
+
|
|
800
|
+
segments[index] = {
|
|
801
|
+
key: `${node.id}:${index}`,
|
|
802
|
+
label,
|
|
803
|
+
groupNodeId: currentNodeId,
|
|
804
|
+
canCollapse: Boolean(currentNode && currentNode.children.length > 0),
|
|
805
|
+
isExpanded: currentNodeId ? expandedColumnIds[currentNodeId] !== false : false
|
|
806
|
+
};
|
|
807
|
+
});
|
|
808
|
+
|
|
809
|
+
return segments;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
if (node.depth === structure.columnDimensions.length) {
|
|
813
|
+
node.pathLabels.slice(0, -1).forEach((label, index) => {
|
|
814
|
+
const currentNodeId = node.pathNodeIds[index];
|
|
815
|
+
const currentNode = currentNodeId ? structure.columnTree.nodesById.get(currentNodeId) : undefined;
|
|
816
|
+
|
|
817
|
+
segments[index] = {
|
|
818
|
+
key: `${node.id}:${index}`,
|
|
819
|
+
label,
|
|
820
|
+
groupNodeId: currentNodeId,
|
|
821
|
+
canCollapse: Boolean(currentNode && currentNode.children.length > 0),
|
|
822
|
+
isExpanded: currentNodeId ? expandedColumnIds[currentNodeId] !== false : false
|
|
823
|
+
};
|
|
824
|
+
});
|
|
825
|
+
segments[structure.columnDimensions.length - 1] = {
|
|
826
|
+
key: `${node.id}:subtotal:last`,
|
|
827
|
+
label: `${node.label} ${SUBTOTAL_LABEL}`
|
|
828
|
+
};
|
|
829
|
+
|
|
830
|
+
return segments;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
node.pathLabels.forEach((label, index) => {
|
|
834
|
+
const currentNodeId = node.pathNodeIds[index];
|
|
835
|
+
const currentNode = currentNodeId ? structure.columnTree.nodesById.get(currentNodeId) : undefined;
|
|
836
|
+
|
|
837
|
+
segments[index] = {
|
|
838
|
+
key: `${node.id}:${index}`,
|
|
839
|
+
label,
|
|
840
|
+
groupNodeId: currentNodeId,
|
|
841
|
+
canCollapse: Boolean(currentNode && currentNode.children.length > 0),
|
|
842
|
+
isExpanded: currentNodeId ? expandedColumnIds[currentNodeId] !== false : false
|
|
843
|
+
};
|
|
844
|
+
});
|
|
845
|
+
segments[node.depth] = {
|
|
846
|
+
key: `${node.id}:subtotal:${node.depth}`,
|
|
847
|
+
label: SUBTOTAL_LABEL
|
|
848
|
+
};
|
|
849
|
+
|
|
850
|
+
return segments;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
function createLeafColumnsForMeasureSet<TData>(params: {
|
|
854
|
+
structure: PivotGridStructure<TData>;
|
|
855
|
+
node?: PivotAxisValueNode<TData>;
|
|
856
|
+
kind: PivotGridLeafColumn<TData>['kind'];
|
|
857
|
+
expandedColumnIds: Record<string, boolean>;
|
|
858
|
+
}): Array<PivotGridLeafColumn<TData>> {
|
|
859
|
+
const {
|
|
860
|
+
structure,
|
|
861
|
+
node,
|
|
862
|
+
kind,
|
|
863
|
+
expandedColumnIds
|
|
864
|
+
} = params;
|
|
865
|
+
const baseSegments = createPathSegments({
|
|
866
|
+
structure,
|
|
867
|
+
node,
|
|
868
|
+
kind,
|
|
869
|
+
expandedColumnIds
|
|
870
|
+
});
|
|
871
|
+
const baseHeaderPath = baseSegments.map(segment => segment?.label ?? '');
|
|
872
|
+
const pathLabels = node ? kind === 'leaf' ? [...node.pathLabels] : [...node.pathLabels, SUBTOTAL_LABEL] : [GRAND_TOTAL_LABEL];
|
|
873
|
+
|
|
874
|
+
return structure.measures.map((measure) => {
|
|
875
|
+
const baseId = node ? kind === 'leaf' ? createLeafAggregateId('column', node.id) : createSubtotalAggregateId('column', node.id) : createGrandTotalAggregateId('column');
|
|
876
|
+
const id = `${baseId}__${measure.id}`;
|
|
877
|
+
|
|
878
|
+
return {
|
|
879
|
+
id,
|
|
880
|
+
ownerNodeId: node?.id,
|
|
881
|
+
measure,
|
|
882
|
+
kind,
|
|
883
|
+
headerPath: structure.measures.length > 1 ? [...baseHeaderPath, measure.label] : baseHeaderPath,
|
|
884
|
+
headerPathSegments: structure.measures.length > 1 ? [
|
|
885
|
+
...baseSegments,
|
|
886
|
+
{
|
|
887
|
+
key: `${id}:measure`,
|
|
888
|
+
label: measure.label
|
|
889
|
+
}
|
|
890
|
+
] : baseSegments,
|
|
891
|
+
pathLabels
|
|
892
|
+
};
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
function buildVisibleLeafColumns<TData>(
|
|
897
|
+
structure: PivotGridStructure<TData>,
|
|
898
|
+
expandedColumnIds: Record<string, boolean>
|
|
899
|
+
): Array<PivotGridLeafColumn<TData>> {
|
|
900
|
+
const visibleLeafColumns: Array<PivotGridLeafColumn<TData>> = [];
|
|
901
|
+
const maxDepth = structure.columnDimensions.length;
|
|
902
|
+
|
|
903
|
+
function walk(node: PivotAxisValueNode<TData>) {
|
|
904
|
+
const isExpanded = expandedColumnIds[node.id] !== false;
|
|
905
|
+
|
|
906
|
+
if (node.depth < maxDepth) {
|
|
907
|
+
if (isExpanded) {
|
|
908
|
+
node.children.forEach(walk);
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
visibleLeafColumns.push(
|
|
912
|
+
...createLeafColumnsForMeasureSet({
|
|
913
|
+
structure,
|
|
914
|
+
node,
|
|
915
|
+
kind: 'subtotal',
|
|
916
|
+
expandedColumnIds
|
|
917
|
+
})
|
|
918
|
+
);
|
|
919
|
+
|
|
920
|
+
return;
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
visibleLeafColumns.push(
|
|
924
|
+
...createLeafColumnsForMeasureSet({
|
|
925
|
+
structure,
|
|
926
|
+
node,
|
|
927
|
+
kind: 'leaf',
|
|
928
|
+
expandedColumnIds
|
|
929
|
+
})
|
|
930
|
+
);
|
|
931
|
+
visibleLeafColumns.push(
|
|
932
|
+
...createLeafColumnsForMeasureSet({
|
|
933
|
+
structure,
|
|
934
|
+
node,
|
|
935
|
+
kind: 'subtotal',
|
|
936
|
+
expandedColumnIds
|
|
937
|
+
})
|
|
938
|
+
);
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
structure.columnTree.root.children.forEach(walk);
|
|
942
|
+
visibleLeafColumns.push(
|
|
943
|
+
...createLeafColumnsForMeasureSet({
|
|
944
|
+
structure,
|
|
945
|
+
kind: 'grand-total',
|
|
946
|
+
expandedColumnIds
|
|
947
|
+
})
|
|
948
|
+
);
|
|
949
|
+
|
|
950
|
+
return visibleLeafColumns;
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
function buildRenderedCell<TData>(params: {
|
|
954
|
+
row: PivotGridRenderedRow<TData>;
|
|
955
|
+
column: PivotGridLeafColumn<TData>;
|
|
956
|
+
structure: PivotGridStructure<TData>;
|
|
957
|
+
}): PivotGridRenderedCell<TData> {
|
|
958
|
+
const {
|
|
959
|
+
row,
|
|
960
|
+
column,
|
|
961
|
+
structure
|
|
962
|
+
} = params;
|
|
963
|
+
const accumulator = structure.cellAccumulators.get(
|
|
964
|
+
getAccumulatorKey(row.id, column.id)
|
|
965
|
+
);
|
|
966
|
+
const value = measureAccumulatorValue(column.measure, accumulator);
|
|
967
|
+
|
|
968
|
+
return {
|
|
969
|
+
id: getAccumulatorKey(row.id, column.id),
|
|
970
|
+
rowId: row.id,
|
|
971
|
+
columnId: column.id,
|
|
972
|
+
measureId: column.measure.id,
|
|
973
|
+
value,
|
|
974
|
+
formattedValue: formatMeasureValue(column.measure, value),
|
|
975
|
+
rowPath: [...row.pathLabels],
|
|
976
|
+
columnPath: [...column.pathLabels],
|
|
977
|
+
rawRowCount: accumulator?.rawRowIds.length ?? 0,
|
|
978
|
+
rawRowIndices: [...(accumulator?.rawRowIndices ?? [])],
|
|
979
|
+
rawRowIds: [...(accumulator?.rawRowIds ?? [])],
|
|
980
|
+
kind: column.kind,
|
|
981
|
+
isSubtotal: row.kind === 'subtotal' || column.kind === 'subtotal',
|
|
982
|
+
isTotal:
|
|
983
|
+
row.kind === 'grand-total' || column.kind === 'grand-total',
|
|
984
|
+
isGrandTotal:
|
|
985
|
+
row.kind === 'grand-total' && column.kind === 'grand-total'
|
|
986
|
+
};
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
function attachCellsToRows<TData>(params: {
|
|
990
|
+
rows: Array<PivotGridRenderedRow<TData>>;
|
|
991
|
+
columns: Array<PivotGridLeafColumn<TData>>;
|
|
992
|
+
structure: PivotGridStructure<TData>;
|
|
993
|
+
}): Array<PivotGridRenderedRow<TData>> {
|
|
994
|
+
const {
|
|
995
|
+
rows,
|
|
996
|
+
columns,
|
|
997
|
+
structure
|
|
998
|
+
} = params;
|
|
999
|
+
|
|
1000
|
+
return rows.map((row) => {
|
|
1001
|
+
if (row.kind === 'group') {
|
|
1002
|
+
return row;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
const cellMap = new Map<string, PivotGridRenderedCell<TData>>();
|
|
1006
|
+
|
|
1007
|
+
columns.forEach((column) => {
|
|
1008
|
+
const cell = buildRenderedCell({
|
|
1009
|
+
row,
|
|
1010
|
+
column,
|
|
1011
|
+
structure
|
|
1012
|
+
});
|
|
1013
|
+
|
|
1014
|
+
cellMap.set(column.id, cell);
|
|
1015
|
+
});
|
|
1016
|
+
|
|
1017
|
+
return {
|
|
1018
|
+
...row,
|
|
1019
|
+
cellMap
|
|
1020
|
+
};
|
|
1021
|
+
});
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
export function buildPivotGridView<TData>(params: {
|
|
1025
|
+
structure: PivotGridStructure<TData>;
|
|
1026
|
+
expandedRowIds: Record<string, boolean>;
|
|
1027
|
+
expandedColumnIds: Record<string, boolean>;
|
|
1028
|
+
}): PivotGridViewModel<TData> {
|
|
1029
|
+
const {
|
|
1030
|
+
structure,
|
|
1031
|
+
expandedRowIds,
|
|
1032
|
+
expandedColumnIds
|
|
1033
|
+
} = params;
|
|
1034
|
+
const visibleLeafColumns = buildVisibleLeafColumns(
|
|
1035
|
+
structure,
|
|
1036
|
+
expandedColumnIds
|
|
1037
|
+
);
|
|
1038
|
+
const visibleRows = attachCellsToRows({
|
|
1039
|
+
rows: buildVisibleRows(structure, expandedRowIds),
|
|
1040
|
+
columns: visibleLeafColumns,
|
|
1041
|
+
structure
|
|
1042
|
+
});
|
|
1043
|
+
|
|
1044
|
+
return {
|
|
1045
|
+
visibleRows,
|
|
1046
|
+
visibleLeafColumns
|
|
1047
|
+
};
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
export function buildPivotGridHeaderRows<TData>(params: {
|
|
1051
|
+
columnsById: Map<string, PivotGridLeafColumn<TData>>;
|
|
1052
|
+
orderedColumnIds: Array<string>;
|
|
1053
|
+
pinPositionByColumnId: Map<string, PivotGridPinPosition>;
|
|
1054
|
+
headerDepth: number;
|
|
1055
|
+
}): Array<PivotGridHeaderRow> {
|
|
1056
|
+
const {
|
|
1057
|
+
columnsById,
|
|
1058
|
+
orderedColumnIds,
|
|
1059
|
+
pinPositionByColumnId,
|
|
1060
|
+
headerDepth
|
|
1061
|
+
} = params;
|
|
1062
|
+
const rows: Array<PivotGridHeaderRow> = [];
|
|
1063
|
+
|
|
1064
|
+
for (let level = 0; level < headerDepth; level += 1) {
|
|
1065
|
+
const nextCells: PivotGridHeaderRow['cells'] = [];
|
|
1066
|
+
let currentCell: PivotGridHeaderRow['cells'][number] | null = null;
|
|
1067
|
+
let currentKey = '';
|
|
1068
|
+
|
|
1069
|
+
orderedColumnIds.forEach((columnId) => {
|
|
1070
|
+
const column = columnsById.get(columnId);
|
|
1071
|
+
const segment = column?.headerPathSegments[level] ?? null;
|
|
1072
|
+
const label = segment?.label ?? '';
|
|
1073
|
+
const pinPosition = pinPositionByColumnId.get(columnId) ?? false;
|
|
1074
|
+
const nextKey = `${segment?.key ?? `${columnId}:blank:${level}`}:${pinPosition}`;
|
|
1075
|
+
|
|
1076
|
+
if (
|
|
1077
|
+
currentCell
|
|
1078
|
+
&& currentKey === nextKey
|
|
1079
|
+
&& currentCell.label === label
|
|
1080
|
+
) {
|
|
1081
|
+
currentCell.colSpan += 1;
|
|
1082
|
+
currentCell.columnIds.push(columnId);
|
|
1083
|
+
|
|
1084
|
+
return;
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
currentCell = {
|
|
1088
|
+
key: nextKey,
|
|
1089
|
+
label,
|
|
1090
|
+
level,
|
|
1091
|
+
columnIds: [columnId],
|
|
1092
|
+
colSpan: 1,
|
|
1093
|
+
pinPosition,
|
|
1094
|
+
groupNodeId: segment?.groupNodeId,
|
|
1095
|
+
canCollapse: segment?.canCollapse ?? false,
|
|
1096
|
+
isExpanded: segment?.isExpanded ?? false
|
|
1097
|
+
};
|
|
1098
|
+
currentKey = nextKey;
|
|
1099
|
+
nextCells.push(currentCell);
|
|
1100
|
+
});
|
|
1101
|
+
|
|
1102
|
+
rows.push({
|
|
1103
|
+
id: `pivot-header-${level}`,
|
|
1104
|
+
level,
|
|
1105
|
+
cells: nextCells
|
|
1106
|
+
});
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
return rows;
|
|
1110
|
+
}
|