Skip to content

Commit 5ba8621

Browse files
committed
Clean up IrisGridSimplePivotModel
1 parent 3717221 commit 5ba8621

1 file changed

Lines changed: 42 additions & 47 deletions

File tree

packages/iris-grid/src/IrisGridSimplePivotModel.ts

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,6 @@ export type SimplePivotColumnMap = ReadonlyMap<string, string>;
4242

4343
const GRAND_TOTAL_VALUE = 'Grand Total';
4444

45-
// TODO:
46-
// - totals row formatting [DONE]
47-
// - totals row: copy cell unformatted [DONE]
48-
// - copy selection with headers - fix column mapping, fix case with only totals row selected [DONE]
49-
// - fix Grand Total text in non-string columns [DONE]
50-
// - disable sort on col columns [DONE]
51-
// - disable filters, advanced filters on col columns [DONE]
52-
// - disable Search Bar [DONE]
53-
// - disable Download CSV [DONE]
54-
55-
// - disable Organize columns [DONE]
56-
// - disable Go to row [DONE]
57-
58-
// - fix sub/unsubscribe on model change
59-
// - totals column move to back
60-
61-
// - DH.UI example with selectable aggregation functions
62-
63-
// - column order, column resize, visibility, etc -- save/restore settings on model change (or disable?)
64-
// - if the above works, enable sorting, filtering, etc.
65-
66-
// - ^ unit tests
67-
6845
/**
6946
* Model which proxies calls to IrisGridModel.
7047
* This allows updating the underlying Simple Pivot tables on schema changes.
@@ -88,12 +65,14 @@ class IrisGridSimplePivotModel extends IrisGridModel {
8865

8966
model: IrisGridModel;
9067

91-
private modelPromise: CancelablePromise<IrisGridModel> | null;
68+
private modelPromise: CancelablePromise<[DhType.Table, DhType.Table]> | null;
9269

9370
private nextModel: IrisGridModel | null;
9471

9572
private totalsTable: DhType.Table | null;
9673

74+
private nextTotalsTable: DhType.Table | null;
75+
9776
private totalsRowMap: Map<string, unknown>;
9877

9978
constructor(
@@ -126,6 +105,7 @@ class IrisGridSimplePivotModel extends IrisGridModel {
126105
this.keyTableSubscription = null;
127106
this.pivotWidget = pivotWidget;
128107
this.totalsTable = null;
108+
this.nextTotalsTable = null;
129109
this.totalsRowMap = new Map();
130110

131111
this.columnMap = new Map(
@@ -459,7 +439,8 @@ class IrisGridSimplePivotModel extends IrisGridModel {
459439
this.nextColumnMap = columnMap;
460440
} else {
461441
log.debug('next model not null, all columns match');
462-
this.setModel(this.nextModel, columnMap);
442+
assertNotNull(this.nextTotalsTable);
443+
this.setModel(this.nextModel, columnMap, this.nextTotalsTable);
463444
this.nextModel = null;
464445
}
465446
} else if (
@@ -479,11 +460,8 @@ class IrisGridSimplePivotModel extends IrisGridModel {
479460
const tables = e.detail.exportedObjects;
480461
const tablePromise = tables[0].fetch();
481462
const totalsTablePromise = tables.length === 2 ? tables[1].fetch() : null;
482-
totalsTablePromise?.then(totalsTable => this.setTotalsTable(totalsTable));
483-
const newModelPromise = tablePromise.then(table =>
484-
makeModel(this.dh, table, this.formatter)
485-
);
486-
this.setNextModel(newModelPromise);
463+
const pivotTablesPromise = Promise.all([tablePromise, totalsTablePromise]);
464+
this.setNextSchema(pivotTablesPromise);
487465
}
488466

489467
copyTotalsData(data: DhType.ViewportData): void {
@@ -512,7 +490,7 @@ class IrisGridSimplePivotModel extends IrisGridModel {
512490
);
513491

514492
get layoutHints(): DhType.LayoutHints | null | undefined {
515-
log.debug('get layoutHints');
493+
// log.debug('get layoutHints');
516494
return {
517495
backColumns: ['__TOTALS_COLUMN'],
518496
hiddenColumns: [],
@@ -558,22 +536,35 @@ class IrisGridSimplePivotModel extends IrisGridModel {
558536
this.dispatchEvent(new EventShimCustomEvent(type, { detail }));
559537
}
560538

561-
setModel(model: IrisGridModel, columnMap: SimplePivotColumnMap): void {
562-
log.debug('setModel', model, {
563-
prev: JSON.stringify([...columnMap]),
564-
columns: JSON.stringify([...model.columns.map(c => c.name)]),
565-
});
539+
setModel(
540+
model: IrisGridModel,
541+
columnMap: SimplePivotColumnMap,
542+
totalsTable: DhType.Table
543+
): void {
544+
log.debug('setModel', model);
566545

567546
const oldModel = this.model;
568547
oldModel.close();
548+
if (this.listenerCount > 0) {
549+
this.removeListeners(oldModel);
550+
}
569551

570552
this.model = model;
553+
this.setTotalsTable(totalsTable);
571554
this.columnMap = columnMap;
572555
this.columnHeaderGroups = this.getCachedColumnHeaderGroups(
573556
this.columnMap,
574557
this.schema
575558
);
576559

560+
if (
561+
!isIrisGridTableModelTemplate(model) ||
562+
!isIrisGridTableModelTemplate(oldModel)
563+
) {
564+
throw new Error('Invalid model, setModel not available');
565+
}
566+
log.debug('[3] setModel', oldModel.movedColumns);
567+
577568
// TODO: set totals table, subscribe to updates
578569
if (this.listenerCount > 0) {
579570
this.addListeners(model);
@@ -599,40 +590,44 @@ class IrisGridSimplePivotModel extends IrisGridModel {
599590
);
600591
}
601592

602-
setNextModel(modelPromise: Promise<IrisGridModel>): void {
603-
log.debug2('setNextModel');
593+
setNextSchema(
594+
pivotTablesPromise: Promise<[DhType.Table, DhType.Table]>
595+
): void {
596+
log.debug2('setNextSchema');
604597

605598
if (this.modelPromise) {
606599
this.modelPromise.cancel();
607600
}
608601

609-
if (this.listenerCount > 0) {
610-
this.removeListeners(this.model);
611-
}
612-
602+
// TODO: rename
613603
this.modelPromise = PromiseUtils.makeCancelable(
614-
modelPromise,
615-
(model: IrisGridModel) => model.close()
604+
pivotTablesPromise,
605+
([table, totalsTable]: [DhType.Table, DhType.Table]) => {
606+
table.close();
607+
totalsTable.close();
608+
}
616609
);
617610
this.modelPromise
618-
.then(model => {
611+
.then(([table, totalsTable]) => {
619612
this.modelPromise = null;
613+
const model = makeModel(this.dh, table, this.formatter);
620614
if (this.nextColumnMap != null) {
621615
log.debug(
622616
'Schema updated, nextColumnMap is not null, setting new model'
623617
);
624-
this.setModel(model, this.nextColumnMap);
618+
this.setModel(model, this.nextColumnMap, totalsTable);
625619
this.nextColumnMap = null;
626620
} else {
627621
log.debug(
628622
'Schema updated, nextColumnMap is null, save new model as nextModel'
629623
);
630624
this.nextModel = model;
625+
this.nextTotalsTable = totalsTable;
631626
}
632627
})
633628
.catch((err: unknown) => {
634629
if (PromiseUtils.isCanceled(err)) {
635-
log.debug2('setNextModel cancelled');
630+
log.debug2('setNextSchema cancelled');
636631
return;
637632
}
638633

0 commit comments

Comments
 (0)