@vuu-ui/vuu-data-local 2.1.19-beta.1 → 2.1.19-beta.3

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