| 1 |
import { DataModel } from '@lumino/datagrid'; |
| 2 |
import { deserialize_data_simple } from './deserialize'; |
| 3 |
import { StreamingView } from './streamingview'; |
| 4 |
import { TransformStateManager } from './transformStateManager'; |
| 5 |
import { ViewBasedJSONModel } from './viewbasedjsonmodel'; |
| 6 |
import { DataGridModel } from '../datagrid'; |
| 7 |
import { DataSource } from '../datasource'; |
| 8 |
|
| 9 |
interface IUnique { |
| 10 |
region: DataModel.CellRegion; |
| 11 |
column: string; |
| 12 |
values: any[]; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* A view based data model implementation for in-memory JSON data. |
| 17 |
*/ |
| 18 |
export class StreamingViewBasedJSONModel extends ViewBasedJSONModel { |
| 19 |
constructor(options: StreamingViewBasedJSONModel.IOptions) { |
| 20 |
super(options); |
| 21 |
} |
| 22 |
|
| 23 |
setModelRangeData( |
| 24 |
r1: number, |
| 25 |
r2: number, |
| 26 |
c1: number, |
| 27 |
c2: number, |
| 28 |
value: DataSource, |
| 29 |
) { |
| 30 |
this._currentView.setDataRange(r1, r2, c1, c2, value); |
| 31 |
|
| 32 |
this.emitChanged({ |
| 33 |
type: 'cells-changed', |
| 34 |
region: 'body', |
| 35 |
row: r1, |
| 36 |
column: c1, |
| 37 |
rowSpan: r2 - r1 + 1, |
| 38 |
columnSpan: c2 - c1 + 1, |
| 39 |
}); |
| 40 |
|
| 41 |
this.emitChanged({ |
| 42 |
type: 'cells-changed', |
| 43 |
region: 'row-header', |
| 44 |
row: r1, |
| 45 |
column: 0, |
| 46 |
rowSpan: r2 - r1 + 1, |
| 47 |
columnSpan: this._currentView.columnCount('row-header'), |
| 48 |
}); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns a Promise that resolves to an array of unique values contained in |
| 53 |
* the provided column index. |
| 54 |
* |
| 55 |
* @param region - The CellRegion to retrieve unique values for. |
| 56 |
* @param column - The column to retrieve unique values for. |
| 57 |
*/ |
| 58 |
uniqueValues(region: DataModel.CellRegion, column: string): Promise<any[]> { |
| 59 |
if ( |
| 60 |
this._unique && |
| 61 |
region == this._unique.region && |
| 62 |
column == this._unique.column |
| 63 |
) { |
| 64 |
return Promise.resolve(this._unique.values); |
| 65 |
} |
| 66 |
|
| 67 |
const promise = new Promise<any>(resolve => { |
| 68 |
this._dataModel.once('msg:custom', (content, buffers) => { |
| 69 |
if (content.event_type === 'unique-values-reply') { |
| 70 |
const { value } = content; |
| 71 |
|
| 72 |
// Bring back buffers at their original position in the data structure |
| 73 |
for (const col of Object.keys(value.data)) { |
| 74 |
if (value.data[col].type !== 'raw') { |
| 75 |
value.data[col].value = buffers[value.data[col].value]; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
const deserialized = deserialize_data_simple(value, null); |
| 80 |
const values = deserialized[content.column]; |
| 81 |
|
| 82 |
this._unique = { region, column: content.column, values }; |
| 83 |
resolve(this._unique.values); |
| 84 |
} |
| 85 |
}); |
| 86 |
}); |
| 87 |
|
| 88 |
const msg = { type: 'unique-values-request', column: column }; |
| 89 |
this._dataModel.send(msg); |
| 90 |
return promise; |
| 91 |
} |
| 92 |
|
| 93 |
updateDataset(options: StreamingViewBasedJSONModel.IOptions): void { |
| 94 |
this._dataModel = options.dataModel; |
| 95 |
this._dataset = options.datasource; |
| 96 |
this._updatePrimaryKeyMap(); |
| 97 |
const view = new StreamingView({ |
| 98 |
datasource: this._dataset, |
| 99 |
rowCount: options.rowCount, |
| 100 |
}); |
| 101 |
this.currentView = view; |
| 102 |
} |
| 103 |
|
| 104 |
updateCellValue(options: ViewBasedJSONModel.IUpdateCellValuesOptions): void { |
| 105 |
if (options.region != 'body') { |
| 106 |
return; |
| 107 |
} |
| 108 |
this._currentView.setData(options.value, options.row, options.column); |
| 109 |
} |
| 110 |
|
| 111 |
get currentView(): StreamingView { |
| 112 |
return this._currentView; |
| 113 |
} |
| 114 |
|
| 115 |
set currentView(view: StreamingView) { |
| 116 |
super.currentView = view; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Handler for transformState.changed events. |
| 121 |
* |
| 122 |
* @param sender - TransformStateManager |
| 123 |
* |
| 124 |
* @param value - Event. |
| 125 |
*/ |
| 126 |
protected _transformStateChangedHandler( |
| 127 |
sender: TransformStateManager, |
| 128 |
value: TransformStateManager.IEvent, |
| 129 |
) { |
| 130 |
this._transformSignal.emit(value); |
| 131 |
} |
| 132 |
|
| 133 |
protected _currentView: StreamingView; |
| 134 |
protected _dataModel: DataGridModel; |
| 135 |
protected _unique?: IUnique; |
| 136 |
} |
| 137 |
|
| 138 |
export namespace StreamingViewBasedJSONModel { |
| 139 |
/** |
| 140 |
* An options object for initializing the data model. |
| 141 |
*/ |
| 142 |
export interface IOptions extends ViewBasedJSONModel.IOptions { |
| 143 |
/** |
| 144 |
* The row number of the grid. |
| 145 |
*/ |
| 146 |
rowCount: number; |
| 147 |
|
| 148 |
dataModel: DataGridModel; |
| 149 |
} |
| 150 |
} |