1
- import { GridMetricCalculator , ModelSizeMap } from '@deephaven/grid' ;
1
+ import {
2
+ GridMetricCalculator ,
3
+ GridMetrics ,
4
+ ModelIndex ,
5
+ ModelSizeMap ,
6
+ trimMap ,
7
+ } from '@deephaven/grid' ;
2
8
import type { GridMetricState } from '@deephaven/grid' ;
3
9
import type { dh } from '@deephaven/jsapi-types' ;
10
+ import { assertNotNull } from '@deephaven/utils' ;
4
11
import type IrisGridModel from './IrisGridModel' ;
5
12
import { IrisGridThemeType } from './IrisGridTheme' ;
13
+ import {
14
+ ColumnName ,
15
+ ReadonlyAdvancedFilterMap ,
16
+ ReadonlyQuickFilterMap ,
17
+ } from './CommonTypes' ;
6
18
7
19
export interface IrisGridMetricState extends GridMetricState {
8
20
model : IrisGridModel ;
9
21
theme : IrisGridThemeType ;
10
22
isFilterBarShown : boolean ;
11
- advancedFilters : Map <
12
- string ,
13
- { options : unknown ; filter : dh . FilterCondition | null }
14
- > ;
15
- quickFilters : Map <
16
- string ,
17
- { text : string ; filter : dh . FilterCondition | null }
18
- > ;
19
- sorts : dh . Sort [ ] ;
23
+ advancedFilters : ReadonlyAdvancedFilterMap ;
24
+ quickFilters : ReadonlyQuickFilterMap ;
25
+ sorts : readonly dh . Sort [ ] ;
20
26
reverse : boolean ;
21
27
}
22
28
23
- /**
24
- * Class to calculate all the metrics for a grid.
25
- * Call getMetrics() with the state to get metrics
26
- */
27
29
export class IrisGridMetricCalculator extends GridMetricCalculator {
30
+ // Column widths by name to keep track of columns going in and out of viewport
31
+ userColumnWidthsByName : Map < ColumnName , number > = new Map ( ) ;
32
+
33
+ private cachedModelColumns : readonly dh . Column [ ] | undefined ;
34
+
35
+ // constructor(...args: IrisGridMetricCalculatorParameters) {
36
+ // super(...args);
37
+ // const [config] = args;
38
+ // const { model, userColumnWidths } = config;
39
+
40
+ // this.model = model;
41
+
42
+ // // Populate userColumnWidthsByName from index-based userColumnWidths arg
43
+ // // and the map of existing column names.
44
+ // // We need to persist widths for all columns, even the ones that are not currently present because the model can expand and collapse
45
+ // // TODO: this will be problematic for user width persistence since the model can initialize with partial set of columns
46
+ // // and populates the rest on model update.
47
+ // // TODO: change the arg type to Map<ColumnName, number>?
48
+ // this.userColumnWidthsByName = new Map<ColumnName, number>();
49
+ // userColumnWidths?.forEach((width, modelIndex) => {
50
+ // const name = model.columns[modelIndex]?.name;
51
+ // if (name != null) {
52
+ // this.userColumnWidthsByName.set(name, width);
53
+ // }
54
+ // });
55
+ // }
56
+
57
+ /**
58
+ * Updates the user column widths based on the current model state
59
+ * @param model The current IrisGridModel
60
+ */
61
+ private updateUserColumnWidths ( model : IrisGridModel ) : void {
62
+ this . userColumnWidths = new Map < ModelIndex , number > ( ) ;
63
+ this . userColumnWidthsByName . forEach ( ( width , name ) => {
64
+ const modelIndex = model . getColumnIndexByName ( name ) ;
65
+ if ( modelIndex != null ) {
66
+ super . setColumnWidth ( modelIndex , width ) ;
67
+ }
68
+ } ) ;
69
+ }
70
+
71
+ /**
72
+ * Updates the user and calculated column widths if the model columns have changed
73
+ * @param model The current IrisGridModel
74
+ */
75
+ private updateColumnWidthsIfNecessary ( model : IrisGridModel ) : void {
76
+ if ( model . columns !== this . cachedModelColumns ) {
77
+ this . resetCalculatedColumnWidths ( ) ;
78
+ this . updateUserColumnWidths ( model ) ;
79
+ }
80
+ }
81
+
28
82
getGridY ( state : IrisGridMetricState ) : number {
83
+ // The state here seems to be a GridMetricState with stateOverrides passed from IrisGrid in the props,
84
+ // not guaranteed to be IrisGridMetricState
29
85
let gridY = super . getGridY ( state ) ;
30
86
const {
31
87
isFilterBarShown,
@@ -50,7 +106,69 @@ export class IrisGridMetricCalculator extends GridMetricCalculator {
50
106
return gridY ;
51
107
}
52
108
109
+ /**
110
+ * Gets the metrics for the current state. This method has to be called to update cachedModelColumns on model columns change.
111
+ * @param state The current IrisGridMetricState
112
+ * @returns The metrics for the current state
113
+ */
114
+ getMetrics ( state : IrisGridMetricState ) : GridMetrics {
115
+ const { model } = state ;
116
+ // Update column widths if columns in the cached model don't match the current model passed in the state
117
+ this . updateColumnWidthsIfNecessary ( model ) ;
118
+
119
+ this . cachedModelColumns = model . columns ;
120
+ return super . getMetrics ( state ) ;
121
+ }
122
+
123
+ /**
124
+ * Sets the width for a specific column by index
125
+ * @param column The index of the column to set
126
+ * @param size The new width for the column
127
+ */
128
+ setColumnWidth ( column : number , size : number ) : void {
129
+ super . setColumnWidth ( column , size ) ;
130
+ assertNotNull (
131
+ this . cachedModelColumns ,
132
+ 'setColumnWidth should be called after getMetrics'
133
+ ) ;
134
+ const name = this . cachedModelColumns [ column ] ?. name ;
135
+ if ( name != null ) {
136
+ this . userColumnWidthsByName . set ( name , size ) ;
137
+ trimMap ( this . userColumnWidthsByName ) ;
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Resets the width for a specific column by index
143
+ * @param column The index of the column to reset
144
+ */
145
+ resetColumnWidth ( column : number ) : void {
146
+ super . resetColumnWidth ( column ) ;
147
+ assertNotNull (
148
+ this . cachedModelColumns ,
149
+ 'resetColumnWidth should be called after getMetrics'
150
+ ) ;
151
+ const name = this . cachedModelColumns [ column ] ?. name ;
152
+ if ( name != null ) {
153
+ this . userColumnWidthsByName . delete ( name ) ;
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Resets all user column widths
159
+ */
160
+ resetAllColumnWidths ( ) : void {
161
+ this . userColumnWidths = new Map < ModelIndex , number > ( ) ;
162
+ this . userColumnWidthsByName = new Map < ColumnName , number > ( ) ;
163
+ }
164
+
165
+ /**
166
+ * Gets the user column widths for the current state
167
+ * @param state The current IrisGridMetricState
168
+ * @returns A map of user column widths
169
+ */
53
170
getUserColumnWidths ( ) : ModelSizeMap {
171
+ // This might return stale data if getMetrics hasn't been called
54
172
return this . userColumnWidths ;
55
173
}
56
174
}
0 commit comments