Debian Codegraph @

js/core/viewbasedjsonmodel.ts

raw
1 import { CellGroup, DataModel, MutableDataModel } from '@lumino/datagrid';
2
3 import { ReadonlyJSONValue } from '@lumino/coreutils';
4
5 import { ISignal, Signal } from '@lumino/signaling';
6
7 import { Transform } from './transform';
8
9 import { View } from './view';
10
11 import { TransformStateManager } from './transformStateManager';
12
13 import { ArrayUtils } from '../utils';
14 import { DataSource } from '../datasource';
15
16 /**
17 * A view based data model implementation for in-memory JSON data.
18 */
19 export class ViewBasedJSONModel extends MutableDataModel {
20 /**
21 * Create a data model with static JSON data.
22 *
23 * @param options - The options for creating the data model.
24 */
25 constructor(options: ViewBasedJSONModel.IOptions) {
26 super();
27 this.updateDataset(options);
28
29 this._transformState = new TransformStateManager();
30 this._transformState.changed.connect((sender, value) => {
31 // Repaint grid on transform state update
32 // Note: This will also result in the `model-reset` signal being sent.
33 this._transformStateChangedHandler(sender, value);
34 });
35 // first run: generate a list of indices corresponding
36 // to the locations of multi-index arrays.
37 const multiIndexArrayLocations =
38 ArrayUtils.generateMultiIndexArrayLocations(this);
39 // second run: map the index locations generated above to
40 // the dataset so we have access to the multi index arrays
41 // only.
42 let mergedColumnLocations = ArrayUtils.generateColMergedCellLocations(
43 this,
44 multiIndexArrayLocations,
45 );
46 // final run: we need to check that the merging hierarchy makes sense. i.e. we don't
47 // want to render a merged range below a non-merged range. This function will check
48 // that this requirement is met. If it is not, we simply render each cell individually
49 // as if it wasn't grouped.
50 if (!ArrayUtils.validateMergingHierarchy(mergedColumnLocations)) {
51 mergedColumnLocations = [];
52 }
53 this._mergedColumnCellLocations = mergedColumnLocations;
54
55 // Creating column merged cell groups from index locations
56 this._columnCellGroups = ArrayUtils.generateColumnCellGroups(
57 this._mergedColumnCellLocations,
58 );
59
60 // Creating merged row cell groups
61 let mergedRowLocations = ArrayUtils.generateRowMergedCellLocations(this);
62 if (!ArrayUtils.validateMergingHierarchy(mergedRowLocations)) {
63 mergedRowLocations = [];
64 }
65 this._mergedRowCellLocations = mergedRowLocations;
66 this._rowCellGroups = ArrayUtils.generateRowCellGroups(
67 this._mergedRowCellLocations,
68 );
69 }
70
71 /**
72 * Sets the dataset for this model.
73 *
74 * @param options - The options for creating the model
75 */
76 updateDataset(options: ViewBasedJSONModel.IOptions): void {
77 this._dataset = options.datasource;
78 this._updatePrimaryKeyMap();
79 const view = new View(this._dataset);
80 this.currentView = view;
81 }
82
83 /**
84 * Updates the primary key map, which provides a mapping from primary key
85 * value to row in the full dataset.
86 */
87 protected _updatePrimaryKeyMap(): void {
88 this._primaryKeyMap.clear();
89
90 const primaryKey = this._dataset.schema.primaryKey as string[];
91
92 for (let idx = 0; idx < this._dataset.length; idx++) {
93 const keys = primaryKey.map((key) => this._dataset.data[key][idx]);
94 this._primaryKeyMap.set(JSON.stringify(keys), idx);
95 }
96 }
97
98 areCellsMerged(cell1: number[], cell2: number[]): boolean {
99 const [row2, col2] = cell2;
100
101 const siblings = this.getMergedSiblingCells(cell1);
102
103 for (const sibling of siblings) {
104 if (row2 === sibling[0] && col2 === sibling[1]) {
105 return true;
106 }
107 }
108
109 return false;
110 }
111 /**
112 * Returns a list of [row, column] cells indices forming a merged cell group
113 * @param row row index
114 * @param column column index
115 */
116 getMergedSiblingCells(cell: number[]): any[] {
117 const [row, column] = cell;
118 if (
119 row < 0 ||
120 column < 0 ||
121 row >= this._mergedColumnCellLocations.length
122 ) {
123 return [];
124 }
125
126 for (const cellGroup of this._mergedColumnCellLocations[row]) {
127 for (const rowCell of cellGroup) {
128 const [rowIndex, columnIndex] = rowCell;
129 if (row === rowIndex && column == columnIndex) {
130 return cellGroup;
131 }
132 }
133 }
134 return [];
135 }
136
137 /**
138 * Get the row count for a region in the data model.
139 *
140 * @param region - The row region of interest.
141 *
142 * @returns - The row count for the region.
143 */
144 rowCount(region: DataModel.RowRegion): number {
145 return this.currentView.rowCount(region);
146 }
147
148 /**
149 * Get the column count for a region in the data model.
150 *
151 * @param region - The column region of interest.
152 *
153 * @returns - The column count for the region.
154 */
155 columnCount(region: DataModel.ColumnRegion): number {
156 return this.currentView.columnCount(region);
157 }
158
159 /**
160 * Get the group count for each region
161 * @param region
162 * @returns
163 */
164 groupCount(region: DataModel.RowRegion): number {
165 if (region === 'body') {
166 return 0;
167 } else if (region === 'column-header') {
168 return this._columnCellGroups.length;
169 } else if (region === 'row-header') {
170 return this._rowCellGroups.length;
171 }
172 return 0;
173 }
174
175 /**
176 * Specify merged cell groups for each region
177 * @param region
178 * @param groupIndex
179 * @returns
180 */
181 group(region: DataModel.CellRegion, groupIndex: number): CellGroup | null {
182 if (region === 'column-header') {
183 return this._columnCellGroups[groupIndex];
184 }
185
186 if (region === 'row-header') {
187 return this._rowCellGroups[groupIndex];
188 }
189
190 return null;
191 }
192
193 /**
194 * Get the metadata for a cell in the data model.
195 *
196 * @param region - The cell region of interest.
197 *
198 * @param row - The index of the row of interest.
199 *
200 * @param column - The index of the column of interest.
201 *
202 * @returns The metadata for the cell.
203 */
204 metadata(
205 region: DataModel.CellRegion,
206 row: number,
207 column: number,
208 ): DataModel.Metadata {
209 const md = this.currentView.metadata(region, row, column);
210 if (region == 'body') {
211 md.row = row;
212 md.column = column;
213 md.data = (row: number, column: string) => {
214 const columnIndex = this.columnNameToIndex(column.toString());
215 return this.data('body', row, columnIndex);
216 };
217 }
218 return md;
219 }
220
221 /**
222 * Get the data value for a cell in the data model.
223 *
224 * @param region - The cell region of interest.
225 *
226 * @param row - The row index of the cell of interest.
227 *
228 * @param column - The column index of the cell of interest.
229 *
230 * @param returns - The data value for the specified cell.
231 */
232 data(region: DataModel.CellRegion, row: number, column: number): any {
233 return this.currentView.data(region, row, column);
234 }
235
236 /**
237 * Updates the cell value of the currently displayed View.
238 *
239 * @param region - The cell region of interest.
240 *
241 * @param row - The row index of the cell of interest.
242 *
243 * @param column - The column index of the cell of interest.
244 *
245 * @param value - The new value to update the indicated cell with.
246 *
247 */
248 setData(
249 region: DataModel.CellRegion,
250 row: number,
251 column: number,
252 value: any,
253 ): boolean {
254 const datasetRow = this.getDatasetRowFromView(region, row);
255 this.updateCellValue({
256 region: region,
257 row: datasetRow,
258 column: column,
259 value: value,
260 });
261 this.emitChanged({
262 type: 'cells-changed',
263 region: region,
264 row: row,
265 column: column,
266 rowSpan: 1,
267 columnSpan: 1,
268 });
269
270 return true;
271 }
272
273 /**
274 * Updates the cell value of the currently displayed View.
275 *
276 * @param region - The cell region of interest.
277 *
278 * @param row - The row index of the cell of interest.
279 *
280 * @param column - The column index of the cell of interest.
281 *
282 * @param value - The new value to update the indicated cell with.
283 *
284 */
285 setModelData(
286 region: DataModel.CellRegion,
287 row: number,
288 column: number,
289 value: any,
290 ): boolean {
291 this.updateCellValue({
292 region: region,
293 row: row,
294 column: column,
295 value: value,
296 });
297 this.emitChanged({
298 type: 'cells-changed',
299 region: region,
300 row: row,
301 column: column,
302 rowSpan: 1,
303 columnSpan: 1,
304 });
305
306 return true;
307 }
308
309 /**
310 * Updates the row value of the currently displayed View.
311 *
312 * @param row - The row index of the cell of interest.
313 *
314 * @param value - The new value to update the indicated row with.
315 *
316 */
317 setRowData(row: number, value: any): boolean {
318 this.updateRowValue({
319 row: row,
320 value: value,
321 });
322 this.emitChanged({
323 type: 'cells-changed',
324 region: 'body',
325 row,
326 column: 0,
327 rowSpan: 1,
328 columnSpan: 1,
329 });
330
331 return true;
332 }
333
334 public columnNameToIndex(name: string): number {
335 const schema = this.dataset.schema;
336 const primaryKeysLength = schema.primaryKey.length - 1;
337
338 let index = -1;
339
340 if (schema.primaryKey.includes(name)) {
341 index = schema.primaryKey.indexOf(name);
342 } else {
343 const fields = schema.fields;
344
345 fields.forEach((value, i) => {
346 if (value.name == name) {
347 index = i - primaryKeysLength;
348 }
349 });
350 }
351 return index;
352 }
353
354 public columnIndexToName(
355 index: number,
356 region: DataModel.ColumnRegion,
357 ): string {
358 const schema = this.dataset.schema;
359 if (region == 'row-header') {
360 return schema.primaryKey[index];
361 } else {
362 return schema.fields[schema.primaryKey.length + index - 1].name;
363 }
364 }
365
366 public columnNameToRegion(name: string): DataModel.ColumnRegion {
367 const schema = this.dataset.schema;
368
369 if (schema.primaryKey.includes(name)) {
370 return 'row-header';
371 } else {
372 return 'body';
373 }
374 }
375
376 /**
377 * Get the current View for the model.
378 */
379 get currentView(): View {
380 return this._currentView;
381 }
382
383 /**
384 * Sets the provided View as the current View, then emits a changed signal.
385 */
386 set currentView(view: View) {
387 this._currentView = view;
388
389 this.emitChanged({ type: 'model-reset' });
390
391 const primaryKey = !Array.isArray(this._dataset.schema.primaryKey)
392 ? [this._dataset.schema.primaryKey]
393 : this._dataset.schema.primaryKey;
394
395 const indices: number[] = [];
396
397 for (let idx = 0; idx < view.dataset.length; idx++) {
398 const lookupVal = JSON.stringify(
399 primaryKey.map((key) => view.dataset.data[key][idx]),
400 );
401 const retrievedVal = this._primaryKeyMap.get(lookupVal);
402
403 indices.push(typeof retrievedVal === 'undefined' ? idx : retrievedVal);
404 }
405
406 this.dataSync.emit({
407 type: 'row-indices-updated',
408 indices: indices,
409 });
410 }
411
412 /**
413 * Add a new transform to the currently active transforms.
414 *
415 * @param transform - The transform to be added.
416 */
417 addTransform(transform: Transform.TransformSpec): void {
418 this._transformState.add(transform);
419 }
420
421 /**
422 * Removes the provided transformation from the active state.
423 *
424 * @param column - The column state to be removed.
425 *
426 * @param transformType - The type of the transform to be removed from state.
427 */
428 removeTransform(column: string, transformType: string): void {
429 this._transformState.remove(column, transformType);
430 }
431
432 /**
433 * Apply an array of transformations to the dataset and update the current
434 * View. The provided transforms will replace any existing ones.
435 *
436 * @param transforms - Array of transformations to apply to the dataset
437 */
438 replaceTransforms(transforms: Transform.TransformSpec[]): void {
439 this._transformState.replace(transforms);
440 }
441
442 /**
443 * Removes all active transforms.
444 */
445 clearTransforms(): void {
446 this._transformState.clear();
447 }
448
449 /**
450 * Returns the transform metadata for the provided column.
451 *
452 * @param column - The column of the metadata to be retrieved.
453 */
454 transformMetadata(column: string): TransformStateManager.IColumn | undefined {
455 return this._transformState.metadata(column);
456 }
457
458 /**
459 * Returns a Promise that resolves to an array of unique values contained in
460 * the provided column index.
461 *
462 * @param region - The CellRegion to retrieve unique values for.
463 * @param column - The column to retrieve unique values for.
464 */
465 uniqueValues(region: DataModel.CellRegion, column: string): Promise<any[]> {
466 return Promise.resolve(Array.from(new Set(this.dataset.data[column])));
467 }
468
469 /**
470 * Returns a Promise that resolves to an array of unique values contained in
471 * the provided column index after all transforms have been applied.
472 *
473 * @param region - The CellRegion to retrieve unique values for.
474 * @param column - The column to retrieve unique values for.
475 */
476 async uniqueValuesVisible(
477 region: DataModel.CellRegion,
478 column: string,
479 ): Promise<any[]> {
480 return this._currentView.uniqueValues(region, column);
481 }
482
483 get transformStateChanged(): ISignal<this, TransformStateManager.IEvent> {
484 return this._transformSignal;
485 }
486
487 /**
488 * Returns the active transforms for the current model
489 */
490 get activeTransforms(): Transform.TransformSpec[] {
491 return this._transformState.activeTransforms;
492 }
493
494 getFilterTransform(column: string): Transform.TransformSpec | undefined {
495 return this._transformState.getFilterTransform(column);
496 }
497
498 /**
499 * Updates the indicated value in the dataset.
500 *
501 * Note: provided row/column values should correspond to the currently
502 * active View, not the full dataset.
503 * Note: Currently, only updating `body` cells is supported.
504 *
505 * @param options - The options for this method.
506 */
507 getDatasetRowFromView(region: DataModel.CellRegion, row: number): number {
508 if (region == 'column-header' || region == 'corner-header') {
509 return row;
510 }
511 // Get the index of the row in the full dataset to be updated
512 const primaryKey = Array.isArray(this._dataset.schema.primaryKey)
513 ? this._dataset.schema.primaryKey
514 : [this._dataset.schema.primaryKey];
515
516 const keyValues = primaryKey.map(
517 (key) => this._currentView.dataset.data[key][row],
518 );
519
520 const lookupIndex: number = this._primaryKeyMap.get(
521 JSON.stringify(keyValues),
522 )!;
523
524 return lookupIndex;
525 }
526
527 /**
528 * Updates a value in the full dataset of the model.
529 *
530 * @param options - The options for this function.
531 */
532 updateCellValue(options: ViewBasedJSONModel.IUpdateCellValuesOptions): void {
533 // Bail if cell region isn't the body
534 // TODO: Support modifying the schema
535 if (options.region !== 'body') {
536 return;
537 }
538 const columnName = this.metadata('body', 0, options.column)['name'];
539 this._dataset.data[columnName][options.row] = options.value;
540
541 // Notify listeners of the cell change event
542 this.dataSync.emit({
543 type: 'cell-edit-event',
544 column: columnName,
545 columnIndex: options.column,
546 row: options.row,
547 region: options.region,
548 value: options.value,
549 });
550 // We need to rerun the transforms, as the changed cell may change the order
551 // or visibility of other rows
552 this.currentView = this._transformState.createView(this._dataset);
553 }
554
555 /**
556 * Updates a row in the full dataset of the model.
557 *
558 * @param options - The options for this function.
559 */
560 updateRowValue(options: ViewBasedJSONModel.IUpdateRowValuesOptions): void {
561 // Create new row and add it to new dataset
562 for (const columnIndex of Array(options.value.length).keys()) {
563 const columnName = this.metadata('body', 0, columnIndex)['name'];
564 this._dataset.data[columnName][options.row] = options.value[columnIndex];
565 }
566 // We need to rerun the transforms, as the changed cells may change the order
567 // or visibility of other rows
568 this.currentView = this._transformState.createView(this._dataset);
569 }
570
571 /**
572 * A signal emitted when the data model has changes to sync to the kernel.
573 */
574 get dataSync(): Signal<this, ViewBasedJSONModel.IDataSyncEvent> {
575 return this._dataSyncSignal;
576 }
577
578 /**
579 * Returns the current full dataset.
580 */
581 get dataset(): DataSource {
582 return this._dataset;
583 }
584
585 /**
586 * Returns the index in the schema that relates to the index by region.
587 *
588 * @param region - The `CellRegion` of interest.
589 *
590 * @param index - The column index to look up.
591 */
592 getSchemaIndex(region: DataModel.CellRegion, index: number): number {
593 return this.currentView.getSchemaIndex(region, index);
594 }
595
596 /**
597 * Handler for transformState.changed events.
598 *
599 * @param sender - TransformStateManager
600 *
601 * @param value - Event.
602 */
603 protected _transformStateChangedHandler(
604 sender: TransformStateManager,
605 value: TransformStateManager.IEvent,
606 ) {
607 this.currentView = this._transformState.createView(this._dataset);
608 this._transformSignal.emit(value);
609 }
610
611 protected _currentView: View;
612 protected _transformSignal = new Signal<this, TransformStateManager.IEvent>(
613 this,
614 );
615 private _dataSyncSignal = new Signal<this, ViewBasedJSONModel.IDataSyncEvent>(
616 this,
617 );
618 private _primaryKeyMap: Map<ReadonlyJSONValue, number> = new Map();
619
620 protected _dataset: DataSource;
621 protected readonly _transformState: TransformStateManager;
622 private _mergedColumnCellLocations: any[];
623 private _mergedRowCellLocations: any[];
624 private _rowCellGroups: CellGroup[];
625 private _columnCellGroups: CellGroup[];
626 }
627
628 /**
629 * The namespace for the `ViewBasedJSONModel` class statics.
630 */
631 export namespace ViewBasedJSONModel {
632 export interface IOptions {
633 /**
634 * The `DataSource` for the model.
635 */
636 datasource: DataSource;
637 }
638
639 export interface IUpdateCellValuesOptions {
640 /**
641 * The `CellRegion` of the cell to be updated.
642 */
643 region: DataModel.CellRegion;
644
645 /**
646 * The index of the target row in the current view.
647 */
648 column: number;
649
650 /**
651 * The index of the target row in the current view.
652 */
653 row: number;
654
655 /**
656 * The new value to replace the old one.
657 */
658 value: any;
659 }
660
661 export interface IUpdateRowValuesOptions {
662 /**
663 * The index of the target row in the current view.
664 */
665 row: number;
666
667 /**
668 * The new value to replace the old one.
669 */
670 value: any[];
671 }
672
673 export type IDataSyncEvent = ISyncRowIndices | ICellEditEvent;
674
675 export interface ISyncRowIndices {
676 /**
677 * The discriminated type of the args object.
678 */
679 type: 'row-indices-updated';
680
681 /**
682 * An list of the rows in the untransformed dataset that are currently
683 * represented in the `View`.
684 */
685 indices: number[];
686 }
687
688 export interface ICellEditEvent {
689 /**
690 * The discriminated type of the args object.
691 */
692 type: 'cell-edit-event';
693
694 /**
695 * The CellRegion associated with this change.
696 */
697 region: DataModel.CellRegion;
698
699 /**
700 * The row number associated with this change.
701 */
702 row: number;
703
704 /**
705 * The column name associated with this change.
706 */
707 column: string;
708
709 /**
710 * TODO Deprecate this
711 * The column index associated with this change.
712 */
713 columnIndex: number;
714
715 /**
716 * The new data value
717 */
718 value: ReadonlyJSONValue;
719 }
720 }