1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/**
* @classdesc Table row widget. A RowWidget is used in conjunction with
* {@link mw.widgets.TableWidget table widgets} and should not be instantiated by themselves.
* They group together {@link OO.ui.TextInputWidget text input widgets} to form a unified row of
* editable data.
*
* @class
* @extends OO.ui.Widget
* @mixes OO.ui.mixin.GroupElement
*
* @constructor
* @description Create an instance of `mw.widgets.RowWidget`.
* @param {Object} [config] Configuration options
* @param {Array} [config.data] The data of the cells
* @param {Array} [config.keys] An array of keys for easy cell selection
* @param {RegExp|Function|string} [config.validate] Validation pattern to apply on every cell
* @param {number} [config.index] The row index.
* @param {string} [config.label] The row label to display. If not provided, the row index will
* be used be default. If set to null, no label will be displayed.
* @param {boolean} [config.showLabel=true] Show row label. Defaults to true.
* @param {boolean} [config.deletable=true] Whether the table should provide deletion UI tools
* for this row or not. Defaults to true.
*/
mw.widgets.RowWidget = function MwWidgetsRowWidget( config ) {
config = config || {};
// Parent constructor
mw.widgets.RowWidget.super.call( this, config );
// Mixin constructor
OO.ui.mixin.GroupElement.call( this, config );
// Set up model
this.model = new mw.widgets.RowWidgetModel( config );
// Set up group element
this.setGroupElement(
$( '<div>' )
.addClass( 'mw-widgets-rowWidget-cells' )
);
// Set up label
this.labelCell = new OO.ui.LabelWidget( {
classes: [ 'mw-widgets-rowWidget-label' ]
} );
// Set up delete button
if ( this.model.getRowProperties().isDeletable ) {
this.deleteButton = new OO.ui.ButtonWidget( {
icon: 'trash',
classes: [ 'mw-widgets-rowWidget-delete-button' ],
flags: 'destructive',
title: mw.msg( 'mw-widgets-table-row-delete' )
} );
}
// Events
this.model.connect( this, {
valueChange: 'onValueChange',
insertCell: 'onInsertCell',
removeCell: 'onRemoveCell',
clear: 'onClear',
labelUpdate: 'onLabelUpdate'
} );
this.aggregate( {
change: 'cellChange'
} );
this.connect( this, {
cellChange: 'onCellChange'
} );
if ( this.model.getRowProperties().isDeletable ) {
this.deleteButton.connect( this, {
click: 'onDeleteButtonClick'
} );
}
// Initialization
this.$element.addClass( 'mw-widgets-rowWidget' );
this.$element.append(
this.labelCell.$element,
this.$group
);
if ( this.model.getRowProperties().isDeletable ) {
this.$element.append( this.deleteButton.$element );
}
this.setLabel( this.model.getRowProperties().label );
this.model.setupRow();
};
/* Inheritance */
OO.inheritClass( mw.widgets.RowWidget, OO.ui.Widget );
OO.mixinClass( mw.widgets.RowWidget, OO.ui.mixin.GroupElement );
/* Events */
/**
* Change when an input contained within the row is updated.
*
* @event mw.widgets.RowWidget.inputChange
* @param {number} index The index of the cell that changed
* @param {string} value The new value of the cell
*/
/**
* Fired when the delete button for the row is pressed.
*
* @event mw.widgets.RowWidget.deleteButtonClick
*/
/* Methods */
/**
* @private
* @inheritdoc
*/
mw.widgets.RowWidget.prototype.addItems = function ( items, index ) {
let i, len;
OO.ui.mixin.GroupElement.prototype.addItems.call( this, items, index );
for ( i = index, len = items.length; i < len; i++ ) {
items[ i ].setData( i );
}
};
/**
* @private
* @inheritdoc
*/
mw.widgets.RowWidget.prototype.removeItems = function ( items ) {
OO.ui.mixin.GroupElement.prototype.removeItems.call( this, items );
const cells = this.getItems();
for ( let i = 0, len = cells.length; i < len; i++ ) {
cells[ i ].setData( i );
}
};
/**
* Get the row index.
*
* @return {number} The row index
*/
mw.widgets.RowWidget.prototype.getIndex = function () {
return this.model.getRowProperties().index;
};
/**
* Set the row index.
*
* @param {number} index The new index
*/
mw.widgets.RowWidget.prototype.setIndex = function ( index ) {
this.model.setIndex( index );
};
/**
* Get the label displayed on the row. If no custom label is set, the
* row index is used instead.
*
* @return {string} The row label
*/
mw.widgets.RowWidget.prototype.getLabel = function () {
const props = this.model.getRowProperties();
if ( props.label === null ) {
return '';
} else if ( !props.label ) {
return props.index.toString();
} else {
return props.label;
}
};
/**
* @event mw.widgets.RowWidget.labelUpdate
* @param {string} label
*/
/**
* Set the label to be displayed on the widget.
*
* @param {string} label The new label
* @fires mw.widgets.RowWidget.labelUpdate
*/
mw.widgets.RowWidget.prototype.setLabel = function ( label ) {
this.model.setLabel( label );
};
/**
* Set the value of a particular cell.
*
* @param {number} index The cell index
* @param {string} value The new value
*/
mw.widgets.RowWidget.prototype.setValue = function ( index, value ) {
this.model.setValue( index, value );
};
/**
* Insert a cell at a specified index.
*
* @param {string} data The cell data
* @param {number} index The index to insert the cell at
* @param {string} key A key for easy cell selection
*/
mw.widgets.RowWidget.prototype.insertCell = function ( data, index, key ) {
this.model.insertCell( data, index, key );
};
/**
* Removes a column at a specified index.
*
* @param {number} index The index to removeColumn
*/
mw.widgets.RowWidget.prototype.removeCell = function ( index ) {
this.model.removeCell( index );
};
/**
* Clear the field values.
*/
mw.widgets.RowWidget.prototype.clear = function () {
this.model.clear();
};
/**
* Handle model value changes.
*
* @param {number} index The column index of the updated cell
* @param {number} value The new value
*
* @fires mw.widgets.RowWidget.inputChange
*/
mw.widgets.RowWidget.prototype.onValueChange = function ( index, value ) {
this.getItems()[ index ].setValue( value );
this.emit( 'inputChange', index, value );
};
/**
* Handle model cell insertions.
*
* @param {string} data The initial data
* @param {number} index The index in which to insert the new cell
*/
mw.widgets.RowWidget.prototype.onInsertCell = function ( data, index ) {
this.addItems( [
new OO.ui.TextInputWidget( {
data: index,
value: data,
validate: this.model.getValidationPattern()
} )
], index );
};
/**
* Handle model cell removals.
*
* @param {number} index The removed cell index
*/
mw.widgets.RowWidget.prototype.onRemoveCell = function ( index ) {
this.removeItems( [ index ] );
};
/**
* Handle clear requests.
*/
mw.widgets.RowWidget.prototype.onClear = function () {
const cells = this.getItems();
for ( let i = 0, len = cells.length; i < len; i++ ) {
cells[ i ].setValue( '' );
}
};
/**
* Update model label changes.
*/
mw.widgets.RowWidget.prototype.onLabelUpdate = function () {
this.labelCell.setLabel( this.getLabel() );
};
/**
* React to cell input change.
*
* @private
* @param {OO.ui.TextInputWidget} input The input that fired the event
* @param {string} value The value of the input
*/
mw.widgets.RowWidget.prototype.onCellChange = function ( input, value ) {
// FIXME: The table itself should know if it contains invalid data
// in order to pass form state to the dialog when it asks if the Apply
// button should be enabled or not. This probably requires the table
// and each individual row to handle validation through an array of promises
// fed from the cells within.
// Right now, the table can't know if it's valid or not because the events
// don't get passed through.
input.getValidity().done( () => {
this.model.setValue( input.getData(), value );
} );
};
/**
* Handle delete button clicks.
*
* @private
* @fires mw.widgets.RowWidget.deleteButtonClick
*/
mw.widgets.RowWidget.prototype.onDeleteButtonClick = function () {
this.emit( 'deleteButtonClick' );
};
/**
* @inheritdoc
*/
mw.widgets.RowWidget.prototype.setDisabled = function ( disabled ) {
// Parent method
mw.widgets.RowWidget.super.prototype.setDisabled.call( this, disabled );
if ( !this.items ) {
return;
}
if ( this.model.getRowProperties().isDeletable ) {
this.deleteButton.setDisabled( disabled );
}
this.getItems().forEach( ( cell ) => {
cell.setDisabled( disabled );
} );
};
|