Debian Codegraph @

js/core/view.ts

raw
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 { ReadonlyJSONObject } from '@lumino/coreutils';
10
11 import { DataModel } from '@lumino/datagrid';
12 import { DataSource } from '../datasource';
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 View {
21 /**
22 * Create a view with static JSON data.
23 *
24 * @param datasource - The datasource for initializing the view.
25 */
26 constructor(datasource: DataSource | Readonly<DataSource>) {
27 const split = Private.splitFields(datasource.schema);
28 this._data = datasource;
29 this._bodyFields = split.bodyFields;
30 this._headerFields = split.headerFields;
31 this._missingValues = Private.createMissingMap(datasource.schema);
32 }
33
34 /**
35 * Get the row count for a region in the view.
36 *
37 * @param region - The row region of interest.
38 *
39 * @returns - The row count for the region.
40 */
41 rowCount(region: DataModel.RowRegion): number {
42 if (region === 'body') {
43 return this._data.length;
44 } else {
45 return this._bodyFields[0]?.rows.length ?? 1;
46 }
47 }
48
49 /**
50 * Get the column count for a region in the view.
51 *
52 * @param region - The column region of interest.
53 *
54 * @returns - The column count for the region.
55 */
56 columnCount(region: DataModel.ColumnRegion): number {
57 if (region === 'body') {
58 return this._bodyFields.length;
59 }
60 return this._headerFields.length;
61 }
62
63 /**
64 * Get the metadata for a column in the view.
65 *
66 * @param region - The cell region of interest.
67 *
68 * @param column - The index of the column of interest.
69 *
70 * @returns The metadata for the column.
71 */
72 metadata(
73 region: DataModel.CellRegion,
74 row: number,
75 column: number,
76 ): DataModel.Metadata {
77 if (region === 'body' || region === 'column-header') {
78 return { row: row, column: column, ...this._bodyFields[column] };
79 }
80 return { row: row, column: column, ...this._headerFields[column] };
81 }
82
83 /**
84 * Get the data value for a cell in the view.
85 *
86 * @param region - The cell region of interest.
87 *
88 * @param row - The row index of the cell of interest.
89 *
90 * @param column - The column index of the cell of interest.
91 *
92 * @param returns - The data value for the specified cell.
93 *
94 * #### Notes
95 * A `missingValue` as defined by the schema is converted to `null`.
96 */
97 data(region: DataModel.CellRegion, row: number, column: number): any {
98 // Set up the field and value variables.
99
100 let field: DataSource.IField;
101 let value: any;
102
103 // Look up the field and value for the region.
104 switch (region) {
105 case 'body':
106 field = this._bodyFields[column];
107 value = this._data.data[field.name][row];
108 break;
109 case 'column-header':
110 field = this._bodyFields[column];
111 value = field.rows[row];
112 break;
113 case 'row-header':
114 field = this._headerFields[column];
115 value = this._data.data[field.name][row];
116 break;
117 case 'corner-header':
118 field = this._headerFields[column];
119 value = field.rows[row];
120 break;
121 default:
122 throw 'unreachable';
123 }
124
125 // Test whether the value is a missing value.
126 const missing =
127 this._missingValues !== null &&
128 typeof value === 'string' &&
129 this._missingValues[value] === true;
130
131 // Return the final value.
132 return missing ? null : value;
133 }
134
135 /**
136 * Returns a reference to the dataset from this View.
137 */
138 get dataset(): DataSource | Readonly<DataSource> {
139 return this._data;
140 }
141
142 /**
143 * Returns the index in the schema that relates to the index by region.
144 *
145 * @param region - The `CellRegion` of interest.
146 *
147 * @param index - The column index to look up.
148 */
149 getSchemaIndex(region: DataModel.CellRegion, index: number): number {
150 if (region === 'corner-header') {
151 return index;
152 } else {
153 return this._headerFields.length + index;
154 }
155 }
156
157 /**
158 * Returns a Promise that resolves to an array of unique values contained in
159 * the provided column index.
160 *
161 * @param column - The column to retrieve unique values for.
162 */
163 uniqueValues(region: DataModel.CellRegion, column: string): any[] {
164 return Array.from(new Set(this.dataset.data[column]));
165 }
166
167 protected readonly _data: DataSource | Readonly<DataSource>;
168 protected readonly _bodyFields: DataSource.IField[];
169 protected readonly _headerFields: DataSource.IField[];
170 protected readonly _missingValues: Private.MissingValuesMap | null;
171 }
172
173 /**
174 * The namespace for the `View` class statics.
175 */
176 export namespace View {
177 /**
178 * An options object for initializing a view.
179 */
180 export interface IOptions {
181 /**
182 * The data source for the view.
183 *
184 * The data model takes full ownership of the data source.
185 */
186 data: DataSource;
187 }
188 }
189
190 /**
191 * The namespace for the module implementation details.
192 */
193 namespace Private {
194 /**
195 * An object which holds the results of splitting schema fields.
196 */
197 export interface ISplitFieldsResult {
198 /**
199 * The non-primary key fields to use for the grid body.
200 */
201 bodyFields: DataSource.IField[];
202
203 /**
204 * The primary key fields to use for the grid headers.
205 */
206 headerFields: DataSource.IField[];
207 }
208
209 /**
210 * Split the schema fields into header and body fields.
211 */
212 export function splitFields(schema: DataSource.ISchema): ISplitFieldsResult {
213 // Normalize the primary keys.
214 let primaryKeys: string[];
215 if (schema.primaryKey === undefined) {
216 primaryKeys = [];
217 } else if (typeof schema.primaryKey === 'string') {
218 primaryKeys = [schema.primaryKey];
219 } else {
220 primaryKeys = schema.primaryKey;
221 }
222 // Separate the fields for the body and header.
223 const bodyFields: DataSource.IField[] = [];
224 const headerFields: DataSource.IField[] = [];
225 for (const field of schema.fields) {
226 // Skipping the primary key unique identifier so
227 // it is not rendered.
228 if (field.rows[0] == schema.primaryKeyUuid) {
229 continue;
230 }
231 if (primaryKeys.indexOf(field.name) === -1) {
232 bodyFields.push(field);
233 } else {
234 headerFields.push(field);
235 }
236 }
237
238 // Return the separated fields.
239 return { bodyFields, headerFields };
240 }
241
242 /**
243 * A type alias for a missing value map.
244 */
245 export type MissingValuesMap = { [key: string]: boolean };
246
247 /**
248 * Create a missing values map for a schema.
249 *
250 * This returns `null` if there are no missing values.
251 */
252 export function createMissingMap(
253 schema: DataSource.ISchema,
254 ): MissingValuesMap | null {
255 // Bail early if there are no missing values.
256 if (!schema.missingValues || schema.missingValues.length === 0) {
257 return null;
258 }
259
260 // Collect the missing values into a map.
261 const result: MissingValuesMap = Object.create(null);
262 for (const value of schema.missingValues) {
263 result[value] = true;
264 }
265
266 // Return the populated map.
267 return result;
268 }
269 }