@vuu-ui/vuu-data-local 2.1.19-beta.2 → 2.1.19-beta.4

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.
Files changed (53) hide show
  1. package/cjs/array-data-source/aggregate-utils.js +237 -0
  2. package/cjs/array-data-source/aggregate-utils.js.map +1 -0
  3. package/cjs/array-data-source/array-data-source.js +914 -0
  4. package/cjs/array-data-source/array-data-source.js.map +1 -0
  5. package/cjs/array-data-source/array-data-utils.js +62 -0
  6. package/cjs/array-data-source/array-data-utils.js.map +1 -0
  7. package/cjs/array-data-source/group-utils.js +187 -0
  8. package/cjs/array-data-source/group-utils.js.map +1 -0
  9. package/cjs/array-data-source/sort-utils.js +73 -0
  10. package/cjs/array-data-source/sort-utils.js.map +1 -0
  11. package/cjs/index.js +12 -0
  12. package/cjs/index.js.map +1 -0
  13. package/cjs/json-data-source/JsonDataSource.js +404 -0
  14. package/cjs/json-data-source/JsonDataSource.js.map +1 -0
  15. package/cjs/tree-data-source/IconProvider.js +28 -0
  16. package/cjs/tree-data-source/IconProvider.js.map +1 -0
  17. package/cjs/tree-data-source/TreeDataSource.js +434 -0
  18. package/cjs/tree-data-source/TreeDataSource.js.map +1 -0
  19. package/esm/array-data-source/aggregate-utils.js +235 -0
  20. package/esm/array-data-source/aggregate-utils.js.map +1 -0
  21. package/esm/array-data-source/array-data-source.js +912 -0
  22. package/esm/array-data-source/array-data-source.js.map +1 -0
  23. package/esm/array-data-source/array-data-utils.js +59 -0
  24. package/esm/array-data-source/array-data-utils.js.map +1 -0
  25. package/esm/array-data-source/group-utils.js +183 -0
  26. package/esm/array-data-source/group-utils.js.map +1 -0
  27. package/esm/array-data-source/sort-utils.js +69 -0
  28. package/esm/array-data-source/sort-utils.js.map +1 -0
  29. package/esm/index.js +4 -0
  30. package/esm/index.js.map +1 -0
  31. package/esm/json-data-source/JsonDataSource.js +402 -0
  32. package/esm/json-data-source/JsonDataSource.js.map +1 -0
  33. package/esm/tree-data-source/IconProvider.js +26 -0
  34. package/esm/tree-data-source/IconProvider.js.map +1 -0
  35. package/esm/tree-data-source/TreeDataSource.js +432 -0
  36. package/esm/tree-data-source/TreeDataSource.js.map +1 -0
  37. package/package.json +14 -11
  38. package/types/array-data-source/aggregate-utils.d.ts +2 -2
  39. package/types/array-data-source/array-data-source.d.ts +12 -12
  40. package/types/array-data-source/array-data-utils.d.ts +2 -2
  41. package/types/array-data-source/group-utils.d.ts +4 -4
  42. package/types/array-data-source/sort-utils.d.ts +4 -4
  43. package/types/json-data-source/JsonDataSource.d.ts +2 -2
  44. package/types/tree-data-source/TreeDataSource.d.ts +2 -2
  45. package/src/array-data-source/aggregate-utils.js +0 -121
  46. package/src/array-data-source/array-data-source.js +0 -671
  47. package/src/array-data-source/array-data-utils.js +0 -41
  48. package/src/array-data-source/group-utils.js +0 -138
  49. package/src/array-data-source/sort-utils.js +0 -56
  50. package/src/index.js +0 -3
  51. package/src/json-data-source/JsonDataSource.js +0 -319
  52. package/src/tree-data-source/IconProvider.js +0 -11
  53. package/src/tree-data-source/TreeDataSource.js +0 -353
