@visactor/vtable 1.22.11-alpha.5 → 1.22.11-alpha.7

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.
@@ -89,14 +89,19 @@ export declare class DataSource extends EventTarget implements DataSourceAPI {
89
89
  changeFieldValueByRecordIndex(value: FieldData, recordIndex: number | number[], field: FieldDef, table?: BaseTableAPI): FieldData;
90
90
  cacheBeforeChangedRecord(dataIndex: number | number[], table?: BaseTableAPI): void;
91
91
  setRecord(record: any, index: number): void;
92
- addRecord(record: any, index: number): void;
93
- addRecords(recordArr: any, index: number): void;
92
+ private _getRawRecordsArray;
93
+ private _hasFilterInEffect;
94
+ private _normalizeInsertIndex;
95
+ private _mapViewInsertIndexToRawInsertIndex;
96
+ private _resetIndexingFromViewRecords;
97
+ addRecord(record: any, index: number, syncToOriginalRecords?: boolean): void;
98
+ addRecords(recordArr: any, index: number, syncToOriginalRecords?: boolean): void;
94
99
  addRecordForSorted(record: any): void;
95
100
  addRecordsForSorted(recordArr: any): void;
96
101
  adjustBeforeChangedRecordsMap(insertIndex: number, insertCount: number, type?: 'add' | 'delete'): void;
97
- deleteRecords(recordIndexs: number[]): number[];
102
+ deleteRecords(recordIndexs: number[], syncToOriginalRecords?: boolean): number[];
98
103
  deleteRecordsForSorted(recordIndexs: number[]): void;
99
- updateRecords(records: any[], recordIndexs: (number | number[])[]): (number | number[])[];
104
+ updateRecords(records: any[], recordIndexs: (number | number[])[], syncToOriginalRecords?: boolean): (number | number[])[];
100
105
  updateRecordsForSorted(records: any[], recordIndexs: number[]): void;
101
106
  sort(states: Array<SortState>): void;
102
107
  setSortedIndexMap(field: FieldDef, filedMap: ISortedMapItem): void;
@@ -356,38 +356,82 @@ export class DataSource extends EventTarget {
356
356
  Array.isArray(indexed) || this.records.splice(indexed, 1, record);
357
357
  }
358
358
  }
