@vuu-ui/vuu-data-local 3.0.0 → 3.1.0-beta.2

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