| 1 |
/*----------------------------------------------------------------------------- |
| 2 |
| Copyright (c) 2014-2017, PhosphorJS Contributors |
| 3 |
| |
| 4 |
| Distributed under the terms of the BSD 3-Clause License. |
| 5 |
| |
| 6 |
| The full license is in the file LICENSE, distributed with this software. |
| 7 |
|----------------------------------------------------------------------------*/ |
| 8 |
|
| 9 |
import { DataModel } from '@lumino/datagrid'; |
| 10 |
import { View } from './view'; |
| 11 |
import { DataSource } from '../datasource'; |
| 12 |
import { Dict } from '@jupyter-widgets/base'; |
| 13 |
|
| 14 |
/** |
| 15 |
* A View implementation for immutable in-memory JSON data. |
| 16 |
* |
| 17 |
* Note: Most of this is just repurposed from JSONModel, and can likely be |
| 18 |
* streamlined quite a bit. |
| 19 |
*/ |
| 20 |
export class StreamingView extends View { |
| 21 |
/** |
| 22 |
* Create a view with streaming data. |
| 23 |
* |
| 24 |
* @param options - The datasource for initializing the view. |
| 25 |
*/ |
| 26 |
constructor(options: StreamingView.IOptions) { |
| 27 |
super(options.datasource); |
| 28 |
|
| 29 |
this._rowCount = options.rowCount; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get the row count for a region in the view. |
| 34 |
* |
| 35 |
* @param region - The row region of interest. |
| 36 |
* |
| 37 |
* @returns - The row count for the region. |
| 38 |
*/ |
| 39 |
rowCount(region: DataModel.RowRegion): number { |
| 40 |
if (region === 'body') { |
| 41 |
return this._rowCount; |
| 42 |
} else { |
| 43 |
return this._bodyFields[0].rows.length; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get the data value for a cell in the view. |
| 49 |
* |
| 50 |
* @param region - The cell region of interest. |
| 51 |
* |
| 52 |
* @param row - The row index of the cell of interest. |
| 53 |
* |
| 54 |
* @param column - The column index of the cell of interest. |
| 55 |
* |
| 56 |
* @param returns - The data value for the specified cell. |
| 57 |
* |
| 58 |
* #### Notes |
| 59 |
* A `missingValue` as defined by the schema is converted to `null`. |
| 60 |
*/ |
| 61 |
data(region: DataModel.CellRegion, row: number, column: number): any { |
| 62 |
if (region == 'body') { |
| 63 |
const field = this._bodyFields[column]; |
| 64 |
|
| 65 |
if (this._streamed_data[field.name] !== undefined) { |
| 66 |
const value = this._streamed_data[field.name][row]; |
| 67 |
return value !== undefined ? value : '...'; |
| 68 |
} |
| 69 |
|
| 70 |
return '...'; |
| 71 |
} |
| 72 |
|
| 73 |
if (region == 'row-header') { |
| 74 |
const field = this._headerFields[column]; |
| 75 |
|
| 76 |
if (this._streamed_data[field.name] !== undefined) { |
| 77 |
const value = this._streamed_data[field.name][row]; |
| 78 |
return value !== undefined ? value : '...'; |
| 79 |
} |
| 80 |
|
| 81 |
return '...'; |
| 82 |
} |
| 83 |
|
| 84 |
return super.data(region, row, column); |
| 85 |
} |
| 86 |
|
| 87 |
setDataRange( |
| 88 |
r1: number, |
| 89 |
r2: number, |
| 90 |
c1: number, |
| 91 |
c2: number, |
| 92 |
value: DataSource, |
| 93 |
) { |
| 94 |
// Columns need to be set some time, not sure if this is the best place. |
| 95 |
this._data.setColumns(value.columns); |
| 96 |
|
| 97 |
let field: DataSource.IField; |
| 98 |
|
| 99 |
// Update body |
| 100 |
for (let column = c1; column <= c2; column++) { |
| 101 |
field = this._bodyFields[column]; |
| 102 |
|
| 103 |
if (this._streamed_data[field.name] === undefined) { |
| 104 |
this._streamed_data[field.name] = []; |
| 105 |
} |
| 106 |
|
| 107 |
let r = 0; |
| 108 |
|
| 109 |
for (let row = r1; row <= r2; row++) { |
| 110 |
this._streamed_data[field.name][row] = value.data[field.name][r]; |
| 111 |
r++; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// Update header fields |
| 116 |
for (let column = 0; column < this._headerFields.length; column++) { |
| 117 |
field = this._headerFields[column]; |
| 118 |
|
| 119 |
if (this._streamed_data[field.name] === undefined) { |
| 120 |
this._streamed_data[field.name] = []; |
| 121 |
} |
| 122 |
|
| 123 |
let r = 0; |
| 124 |
for (let row = r1; row <= r2; row++) { |
| 125 |
this._streamed_data[field.name][row] = value.data[field.name][r]; |
| 126 |
r++; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
setData(value: any, row: number, column: number) { |
| 132 |
const field = this._bodyFields[column]; |
| 133 |
|
| 134 |
if (this._streamed_data[field.name] === undefined) { |
| 135 |
this._streamed_data[field.name] = []; |
| 136 |
} |
| 137 |
|
| 138 |
this._streamed_data[field.name][row] = value; |
| 139 |
} |
| 140 |
|
| 141 |
setRowCount(rowCount: number) { |
| 142 |
this._rowCount = rowCount; |
| 143 |
} |
| 144 |
|
| 145 |
private _streamed_data: Dict<any[]> = {}; |
| 146 |
private _rowCount: number; |
| 147 |
} |
| 148 |
|
| 149 |
export namespace StreamingView { |
| 150 |
export interface IOptions { |
| 151 |
/** |
| 152 |
* The datasource. |
| 153 |
*/ |
| 154 |
datasource: DataSource; |
| 155 |
|
| 156 |
/** |
| 157 |
* The row number of the grid. |
| 158 |
*/ |
| 159 |
rowCount: number; |
| 160 |
} |
| 161 |
} |