Debian Codegraph @

resources/src/mediawiki.widgets/Table/mw.widgets.TableWidget.js

raw symbols SCIP
1 /**
2 * @classdesc Groups {@link mw.widgets.RowWidget row widgets} together to form a bidimensional
3 * grid of text inputs.
4 *
5 * @class
6 * @extends OO.ui.Widget
7 * @mixes OO.ui.mixin.GroupElement
8 *
9 * @constructor
10 * @description Create an instance of `mw.widgets.TableWidget`.
11 * @param {mw.widgets.TableWidgetModel~Config} [config] Configuration options
12 */
13 mw.widgets.TableWidget = function MwWidgetsTableWidget( config ) {
14 // Configuration initialization
15 config = config || {};
16
17 // Parent constructor
18 mw.widgets.TableWidget.super.call( this, config );
19
20 // Mixin constructors
21 OO.ui.mixin.GroupElement.call( this, config );
22
23 // Set up model
24 this.model = new mw.widgets.TableWidgetModel( config );
25
26 // Properties
27 this.listeningToInsertionRowChanges = true;
28
29 // Set up group element
30 this.setGroupElement(
31 $( '<div>' )
32 .addClass( 'mw-widgets-tableWidget-rows' )
33 );
34
35 // Set up static rows
36 const columnProps = this.model.getAllColumnProperties();
37
38 if ( this.model.getTableProperties().showHeaders ) {
39 const headerRowItems = [];
40
41 this.headerRow = new mw.widgets.RowWidget( {
42 deletable: false,
43 label: null
44 } );
45
46 for ( let i = 0, len = columnProps.length; i < len; i++ ) {
47 const prop = columnProps[ i ];
48 headerRowItems.push(
49 this.getHeaderRowItem( prop.label, prop.key, prop.index )
50 );
51 }
52
53 this.headerRow.addItems( headerRowItems );
54 }
55
56 if ( this.model.getTableProperties().allowRowInsertion ) {
57 const insertionRowItems = [];
58
59 this.insertionRow = new mw.widgets.RowWidget( {
60 classes: [ 'mw-widgets-rowWidget-insertionRow' ],
61 deletable: false,
62 label: null
63 } );
64
65 for ( let i = 0, len = columnProps.length; i < len; i++ ) {
66 insertionRowItems.push( new OO.ui.TextInputWidget( {
67 data: columnProps[ i ].key ? columnProps[ i ].key : columnProps[ i ].index,
68 disabled: this.isDisabled()
69 } ) );
70 }
71
72 this.insertionRow.addItems( insertionRowItems );
73 }
74
75 // Set up initial rows
76 if ( Array.isArray( config.items ) ) {
77 this.addItems( config.items );
78 }
79
80 // Events
81 this.model.connect( this, {
82 valueChange: 'onValueChange',
83 insertRow: 'onInsertRow',
84 insertColumn: 'onInsertColumn',
85 removeRow: 'onRemoveRow',
86 removeColumn: 'onRemoveColumn',
87 clear: 'onClear'
88 } );
89
90 this.aggregate( {
91 inputChange: 'rowInputChange',
92 deleteButtonClick: 'rowDeleteButtonClick'
93 } );
94
95 this.connect( this, {
96 rowInputChange: 'onRowInputChange',
97 rowDeleteButtonClick: 'onRowDeleteButtonClick'
98 } );
99
100 if ( this.model.getTableProperties().allowRowInsertion ) {
101 this.insertionRow.connect( this, {
102 inputChange: 'onInsertionRowInputChange'
103 } );
104 }
105
106 // Initialization
107 this.$element.addClass( 'mw-widgets-tableWidget' );
108
109 if ( this.model.getTableProperties().showHeaders ) {
110 this.$element.append( this.headerRow.$element );
111 }
112 this.$element.append( this.$group );
113 if ( this.model.getTableProperties().allowRowInsertion ) {
114 this.$element.append( this.insertionRow.$element );
115 }
116
117 this.$element.toggleClass(
118 'mw-widgets-tableWidget-no-labels',
119 !this.model.getTableProperties().showRowLabels
120 );
121
122 this.model.setupTable();
123 };
124
125 /* Inheritance */
126
127 OO.inheritClass( mw.widgets.TableWidget, OO.ui.Widget );
128 OO.mixinClass( mw.widgets.TableWidget, OO.ui.mixin.GroupElement );
129
130 /* Static Properties */
131 mw.widgets.TableWidget.static.patterns = {
132
133 validate: /^[0-9]+(\.[0-9]+)?$/,
134
135 filter: /[0-9]+(\.[0-9]+)?/
136 };
137
138 /* Events */
139
140 /**
141 * Change when the data within the table has been updated.
142 *
143 * @event mw.widgets.TableWidget.change
144 * @param {number} rowIndex The index of the row that changed
145 * @param {string} rowKey The key of the row that changed, or `undefined` if it doesn't exist
146 * @param {number} columnIndex The index of the column that changed
147 * @param {string} columnKey The key of the column that changed, or `undefined` if it doesn't exist
148 * @param {string} value The new value
149 */
150
151 /**
152 * Fires when a row is removed from the table.
153 *
154 * @event mw.widgets.TableWidget.removeRow
155 * @param {number} index The index of the row being deleted
156 * @param {string} key The key of the row being deleted
157 */
158
159 /* Methods */
160
161 /**
162 * Set the value of a particular cell.
163 *
164 * @param {number|string} row The row containing the cell to edit. Can be either
165 * the row index or string key if one has been set for the row.
166 * @param {number|string} col The column containing the cell to edit. Can be either
167 * the column index or string key if one has been set for the column.
168 * @param {any} value The new value
169 */
170 mw.widgets.TableWidget.prototype.setValue = function ( row, col, value ) {
171 this.model.setValue( row, col, value );
172 };
173
174 /**
175 * Set the table data.
176 *
177 * @param {Array} data The new table data
178 * @return {boolean} The data has been successfully changed
179 */
180 mw.widgets.TableWidget.prototype.setData = function ( data ) {
181 if ( !Array.isArray( data ) ) {
182 return false;
183 }
184
185 this.model.setData( data );
186 return true;
187 };
188
189 /**
190 * Inserts a row into the table. If the row isn't added at the end of the table,
191 * all the following data will be shifted back one row.
192 *
193 * @param {Array} [data] The data to insert to the row.
194 * @param {number} [index] The index in which to insert the new row.
195 * If unset or set to null, the row will be added at the end of the table.
196 * @param {string} [key] A key to quickly select this row.
197 * If unset or set to null, no key will be set.
198 * @param {string} [label] A label to display next to the row.
199 * If unset or set to null, the key will be used if there is one.
200 */
201 mw.widgets.TableWidget.prototype.insertRow = function ( data, index, key, label ) {
202 this.model.insertRow( data, index, key, label );
203 };
204
205 /**
206 * Inserts a column into the table. If the column isn't added at the end of the table,
207 * all the following data will be shifted back one column.
208 *
209 * @param {Array} [data] The data to insert to the column.
210 * @param {number} [index] The index in which to insert the new column.
211 * If unset or set to null, the column will be added at the end of the table.
212 * @param {string} [key] A key to quickly select this column.
213 * If unset or set to null, no key will be set.
214 * @param {string} [label] A label to display next to the column.
215 * If unset or set to null, the key will be used if there is one.
216 */
217 mw.widgets.TableWidget.prototype.insertColumn = function ( data, index, key, label ) {
218 this.model.insertColumn( data, index, key, label );
219 };
220
221 /**
222 * Removes a row from the table. If the row removed isn't at the end of the table,
223 * all the following rows will be shifted back one row.
224 *
225 * @param {number|string} key The key or numerical index of the row to remove.
226 */
227 mw.widgets.TableWidget.prototype.removeRow = function ( key ) {
228 this.model.removeRow( key );
229 };
230
231 /**
232 * Removes a column from the table. If the column removed isn't at the end of the table,
233 * all the following columns will be shifted back one column.
234 *
235 * @param {number|string} key The key or numerical index of the column to remove.
236 */
237 mw.widgets.TableWidget.prototype.removeColumn = function ( key ) {
238 this.model.removeColumn( key );
239 };
240
241 /**
242 * Clears all values from the table, without wiping any row or column properties.
243 */
244 mw.widgets.TableWidget.prototype.clear = function () {
245 this.model.clear();
246 };
247
248 /**
249 * Clears the table data, as well as all row and column properties.
250 */
251 mw.widgets.TableWidget.prototype.clearWithProperties = function () {
252 this.model.clearWithProperties();
253 };
254
255 /**
256 * Filter cell input once it is changed.
257 *
258 * @param {string} value The input value
259 * @return {string} The filtered input
260 */
261 mw.widgets.TableWidget.prototype.filterCellInput = function ( value ) {
262 const matches = value.match( mw.widgets.TableWidget.static.patterns.filter );
263 return ( Array.isArray( matches ) ) ? matches[ 0 ] : '';
264 };
265
266 /**
267 * Get an input item for the header row.
268 *
269 * @private
270 * @param {string} label The column label
271 * @param {string} key The column key
272 * @param {number} index The column index
273 * @return {OO.ui.TextInputWidget} An input item for the header row
274 */
275 mw.widgets.TableWidget.prototype.getHeaderRowItem = function ( label, key, index ) {
276 return new OO.ui.TextInputWidget( {
277 value: label || key || index,
278 // TODO: Allow editing of fields
279 disabled: true
280 } );
281 };
282
283 /**
284 * @private
285 * @inheritdoc
286 */
287 mw.widgets.TableWidget.prototype.addItems = function ( items, index ) {
288 let i, len;
289
290 OO.ui.mixin.GroupElement.prototype.addItems.call( this, items, index );
291
292 for ( i = index, len = items.length; i < len; i++ ) {
293 items[ i ].setIndex( i );
294 }
295 };
296
297 /**
298 * @private
299 * @inheritdoc
300 */
301 mw.widgets.TableWidget.prototype.removeItems = function ( items ) {
302 OO.ui.mixin.GroupElement.prototype.removeItems.call( this, items );
303
304 const rows = this.getItems();
305 for ( let i = 0, len = rows.length; i < len; i++ ) {
306 rows[ i ].setIndex( i );
307 }
308 };
309
310 /**
311 * Handle model value changes
312 *
313 * @private
314 * @param {number} row The row index of the changed cell
315 * @param {number} col The column index of the changed cell
316 * @param {any} value The new value
317 * @fires mw.widgets.TableWidget.change
318 */
319 mw.widgets.TableWidget.prototype.onValueChange = function ( row, col, value ) {
320 const rowProps = this.model.getRowProperties( row ),
321 colProps = this.model.getColumnProperties( col );
322
323 this.getItems()[ row ].setValue( col, value );
324
325 this.emit( 'change', row, rowProps.key, col, colProps.key, value );
326 };
327
328 /**
329 * Handle model row insertions
330 *
331 * @private
332 * @param {Array} data The initial data
333 * @param {number} index The index in which the new row was inserted
334 * @param {string} key The row key
335 * @param {string} label The row label
336 * @fires mw.widgets.TableWidget.change
337 */
338 mw.widgets.TableWidget.prototype.onInsertRow = function ( data, index, key, label ) {
339 const colProps = this.model.getAllColumnProperties(),
340 keys = [];
341
342 for ( let i = 0, len = colProps.length; i < len; i++ ) {
343 keys.push( ( colProps[ i ].key ) ? colProps[ i ].key : i );
344 }
345
346 const newRow = new mw.widgets.RowWidget( {
347 data: data,
348 keys: keys,
349 validate: this.model.getValidationPattern(),
350 label: label,
351 showLabel: this.model.getTableProperties().showRowLabels,
352 deletable: this.model.getTableProperties().allowRowDeletion
353 } );
354
355 newRow.setDisabled( this.isDisabled() );
356
357 // TODO: Handle index parameter. Right now all new rows are inserted at the end
358 this.addItems( [ newRow ] );
359
360 // If this is the first data being added, refresh headers and insertion row
361 if ( this.model.getAllRowProperties().length === 1 ) {
362 this.refreshTableMarginals();
363 }
364
365 for ( let i = 0, len = data.length; i < len; i++ ) {
366 this.emit( 'change', index, key, i, colProps[ i ].key, data[ i ] );
367 }
368 };
369
370 /**
371 * Handle model column insertions
372 *
373 * @private
374 * @param {Array} data The initial data
375 * @param {number} index The index in which to insert the new column
376 * @param {string} key The row key
377 * @param {string} label The row label
378 *
379 * @fires mw.widgets.TableWidget.change
380 */
381 mw.widgets.TableWidget.prototype.onInsertColumn = function ( data, index, key, label ) {
382 const tableProps = this.model.getTableProperties(),
383 items = this.getItems(),
384 rowProps = this.model.getAllRowProperties();
385
386 for ( let i = 0, len = items.length; i < len; i++ ) {
387 items[ i ].insertCell( data[ i ], index, key );
388 this.emit( 'change', i, rowProps[ i ].key, index, key, data[ i ] );
389 }
390
391 if ( tableProps.showHeaders ) {
392 this.headerRow.addItems( [
393 this.getHeaderRowItem( label, key, index )
394 ] );
395 }
396
397 if ( tableProps.handleRowInsertion ) {
398 this.insertionRow.addItems( [
399 new OO.ui.TextInputWidget( {
400 validate: this.model.getValidationPattern(),
401 disabled: this.isDisabled()
402 } )
403 ] );
404 }
405 };
406
407 /**
408 * Handle model row removals
409 *
410 * @private
411 * @param {number} index The removed row index
412 * @param {string} key The removed row key
413 * @fires mw.widgets.TableWidget.removeRow
414 */
415 mw.widgets.TableWidget.prototype.onRemoveRow = function ( index, key ) {
416 this.removeItems( [ this.getItems()[ index ] ] );
417 this.emit( 'removeRow', index, key );
418 };
419
420 /**
421 * Handle model column removals
422 *
423 * @private
424 * @param {number} index The removed column index
425 * @param {string} key The removed column key
426 * @fires mw.widgets.TableWidget.removeColumn
427 */
428 mw.widgets.TableWidget.prototype.onRemoveColumn = function ( index, key ) {
429 const items = this.getItems();
430
431 for ( let i = 0; i < items.length; i++ ) {
432 items[ i ].removeCell( index );
433 }
434
435 this.emit( 'removeColumn', index, key );
436 };
437
438 /**
439 * Handle model table clears
440 *
441 * @private
442 * @param {boolean} withProperties Clear row/column properties
443 */
444 mw.widgets.TableWidget.prototype.onClear = function ( withProperties ) {
445 let i, len, rows;
446
447 if ( withProperties ) {
448 this.removeItems( this.getItems() );
449 } else {
450 rows = this.getItems();
451
452 for ( i = 0, len = rows.length; i < len; i++ ) {
453 rows[ i ].clear();
454 }
455 }
456 };
457
458 /**
459 * React to input changes bubbled up from event aggregation
460 *
461 * @private
462 * @param {mw.widgets.RowWidget} row The row that changed
463 * @param {number} colIndex The column index of the cell that changed
464 * @param {string} value The new value of the input
465 * @fires mw.widgets.TableWidget.change
466 */
467 mw.widgets.TableWidget.prototype.onRowInputChange = function ( row, colIndex, value ) {
468 const items = this.getItems();
469
470 let rowIndex;
471
472 for ( let i = 0, len = items.length; i < len; i++ ) {
473 if ( row === items[ i ] ) {
474 rowIndex = i;
475 break;
476 }
477 }
478
479 this.model.setValue( rowIndex, colIndex, value );
480 };
481
482 /**
483 * React to new row input changes
484 *
485 * @private
486 * @param {number} colIndex The column index of the input that fired the change
487 * @param {string} value The new row value
488 */
489 mw.widgets.TableWidget.prototype.onInsertionRowInputChange = function ( colIndex, value ) {
490 const insertionRowItems = this.insertionRow.getItems(),
491 newRowData = [];
492
493 if ( this.listeningToInsertionRowChanges ) {
494 for ( let i = 0, len = insertionRowItems.length; i < len; i++ ) {
495 if ( i === colIndex ) {
496 newRowData.push( value );
497 } else {
498 newRowData.push( '' );
499 }
500 }
501
502 this.insertRow( newRowData );
503
504 // Focus newly inserted row
505 const lastRow = this.getItems().slice( -1 )[ 0 ];
506 lastRow.getItems()[ colIndex ].focus();
507
508 // Reset insertion row
509 this.listeningToInsertionRowChanges = false;
510 this.insertionRow.clear();
511 this.listeningToInsertionRowChanges = true;
512 }
513 };
514
515 /**
516 * Handle row deletion input
517 *
518 * @private
519 * @param {mw.widgets.RowWidget} row The row that asked for the deletion
520 */
521 mw.widgets.TableWidget.prototype.onRowDeleteButtonClick = function ( row ) {
522 const items = this.getItems();
523
524 let i = -1, len;
525
526 for ( i = 0, len = items.length; i < len; i++ ) {
527 if ( items[ i ] === row ) {
528 break;
529 }
530 }
531
532 this.removeRow( i );
533 };
534
535 /**
536 * @inheritdoc
537 */
538 mw.widgets.TableWidget.prototype.setDisabled = function ( disabled ) {
539 // Parent method
540 mw.widgets.TableWidget.super.prototype.setDisabled.call( this, disabled );
541
542 if ( !this.items ) {
543 return;
544 }
545
546 this.getItems().forEach( ( row ) => {
547 row.setDisabled( disabled );
548 } );
549
550 if ( this.model.getTableProperties().allowRowInsertion ) {
551 this.insertionRow.getItems().forEach( ( row ) => {
552 row.setDisabled( disabled );
553 } );
554 }
555 };
556
557 /**
558 * Refresh table header and insertion row.
559 */
560 mw.widgets.TableWidget.prototype.refreshTableMarginals = function () {
561 const tableProps = this.model.getTableProperties(),
562 columnProps = this.model.getAllColumnProperties();
563
564 if ( tableProps.showHeaders ) {
565 this.headerRow.removeItems( this.headerRow.getItems() );
566 const rowItems = [];
567
568 for ( let i = 0, len = columnProps.length; i < len; i++ ) {
569 const prop = columnProps[ i ];
570 rowItems.push(
571 this.getHeaderRowItem( prop.label, prop.key, prop.index )
572 );
573 }
574
575 this.headerRow.addItems( rowItems );
576 }
577
578 if ( tableProps.allowRowInsertion ) {
579 this.insertionRow.clear();
580 this.insertionRow.removeItems( this.insertionRow.getItems() );
581
582 for ( let i = 0, len = columnProps.length; i < len; i++ ) {
583 this.insertionRow.insertCell( '', columnProps[ i ].index, columnProps[ i ].key );
584 }
585 }
586 };