Debian Codegraph @

packages/datagrid/src/celleditorcontroller.ts

raw
1 /*
2 * Copyright (c) Jupyter Development Team.
3 * Distributed under the terms of the Modified BSD License.
4 */
5
6 /*-----------------------------------------------------------------------------
7 | Copyright (c) 2014-2019, PhosphorJS Contributors
8 |
9 | Distributed under the terms of the BSD 3-Clause License.
10 |
11 | The full license is in the file LICENSE, distributed with this software.
12 |----------------------------------------------------------------------------*/
13 import {
14 BooleanCellEditor,
15 CellDataType,
16 CellEditor,
17 DateCellEditor,
18 DynamicOptionCellEditor,
19 ICellEditOptions,
20 ICellEditor,
21 ICellEditResponse,
22 IntegerCellEditor,
23 NumberCellEditor,
24 OptionCellEditor,
25 TextCellEditor
26 } from './celleditor';
27
28 import { CellGroup } from './cellgroup';
29
30 import { DataModel, MutableDataModel } from './datamodel';
31
32 /**
33 * A type alias for cell editor override identifier.
34 */
35 export type EditorOverrideIdentifier =
36 | CellDataType
37 | DataModel.Metadata
38 | 'default';
39
40 /**
41 * An object which manages cell editing.
42 */
43 export interface ICellEditorController {
44 /**
45 * Override cell editor for the cells matching the identifier.
46 *
47 * @param identifier - Cell identifier to use when matching cells.
48 * if identifier is a CellDataType, then cell matching is done using data type of the cell,
49 * if identifier is a Metadata, then partial match of the cell metadata with identifier is used for match,
50 * if identifier is 'default' then override is used as default editor when no other editor is found suitable
51 *
52 * @param editor - The cell editor to use or resolver to use to get an editor for matching cells.
53 */
54 setEditor(
55 identifier: EditorOverrideIdentifier,
56 editor: ICellEditor | Resolver
57 ): void;
58 /**
59 * Start editing a cell.
60 *
61 * @param cell - The object holding cell configuration data.
62 *
63 * @param options - The cell editing options.
64 */
65 edit(cell: CellEditor.CellConfig, options?: ICellEditOptions): boolean;
66 /**
67 * Cancel editing.
68 */
69 cancel(): void;
70 }
71
72 /**
73 * A type alias for a cell editor config function.
74 *
75 * This type is used to compute a value from a cell config object.
76 */
77 export type ConfigFunc<T> = (config: CellEditor.CellConfig) => T;
78
79 /**
80 * A type alias for a cell editor config option.
81 *
82 * A config option can be a static value or a config function.
83 */
84 export type ConfigOption<T> = T | ConfigFunc<T>;
85
86 /**
87 * A type alias for a cell editor resolver function.
88 */
89 export type Resolver = ConfigFunc<ICellEditor | undefined>;
90
91 /**
92 * Resolve a config option for a cell editor.
93 *
94 * @param option - The config option to resolve.
95 *
96 * @param config - The cell config object.
97 *
98 * @returns The resolved value for the option.
99 */
100 export function resolveOption<T>(
101 option: ConfigOption<T>,
102 config: CellEditor.CellConfig
103 ): T {
104 return typeof option === 'function'
105 ? (option as ConfigFunc<T>)(config)
106 : option;
107 }
108
109 /**
110 * An object which manages cell editing. It stores editor overrides,
111 * decides which editor to use for a cell, makes sure there is only one editor active.
112 */
113 export class CellEditorController implements ICellEditorController {
114 /**
115 * Override cell editor for the cells matching the identifier.
116 *
117 * @param identifier - Cell identifier to use when matching cells.
118 * if identifier is a CellDataType, then cell matching is done using data type of the cell,
119 * if identifier is a Metadata, then partial match of the cell metadata with identifier is used for match,
120 * if identifier is 'default' then override is used as default editor when no other editor is found suitable
121 *
122 * @param editor - The cell editor to use or resolver to use to get an editor for matching cells.
123 */
124 setEditor(
125 identifier: EditorOverrideIdentifier,
126 editor: ICellEditor | Resolver
127 ): void {
128 if (typeof identifier === 'string') {
129 this._typeBasedOverrides.set(identifier, editor);
130 } else {
131 const key = this._metadataIdentifierToKey(identifier);
132 this._metadataBasedOverrides.set(key, [identifier, editor]);
133 }
134 }
135
136 /**
137 * Start editing a cell.
138 *
139 * @param cell - The object holding cell configuration data.
140 *
141 * @param options - The cell editing options.
142 */
143 edit(cell: CellEditor.CellConfig, options?: ICellEditOptions): boolean {
144 const grid = cell.grid;
145
146 if (!grid.editable) {
147 console.error('Grid cannot be edited!');
148 return false;
149 }
150
151 this.cancel();
152
153 this._cell = cell;
154
155 options = options || {};
156 options.onCommit = options.onCommit || this._onCommit.bind(this);
157 options.onCancel = options.onCancel || this._onCancel.bind(this);
158
159 // if an editor is passed in with options, then use it for editing
160 if (options.editor) {
161 this._editor = options.editor;
162 options.editor.edit(cell, options);
163 return true;
164 }
165
166 // choose an editor based on overrides / cell data type
167 const editor = this._getEditor(cell);
168 if (editor) {
169 this._editor = editor;
170 editor.edit(cell, options);
171 return true;
172 }
173
174 return false;
175 }
176
177 /**
178 * Cancel editing.
179 */
180 cancel(): void {
181 if (this._editor) {
182 this._editor.cancel();
183 this._editor = null;
184 }
185
186 this._cell = null;
187 }
188
189 private _onCommit(response: ICellEditResponse): void {
190 const cell = this._cell;
191
192 if (!cell) {
193 return;
194 }
195
196 const grid = cell.grid;
197 const dataModel = grid.dataModel as MutableDataModel;
198 let row = cell.row;
199 let column = cell.column;
200
201 const cellGroup = CellGroup.getGroup(grid.dataModel!, 'body', row, column);
202 if (cellGroup) {
203 row = cellGroup.r1;
204 column = cellGroup.c1;
205 }
206
207 dataModel.setData('body', row, column, response.value);
208 grid.viewport.node.focus();
209 if (response.cursorMovement !== 'none') {
210 grid.moveCursor(response.cursorMovement);
211 grid.scrollToCursor();
212 }
213 }
214
215 private _onCancel(): void {
216 if (!this._cell) {
217 return;
218 }
219
220 this._cell.grid.viewport.node.focus();
221 }
222
223 private _getDataTypeKey(cell: CellEditor.CellConfig): string {
224 const metadata = cell.grid.dataModel
225 ? cell.grid.dataModel.metadata('body', cell.row, cell.column)
226 : null;
227
228 if (!metadata) {
229 return 'default';
230 }
231
232 let key = '';
233
234 if (metadata) {
235 key = metadata.type;
236 }
237
238 if (metadata.constraint && metadata.constraint.enum) {
239 if (metadata.constraint.enum === 'dynamic') {
240 key += ':dynamic-option';
241 } else {
242 key += ':option';
243 }
244 }
245
246 return key;
247 }
248
249 private _objectToKey(object: any): string {
250 let str = '';
251 for (let key in object) {
252 const value = object[key];
253 if (typeof value === 'object') {
254 str += `${key}:${this._objectToKey(value)}`;
255 } else {
256 str += `[${key}:${value}]`;
257 }
258 }
259
260 return str;
261 }
262
263 private _metadataIdentifierToKey(metadata: DataModel.Metadata): string {
264 return this._objectToKey(metadata);
265 }
266
267 private _metadataMatchesIdentifier(
268 metadata: DataModel.Metadata,
269 identifier: DataModel.Metadata
270 ): boolean {
271 for (let key in identifier) {
272 if (!metadata.hasOwnProperty(key)) {
273 return false;
274 }
275
276 const identifierValue = identifier[key];
277 const metadataValue = metadata[key];
278 if (typeof identifierValue === 'object') {
279 if (!this._metadataMatchesIdentifier(metadataValue, identifierValue)) {
280 return false;
281 }
282 } else if (metadataValue !== identifierValue) {
283 return false;
284 }
285 }
286
287 return true;
288 }
289
290 private _getMetadataBasedEditor(
291 cell: CellEditor.CellConfig
292 ): ICellEditor | undefined {
293 let editorMatched: ICellEditor | undefined;
294 const metadata = cell.grid.dataModel!.metadata(
295 'body',
296 cell.row,
297 cell.column
298 );
299 if (metadata) {
300 this._metadataBasedOverrides.forEach(value => {
301 if (!editorMatched) {
302 let [identifier, editor] = value;
303 if (this._metadataMatchesIdentifier(metadata, identifier)) {
304 editorMatched = resolveOption(editor, cell);
305 }
306 }
307 });
308 }
309
310 return editorMatched;
311 }
312
313 /**
314 * Choose the most appropriate cell editor to use based on overrides / cell data type.
315 *
316 * If no match is found in overrides or based on cell data type, and if cell has a primitive
317 * data type then TextCellEditor is used as default cell editor. If 'default' cell editor
318 * is overridden, then it is used instead of TextCellEditor for default.
319 */
320 private _getEditor(cell: CellEditor.CellConfig): ICellEditor | undefined {
321 const dtKey = this._getDataTypeKey(cell);
322
323 // find an editor based on data type based override
324 if (this._typeBasedOverrides.has(dtKey)) {
325 const editor = this._typeBasedOverrides.get(dtKey);
326 return resolveOption(editor!, cell);
327 } // find an editor based on metadata match based override
328 else if (this._metadataBasedOverrides.size > 0) {
329 const editor = this._getMetadataBasedEditor(cell);
330 if (editor) {
331 return editor;
332 }
333 }
334
335 // choose an editor based on data type
336 switch (dtKey) {
337 case 'string':
338 return new TextCellEditor();
339 case 'number':
340 return new NumberCellEditor();
341 case 'integer':
342 return new IntegerCellEditor();
343 case 'boolean':
344 return new BooleanCellEditor();
345 case 'date':
346 return new DateCellEditor();
347 case 'string:option':
348 case 'number:option':
349 case 'integer:option':
350 case 'date:option':
351 case 'array:option':
352 return new OptionCellEditor();
353 case 'string:dynamic-option':
354 case 'number:dynamic-option':
355 case 'integer:dynamic-option':
356 case 'date:dynamic-option':
357 return new DynamicOptionCellEditor();
358 }
359
360 // if an override exists for 'default', then use it
361 if (this._typeBasedOverrides.has('default')) {
362 const editor = this._typeBasedOverrides.get('default');
363 return resolveOption(editor!, cell);
364 }
365
366 // if cell has a primitive data type then use TextCellEditor
367 const data = cell.grid.dataModel!.data('body', cell.row, cell.column);
368 if (!data || typeof data !== 'object') {
369 return new TextCellEditor();
370 }
371
372 // no suitable editor found for the cell
373 return undefined;
374 }
375
376 // active cell editor
377 private _editor: ICellEditor | null = null;
378 // active cell being edited
379 private _cell: CellEditor.CellConfig | null = null;
380 // cell editor overrides based on cell data type identifier
381 private _typeBasedOverrides: Map<string, ICellEditor | Resolver> = new Map();
382 // cell editor overrides based on partial metadata match
383 private _metadataBasedOverrides: Map<
384 string,
385 [DataModel.Metadata, ICellEditor | Resolver]
386 > = new Map();
387 }