@salmonui/pivot-table 0.0.0-stage → 0.3.0
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/LICENSE +21 -0
- package/README.md +49 -2
- package/dist/engine/csv.d.ts +12 -0
- package/dist/engine/csv.d.ts.map +1 -0
- package/dist/engine/csv.js +93 -0
- package/dist/engine/csv.js.map +1 -0
- package/dist/engine/engine.d.ts +103 -0
- package/dist/engine/engine.d.ts.map +1 -0
- package/dist/engine/engine.js +1851 -0
- package/dist/engine/engine.js.map +1 -0
- package/dist/engine/fields.d.ts +59 -0
- package/dist/engine/fields.d.ts.map +1 -0
- package/dist/engine/fields.js +293 -0
- package/dist/engine/fields.js.map +1 -0
- package/dist/engine/formula.d.ts +29 -0
- package/dist/engine/formula.d.ts.map +1 -0
- package/dist/engine/formula.js +286 -0
- package/dist/engine/formula.js.map +1 -0
- package/dist/engine/hash.d.ts +8 -0
- package/dist/engine/hash.d.ts.map +1 -0
- package/dist/engine/hash.js +71 -0
- package/dist/engine/hash.js.map +1 -0
- package/dist/engine/index.d.ts +9 -0
- package/dist/engine/index.d.ts.map +1 -0
- package/dist/engine/index.js +12 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/engine/labels.d.ts +24 -0
- package/dist/engine/labels.d.ts.map +1 -0
- package/dist/engine/labels.js +47 -0
- package/dist/engine/labels.js.map +1 -0
- package/dist/engine/model.d.ts +11 -0
- package/dist/engine/model.d.ts.map +1 -0
- package/dist/engine/model.js +76 -0
- package/dist/engine/model.js.map +1 -0
- package/dist/engine/types.d.ts +252 -0
- package/dist/engine/types.d.ts.map +1 -0
- package/dist/engine/types.js +8 -0
- package/dist/engine/types.js.map +1 -0
- package/dist/engine/worker-host.d.ts +42 -0
- package/dist/engine/worker-host.d.ts.map +1 -0
- package/dist/engine/worker-host.js +126 -0
- package/dist/engine/worker-host.js.map +1 -0
- package/dist/engine/worker-protocol.d.ts +105 -0
- package/dist/engine/worker-protocol.d.ts.map +1 -0
- package/dist/engine/worker-protocol.js +66 -0
- package/dist/engine/worker-protocol.js.map +1 -0
- package/dist/engine/worker.d.ts +5 -0
- package/dist/engine/worker.d.ts.map +1 -0
- package/dist/engine/worker.js +79 -0
- package/dist/engine/worker.js.map +1 -0
- package/dist/pivot-table/PivotAxisBar.d.ts +17 -0
- package/dist/pivot-table/PivotAxisBar.d.ts.map +1 -0
- package/dist/pivot-table/PivotAxisBar.js +68 -0
- package/dist/pivot-table/PivotAxisBar.js.map +1 -0
- package/dist/pivot-table/PivotFieldList.d.ts +23 -0
- package/dist/pivot-table/PivotFieldList.d.ts.map +1 -0
- package/dist/pivot-table/PivotFieldList.js +106 -0
- package/dist/pivot-table/PivotFieldList.js.map +1 -0
- package/dist/pivot-table/PivotTable.d.ts +91 -0
- package/dist/pivot-table/PivotTable.d.ts.map +1 -0
- package/dist/pivot-table/PivotTable.js +320 -0
- package/dist/pivot-table/PivotTable.js.map +1 -0
- package/dist/pivot-table/PivotToolbar.d.ts +21 -0
- package/dist/pivot-table/PivotToolbar.d.ts.map +1 -0
- package/dist/pivot-table/PivotToolbar.js +102 -0
- package/dist/pivot-table/PivotToolbar.js.map +1 -0
- package/dist/pivot-table/chart-view.d.ts +48 -0
- package/dist/pivot-table/chart-view.d.ts.map +1 -0
- package/dist/pivot-table/chart-view.js +84 -0
- package/dist/pivot-table/chart-view.js.map +1 -0
- package/dist/pivot-table/chart.d.ts +122 -0
- package/dist/pivot-table/chart.d.ts.map +1 -0
- package/dist/pivot-table/chart.js +207 -0
- package/dist/pivot-table/chart.js.map +1 -0
- package/dist/pivot-table/columns.d.ts +49 -0
- package/dist/pivot-table/columns.d.ts.map +1 -0
- package/dist/pivot-table/columns.js +279 -0
- package/dist/pivot-table/columns.js.map +1 -0
- package/dist/pivot-table/controller.d.ts +164 -0
- package/dist/pivot-table/controller.d.ts.map +1 -0
- package/dist/pivot-table/controller.js +691 -0
- package/dist/pivot-table/controller.js.map +1 -0
- package/dist/pivot-table/default-text.d.ts +167 -0
- package/dist/pivot-table/default-text.d.ts.map +1 -0
- package/dist/pivot-table/default-text.js +55 -0
- package/dist/pivot-table/default-text.js.map +1 -0
- package/dist/pivot-table/dialogs.d.ts +69 -0
- package/dist/pivot-table/dialogs.d.ts.map +1 -0
- package/dist/pivot-table/dialogs.js +409 -0
- package/dist/pivot-table/dialogs.js.map +1 -0
- package/dist/pivot-table/export-impl.d.ts +11 -0
- package/dist/pivot-table/export-impl.d.ts.map +1 -0
- package/dist/pivot-table/export-impl.js +175 -0
- package/dist/pivot-table/export-impl.js.map +1 -0
- package/dist/pivot-table/export.d.ts +36 -0
- package/dist/pivot-table/export.d.ts.map +1 -0
- package/dist/pivot-table/export.js +6 -0
- package/dist/pivot-table/export.js.map +1 -0
- package/dist/pivot-table/field-item.d.ts +42 -0
- package/dist/pivot-table/field-item.d.ts.map +1 -0
- package/dist/pivot-table/field-item.js +159 -0
- package/dist/pivot-table/field-item.js.map +1 -0
- package/dist/pivot-table/field-menu.d.ts +26 -0
- package/dist/pivot-table/field-menu.d.ts.map +1 -0
- package/dist/pivot-table/field-menu.js +94 -0
- package/dist/pivot-table/field-menu.js.map +1 -0
- package/dist/pivot-table/grid-model.d.ts +54 -0
- package/dist/pivot-table/grid-model.d.ts.map +1 -0
- package/dist/pivot-table/grid-model.js +154 -0
- package/dist/pivot-table/grid-model.js.map +1 -0
- package/dist/pivot-table/locale.d.ts +139 -0
- package/dist/pivot-table/locale.d.ts.map +1 -0
- package/dist/pivot-table/locale.js +125 -0
- package/dist/pivot-table/locale.js.map +1 -0
- package/dist/pivot-table/menu-popup.d.ts +11 -0
- package/dist/pivot-table/menu-popup.d.ts.map +1 -0
- package/dist/pivot-table/menu-popup.js +17 -0
- package/dist/pivot-table/menu-popup.js.map +1 -0
- package/dist/pivot-table/model-ops.d.ts +27 -0
- package/dist/pivot-table/model-ops.d.ts.map +1 -0
- package/dist/pivot-table/model-ops.js +115 -0
- package/dist/pivot-table/model-ops.js.map +1 -0
- package/dist/pivot-table/state.d.ts +6 -0
- package/dist/pivot-table/state.d.ts.map +1 -0
- package/dist/pivot-table/state.js +61 -0
- package/dist/pivot-table/state.js.map +1 -0
- package/dist/pivot-table/types.d.ts +330 -0
- package/dist/pivot-table/types.d.ts.map +1 -0
- package/dist/pivot-table/types.js +4 -0
- package/dist/pivot-table/types.js.map +1 -0
- package/dist/pivot-table/usePivot.d.ts +11 -0
- package/dist/pivot-table/usePivot.d.ts.map +1 -0
- package/dist/pivot-table/usePivot.js +41 -0
- package/dist/pivot-table/usePivot.js.map +1 -0
- package/dist/pivot-table/windowed.d.ts +147 -0
- package/dist/pivot-table/windowed.d.ts.map +1 -0
- package/dist/pivot-table/windowed.js +265 -0
- package/dist/pivot-table/windowed.js.map +1 -0
- package/dist/pivot-table-chart.d.ts +3 -0
- package/dist/pivot-table-chart.d.ts.map +1 -0
- package/dist/pivot-table-chart.js +8 -0
- package/dist/pivot-table-chart.js.map +1 -0
- package/dist/pivot-table.css +4762 -0
- package/dist/pivot-table.d.ts +19 -0
- package/dist/pivot-table.d.ts.map +1 -0
- package/dist/pivot-table.js +18 -0
- package/dist/pivot-table.js.map +1 -0
- package/package.json +64 -4
|
@@ -0,0 +1,691 @@
|
|
|
1
|
+
// PivotController: the store shared by
|
|
2
|
+
// PivotTable, PivotFieldList, PivotAxisBar, PivotToolbar and the chart
|
|
3
|
+
// adapters. The engine owns aggregation and the result; the controller owns
|
|
4
|
+
// the applied model, the draft model (defer update), the compute status, the
|
|
5
|
+
// selection and the open dialog, and runs every change as a cancellable
|
|
6
|
+
// command (before-event, apply, after-event). React reads it through
|
|
7
|
+
// `useSyncExternalStore` with narrow selectors (`usePivotState`).
|
|
8
|
+
//
|
|
9
|
+
// Render purity: the constructor runs inside a `useState` initializer (it
|
|
10
|
+
// computes the first result synchronously for local data so the server and
|
|
11
|
+
// the client render the same markup); everything else runs in effects and
|
|
12
|
+
// event handlers.
|
|
13
|
+
import { createPivotEngine, createPivotWorkerEngine, normalizePivotModel, stableStringifyModel, } from '@salmonui/pivot-table/engine';
|
|
14
|
+
import { gridEvent } from '@salmonui/core/internal/events';
|
|
15
|
+
import { fieldInfosOf, isAsyncEngine, isRemoteEngine, remoteAsyncEngine, WindowedPivot } from './windowed.js';
|
|
16
|
+
import { axisItems, axisOf, moveField as moveFieldOp, omit, setAggregate as setAggregateOp, VALUES_FIELD } from './model-ops.js';
|
|
17
|
+
/** Rows above which the first compute runs on the client after the first paint (overridable). */
|
|
18
|
+
export const DEFAULT_DEFER_COMPUTE_ABOVE = 50_000;
|
|
19
|
+
const now = () => (typeof performance !== 'undefined' ? performance.now() : Date.now());
|
|
20
|
+
const isPromise = (value) => typeof value?.then === 'function';
|
|
21
|
+
const toError = (error) => (error instanceof Error ? error : new Error(String(error)));
|
|
22
|
+
const samePath = (a, b) => a.length === b.length && a.every((x, i) => JSON.stringify(x) === JSON.stringify(b[i]));
|
|
23
|
+
/**
|
|
24
|
+
* The store of one pivot report. Create it with `usePivot()`; pass it to the
|
|
25
|
+
* pivot parts as `pivot`.
|
|
26
|
+
*/
|
|
27
|
+
export class PivotController {
|
|
28
|
+
/** Latest event handlers (replaced on every `usePivot()` render, read when an event fires). */
|
|
29
|
+
handlers;
|
|
30
|
+
options;
|
|
31
|
+
state;
|
|
32
|
+
listeners = new Set();
|
|
33
|
+
engine = null;
|
|
34
|
+
unsubscribeEngine = null;
|
|
35
|
+
/** Window adapter of an asynchronous (Worker) engine; null for the local engine. */
|
|
36
|
+
windowed = null;
|
|
37
|
+
memberCache = { version: null, data: null, fields: new Map() };
|
|
38
|
+
data;
|
|
39
|
+
initial;
|
|
40
|
+
mounted = false;
|
|
41
|
+
appliedPropKey;
|
|
42
|
+
pending = 0;
|
|
43
|
+
constructor(options) {
|
|
44
|
+
this.options = options;
|
|
45
|
+
this.handlers = options;
|
|
46
|
+
this.data = options.data ?? [];
|
|
47
|
+
const model = normalizePivotModel(options.pivotModel ?? options.defaultPivotModel);
|
|
48
|
+
this.appliedPropKey = options.pivotModel ? stableStringifyModel(model) : null;
|
|
49
|
+
const deferred = this.isLarge();
|
|
50
|
+
// The definitions' captions show before the first result (a remote or deferred compute).
|
|
51
|
+
const fields = options.fields ? fieldInfosOf(options.fields) : [];
|
|
52
|
+
this.state = { result: null, model, draft: null, status: deferred ? 'computing' : 'idle', error: null, fields, selection: [], dialog: null };
|
|
53
|
+
const t0 = now();
|
|
54
|
+
if (!deferred)
|
|
55
|
+
this.createEngine(model);
|
|
56
|
+
this.initial = { duration: now() - t0, deferred };
|
|
57
|
+
}
|
|
58
|
+
// -------------------------------------------------------------------------
|
|
59
|
+
// Store
|
|
60
|
+
// -------------------------------------------------------------------------
|
|
61
|
+
subscribe = (listener) => {
|
|
62
|
+
this.listeners.add(listener);
|
|
63
|
+
return () => {
|
|
64
|
+
this.listeners.delete(listener);
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
getSnapshot = () => this.state;
|
|
68
|
+
/** Current result (null before the first compute). */
|
|
69
|
+
get result() {
|
|
70
|
+
return this.state.result;
|
|
71
|
+
}
|
|
72
|
+
get status() {
|
|
73
|
+
return this.state.status;
|
|
74
|
+
}
|
|
75
|
+
get model() {
|
|
76
|
+
return this.state.model;
|
|
77
|
+
}
|
|
78
|
+
/** Raw rows the engine aggregates (after drill-through edits). */
|
|
79
|
+
getData() {
|
|
80
|
+
return this.data;
|
|
81
|
+
}
|
|
82
|
+
set(patch) {
|
|
83
|
+
let changed = false;
|
|
84
|
+
for (const key of Object.keys(patch))
|
|
85
|
+
if (this.state[key] !== patch[key])
|
|
86
|
+
changed = true;
|
|
87
|
+
if (!changed)
|
|
88
|
+
return;
|
|
89
|
+
this.state = { ...this.state, ...patch };
|
|
90
|
+
for (const listener of [...this.listeners])
|
|
91
|
+
listener();
|
|
92
|
+
}
|
|
93
|
+
// -------------------------------------------------------------------------
|
|
94
|
+
// Engine lifecycle
|
|
95
|
+
// -------------------------------------------------------------------------
|
|
96
|
+
engineOptions(model) {
|
|
97
|
+
const o = this.options;
|
|
98
|
+
const options = { data: this.data, model };
|
|
99
|
+
if (o.fields)
|
|
100
|
+
options.fields = o.fields;
|
|
101
|
+
if (o.locale)
|
|
102
|
+
options.locale = o.locale;
|
|
103
|
+
if (o.customAggregates)
|
|
104
|
+
options.customAggregates = o.customAggregates;
|
|
105
|
+
if (o.getCellValue)
|
|
106
|
+
options.getCellValue = o.getCellValue;
|
|
107
|
+
return options;
|
|
108
|
+
}
|
|
109
|
+
createEngine(model) {
|
|
110
|
+
try {
|
|
111
|
+
const o = this.options;
|
|
112
|
+
if (o.createEngine) {
|
|
113
|
+
const created = o.createEngine({ model, locale: o.locale });
|
|
114
|
+
// A remote engine (`createJsonPivotSource().createEngine()`) is driven through the window adapter.
|
|
115
|
+
this.engine = (isRemoteEngine(created) ? remoteAsyncEngine(created, o.fields) : created);
|
|
116
|
+
}
|
|
117
|
+
else if (o.worker) {
|
|
118
|
+
const { customAggregates: _c, getCellValue: _g, ...rest } = this.engineOptions(model);
|
|
119
|
+
// The Worker host answers asynchronously; commands resolve through `run`, cells through the window adapter.
|
|
120
|
+
this.engine = createPivotWorkerEngine(o.worker, rest);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
this.engine = createPivotEngine(this.engineOptions(model));
|
|
124
|
+
}
|
|
125
|
+
if (isAsyncEngine(this.engine)) {
|
|
126
|
+
const windowed = new WindowedPivot(this.engine, this.syncFromEngine);
|
|
127
|
+
this.windowed = windowed;
|
|
128
|
+
this.unsubscribeEngine = () => windowed.destroy();
|
|
129
|
+
}
|
|
130
|
+
else
|
|
131
|
+
this.unsubscribeEngine = this.engine.subscribe(this.syncFromEngine);
|
|
132
|
+
this.syncFromEngine();
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
this.engine = null;
|
|
136
|
+
this.state = { ...this.state, status: 'error', error: toError(error) };
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/** Reads the engine snapshot into the store (also the engine listener). */
|
|
140
|
+
syncFromEngine = () => {
|
|
141
|
+
const engine = this.engine;
|
|
142
|
+
if (!engine)
|
|
143
|
+
return;
|
|
144
|
+
const windowed = this.windowed;
|
|
145
|
+
const result = windowed ? windowed.result : (engine.getSnapshot() ?? null);
|
|
146
|
+
const model = typeof engine.getModel === 'function' ? (engine.getModel() ?? this.state.model) : this.state.model;
|
|
147
|
+
// The window adapter has no fields until its engine is ready: keep the definitions' infos until then.
|
|
148
|
+
const fields = windowed ? (windowed.fields.length ? windowed.fields : this.state.fields) : (engine.getFields?.() ?? this.state.fields);
|
|
149
|
+
const asyncStatus = windowed ? engine.getStatus() : null;
|
|
150
|
+
const status = asyncStatus === 'error' ? 'error' : this.pending > 0 || asyncStatus === 'computing' ? 'computing' : result ? 'ready' : this.state.status === 'error' ? 'error' : 'computing';
|
|
151
|
+
// Keep the model identity when the engine returns an equal copy.
|
|
152
|
+
const nextModel = model === this.state.model || stableStringifyModel(model) === stableStringifyModel(this.state.model) ? this.state.model : model;
|
|
153
|
+
this.set({ result, model: nextModel, fields, status, error: status === 'ready' ? null : this.state.error });
|
|
154
|
+
};
|
|
155
|
+
/** Called from an effect: subscribes to the engine, starts a deferred compute, reports the initial compute. */
|
|
156
|
+
mount() {
|
|
157
|
+
this.mounted = true;
|
|
158
|
+
let timer;
|
|
159
|
+
const initial = this.initial;
|
|
160
|
+
this.initial = null;
|
|
161
|
+
if (!this.engine) {
|
|
162
|
+
if (initial?.deferred ?? this.isLarge()) {
|
|
163
|
+
// Deferred (large input or Worker): compute after the first paint; the loading state shows meanwhile.
|
|
164
|
+
this.emitCompute('onComputeStart', 'initial', 0);
|
|
165
|
+
const t0 = now();
|
|
166
|
+
timer = setTimeout(() => {
|
|
167
|
+
this.createEngine(this.state.model);
|
|
168
|
+
if (this.state.status === 'error')
|
|
169
|
+
this.emitError(this.state.error, 'compute');
|
|
170
|
+
else
|
|
171
|
+
this.whenReady(() => this.emitCompute('onComputeEnd', 'initial', now() - t0));
|
|
172
|
+
}, 0);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
// Recreated after an unmount (Strict Mode); the previous result stays on screen until then.
|
|
176
|
+
this.createEngine(this.state.model);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
else if (initial) {
|
|
180
|
+
this.emitCompute('onComputeStart', 'initial', 0);
|
|
181
|
+
if (this.state.status === 'error')
|
|
182
|
+
this.emitError(this.state.error, 'compute');
|
|
183
|
+
else
|
|
184
|
+
this.emitCompute('onComputeEnd', 'initial', initial.duration);
|
|
185
|
+
}
|
|
186
|
+
return () => {
|
|
187
|
+
this.mounted = false;
|
|
188
|
+
if (timer !== undefined)
|
|
189
|
+
clearTimeout(timer);
|
|
190
|
+
this.unsubscribeEngine?.();
|
|
191
|
+
this.unsubscribeEngine = null;
|
|
192
|
+
this.engine?.destroy();
|
|
193
|
+
this.engine = null;
|
|
194
|
+
this.windowed = null;
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
isLarge() {
|
|
198
|
+
return this.options.worker !== undefined || this.options.createEngine !== undefined || this.data.length > (this.options.deferComputeAbove ?? DEFAULT_DEFER_COMPUTE_ABOVE);
|
|
199
|
+
}
|
|
200
|
+
whenReady(callback) {
|
|
201
|
+
if (this.state.status !== 'computing') {
|
|
202
|
+
callback();
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const off = this.subscribe(() => {
|
|
206
|
+
if (this.state.status === 'computing')
|
|
207
|
+
return;
|
|
208
|
+
off();
|
|
209
|
+
callback();
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
/** Latest options (from `usePivot()` renders): handlers and creation-only fields. */
|
|
213
|
+
updateOptions(options) {
|
|
214
|
+
this.options = options;
|
|
215
|
+
this.handlers = options;
|
|
216
|
+
}
|
|
217
|
+
/** Applies the reactive `pivotModel` prop when it changed structurally. */
|
|
218
|
+
applyModelProp(input) {
|
|
219
|
+
if (!input)
|
|
220
|
+
return;
|
|
221
|
+
const model = normalizePivotModel(input);
|
|
222
|
+
const key = stableStringifyModel(model);
|
|
223
|
+
if (key === this.appliedPropKey)
|
|
224
|
+
return;
|
|
225
|
+
this.appliedPropKey = key;
|
|
226
|
+
if (key === stableStringifyModel(this.state.model))
|
|
227
|
+
return;
|
|
228
|
+
this.commit(model, 'prop', null, 'api', null, true);
|
|
229
|
+
}
|
|
230
|
+
/** Applies a new `data` prop (identity change). */
|
|
231
|
+
applyDataProp(data) {
|
|
232
|
+
const next = data ?? [];
|
|
233
|
+
if (next === this.data)
|
|
234
|
+
return;
|
|
235
|
+
this.data = next;
|
|
236
|
+
if (this.engine)
|
|
237
|
+
this.run('compute', 'update', () => this.engine?.setData(next));
|
|
238
|
+
}
|
|
239
|
+
// -------------------------------------------------------------------------
|
|
240
|
+
// Events
|
|
241
|
+
// -------------------------------------------------------------------------
|
|
242
|
+
event(name, source, fields) {
|
|
243
|
+
const handler = this.handlers[name];
|
|
244
|
+
if (!handler)
|
|
245
|
+
return null;
|
|
246
|
+
const event = gridEvent(name.slice(2, 3).toLowerCase() + name.slice(3), source, fields);
|
|
247
|
+
handler(event);
|
|
248
|
+
return event;
|
|
249
|
+
}
|
|
250
|
+
/** Fires a before-event; true when it was not cancelled. */
|
|
251
|
+
allowed(name, source, fields) {
|
|
252
|
+
const event = this.event(name, source, fields);
|
|
253
|
+
return !event?.defaultPrevented;
|
|
254
|
+
}
|
|
255
|
+
emitCompute(name, phase, duration) {
|
|
256
|
+
this.event(name, 'api', { phase, status: this.state.status, duration, result: this.state.result });
|
|
257
|
+
}
|
|
258
|
+
emitError(error, action) {
|
|
259
|
+
if (!this.event('onError', 'api', { error, action })) {
|
|
260
|
+
if (typeof console !== 'undefined')
|
|
261
|
+
console.error(error);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
/** Runs an engine command with the compute events, the status, and error reporting. */
|
|
265
|
+
run(action, phase, fn) {
|
|
266
|
+
const engine = this.engine;
|
|
267
|
+
if (!engine)
|
|
268
|
+
return;
|
|
269
|
+
const t0 = now();
|
|
270
|
+
this.emitCompute('onComputeStart', phase, 0);
|
|
271
|
+
let value;
|
|
272
|
+
try {
|
|
273
|
+
value = fn();
|
|
274
|
+
}
|
|
275
|
+
catch (error) {
|
|
276
|
+
this.set({ status: 'error', error: toError(error) });
|
|
277
|
+
this.emitError(toError(error), action);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (isPromise(value)) {
|
|
281
|
+
this.pending++;
|
|
282
|
+
this.set({ status: 'computing' });
|
|
283
|
+
value.then(() => {
|
|
284
|
+
this.pending--;
|
|
285
|
+
this.syncFromEngine();
|
|
286
|
+
this.emitCompute('onComputeEnd', phase, now() - t0);
|
|
287
|
+
}, (error) => {
|
|
288
|
+
this.pending--;
|
|
289
|
+
this.set({ status: 'error', error: toError(error) });
|
|
290
|
+
this.emitError(toError(error), action);
|
|
291
|
+
});
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
this.syncFromEngine();
|
|
295
|
+
this.emitCompute('onComputeEnd', phase, now() - t0);
|
|
296
|
+
}
|
|
297
|
+
// -------------------------------------------------------------------------
|
|
298
|
+
// Model commands
|
|
299
|
+
// -------------------------------------------------------------------------
|
|
300
|
+
/**
|
|
301
|
+
* Applies a model (cancellable through `onBeforePivotModelChange`).
|
|
302
|
+
* Returns false when the change was cancelled or changed nothing.
|
|
303
|
+
*/
|
|
304
|
+
setModel(input, options = {}) {
|
|
305
|
+
const base = options.draft ? (this.state.draft ?? this.state.model) : this.state.model;
|
|
306
|
+
const next = normalizePivotModel(typeof input === 'function' ? input(base) : input);
|
|
307
|
+
if (options.draft) {
|
|
308
|
+
this.set({ draft: next });
|
|
309
|
+
return true;
|
|
310
|
+
}
|
|
311
|
+
return this.commit(next, options.action ?? 'set', options.field ?? null, options.source ?? 'api', null);
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Shared commit path: model before-event, the engine command, the after-event.
|
|
315
|
+
* `apply` runs a specialized engine command (expand, collapse) instead of `setModel`.
|
|
316
|
+
*/
|
|
317
|
+
commit(next, action, field, source, apply, force = false) {
|
|
318
|
+
const previousModel = this.state.model;
|
|
319
|
+
if (!force && stableStringifyModel(next) === stableStringifyModel(previousModel))
|
|
320
|
+
return false;
|
|
321
|
+
if (!this.allowed('onBeforePivotModelChange', source, { action, model: next, previousModel, field }))
|
|
322
|
+
return false;
|
|
323
|
+
if (this.engine) {
|
|
324
|
+
this.run(action, 'update', apply ?? (() => this.engine?.setModel(next)));
|
|
325
|
+
}
|
|
326
|
+
// Without an engine yet (deferred compute) the model is kept and computed on creation.
|
|
327
|
+
if (!this.engine || this.state.model === previousModel)
|
|
328
|
+
this.set({ model: next });
|
|
329
|
+
this.event('onPivotModelChange', source, { action, model: this.state.model, previousModel, field });
|
|
330
|
+
return true;
|
|
331
|
+
}
|
|
332
|
+
/** Commits the draft model of a deferred field list (Apply). */
|
|
333
|
+
applyDraft(source = 'api') {
|
|
334
|
+
const draft = this.state.draft;
|
|
335
|
+
if (!draft)
|
|
336
|
+
return false;
|
|
337
|
+
this.set({ draft: null });
|
|
338
|
+
return this.commit(draft, 'apply', null, source, null);
|
|
339
|
+
}
|
|
340
|
+
/** Drops the draft model (Cancel). */
|
|
341
|
+
cancelDraft() {
|
|
342
|
+
this.set({ draft: null });
|
|
343
|
+
}
|
|
344
|
+
/** Starts a draft from the applied model (a deferred field list opening). */
|
|
345
|
+
beginDraft() {
|
|
346
|
+
if (!this.state.draft)
|
|
347
|
+
this.set({ draft: this.state.model });
|
|
348
|
+
}
|
|
349
|
+
/** Moves a field between axes or inside one; `to: null` removes it (cancellable through `onBeforeFieldMove`). */
|
|
350
|
+
moveField(field, to, index = -1, options = {}) {
|
|
351
|
+
const base = options.draft ? (this.state.draft ?? this.state.model) : this.state.model;
|
|
352
|
+
const from = field === VALUES_FIELD ? base.valuesPlacement.axis : axisOf(base, field);
|
|
353
|
+
const source = options.source ?? 'api';
|
|
354
|
+
if (!this.allowed('onBeforeFieldMove', source, { field, from, to, index }))
|
|
355
|
+
return false;
|
|
356
|
+
const done = this.setModel(moveFieldOp(base, field, to, index, this.state.fields), { ...options, action: to === null ? 'removeField' : 'moveField', field });
|
|
357
|
+
if (done)
|
|
358
|
+
this.event('onFieldMove', source, { field, from, to, index });
|
|
359
|
+
return done;
|
|
360
|
+
}
|
|
361
|
+
/** Moves a field one step inside its axis (Alt+ArrowUp / ArrowDown). */
|
|
362
|
+
stepField(field, axis, step, options = {}) {
|
|
363
|
+
const base = options.draft ? (this.state.draft ?? this.state.model) : this.state.model;
|
|
364
|
+
const items = axisItems(base, axis);
|
|
365
|
+
const i = items.indexOf(field);
|
|
366
|
+
const j = i + step;
|
|
367
|
+
if (i < 0 || j < 0 || j >= items.length)
|
|
368
|
+
return false;
|
|
369
|
+
return this.moveField(field, axis, j, options);
|
|
370
|
+
}
|
|
371
|
+
/** Member, label or value filter of a field; null clears it (cancellable through `onBeforeFilterChange`). */
|
|
372
|
+
setFilter(field, filter, options = {}) {
|
|
373
|
+
const base = options.draft ? (this.state.draft ?? this.state.model) : this.state.model;
|
|
374
|
+
const previousFilter = base.filterBy[field] ?? null;
|
|
375
|
+
const source = options.source ?? 'api';
|
|
376
|
+
if (!this.allowed('onBeforeFilterChange', source, { field, filter, previousFilter }))
|
|
377
|
+
return false;
|
|
378
|
+
const filterBy = filter ? { ...base.filterBy, [field]: filter } : omit(base.filterBy, field);
|
|
379
|
+
const done = this.setModel({ ...base, filterBy }, { ...options, action: 'filter', field });
|
|
380
|
+
if (done)
|
|
381
|
+
this.event('onFilterChange', source, { field, filter, previousFilter });
|
|
382
|
+
return done;
|
|
383
|
+
}
|
|
384
|
+
/** Member sort of a field (cancellable through `onBeforeSortChange`). */
|
|
385
|
+
setSort(field, sort, options = {}) {
|
|
386
|
+
const base = options.draft ? (this.state.draft ?? this.state.model) : this.state.model;
|
|
387
|
+
const source = options.source ?? 'api';
|
|
388
|
+
if (!this.allowed('onBeforeSortChange', source, { field, sort, valueSort: base.valueSort }))
|
|
389
|
+
return false;
|
|
390
|
+
const done = this.setModel({ ...base, sort: { ...base.sort, [field]: sort } }, { ...options, action: 'sort', field });
|
|
391
|
+
if (done)
|
|
392
|
+
this.event('onSortChange', source, { field, sort, valueSort: this.state.model.valueSort });
|
|
393
|
+
return done;
|
|
394
|
+
}
|
|
395
|
+
/** Value sort; null clears it. */
|
|
396
|
+
setValueSort(valueSort, options = {}) {
|
|
397
|
+
const base = this.state.model;
|
|
398
|
+
const source = options.source ?? 'api';
|
|
399
|
+
if (!this.allowed('onBeforeSortChange', source, { field: null, sort: null, valueSort }))
|
|
400
|
+
return false;
|
|
401
|
+
const done = this.setModel({ ...base, valueSort }, { ...options, action: 'valueSort' });
|
|
402
|
+
if (done)
|
|
403
|
+
this.event('onSortChange', source, { field: null, sort: null, valueSort });
|
|
404
|
+
return done;
|
|
405
|
+
}
|
|
406
|
+
/** Aggregate of the value field at `index`. */
|
|
407
|
+
setAggregate(index, aggregate, options = {}) {
|
|
408
|
+
const base = options.draft ? (this.state.draft ?? this.state.model) : this.state.model;
|
|
409
|
+
return this.setModel(setAggregateOp(base, index, aggregate), { ...options, action: 'aggregate', field: base.values[index]?.field ?? null });
|
|
410
|
+
}
|
|
411
|
+
/** Aggregates the menu offers; `onBeforeAggregateMenuOpen` may cancel or edit `items`. Null when cancelled. */
|
|
412
|
+
aggregateMenuItems(field, index, items, source = 'api') {
|
|
413
|
+
const list = [...items];
|
|
414
|
+
const event = this.event('onBeforeAggregateMenuOpen', source, { field, index, items: list });
|
|
415
|
+
return event?.defaultPrevented ? null : list;
|
|
416
|
+
}
|
|
417
|
+
/** Adds or replaces a calculated field (by `name`), or removes it (`calculatedField: null`). */
|
|
418
|
+
setCalculatedField(name, calculatedField, options = {}) {
|
|
419
|
+
const base = options.draft ? (this.state.draft ?? this.state.model) : this.state.model;
|
|
420
|
+
const previous = base.calculatedFields.find((c) => c.name === name) ?? null;
|
|
421
|
+
const source = options.source ?? 'api';
|
|
422
|
+
if (!this.allowed('onBeforeCalculatedFieldChange', source, { name, calculatedField, previous }))
|
|
423
|
+
return false;
|
|
424
|
+
const list = base.calculatedFields.filter((c) => c.name !== name);
|
|
425
|
+
const calculatedFields = calculatedField ? (previous ? base.calculatedFields.map((c) => (c.name === name ? calculatedField : c)) : [...list, calculatedField]) : list;
|
|
426
|
+
let values = base.values;
|
|
427
|
+
if (calculatedField && options.addToValues !== false && !values.some((v) => v.field === calculatedField.name))
|
|
428
|
+
values = [...values, calculatedField.caption ? { field: calculatedField.name, caption: calculatedField.caption, aggregate: 'sum' } : { field: calculatedField.name, aggregate: 'sum' }];
|
|
429
|
+
if (!calculatedField)
|
|
430
|
+
values = values.filter((v) => v.field !== name);
|
|
431
|
+
const formats = calculatedField?.format ? { ...base.formats, [calculatedField.name]: calculatedField.format } : base.formats;
|
|
432
|
+
const done = this.setModel({ ...base, calculatedFields, values, formats }, { ...options, action: 'calculatedField', field: name });
|
|
433
|
+
if (done)
|
|
434
|
+
this.event('onCalculatedFieldChange', source, { name, calculatedField, previous });
|
|
435
|
+
return done;
|
|
436
|
+
}
|
|
437
|
+
/** Number or date format of a value field or a member field (null clears it). */
|
|
438
|
+
setFormat(field, format, options = {}) {
|
|
439
|
+
const base = this.state.model;
|
|
440
|
+
const formats = format ? { ...base.formats, [field]: format } : omit(base.formats, field);
|
|
441
|
+
return this.setModel({ ...base, formats }, { ...options, action: 'format', field });
|
|
442
|
+
}
|
|
443
|
+
setConditionalFormats(conditionalFormats, options = {}) {
|
|
444
|
+
return this.setModel({ ...this.state.model, conditionalFormats }, { ...options, action: 'conditionalFormat' });
|
|
445
|
+
}
|
|
446
|
+
/** Number range, date or custom grouping of a field; null ungroups it. */
|
|
447
|
+
setGrouping(field, grouping, options = {}) {
|
|
448
|
+
const base = this.state.model;
|
|
449
|
+
const groups = grouping ? { ...base.groups, [field]: grouping } : omit(base.groups, field);
|
|
450
|
+
return this.setModel({ ...base, groups }, { ...options, action: 'group', field });
|
|
451
|
+
}
|
|
452
|
+
setTotals(totals, options = {}) {
|
|
453
|
+
const base = this.state.model;
|
|
454
|
+
return this.setModel({ ...base, totals: { ...base.totals, ...totals } }, { ...options, action: 'totals' });
|
|
455
|
+
}
|
|
456
|
+
// -------------------------------------------------------------------------
|
|
457
|
+
// Drill
|
|
458
|
+
// -------------------------------------------------------------------------
|
|
459
|
+
isExpanded(path) {
|
|
460
|
+
const expanded = this.state.model.expanded;
|
|
461
|
+
return expanded === 'all' || expanded.some((p) => samePath(p, path));
|
|
462
|
+
}
|
|
463
|
+
/** Expands a member (incremental in the engine). */
|
|
464
|
+
expand(path, options = {}) {
|
|
465
|
+
return this.drill(path, true, options.source ?? 'api');
|
|
466
|
+
}
|
|
467
|
+
collapse(path, options = {}) {
|
|
468
|
+
return this.drill(path, false, options.source ?? 'api');
|
|
469
|
+
}
|
|
470
|
+
/** Expands or collapses a header node. */
|
|
471
|
+
toggle(node, options = {}) {
|
|
472
|
+
if (!node.expandable)
|
|
473
|
+
return false;
|
|
474
|
+
const path = [node.axis, ...node.path];
|
|
475
|
+
return this.drill(path, !node.expanded, options.source ?? 'api');
|
|
476
|
+
}
|
|
477
|
+
drill(path, expand, source) {
|
|
478
|
+
if (this.isExpanded(path) === expand && this.state.model.expanded !== 'all')
|
|
479
|
+
return false;
|
|
480
|
+
const action = expand ? 'expand' : 'collapse';
|
|
481
|
+
const axis = path[0];
|
|
482
|
+
if (!this.allowed('onBeforeExpandedChange', source, { action, path, axis, expanded: expand }))
|
|
483
|
+
return false;
|
|
484
|
+
const current = this.state.model.expanded;
|
|
485
|
+
const list = current === 'all' ? this.expandedPathsFromResult() : current;
|
|
486
|
+
const expanded = expand ? [...list, path] : list.filter((p) => !samePath(p, path) && !(p[0] === path[0] && p.length > path.length && samePath(p.slice(0, path.length), path)));
|
|
487
|
+
const next = normalizePivotModel({ ...this.state.model, expanded });
|
|
488
|
+
const done = this.commit(next, action, null, source, () => (expand ? this.engine?.expand(path) : this.engine?.collapse(path)));
|
|
489
|
+
if (done)
|
|
490
|
+
this.event('onExpandedChange', source, { action, path, axis, expanded: expand });
|
|
491
|
+
return done;
|
|
492
|
+
}
|
|
493
|
+
/** Paths of every expanded node of the current result (to turn `expanded: 'all'` into a list). */
|
|
494
|
+
expandedPathsFromResult() {
|
|
495
|
+
const out = [];
|
|
496
|
+
const result = this.state.result;
|
|
497
|
+
if (!result)
|
|
498
|
+
return out;
|
|
499
|
+
const visit = (nodes) => {
|
|
500
|
+
for (const node of nodes) {
|
|
501
|
+
if (node.kind === 'member' && node.expanded)
|
|
502
|
+
out.push([node.axis, ...node.path]);
|
|
503
|
+
if (node.children)
|
|
504
|
+
visit(node.children);
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
visit(result.rowTree);
|
|
508
|
+
visit(result.columnTree);
|
|
509
|
+
return out;
|
|
510
|
+
}
|
|
511
|
+
/** Expands every member of one axis or both (Ctrl+ArrowDown). */
|
|
512
|
+
expandAll(axis, options = {}) {
|
|
513
|
+
return this.drillAll(true, axis, options.source ?? 'api');
|
|
514
|
+
}
|
|
515
|
+
collapseAll(axis, options = {}) {
|
|
516
|
+
return this.drillAll(false, axis, options.source ?? 'api');
|
|
517
|
+
}
|
|
518
|
+
drillAll(expand, axis, source) {
|
|
519
|
+
const action = expand ? 'expandAll' : 'collapseAll';
|
|
520
|
+
if (!this.allowed('onBeforeExpandedChange', source, { action, path: null, axis: axis ?? null, expanded: expand }))
|
|
521
|
+
return false;
|
|
522
|
+
// Without an axis the model says it (`'all'` or none); per axis the engine expands every level and the
|
|
523
|
+
// model mirrors its answer after the command.
|
|
524
|
+
const next = axis ? this.state.model : normalizePivotModel({ ...this.state.model, expanded: expand ? 'all' : [] });
|
|
525
|
+
const done = this.commit(next, action, null, source, () => (expand ? this.engine?.expandAll(axis) : this.engine?.collapseAll(axis)), axis !== undefined);
|
|
526
|
+
if (done)
|
|
527
|
+
this.event('onExpandedChange', source, { action, path: null, axis: axis ?? null, expanded: expand });
|
|
528
|
+
return done;
|
|
529
|
+
}
|
|
530
|
+
// -------------------------------------------------------------------------
|
|
531
|
+
// Selection
|
|
532
|
+
// -------------------------------------------------------------------------
|
|
533
|
+
/** Replaces the selected cells (cancellable through `onBeforeSelectionChange`). */
|
|
534
|
+
setSelection(cells, options = {}) {
|
|
535
|
+
const keys = cells.map((c) => `${c.rowKey}|${c.columnKey}`);
|
|
536
|
+
const prev = this.state.selection;
|
|
537
|
+
if (keys.length === prev.length && keys.every((k, i) => k === prev[i]))
|
|
538
|
+
return false;
|
|
539
|
+
const source = options.source ?? 'api';
|
|
540
|
+
// `confirmed`: the renderer already ran the before-event (the Data Grid selection command).
|
|
541
|
+
if (!options.confirmed && !this.allowed('onBeforeSelectionChange', source, { cells, keys }))
|
|
542
|
+
return false;
|
|
543
|
+
this.set({ selection: keys });
|
|
544
|
+
this.event('onSelectionChange', source, { cells, keys });
|
|
545
|
+
return true;
|
|
546
|
+
}
|
|
547
|
+
// -------------------------------------------------------------------------
|
|
548
|
+
// Data, members, drill through
|
|
549
|
+
// -------------------------------------------------------------------------
|
|
550
|
+
/** Members of a field for the member editor: every member, searchable, no node limit. */
|
|
551
|
+
getMembers(field, options) {
|
|
552
|
+
const value = this.engine?.getMembers(field, options) ?? [];
|
|
553
|
+
return isPromise(value) ? [] : value;
|
|
554
|
+
}
|
|
555
|
+
/** Members, waiting for an asynchronous engine. */
|
|
556
|
+
async getMembersAsync(field, options) {
|
|
557
|
+
if (options)
|
|
558
|
+
return (await this.engine?.getMembers(field, options)) ?? [];
|
|
559
|
+
// Every member of a field: kept per data and model version, so reopening the member editor of an
|
|
560
|
+
// asynchronous (Worker) engine does not copy 100,000 members across the boundary again.
|
|
561
|
+
const version = this.state.result?.model ?? this.state.model;
|
|
562
|
+
if (this.memberCache.version !== version || this.memberCache.data !== this.data)
|
|
563
|
+
this.memberCache = { version, data: this.data, fields: new Map() };
|
|
564
|
+
const cache = this.memberCache;
|
|
565
|
+
let entry = cache.fields.get(field);
|
|
566
|
+
if (!entry) {
|
|
567
|
+
entry = { members: null, promise: Promise.resolve(this.engine?.getMembers(field)).then((m) => (entry && (entry.members = m ?? []), m ?? [])) };
|
|
568
|
+
cache.fields.set(field, entry);
|
|
569
|
+
}
|
|
570
|
+
return entry.promise;
|
|
571
|
+
}
|
|
572
|
+
/** Members already loaded by `getMembersAsync()` (null when not loaded yet). */
|
|
573
|
+
getLoadedMembers(field) {
|
|
574
|
+
const version = this.state.result?.model ?? this.state.model;
|
|
575
|
+
if (this.memberCache.version !== version || this.memberCache.data !== this.data)
|
|
576
|
+
return null;
|
|
577
|
+
return this.memberCache.fields.get(field)?.members ?? null;
|
|
578
|
+
}
|
|
579
|
+
/** Raw rows behind a value cell. */
|
|
580
|
+
getCellRows(rowIndex, columnIndex) {
|
|
581
|
+
const engine = this.engine;
|
|
582
|
+
if (!engine)
|
|
583
|
+
return [];
|
|
584
|
+
const rows = engine.drillThrough(rowIndex, columnIndex);
|
|
585
|
+
return isPromise(rows) ? [] : rows;
|
|
586
|
+
}
|
|
587
|
+
/** Raw rows behind a value cell, waiting for an asynchronous engine. */
|
|
588
|
+
async getCellRowsAsync(rowIndex, columnIndex) {
|
|
589
|
+
return ((await this.engine?.drillThrough(rowIndex, columnIndex)) ?? []);
|
|
590
|
+
}
|
|
591
|
+
/** Raw row indexes behind a value cell. */
|
|
592
|
+
getCellRowIndexes(rowIndex, columnIndex) {
|
|
593
|
+
return this.state.result?.rowsAt(rowIndex, columnIndex) ?? [];
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Opens drill through for a value cell (cancellable through `onBeforeDrillThrough`).
|
|
597
|
+
* `openDialog: false` only fires the events (an app with its own UI).
|
|
598
|
+
*/
|
|
599
|
+
drillThrough(rowIndex, columnIndex, options = {}) {
|
|
600
|
+
const result = this.state.result;
|
|
601
|
+
if (!result || rowIndex < 0 || columnIndex < 0 || rowIndex >= result.rowCount || columnIndex >= result.columnCount)
|
|
602
|
+
return false;
|
|
603
|
+
const cell = result.cellAt(rowIndex, columnIndex);
|
|
604
|
+
const source = options.source ?? 'api';
|
|
605
|
+
let cache = null;
|
|
606
|
+
const rows = () => (cache ??= this.getCellRows(rowIndex, columnIndex));
|
|
607
|
+
const fields = () => ({ rowKey: cell.rowKey, columnKey: cell.columnKey, measure: cell.measure, rowIndex, columnIndex, cell, rows });
|
|
608
|
+
if (!this.allowed('onBeforeDrillThrough', source, fields()))
|
|
609
|
+
return false;
|
|
610
|
+
if (options.openDialog !== false)
|
|
611
|
+
this.openDialog({ kind: 'drillThrough', rowIndex, columnIndex, rowKey: cell.rowKey, columnKey: cell.columnKey });
|
|
612
|
+
this.event('onDrillThrough', source, fields());
|
|
613
|
+
return true;
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Writes edited raw rows (drill-through editing): replaces the rows at
|
|
617
|
+
* `index`, appends `index: -1` rows, removes `row: null` rows, then
|
|
618
|
+
* re-aggregates incrementally and fires `onDataChange`.
|
|
619
|
+
*/
|
|
620
|
+
updateRows(changes, action = 'update') {
|
|
621
|
+
if (!changes.length)
|
|
622
|
+
return;
|
|
623
|
+
const next = [...this.data];
|
|
624
|
+
const report = [];
|
|
625
|
+
const removed = new Set();
|
|
626
|
+
for (const change of changes) {
|
|
627
|
+
const inRange = change.index >= 0 && change.index < this.data.length;
|
|
628
|
+
if (change.row === null) {
|
|
629
|
+
if (inRange) {
|
|
630
|
+
removed.add(change.index);
|
|
631
|
+
report.push({ index: change.index, row: this.data[change.index], previous: this.data[change.index] });
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
else if (!inRange) {
|
|
635
|
+
next.push(change.row);
|
|
636
|
+
report.push({ index: next.length - 1, row: change.row, previous: undefined });
|
|
637
|
+
}
|
|
638
|
+
else {
|
|
639
|
+
report.push({ index: change.index, row: change.row, previous: next[change.index] });
|
|
640
|
+
next[change.index] = change.row;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
const data = removed.size ? next.filter((_, i) => !removed.has(i)) : next;
|
|
644
|
+
this.data = data;
|
|
645
|
+
if (this.engine)
|
|
646
|
+
this.run('compute', 'update', () => this.engine?.setData(data));
|
|
647
|
+
this.handlers.onDataChange?.({ data, changes: report, action });
|
|
648
|
+
}
|
|
649
|
+
/** Re-reads the data and recomputes. */
|
|
650
|
+
refresh() {
|
|
651
|
+
if (this.engine)
|
|
652
|
+
this.run('compute', 'update', () => this.engine?.refresh());
|
|
653
|
+
}
|
|
654
|
+
// -------------------------------------------------------------------------
|
|
655
|
+
// Dialogs
|
|
656
|
+
// -------------------------------------------------------------------------
|
|
657
|
+
openDialog(dialog, source = 'api') {
|
|
658
|
+
if (dialog.kind === 'memberEditor' && !this.allowed('onBeforeMemberEditorOpen', source, { field: dialog.field }))
|
|
659
|
+
return false;
|
|
660
|
+
this.set({ dialog });
|
|
661
|
+
return true;
|
|
662
|
+
}
|
|
663
|
+
closeDialog() {
|
|
664
|
+
this.set({ dialog: null });
|
|
665
|
+
}
|
|
666
|
+
// -------------------------------------------------------------------------
|
|
667
|
+
// State
|
|
668
|
+
// -------------------------------------------------------------------------
|
|
669
|
+
getState(extra = {}) {
|
|
670
|
+
return { version: 1, pivotModel: this.state.model, ...extra };
|
|
671
|
+
}
|
|
672
|
+
/** Applies a saved state; false (with `onError`) when it is not a pivot state of a known version. */
|
|
673
|
+
restoreState(state, source = 'api') {
|
|
674
|
+
let parsed;
|
|
675
|
+
try {
|
|
676
|
+
parsed = typeof state === 'string' ? JSON.parse(state) : state;
|
|
677
|
+
if (!parsed || typeof parsed !== 'object' || parsed.version !== 1 || typeof parsed.pivotModel !== 'object')
|
|
678
|
+
throw new Error('restoreState: not a pivot state of version 1');
|
|
679
|
+
}
|
|
680
|
+
catch (error) {
|
|
681
|
+
this.emitError(toError(error), 'restore');
|
|
682
|
+
return false;
|
|
683
|
+
}
|
|
684
|
+
return this.commit(normalizePivotModel(parsed.pivotModel), 'restore', null, source, null);
|
|
685
|
+
}
|
|
686
|
+
/** True while mounted (effects ran). */
|
|
687
|
+
get isMounted() {
|
|
688
|
+
return this.mounted;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
//# sourceMappingURL=controller.js.map
|