-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
datamodel.ts
384 lines (343 loc) · 9.27 KB
/
datamodel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2019, PhosphorJS Contributors
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
import { ISignal, Signal } from '@lumino/signaling';
import { CellGroup } from './cellgroup';
/**
* An object which provides the data for a data grid.
*
* #### Notes
* If the predefined data models are insufficient for a particular use
* case, a custom model can be defined which derives from this class.
*/
export abstract class DataModel {
/**
* A signal emitted when the data model has changed.
*/
get changed(): ISignal<this, DataModel.ChangedArgs> {
return this._changed;
}
/**
* Get the row count for a region in the data model.
*
* @param region - The row region of interest.
*
* @returns - The row count for the region.
*
* #### Notes
* This method is called often, and so should be efficient.
*/
abstract rowCount(region: DataModel.RowRegion): number;
/**
* Get the column count for a region in the data model.
*
* @param region - The column region of interest.
*
* @returns - The column count for the region.
*
* #### Notes
* This method is called often, and so should be efficient.
*/
abstract columnCount(region: DataModel.ColumnRegion): number;
/**
* Get the data value for a cell in the data model.
*
* @param region - The cell region of interest.
*
* @param row - The row index of the cell of interest.
*
* @param column - The column index of the cell of interest.
*
* @returns The data value for the specified cell.
*
* #### Notes
* The returned data should be treated as immutable.
*
* This method is called often, and so should be efficient.
*/
abstract data(region: DataModel.CellRegion, row: number, column: number): any;
/**
* Get the count of merged cell groups pertaining to a given
* cell region.
* @param region the target cell region.
*/
groupCount(region: DataModel.CellRegion): number {
return 0;
}
/**
* Get the metadata for a cell in the data model.
*
* @param region - The cell region of interest.
*
* @param row - The row index of the cell of interest.
*
* @param column - The column index of the cell of interest.
*
* @returns The metadata for the specified cell.
*
* #### Notes
* The returned metadata should be treated as immutable.
*
* This method is called often, and so should be efficient.
*
* The default implementation returns `{}`.
*/
metadata(
region: DataModel.CellRegion,
row: number,
column: number
): DataModel.Metadata {
return DataModel.emptyMetadata;
}
/**
* Get the merged cell group corresponding to a region and index number.
* @param region the cell region of cell group.
* @param groupIndex the group index of the cell group.
* @returns a cell group.
*/
group(region: DataModel.CellRegion, groupIndex: number): CellGroup | null {
return null;
}
/**
* Emit the `changed` signal for the data model.
*
* #### Notes
* Subclasses should call this method whenever the data model has
* changed so that attached data grids can update themselves.
*/
protected emitChanged(args: DataModel.ChangedArgs): void {
this._changed.emit(args);
}
private _changed = new Signal<this, DataModel.ChangedArgs>(this);
}
/**
* An object which provides the mutable data for a data grid.
*
* #### Notes
* This object is an extension to `DataModel` and it only adds ability to
* change data for cells.
*/
export abstract class MutableDataModel extends DataModel {
/**
* Set the data value for a cell in the data model.
*
* @param region - The cell region of interest.
*
* @param row - The row index of the cell of interest.
*
* @param column - The column index of the cell of interest.
*
* @returns true if succeeds, false otherwise.
*
*/
abstract setData(
region: DataModel.CellRegion,
row: number,
column: number,
value: unknown
): boolean;
}
/**
* The namespace for the `DataModel` class statics.
*/
export namespace DataModel {
/**
* A type alias for the data model row regions.
*/
export type RowRegion = 'body' | 'column-header';
/**
* A type alias for the data model column regions.
*/
export type ColumnRegion = 'body' | 'row-header';
/**
* A type alias for the data model cell regions.
*/
export type CellRegion =
| 'body'
| 'row-header'
| 'column-header'
| 'corner-header';
/**
* The metadata for a column in a data model.
*/
export type Metadata = { [key: string]: any };
/**
* A singleton empty metadata object.
*/
export const emptyMetadata: Metadata = Object.freeze({});
/**
* An arguments object for the `changed` signal.
*
* #### Notes
* Data models should emit the `changed` signal with this args object
* type when rows are inserted or removed.
*/
export type RowsChangedArgs = {
/**
* The discriminated type of the args object.
*/
readonly type: 'rows-inserted' | 'rows-removed';
/**
* The region which contains the modified rows.
*/
readonly region: RowRegion;
/**
* The index of the first modified row.
*/
readonly index: number;
/**
* The number of modified rows.
*/
readonly span: number;
};
/**
* An arguments object for the `changed` signal.
*
* #### Notes
* Data models should emit the `changed` signal with this args object
* type when columns are inserted or removed.
*/
export type ColumnsChangedArgs = {
/**
* The discriminated type of the args object.
*/
readonly type: 'columns-inserted' | 'columns-removed';
/**
* The region which contains the modified columns.
*/
readonly region: ColumnRegion;
/**
* The index of the first modified column.
*/
readonly index: number;
/**
* The number of modified columns.
*/
readonly span: number;
};
/**
* An arguments object for the `changed` signal.
*
* #### Notes
* Data models should emit the `changed` signal with this args object
* type when rows are moved.
*/
export type RowsMovedArgs = {
/**
* The discriminated type of the args object.
*/
readonly type: 'rows-moved';
/**
* The region which contains the modified rows.
*/
readonly region: RowRegion;
/**
* The starting index of the first modified row.
*/
readonly index: number;
/**
* The number of modified rows.
*/
readonly span: number;
/**
* The ending index of the first modified row.
*/
readonly destination: number;
};
/**
* An arguments object for the `changed` signal.
*
* #### Notes
* Data models should emit the `changed` signal with this args object
* type when columns are moved.
*/
export type ColumnsMovedArgs = {
/**
* The discriminated type of the args object.
*/
readonly type: 'columns-moved';
/**
* The region which contains the modified columns.
*/
readonly region: ColumnRegion;
/**
* The starting index of the first modified column.
*/
readonly index: number;
/**
* The number of modified columns.
*/
readonly span: number;
/**
* The ending index of the first modified column.
*/
readonly destination: number;
};
/**
* An arguments object for the `changed` signal.
*
* #### Notes
* Data models should emit the `changed` signal with this args object
* type when cells are changed in-place.
*/
export type CellsChangedArgs = {
/**
* The discriminated type of the args object.
*/
readonly type: 'cells-changed';
/**
* The region which contains the modified cells.
*/
readonly region: CellRegion;
/**
* The row index of the first modified cell.
*/
readonly row: number;
/**
* The column index of the first modified cell.
*/
readonly column: number;
/**
* The number of rows in the modified cell range.
*/
readonly rowSpan: number;
/**
* The number of columns in the modified cell range.
*/
readonly columnSpan: number;
};
/**
* An arguments object for the `changed` signal.
*
* #### Notes
* Data models should emit the `changed` signal with this args object
* type when the model has changed in a fashion that cannot be easily
* expressed by the other args object types.
*
* This is the "big hammer" approach, and will cause any associated
* data grid to perform a full reset. The other changed args types
* should be used whenever possible.
*/
export type ModelResetArgs = {
/**
* The discriminated type of the args object.
*/
readonly type: 'model-reset';
};
/**
* A type alias for the args objects of the `changed` signal.
*/
export type ChangedArgs =
| RowsChangedArgs
| ColumnsChangedArgs
| RowsMovedArgs
| ColumnsMovedArgs
| CellsChangedArgs
| ModelResetArgs;
}