@@ -0,0 +1,912 @@
1
+ import { parseFilter, filterPredicate } from '@vuu-ui/vuu-filter-parser';
2
+ import { logger, EventEmitter, KeySet, NULL_RANGE, Range, uuid, buildColumnMap, withConfigDefaults, hasFilter, hasBaseFilter, hasSort, isGroupByChanged, hasGroupBy, getAddedItems, combineFilters, isConfigChanged, rangeNewItems, filterAsQuery, metadataKeys, vanillaConfig } from '@vuu-ui/vuu-utils';
3
+ import { aggregateData } from './aggregate-utils.js';
4
+ import { buildDataToClientMap, toClientRow } from './array-data-utils.js';
5
+ import { expandGroup, collapseGroup, groupRows } from './group-utils.js';
6
+ import { sortRows, sortComparator, binarySearch } from './sort-utils.js';
7
+
8
+ var __defProp = Object.defineProperty;
9
+ var __typeError = (msg) => {
10
+ throw TypeError(msg);
11
+ };
12
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
14
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
15
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
16
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
17
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
18
+ var _columnMap, _data, _freezeTimestamp, _keys, _links, _range, _status, _title;
19
+ const { debug, info } = logger("ArrayDataSource");
20
+ const { KEY } = metadataKeys;
21
+ const toDataSourceRow = (indexOfKeyColumn, index) => (data, idx) => {
22
+ const key = `${data[indexOfKeyColumn]}`;
23
+ index?.set(key, idx);
24
+ return [
25
+ idx,
26
+ idx,
27
+ true,
28
+ false,
29
+ 1,
30
+ 0,
31
+ key,
32
+ 0,
33
+ 0,
34
+ // ts
35
+ false,
36
+ // isNew
37
+ ...data
38
+ ];
39
+ };
40
+ const buildTableSchema = (columns, keyColumn) => {
41
+ const schema = {
42
+ columns: columns.map(({ editable, name, serverDataType = "string" }) => ({
43
+ editable,
44
+ name,
45
+ serverDataType
46
+ })),
47
+ key: keyColumn ?? columns[0].name,
48
+ table: { module: "", table: "Array" }
49
+ };
50
+ return schema;
51
+ };
52
+ class ArrayDataSource extends EventEmitter {
53
+ constructor({
54
+ aggregations,
55
+ baseFilterSpec,
56
+ // different from RemoteDataSource
57
+ columnDescriptors,
58
+ data,
59
+ dataMap,
60
+ filterSpec,
61
+ groupBy,
62
+ keyColumn,
63
+ rangeChangeRowset = "delta",
64
+ sort,
65
+ title,
66
+ viewport
67
+ }) {
68
+ super();
69
+ __publicField(this, "clientCallback");
70
+ __publicField(this, "columnDescriptors");
71
+ /** sorted offsets of data within raw data, reflecting sort order
72
+ * of columns specified by client.
73
+ */
74
+ __publicField(this, "dataIndices");
75
+ /** Map reflecting positions of data items in raw data */
76
+ __publicField(this, "dataMap");
77
+ __publicField(this, "groupMap");
78
+ /** the index of key field within raw data row */
79
+ __publicField(this, "key");
80
+ __publicField(this, "lastRangeServed", { from: 0, to: 0 });
81
+ __publicField(this, "rangeChangeRowset");
82
+ __publicField(this, "openTreeNodes", []);
83
+ // Experimental. There are some config changes - first concrete example is baseFilter applied to
84
+ // 'freeze' data, that should not immediately affect the displayed data, therefore not cause
85
+ // range reset.
86
+ __publicField(this, "preserveScrollPositionAcrossConfigChange", false);
87
+ /** Map reflecting positions of columns in client data sent to user */
88
+ __privateAdd(this, _columnMap);
89
+ __publicField(this, "_config", vanillaConfig);
90
+ __privateAdd(this, _data);
91
+ __privateAdd(this, _freezeTimestamp);
92
+ __privateAdd(this, _keys, new KeySet(NULL_RANGE));
93
+ __privateAdd(this, _links);
94
+ __privateAdd(this, _range, Range(0, 0));
95
+ __privateAdd(this, _status, "initialising");
96
+ __privateAdd(this, _title);
97
+ __publicField(this, "_menu");
98
+ __publicField(this, "selectedRows", /* @__PURE__ */ new Set());
99
+ __publicField(this, "index", /* @__PURE__ */ new Map());
100
+ __publicField(this, "tableSchema");
101
+ __publicField(this, "viewport");
102
+ __publicField(this, "processedData");
103
+ __publicField(this, "insert", (row) => {
104
+ const dataSourceRow = toDataSourceRow(this.key, this.index)(row, this.size);
105
+ __privateGet(this, _data).push(
106
+ dataSourceRow
107
+ );
108
+ const { from, to } = __privateGet(this, _range);
109
+ const [rowIdx] = dataSourceRow;
110
+ const isSorted = hasSort(this.config);
111
+ const isFiltered = hasFilter(this.config) || hasBaseFilter(this.config);
112
+ if (isSorted && isFiltered) {
113
+ const meetsFilterCriteria = this.getFilterPredicate();
114
+ if (meetsFilterCriteria(dataSourceRow)) {
115
+ this.insertIntoSortedData(dataSourceRow);
116
+ }
117
+ } else if (isSorted) {
118
+ this.insertIntoSortedData(dataSourceRow);
119
+ } else if (isFiltered) {
120
+ const meetsFilterCriteria = this.getFilterPredicate();
121
+ if (meetsFilterCriteria(dataSourceRow)) {
122
+ this.processedData?.push(dataSourceRow);
123
+ this.sendSizeUpdateToClient();
124
+ if (rowIdx >= from && rowIdx < to) {
125
+ this.sendRowsToClient();
126
+ }
127
+ this.emit("resize", __privateGet(this, _data).length);
128
+ }
129
+ } else {
130
+ this.sendSizeUpdateToClient();
131
+ if (rowIdx >= from && rowIdx < to) {
132
+ this.sendRowsToClient();
133
+ }
134
+ this.emit("resize", this.size);
135
+ }
136
+ });
137
+ __publicField(this, "updateDataItem", (keyValue, columnName, value) => {
138
+ this.validateDataValue(columnName, value);
139
+ const colIndex = __privateGet(this, _columnMap)[columnName];
140
+ const dataColIndex = this.dataMap?.[columnName];
141
+ const dataIndex = this.indexOfRowWithKey(keyValue);
142
+ if (dataIndex !== -1 && dataColIndex !== void 0) {
143
+ const dataSourceRow = __privateGet(this, _data)[dataIndex];
144
+ dataSourceRow[colIndex] = value;
145
+ const { from, to } = __privateGet(this, _range);
146
+ const [rowIdx] = dataSourceRow;
147
+ if (rowIdx >= from && rowIdx < to) {
148
+ this.sendRowsToClient(false, dataSourceRow);
149
+ }
150
+ }
151
+ });
152
+ __publicField(this, "indexOfRowWithKey", (key) => __privateGet(this, _data).findIndex((row) => row[KEY] === key));
153
+ __publicField(this, "update", (row, columnName) => {
154
+ const keyValue = row[this.key];
155
+ const dataColIndex = this.dataMap?.[columnName];
156
+ return this.updateDataItem(keyValue, columnName, row[dataColIndex]);
157
+ });
158
+ __publicField(this, "updateRow", (row, _columnName) => {
159
+ const keyValue = row[this.key];
160
+ const dataIndex = __privateGet(this, _data).findIndex((row2) => row2[KEY] === keyValue);
161
+ if (dataIndex !== -1) {
162
+ const dataSourceRow = toDataSourceRow(this.key)(row, dataIndex);
163
+ __privateGet(this, _data)[dataIndex] = dataSourceRow;
164
+ const { from, to } = __privateGet(this, _range);
165
+ const isFiltered = hasFilter(this.config) || hasBaseFilter(this.config);
166
+ const isSorted = hasSort(this.config);
167
+ if (isFiltered) {
168
+ const meetsFilterCriteria = this.getFilterPredicate();
169
+ if (meetsFilterCriteria(dataSourceRow) && this.processedData) {
170
+ const dataIndex2 = this.processedData.findIndex(
171
+ (row2) => row2[KEY] === keyValue
172
+ );
173
+ if (dataIndex2 !== -1) {
174
+ const existingRow = this.processedData[dataIndex2];
175
+ const newFilteredRow = existingRow.slice(0, 10).concat(dataSourceRow.slice(10));
176
+ this.processedData[dataIndex2] = newFilteredRow;
177
+ if (dataIndex2 >= from && dataIndex2 < to) {
178
+ this.sendRowsToClient(false, newFilteredRow);
179
+ }
180
+ }
181
+ } else if (this.processedData) {
182
+ const dataIndex2 = this.processedData.findIndex(
183
+ (row2) => row2[KEY] === keyValue
184
+ );
185
+ if (dataIndex2 !== -1) {
186
+ console.log(`LAMF dataRow no longer in filter set`);
187
+ }
188
+ }
189
+ } else if (isSorted) {
190
+ if (this.processedData) {
191
+ const dataIndex2 = this.processedData.findIndex(
192
+ (row2) => row2[KEY] === keyValue
193
+ );
194
+ if (dataIndex2 !== -1) {
195
+ const existingRow = this.processedData[dataIndex2];
196
+ const newSortedRow = existingRow.slice(0, 10).concat(dataSourceRow.slice(10));
197
+ this.processedData[dataIndex2] = newSortedRow;
198
+ if (dataIndex2 >= from && dataIndex2 < to) {
199
+ this.sendRowsToClient(false, newSortedRow);
200
+ }
201
+ }
202
+ }
203
+ } else {
204
+ if (dataIndex >= from && dataIndex < to) {
205
+ this.sendRowsToClient(false, dataSourceRow);
206
+ }
207
+ }
208
+ }
209
+ });
210
+ __publicField(this, "handleDeleteFromTable", async (key) => {
211
+ const dataIndex = __privateGet(this, _data).findIndex((row) => row[KEY] === key);
212
+ if (dataIndex === -1) {
213
+ return "row not found";
214
+ }
215
+ let doomedIndex = void 0;
216
+ if (this.processedData) {
217
+ for (let i = 0; i < this.processedData.length; i++) {
218
+ if (this.processedData[i][KEY] === key) {
219
+ doomedIndex = i;
220
+ } else if (doomedIndex !== void 0) {
221
+ this.processedData[i][0] -= 1;
222
+ }
223
+ }
224
+ if (doomedIndex !== void 0) {
225
+ this.processedData.splice(doomedIndex, 1);
226
+ }
227
+ }
228
+ __privateGet(this, _data).splice(dataIndex, 1);
229
+ for (let i = dataIndex; i < __privateGet(this, _data).length; i++) {
230
+ __privateGet(this, _data)[i][0] -= 1;
231
+ }
232
+ this.sendSizeUpdateToClient();
233
+ const { from, to } = __privateGet(this, _range);
234
+ const deletedIndex = doomedIndex ?? dataIndex;
235
+ if (deletedIndex >= from && deletedIndex < to) {
236
+ __privateGet(this, _keys).reset(this.range.withBuffer);
237
+ this.sendRowsToClient(true);
238
+ }
239
+ this.emit("resize", this.size);
240
+ return true;
241
+ });
242
+ if (!data || !columnDescriptors) {
243
+ throw Error(
244
+ "ArrayDataSource constructor called without data or without columnDescriptors"
245
+ );
246
+ }
247
+ this.columnDescriptors = columnDescriptors;
248
+ this.dataMap = dataMap;
249
+ this.key = keyColumn ? this.columnDescriptors.findIndex((col) => col.name === keyColumn) : 0;
250
+ this.rangeChangeRowset = rangeChangeRowset;
251
+ this.tableSchema = buildTableSchema(columnDescriptors, keyColumn);
252
+ this.viewport = viewport || uuid();
253
+ __privateSet(this, _title, title);
254
+ const columns = columnDescriptors.map((col) => col.name);
255
+ __privateSet(this, _columnMap, buildColumnMap(columns));
256
+ this.dataIndices = buildDataToClientMap(__privateGet(this, _columnMap), this.dataMap);
257
+ __privateSet(this, _data, data.map(
258
+ toDataSourceRow(this.key, this.index)
259
+ ));
260
+ this.config = {
261
+ ...this._config,
262
+ aggregations: aggregations || this._config.aggregations,
263
+ baseFilterSpec,
264
+ columns,
265
+ filterSpec: filterSpec || this._config.filterSpec,
266
+ groupBy: groupBy || this._config.groupBy,
267
+ sort: sort || this._config.sort
268
+ };
269
+ debug?.(`columnMap: ${JSON.stringify(__privateGet(this, _columnMap))}`);
270
+ }
271
+ async subscribe({
272
+ viewport = this.viewport ?? (this.viewport = uuid()),
273
+ columns,
274
+ aggregations,
275
+ baseFilterSpec,
276
+ range,
277
+ sort,
278
+ groupBy,
279
+ filterSpec
280
+ }, callback) {
281
+ this.clientCallback = callback;
282
+ this.viewport = viewport;
283
+ __privateSet(this, _status, "subscribed");
284
+ this.lastRangeServed = { from: 0, to: 0 };
285
+ let config = this._config;
286
+ const hasConfigProps = aggregations || columns || filterSpec || groupBy || sort;
287
+ if (hasConfigProps) {
288
+ config = {
289
+ ...config,
290
+ aggregations: aggregations || this._config.aggregations,
291
+ baseFilterSpec: baseFilterSpec || this._config.baseFilterSpec,
292
+ columns: columns || this._config.columns,
293
+ filterSpec: filterSpec || this._config.filterSpec,
294
+ groupBy: groupBy || this._config.groupBy,
295
+ sort: sort || this._config.sort
296
+ };
297
+ }
298
+ const subscribedMessage = {
299
+ ...config,
300
+ type: "subscribed",
301
+ clientViewportId: this.viewport,
302
+ range: __privateGet(this, _range),
303
+ tableSchema: this.tableSchema
304
+ };
305
+ this.clientCallback?.(subscribedMessage);
306
+ this.emit("subscribed", subscribedMessage);
307
+ if (hasConfigProps) {
308
+ this.config = config;
309
+ } else {
310
+ this.sendSizeUpdateToClient();
311
+ this.emit(
312
+ "resize",
313
+ this.processedData ? this.processedData.length : __privateGet(this, _data).length
314
+ );
315
+ if (range && !__privateGet(this, _range).equals(range)) {
316
+ this.range = range;
317
+ } else if (__privateGet(this, _range) !== NULL_RANGE) {
318
+ this.sendRowsToClient();
319
+ }
320
+ if (this.range.to !== 0) {
321
+ const pageCount = Math.ceil(
322
+ this.size / (this.range.to - this.range.from)
323
+ );
324
+ this.emit("page-count", pageCount);
325
+ }
326
+ }
327
+ }
328
+ unsubscribe() {
329
+ __privateSet(this, _status, "unsubscribed");
330
+ this.emit("unsubscribed", this.viewport);
331
+ this.removeAllListeners();
332
+ this.clientCallback = void 0;
333
+ }
334
+ suspend() {
335
+ console.log(`[ArrayDataSource] suspend`);
336
+ if (__privateGet(this, _status) !== "unsubscribed") {
337
+ info?.(`suspend #${this.viewport}, current status ${__privateGet(this, _status)}`);
338
+ __privateSet(this, _status, "suspended");
339
+ this.emit("suspended", this.viewport);
340
+ }
341
+ }
342
+ resume(callback) {
343
+ const isSuspended = __privateGet(this, _status) === "suspended";
344
+ info?.(`resume #${this.viewport}, current status ${__privateGet(this, _status)}`);
345
+ if (callback) {
346
+ this.clientCallback = callback;
347
+ }
348
+ if (isSuspended) {
349
+ __privateSet(this, _status, "subscribed");
350
+ }
351
+ this.emit("resumed", this.viewport);
352
+ if (this.selectedRows.size > 0) {
353
+ if (this.selectedRows.has("*")) {
354
+ this.emit("row-selection", this.size);
355
+ } else {
356
+ this.emit("row-selection", this.selectedRows.size);
357
+ }
358
+ }
359
+ this.sendRowsToClient(true);
360
+ }
361
+ disable() {
362
+ this.emit("disabled", this.viewport);
363
+ }
364
+ enable() {
365
+ this.emit("enabled", this.viewport);
366
+ }
367
+ select(selectRequest) {
368
+ switch (selectRequest.type) {
369
+ case "SELECT_ROW": {
370
+ const { preserveExistingSelection, rowKey } = selectRequest;
371
+ if (!preserveExistingSelection) {
372
+ this.selectedRows.clear();
373
+ }
374
+ this.selectedRows.add(rowKey);
375
+ break;
376
+ }
377
+ case "DESELECT_ROW": {
378
+ const { preserveExistingSelection, rowKey } = selectRequest;
379
+ if (!preserveExistingSelection) {
380
+ this.selectedRows.clear();
381
+ } else if (this.selectedRows.has("*")) {
382
+ this.selectedRows.clear();
383
+ for (const key of this.index.keys()) {
384
+ if (key !== rowKey) {
385
+ this.selectedRows.add(key);
386
+ }
387
+ }
388
+ } else {
389
+ this.selectedRows.delete(rowKey);
390
+ }
391
+ break;
392
+ }
393
+ case "SELECT_ROW_RANGE": {
394
+ const { preserveExistingSelection, fromRowKey, toRowKey } = selectRequest;
395
+ if (!preserveExistingSelection) {
396
+ this.selectedRows.clear();
397
+ }
398
+ const fromIdx = this.index.get(fromRowKey);
399
+ const toIdx = this.index.get(toRowKey);
400
+ if (typeof fromIdx === "number" && typeof toIdx === "number") {
401
+ for (let i = fromIdx; i <= toIdx; i++) {
402
+ const { [KEY]: rowKey } = __privateGet(this, _data)[i];
403
+ this.selectedRows.add(rowKey);
404
+ }
405
+ }
406
+ break;
407
+ }
408
+ case "SELECT_ALL": {
409
+ this.selectedRows.clear();
410
+ this.selectedRows.add("*");
411
+ break;
412
+ }
413
+ case "DESELECT_ALL": {
414
+ this.selectedRows.clear();
415
+ break;
416
+ }
417
+ }
418
+ this.setRange(__privateGet(this, _range), true);
419
+ this.emit(
420
+ "row-selection",
421
+ selectRequest.type === "SELECT_ALL" ? this.size : this.selectedRows.size
422
+ );
423
+ }
424
+ getRowKey(keyOrIndex) {
425
+ if (typeof keyOrIndex === "string") {
426
+ return keyOrIndex;
427
+ }
428
+ const row = this.getRowAtIndex(keyOrIndex);
429
+ if (row === void 0) {
430
+ throw Error(`row not found at index ${keyOrIndex}`);
431
+ }
432
+ return row?.[KEY];
433
+ }
434
+ openTreeNode(keyOrIndex) {
435
+ const key = this.getRowKey(keyOrIndex);
436
+ this.openTreeNodes.push(key);
437
+ this.processedData = expandGroup(
438
+ this.openTreeNodes,
439
+ __privateGet(this, _data),
440
+ this._config.groupBy,
441
+ __privateGet(this, _columnMap),
442
+ this.groupMap,
443
+ this.processedData
444
+ );
445
+ this.setRange(__privateGet(this, _range).reset, true);
446
+ }
447
+ closeTreeNode(keyOrIndex) {
448
+ const key = this.getRowKey(keyOrIndex);
449
+ this.openTreeNodes = this.openTreeNodes.filter((value) => value !== key);
450
+ if (this.processedData) {
451
+ this.processedData = collapseGroup(key, this.processedData);
452
+ this.setRange(__privateGet(this, _range).reset, true);
453
+ }
454
+ }
455
+ get pageSize() {
456
+ return __privateGet(this, _range).to - __privateGet(this, _range).from;
457
+ }
458
+ get links() {
459
+ return __privateGet(this, _links);
460
+ }
461
+ set links(links) {
462
+ __privateSet(this, _links, links);
463
+ if (links) {
464
+ this._clientCallback?.({
465
+ clientViewportId: this.viewport,
466
+ type: "vuu-links",
467
+ links
468
+ });
469
+ }
470
+ }
471
+ get menu() {
472
+ return this._menu;
473
+ }
474
+ get status() {
475
+ return __privateGet(this, _status);
476
+ }
477
+ get data() {
478
+ return __privateGet(this, _data);
479
+ }
480
+ // Only used by the UpdateGenerator
481
+ get currentData() {
482
+ return this.processedData ?? __privateGet(this, _data);
483
+ }
484
+ get table() {
485
+ return this.tableSchema.table;
486
+ }
487
+ get columns() {
488
+ return this._config.columns;
489
+ }
490
+ set columns(columns) {
491
+ }
492
+ get config() {
493
+ return this._config;
494
+ }
495
+ set config(config) {
496
+ const originalConfig = this._config;
497
+ const configChanges = this.applyConfig(config);
498
+ if (configChanges) {
499
+ if (config) {
500
+ const newConfig = config?.filterSpec?.filter && config?.filterSpec.filterStruct === void 0 ? {
501
+ ...config,
502
+ filterSpec: {
503
+ filter: config.filterSpec.filter,
504
+ filterStruct: parseFilter(config.filterSpec.filter)
505
+ }
506
+ } : config;
507
+ this._config = withConfigDefaults(newConfig);
508
+ let processedData;
509
+ if (hasFilter(config) || hasBaseFilter(config)) {
510
+ const fn = this.getFilterPredicate();
511
+ processedData = __privateGet(this, _data).filter(fn);
512
+ }
513
+ if (configChanges.columnsChanged) {
514
+ this.processNewColumns(originalConfig.columns, config.columns);
515
+ }
516
+ if (hasSort(config)) {
517
+ processedData = sortRows(
518
+ processedData ?? __privateGet(this, _data),
519
+ config.sort,
520
+ __privateGet(this, _columnMap)
521
+ );
522
+ }
523
+ if (this.openTreeNodes.length > 0 && isGroupByChanged(originalConfig, config)) {
524
+ if (this._config.groupBy.length === 0) {
525
+ this.openTreeNodes.length = 0;
526
+ }
527
+ }
528
+ if (hasGroupBy(config)) {
529
+ const [groupedData, groupMap] = groupRows(
530
+ processedData ?? __privateGet(this, _data),
531
+ config.groupBy,
532
+ __privateGet(this, _columnMap)
533
+ );
534
+ this.groupMap = groupMap;
535
+ processedData = groupedData;
536
+ if (this.openTreeNodes.length > 0) {
537
+ processedData = expandGroup(
538
+ this.openTreeNodes,
539
+ __privateGet(this, _data),
540
+ this._config.groupBy,
541
+ __privateGet(this, _columnMap),
542
+ this.groupMap,
543
+ processedData
544
+ );
545
+ }
546
+ }
547
+ if (processedData) {
548
+ this.processedData = this.indexProcessedData(processedData);
549
+ } else {
550
+ this.processedData = void 0;
551
+ }
552
+ }
553
+ if (configChanges.filterChanged || configChanges.baseFilterChanged || configChanges.groupByChanged) {
554
+ requestAnimationFrame(() => {
555
+ this.emit("resize", this.size);
556
+ });
557
+ }
558
+ if (__privateGet(this, _status) === "subscribed") {
559
+ requestAnimationFrame(() => {
560
+ this.sendSizeUpdateToClient();
561
+ if (this.preserveScrollPositionAcrossConfigChange) {
562
+ this.preserveScrollPositionAcrossConfigChange = false;
563
+ } else {
564
+ this.setRange(__privateGet(this, _range).reset, true);
565
+ }
566
+ this.emit(
567
+ "config",
568
+ this._config,
569
+ this.range,
570
+ void 0,
571
+ configChanges
572
+ );
573
+ });
574
+ }
575
+ }
576
+ }
577
+ processNewColumns(originalColumns, columns) {
578
+ const addedColumns = getAddedItems(originalColumns, columns);
579
+ if (addedColumns.length > 0) ;
580
+ __privateSet(this, _columnMap, buildColumnMap(columns));
581
+ this.dataIndices = buildDataToClientMap(__privateGet(this, _columnMap), this.dataMap);
582
+ }
583
+ indexProcessedData(data) {
584
+ return data?.map((row, i) => {
585
+ const dolly = row.slice();
586
+ dolly[0] = i;
587
+ dolly[1] = i;
588
+ return dolly;
589
+ });
590
+ }
591
+ getFilterPredicate() {
592
+ const {
593
+ filterSpec: { filterStruct }
594
+ } = combineFilters(this._config);
595
+ if (filterStruct) {
596
+ return filterPredicate(__privateGet(this, _columnMap), filterStruct);
597
+ } else {
598
+ throw Error("filter must include filterStruct");
599
+ }
600
+ }
601
+ applyConfig(config, preserveExistingConfigAttributes = false) {
602
+ const { noChanges, ...otherChanges } = isConfigChanged(
603
+ this._config,
604
+ config
605
+ );
606
+ if (noChanges !== true) {
607
+ if (config) {
608
+ const newConfig = config?.filterSpec?.filter && config?.filterSpec.filterStruct === void 0 ? {
609
+ ...config,
610
+ filterSpec: {
611
+ filter: config.filterSpec.filter,
612
+ filterStruct: parseFilter(config.filterSpec.filter)
613
+ }
614
+ } : config;
615
+ if (preserveExistingConfigAttributes) {
616
+ this._config = {
617
+ ...this._config,
618
+ ...config
619
+ };
620
+ } else {
621
+ this._config = withConfigDefaults(newConfig);
622
+ }
623
+ return otherChanges;
624
+ }
625
+ }
626
+ }
627
+ get columnMap() {
628
+ return __privateGet(this, _columnMap);
629
+ }
630
+ get selectedRowsCount() {
631
+ return this.selectedRows.size;
632
+ }
633
+ get size() {
634
+ return this.processedData?.length ?? __privateGet(this, _data).length;
635
+ }
636
+ get range() {
637
+ return __privateGet(this, _range);
638
+ }
639
+ set range(range) {
640
+ this.setRange(range);
641
+ }
642
+ delete(row) {
643
+ console.log(`delete row ${row.join(",")}`);
644
+ }
645
+ insertIntoSortedData(row) {
646
+ const indexedSortDefs = this.config.sort.sortDefs.map(
647
+ ({ column, sortType }) => [this.columnMap[column], sortType]
648
+ );
649
+ if (this.processedData) {
650
+ const comparator = sortComparator(indexedSortDefs);
651
+ const insertPos = binarySearch(this.processedData, row, comparator);
652
+ this.sendSizeUpdateToClient();
653
+ if (insertPos === -1) {
654
+ this.processedData?.unshift(row);
655
+ if (this.processedData) {
656
+ this.processedData = this.indexProcessedData(this.processedData);
657
+ }
658
+ if (insertPos <= __privateGet(this, _range).to) {
659
+ this.sendRowsToClient(true);
660
+ }
661
+ if (this.processedData) {
662
+ this.emit("resize", this.processedData.length);
663
+ }
664
+ } else {
665
+ if (this.processedData) {
666
+ this.emit("resize", this.processedData.length);
667
+ }
668
+ }
669
+ }
670
+ }
671
+ validateDataValue(columnName, value) {
672
+ const columnDescriptor = this.columnDescriptors.find(
673
+ (col) => col.name === columnName
674
+ );
675
+ if (columnDescriptor) {
676
+ switch (columnDescriptor.serverDataType) {
677
+ case "int":
678
+ {
679
+ if (typeof value === "number") {
680
+ if (Math.floor(value) !== value) {
681
+ throw Error(`${columnName} is int but value = ${value}`);
682
+ }
683
+ } else if (typeof value === "string") {
684
+ const numericValue = parseFloat(value);
685
+ if (Math.floor(numericValue) !== numericValue) {
686
+ throw Error(`${columnName} is ${value} is not a valid integer`);
687
+ }
688
+ }
689
+ }
690
+ break;
691
+ }
692
+ } else {
693
+ throw Error(`Unknown column ${columnName}`);
694
+ }
695
+ }
696
+ getRowByKey(key) {
697
+ const data = this.processedData ?? __privateGet(this, _data);
698
+ return data.find((row) => row[KEY] === key);
699
+ }
700
+ getRowAtIndex(rowIndex) {
701
+ return (this.processedData ?? __privateGet(this, _data))[rowIndex];
702
+ }
703
+ setRange(range, forceFullRefresh = false) {
704
+ if (range.from !== __privateGet(this, _range).from || range.to !== __privateGet(this, _range).to) {
705
+ const currentPageCount = Math.ceil(
706
+ this.size / (__privateGet(this, _range).to - __privateGet(this, _range).from)
707
+ );
708
+ const newPageCount = Math.ceil(this.size / (range.to - range.from));
709
+ __privateSet(this, _range, range);
710
+ const keysResequenced = __privateGet(this, _keys).reset(range.withBuffer);
711
+ this.sendRowsToClient(forceFullRefresh || keysResequenced);
712
+ requestAnimationFrame(() => {
713
+ if (newPageCount !== currentPageCount) {
714
+ this.emit("page-count", newPageCount);
715
+ }
716
+ this.emit("range", range);
717
+ });
718
+ } else if (forceFullRefresh) {
719
+ this.sendRowsToClient(forceFullRefresh);
720
+ }
721
+ }
722
+ sendSizeUpdateToClient() {
723
+ this.clientCallback?.({
724
+ clientViewportId: this.viewport,
725
+ mode: "size-only",
726
+ type: "viewport-update",
727
+ size: this.processedData ? this.processedData.length : __privateGet(this, _data).length
728
+ });
729
+ }
730
+ sendRowsToClient(forceFullRefresh = false, row) {
731
+ if (row) {
732
+ this.clientCallback?.({
733
+ clientViewportId: this.viewport,
734
+ mode: "update",
735
+ rows: [
736
+ toClientRow(row, __privateGet(this, _keys), this.selectedRows, this.dataIndices)
737
+ ],
738
+ type: "viewport-update"
739
+ });
740
+ } else {
741
+ const rowRange = this.rangeChangeRowset === "delta" && !forceFullRefresh ? rangeNewItems(this.lastRangeServed, __privateGet(this, _range).withBuffer) : __privateGet(this, _range).withBuffer;
742
+ const data = this.processedData ?? __privateGet(this, _data);
743
+ const rowsWithinViewport = data.slice(rowRange.from, rowRange.to).map(
744
+ (row2) => toClientRow(row2, __privateGet(this, _keys), this.selectedRows, this.dataIndices)
745
+ );
746
+ this.clientCallback?.({
747
+ clientViewportId: this.viewport,
748
+ mode: "batch",
749
+ range: __privateGet(this, _range),
750
+ rows: rowsWithinViewport,
751
+ size: data.length,
752
+ type: "viewport-update"
753
+ });
754
+ this.lastRangeServed = {
755
+ from: __privateGet(this, _range).from,
756
+ // to: this.#range.to,
757
+ to: Math.min(
758
+ __privateGet(this, _range).to,
759
+ __privateGet(this, _range).from + rowsWithinViewport.length
760
+ )
761
+ };
762
+ }
763
+ }
764
+ get aggregations() {
765
+ return this._config.aggregations;
766
+ }
767
+ set aggregations(aggregations) {
768
+ this._config = {
769
+ ...this._config,
770
+ aggregations
771
+ };
772
+ const targetData = this.processedData ?? __privateGet(this, _data);
773
+ const leafData = __privateGet(this, _data);
774
+ aggregateData(
775
+ aggregations,
776
+ targetData,
777
+ this._config.groupBy,
778
+ leafData,
779
+ __privateGet(this, _columnMap),
780
+ this.groupMap
781
+ );
782
+ this.setRange(__privateGet(this, _range).reset, true);
783
+ this.emit("config", this._config, this.range);
784
+ }
785
+ get sort() {
786
+ return this._config.sort;
787
+ }
788
+ set sort(sort) {
789
+ debug?.(`sort ${JSON.stringify(sort)}`);
790
+ this.config = {
791
+ ...this._config,
792
+ sort
793
+ };
794
+ }
795
+ get baseFilter() {
796
+ return this._config.baseFilterSpec;
797
+ }
798
+ set baseFilter(baseFilter) {
799
+ debug?.(`baseFilter ${JSON.stringify(baseFilter)}`);
800
+ this.config = {
801
+ ...this._config,
802
+ baseFilterSpec: baseFilter
803
+ };
804
+ }
805
+ get filter() {
806
+ return this._config.filterSpec;
807
+ }
808
+ set filter(filter) {
809
+ debug?.(`filter ${JSON.stringify(filter)}`);
810
+ this.config = {
811
+ ...this._config,
812
+ filterSpec: filter
813
+ };
814
+ }
815
+ setFilter(filter) {
816
+ const dataSourceFilter = {
817
+ filter: filterAsQuery(filter),
818
+ filterStruct: filter
819
+ };
820
+ this.filter = dataSourceFilter;
821
+ }
822
+ clearFilter() {
823
+ this.filter = { filter: "" };
824
+ }
825
+ get groupBy() {
826
+ return this._config.groupBy;
827
+ }
828
+ set groupBy(groupBy) {
829
+ this.config = {
830
+ ...this._config,
831
+ groupBy
832
+ };
833
+ }
834
+ get title() {
835
+ return __privateGet(this, _title) ?? `${this.table.module} ${this.table.table}`;
836
+ }
837
+ set title(title) {
838
+ __privateSet(this, _title, title);
839
+ this.emit("title-changed", this.viewport, title);
840
+ }
841
+ get _clientCallback() {
842
+ return this.clientCallback;
843
+ }
844
+ createLink({
845
+ parentVpId,
846
+ link: { fromColumn, toColumn }
847
+ }) {
848
+ console.log("create link", {
849
+ parentVpId,
850
+ fromColumn,
851
+ toColumn
852
+ });
853
+ }
854
+ removeLink() {
855
+ console.log("remove link");
856
+ }
857
+ async remoteProcedureCall() {
858
+ return Promise.reject();
859
+ }
860
+ async menuRpcCall(rpcRequest) {
861
+ console.log({ rpcRequest });
862
+ return new Promise(() => {
863
+ });
864
+ }
865
+ // All in BaseDataSource
866
+ freeze() {
867
+ if (!this.isFrozen) {
868
+ __privateSet(this, _freezeTimestamp, (/* @__PURE__ */ new Date()).getTime());
869
+ this.emit("freeze", true, __privateGet(this, _freezeTimestamp));
870
+ this.preserveScrollPositionAcrossConfigChange = true;
871
+ this.baseFilter = {
872
+ filter: `vuuCreatedTimestamp < ${__privateGet(this, _freezeTimestamp)}`
873
+ };
874
+ } else {
875
+ throw Error(
876
+ "[BaseDataSource] cannot freeze, dataSource is already frozen"
877
+ );
878
+ }
879
+ }
880
+ unfreeze() {
881
+ if (this.isFrozen) {
882
+ const freezeTimestamp = __privateGet(this, _freezeTimestamp);
883
+ __privateSet(this, _freezeTimestamp, void 0);
884
+ this.emit("freeze", false, freezeTimestamp);
885
+ this.preserveScrollPositionAcrossConfigChange = true;
886
+ this.baseFilter = {
887
+ filter: ""
888
+ };
889
+ } else {
890
+ throw Error(
891
+ "[BaseDataSource] cannot freeze, dataSource is already frozen"
892
+ );
893
+ }
894
+ }
895
+ get freezeTimestamp() {
896
+ return __privateGet(this, _freezeTimestamp);
897
+ }
898
+ get isFrozen() {
899
+ return typeof __privateGet(this, _freezeTimestamp) === "number";
900
+ }
901
+ }
902
+ _columnMap = new WeakMap();
903
+ _data = new WeakMap();
904
+ _freezeTimestamp = new WeakMap();
905
+ _keys = new WeakMap();
906
+ _links = new WeakMap();
907
+ _range = new WeakMap();
908
+ _status = new WeakMap();
909
+ _title = new WeakMap();
910
+
911
+ export { ArrayDataSource };
912
+ //# sourceMappingURL=array-data-source.js.map