359
- addRecord(record, index) {
359
+ _getRawRecordsArray() {
360
360
  var _a;
361
- if (Array.isArray(this.records)) {
362
- this.records.splice(index, 0, record), this.adjustBeforeChangedRecordsMap(index, 1),
363
- this.currentIndexedData.push(this.currentIndexedData.length), this._sourceLength += 1;
364
- for (let i = 0; i < this.fieldAggregators.length; i++) this.fieldAggregators[i].push(record);
365
- if ("tree" === this.rowHierarchyType && this.initTreeHierarchyState(), this.userPagination) {
366
- this.pagination.totalCount = this._sourceLength;
367
- const {perPageCount: perPageCount, currentPage: currentPage} = this.pagination;
368
- index < perPageCount * (currentPage || 0) + perPageCount && this.updatePagerData();
369
- } else this.pagination.perPageCount = this._sourceLength, this.pagination.totalCount = this._sourceLength,
370
- this.updatePagerData();
371
- (null === (_a = this.dataSourceObj) || void 0 === _a ? void 0 : _a.added) && this.dataSourceObj.added(index, 1);
372
- }
373
- }
374
- addRecords(recordArr, index) {
375
- var _a;
376
- if (Array.isArray(this.records)) {
377
- if (Array.isArray(recordArr)) {
378
- this.records.splice(index, 0, ...recordArr), this.adjustBeforeChangedRecordsMap(index, recordArr.length);
379
- for (let i = 0; i < recordArr.length; i++) this.currentIndexedData.push(this.currentIndexedData.length);
380
- this._sourceLength += recordArr.length;
381
- for (let i = 0; i < this.fieldAggregators.length; i++) for (let j = 0; j < recordArr.length; j++) this.fieldAggregators[i].push(recordArr[j]);
361
+ const rawRecords = null === (_a = this.dataSourceObj) || void 0 === _a ? void 0 : _a.records;
362
+ return Array.isArray(rawRecords) ? rawRecords : null;
363
+ }
364
+ _hasFilterInEffect() {
365
+ var _a, _b, _c, _d, _e;
366
+ return (null !== (_c = null === (_b = null === (_a = this.dataConfig) || void 0 === _a ? void 0 : _a.filterRules) || void 0 === _b ? void 0 : _b.length) && void 0 !== _c ? _c : 0) >= 1 || (null !== (_e = null === (_d = this.lastFilterRules) || void 0 === _d ? void 0 : _d.length) && void 0 !== _e ? _e : 0) >= 1;
367
+ }
368
+ _normalizeInsertIndex(index, length) {
369
+ return null == index || index > length ? length : index < 0 ? 0 : index;
370
+ }
371
+ _mapViewInsertIndexToRawInsertIndex(rawRecords, viewIndex) {
372
+ if (viewIndex >= this.records.length) return rawRecords.length;
373
+ if (viewIndex <= 0) return 0;
374
+ const prevRecord = this.records[viewIndex - 1], rawIndex = rawRecords.indexOf(prevRecord);
375
+ return rawIndex >= 0 ? rawIndex + 1 : rawRecords.length;
376
+ }
377
+ _resetIndexingFromViewRecords() {
378
+ if (this._sourceLength = this.records.length, this.currentIndexedData = Array.from({
379
+ length: this._sourceLength
380
+ }, ((_, i) => i)), "tree" === this.rowHierarchyType && this.initTreeHierarchyState(),
381
+ this.userPagination) return this.pagination.totalCount = this._sourceLength, void this.updatePagerData();
382
+ this.pagination.perPageCount = this._sourceLength, this.pagination.totalCount = this._sourceLength,
383
+ this.updatePagerData();
384
+ }
385
+ addRecord(record, index, syncToOriginalRecords = !1) {
386
+ var _a, _b, _c;
387
+ if (!syncToOriginalRecords) {
388
+ if (Array.isArray(this.records)) {
389
+ this.records.splice(index, 0, record), this.adjustBeforeChangedRecordsMap(index, 1),
390
+ this.currentIndexedData.push(this.currentIndexedData.length), this._sourceLength += 1;
391
+ for (let i = 0; i < this.fieldAggregators.length; i++) this.fieldAggregators[i].push(record);
392
+ if ("tree" === this.rowHierarchyType && this.initTreeHierarchyState(), this.userPagination) {
393
+ this.pagination.totalCount = this._sourceLength;
394
+ const {perPageCount: perPageCount, currentPage: currentPage} = this.pagination;
395
+ index < perPageCount * (currentPage || 0) + perPageCount && this.updatePagerData();
396
+ } else this.pagination.perPageCount = this._sourceLength, this.pagination.totalCount = this._sourceLength,
397
+ this.updatePagerData();
398
+ (null === (_a = this.dataSourceObj) || void 0 === _a ? void 0 : _a.added) && this.dataSourceObj.added(index, 1);
382
399
  }
383
- if (this.userPagination) {
384
- this.pagination.totalCount = this._sourceLength;
385
- const {perPageCount: perPageCount, currentPage: currentPage} = this.pagination;
386
- index < perPageCount * (currentPage || 0) + perPageCount && this.updatePagerData();
387
- } else this.pagination.perPageCount = this._sourceLength, this.pagination.totalCount = this._sourceLength,
388
- this.updatePagerData();
389
- (null === (_a = this.dataSourceObj) || void 0 === _a ? void 0 : _a.added) && this.dataSourceObj.added(index, recordArr.length);
400
+ return;
390
401
  }
402
+ const rawRecords = this._getRawRecordsArray();
403
+ if (!rawRecords) return;
404
+ const viewInsertIndex = this._normalizeInsertIndex(index, this.records.length), rawInsertIndex = this._hasFilterInEffect() ? this._mapViewInsertIndexToRawInsertIndex(rawRecords, viewInsertIndex) : this._normalizeInsertIndex(viewInsertIndex, rawRecords.length);
405
+ rawRecords.splice(rawInsertIndex, 0, record), this.beforeChangedRecordsMap.clear(),
406
+ this.sortedIndexMap.clear(), this.updateFilterRules(null === (_b = this.dataConfig) || void 0 === _b ? void 0 : _b.filterRules),
407
+ (null === (_c = this.dataSourceObj) || void 0 === _c ? void 0 : _c.added) && this.dataSourceObj.added(rawInsertIndex, 1);
408
+ }
409
+ addRecords(recordArr, index, syncToOriginalRecords = !1) {
410
+ var _a, _b, _c;
411
+ if (!syncToOriginalRecords) {
412
+ if (Array.isArray(this.records)) {
413
+ if (Array.isArray(recordArr)) {
414
+ this.records.splice(index, 0, ...recordArr), this.adjustBeforeChangedRecordsMap(index, recordArr.length);
415
+ for (let i = 0; i < recordArr.length; i++) this.currentIndexedData.push(this.currentIndexedData.length);
416
+ this._sourceLength += recordArr.length;
417
+ for (let i = 0; i < this.fieldAggregators.length; i++) for (let j = 0; j < recordArr.length; j++) this.fieldAggregators[i].push(recordArr[j]);
418
+ }
419
+ if (this.userPagination) {
420
+ this.pagination.totalCount = this._sourceLength;
421
+ const {perPageCount: perPageCount, currentPage: currentPage} = this.pagination;
422
+ index < perPageCount * (currentPage || 0) + perPageCount && this.updatePagerData();
423
+ } else this.pagination.perPageCount = this._sourceLength, this.pagination.totalCount = this._sourceLength,
424
+ this.updatePagerData();
425
+ (null === (_a = this.dataSourceObj) || void 0 === _a ? void 0 : _a.added) && this.dataSourceObj.added(index, recordArr.length);
426
+ }
427
+ return;
428
+ }
429
+ const rawRecords = this._getRawRecordsArray();
430
+ if (!rawRecords || !Array.isArray(recordArr) || 0 === recordArr.length) return;
431
+ const viewInsertIndex = this._normalizeInsertIndex(index, this.records.length), rawInsertIndex = this._hasFilterInEffect() ? this._mapViewInsertIndexToRawInsertIndex(rawRecords, viewInsertIndex) : this._normalizeInsertIndex(viewInsertIndex, rawRecords.length);
432
+ rawRecords.splice(rawInsertIndex, 0, ...recordArr), this.beforeChangedRecordsMap.clear(),
433
+ this.sortedIndexMap.clear(), this.updateFilterRules(null === (_b = this.dataConfig) || void 0 === _b ? void 0 : _b.filterRules),
434
+ (null === (_c = this.dataSourceObj) || void 0 === _c ? void 0 : _c.added) && this.dataSourceObj.added(rawInsertIndex, recordArr.length);
391
435
  }
392
436
  addRecordForSorted(record) {
393
437
  Array.isArray(this.records) && (this.beforeChangedRecordsMap.clear(), this.records.push(record),
@@ -416,23 +460,38 @@ export class DataSource extends EventTarget {
416
460
  this.beforeChangedRecordsMap.delete(key.toString()), this.beforeChangedRecordsMap.set((key + delta).toString(), record);
417
461
  }
418
462
  }
419
- deleteRecords(recordIndexs) {
420
- var _a;
421
- if (Array.isArray(this.records)) {
422
- const realDeletedRecordIndexs = [], recordIndexsMaxToMin = recordIndexs.sort(((a, b) => b - a));
423
- for (let index = 0; index < recordIndexsMaxToMin.length; index++) {
424
- const recordIndex = recordIndexsMaxToMin[index];
425
- if (recordIndex >= this._sourceLength || recordIndex < 0) continue;
426
- this.adjustBeforeChangedRecordsMap(recordIndex, 1, "delete"), realDeletedRecordIndexs.push(recordIndex);
427
- const deletedRecord = this.records[recordIndex];
428
- for (let i = 0; i < this.fieldAggregators.length; i++) this.fieldAggregators[i].deleteRecord(deletedRecord);
429
- this.records.splice(recordIndex, 1), this.currentIndexedData.pop(), this._sourceLength -= 1;
463
+ deleteRecords(recordIndexs, syncToOriginalRecords = !1) {
464
+ var _a, _b, _c;
465
+ if (!syncToOriginalRecords) {
466
+ if (Array.isArray(this.records)) {
467
+ const realDeletedRecordIndexs = [], recordIndexsMaxToMin = recordIndexs.sort(((a, b) => b - a));
468
+ for (let index = 0; index < recordIndexsMaxToMin.length; index++) {
469
+ const recordIndex = recordIndexsMaxToMin[index];
470
+ if (recordIndex >= this._sourceLength || recordIndex < 0) continue;
471
+ this.adjustBeforeChangedRecordsMap(recordIndex, 1, "delete"), realDeletedRecordIndexs.push(recordIndex);
472
+ const deletedRecord = this.records[recordIndex];
473
+ for (let i = 0; i < this.fieldAggregators.length; i++) this.fieldAggregators[i].deleteRecord(deletedRecord);
474
+ this.records.splice(recordIndex, 1), this.currentIndexedData.pop(), this._sourceLength -= 1;
475
+ }
476
+ return this.userPagination || (this.pagination.perPageCount = this._sourceLength,
477
+ this.pagination.totalCount = this._sourceLength), this.updatePagerData(), (null === (_a = this.dataSourceObj) || void 0 === _a ? void 0 : _a.deleted) && this.dataSourceObj.deleted(realDeletedRecordIndexs),
478
+ realDeletedRecordIndexs;
430
479
  }
431
- return this.userPagination || (this.pagination.perPageCount = this._sourceLength,
432
- this.pagination.totalCount = this._sourceLength), this.updatePagerData(), (null === (_a = this.dataSourceObj) || void 0 === _a ? void 0 : _a.deleted) && this.dataSourceObj.deleted(realDeletedRecordIndexs),
433
- realDeletedRecordIndexs;
480
+ return [];
434
481
  }
435
- return [];
482
+ const rawRecords = this._getRawRecordsArray();
483
+ if (!rawRecords || !Array.isArray(this.records)) return [];
484
+ const realDeletedRecordIndexs = [], recordIndexsMaxToMin = recordIndexs.slice().sort(((a, b) => b - a)), rawDeletedIndexs = [];
485
+ for (let index = 0; index < recordIndexsMaxToMin.length; index++) {
486
+ const viewIndex = recordIndexsMaxToMin[index];
487
+ if (viewIndex >= this.records.length || viewIndex < 0) continue;
488
+ const deletedRecord = this.records[viewIndex], rawIndex = rawRecords.indexOf(deletedRecord);
489
+ rawIndex >= 0 && (rawRecords.splice(rawIndex, 1), rawDeletedIndexs.push(rawIndex)),
490
+ realDeletedRecordIndexs.push(viewIndex);
491
+ }
492
+ return this.beforeChangedRecordsMap.clear(), this.sortedIndexMap.clear(), this.updateFilterRules(null === (_b = this.dataConfig) || void 0 === _b ? void 0 : _b.filterRules),
493
+ (null === (_c = this.dataSourceObj) || void 0 === _c ? void 0 : _c.deleted) && this.dataSourceObj.deleted(rawDeletedIndexs),
494
+ realDeletedRecordIndexs;
436
495
  }
437
496
  deleteRecordsForSorted(recordIndexs) {
438
497
  if (Array.isArray(this.records)) {
@@ -447,20 +506,38 @@ export class DataSource extends EventTarget {
447
506
  this.pagination.totalCount = this._sourceLength), this.beforeChangedRecordsMap.clear();
448
507
  }
449
508
  }
450
- updateRecords(records, recordIndexs) {
451
- const realDeletedRecordIndexs = [];
509
+ updateRecords(records, recordIndexs, syncToOriginalRecords = !1) {
510
+ var _a;
511
+ if (!syncToOriginalRecords) {
512
+ const realDeletedRecordIndexs = [];
513
+ for (let index = 0; index < recordIndexs.length; index++) {
514
+ const recordIndex = recordIndexs[index];
515
+ if (Array.isArray(recordIndex)) this.beforeChangedRecordsMap.delete(recordIndex.toString()),
516
+ realDeletedRecordIndexs.push(recordIndex), recordIndex.slice(0, -1).reduce(((acc, key) => (void 0 === acc[key] && (acc[key] = {}),
517
+ acc[key].children)), this.records)[recordIndex[recordIndex.length - 1]] = records[index]; else {
518
+ if (recordIndex >= this._sourceLength || recordIndex < 0) continue;
519
+ this.beforeChangedRecordsMap.delete(recordIndex.toString()), realDeletedRecordIndexs.push(recordIndex);
520
+ for (let i = 0; i < this.fieldAggregators.length; i++) this.fieldAggregators[i].updateRecord(this.records[recordIndex], records[index]);
521
+ this.records[recordIndex] = records[index];
522
+ }
523
+ }
524
+ return this.userPagination && this.updatePagerData(), realDeletedRecordIndexs;
525
+ }
526
+ const rawRecords = this._getRawRecordsArray();
527
+ if (!rawRecords || !Array.isArray(this.records)) return [];
528
+ const realUpdatedIndexs = [];
452
529
  for (let index = 0; index < recordIndexs.length; index++) {
453
530
  const recordIndex = recordIndexs[index];
454
531
  if (Array.isArray(recordIndex)) this.beforeChangedRecordsMap.delete(recordIndex.toString()),
455
- realDeletedRecordIndexs.push(recordIndex), recordIndex.slice(0, -1).reduce(((acc, key) => (void 0 === acc[key] && (acc[key] = {}),
456
- acc[key].children)), this.records)[recordIndex[recordIndex.length - 1]] = records[index]; else {
457
- if (recordIndex >= this._sourceLength || recordIndex < 0) continue;
458
- this.beforeChangedRecordsMap.delete(recordIndex.toString()), realDeletedRecordIndexs.push(recordIndex);
459
- for (let i = 0; i < this.fieldAggregators.length; i++) this.fieldAggregators[i].updateRecord(this.records[recordIndex], records[index]);
460
- this.records[recordIndex] = records[index];
532
+ realUpdatedIndexs.push(recordIndex), recordIndex.slice(0, -1).reduce(((acc, key) => (void 0 === acc[key] && (acc[key] = {}),
533
+ acc[key].children)), rawRecords)[recordIndex[recordIndex.length - 1]] = records[index]; else {
534
+ if (recordIndex >= this.records.length || recordIndex < 0) continue;
535
+ const oldRecord = this.records[recordIndex], rawIndex = rawRecords.indexOf(oldRecord);
536
+ rawIndex >= 0 && (rawRecords[rawIndex] = records[index]), realUpdatedIndexs.push(recordIndex);
461
537
  }
462
538
  }
463
- return this.userPagination && this.updatePagerData(), realDeletedRecordIndexs;
539
+ return this.beforeChangedRecordsMap.clear(), this.sortedIndexMap.clear(), this.updateFilterRules(null === (_a = this.dataConfig) || void 0 === _a ? void 0 : _a.filterRules),
540
+ realUpdatedIndexs;
464
541
  }
465
542
  updateRecordsForSorted(records, recordIndexs) {
466
543
  const realDeletedRecordIndexs = [];