| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
mw.widgets.RowWidget = function MwWidgetsRowWidget( config ) { |
| 25 |
config = config || {}; |
| 26 |
|
| 27 |
|
| 28 |
mw.widgets.RowWidget.super.call( this, config ); |
| 29 |
|
| 30 |
|
| 31 |
OO.ui.mixin.GroupElement.call( this, config ); |
| 32 |
|
| 33 |
|
| 34 |
this.model = new mw.widgets.RowWidgetModel( config ); |
| 35 |
|
| 36 |
|
| 37 |
this.setGroupElement( |
| 38 |
$( '<div>' ) |
| 39 |
.addClass( 'mw-widgets-rowWidget-cells' ) |
| 40 |
); |
| 41 |
|
| 42 |
|
| 43 |
this.labelCell = new OO.ui.LabelWidget( { |
| 44 |
classes: [ 'mw-widgets-rowWidget-label' ] |
| 45 |
} ); |
| 46 |
|
| 47 |
|
| 48 |
if ( this.model.getRowProperties().isDeletable ) { |
| 49 |
this.deleteButton = new OO.ui.ButtonWidget( { |
| 50 |
icon: 'trash', |
| 51 |
classes: [ 'mw-widgets-rowWidget-delete-button' ], |
| 52 |
flags: 'destructive', |
| 53 |
title: mw.msg( 'mw-widgets-table-row-delete' ) |
| 54 |
} ); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
this.model.connect( this, { |
| 59 |
valueChange: 'onValueChange', |
| 60 |
insertCell: 'onInsertCell', |
| 61 |
removeCell: 'onRemoveCell', |
| 62 |
clear: 'onClear', |
| 63 |
labelUpdate: 'onLabelUpdate' |
| 64 |
} ); |
| 65 |
|
| 66 |
this.aggregate( { |
| 67 |
change: 'cellChange' |
| 68 |
} ); |
| 69 |
|
| 70 |
this.connect( this, { |
| 71 |
cellChange: 'onCellChange' |
| 72 |
} ); |
| 73 |
|
| 74 |
if ( this.model.getRowProperties().isDeletable ) { |
| 75 |
this.deleteButton.connect( this, { |
| 76 |
click: 'onDeleteButtonClick' |
| 77 |
} ); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
this.$element.addClass( 'mw-widgets-rowWidget' ); |
| 82 |
|
| 83 |
this.$element.append( |
| 84 |
this.labelCell.$element, |
| 85 |
this.$group |
| 86 |
); |
| 87 |
|
| 88 |
if ( this.model.getRowProperties().isDeletable ) { |
| 89 |
this.$element.append( this.deleteButton.$element ); |
| 90 |
} |
| 91 |
|
| 92 |
this.setLabel( this.model.getRowProperties().label ); |
| 93 |
|
| 94 |
this.model.setupRow(); |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
OO.inheritClass( mw.widgets.RowWidget, OO.ui.Widget ); |
| 100 |
OO.mixinClass( mw.widgets.RowWidget, OO.ui.mixin.GroupElement ); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
mw.widgets.RowWidget.prototype.addItems = function ( items, index ) { |
| 125 |
let i, len; |
| 126 |
|
| 127 |
OO.ui.mixin.GroupElement.prototype.addItems.call( this, items, index ); |
| 128 |
|
| 129 |
for ( i = index, len = items.length; i < len; i++ ) { |
| 130 |
items[ i ].setData( i ); |
| 131 |
} |
| 132 |
}; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
mw.widgets.RowWidget.prototype.removeItems = function ( items ) { |
| 139 |
OO.ui.mixin.GroupElement.prototype.removeItems.call( this, items ); |
| 140 |
|
| 141 |
const cells = this.getItems(); |
| 142 |
for ( let i = 0, len = cells.length; i < len; i++ ) { |
| 143 |
cells[ i ].setData( i ); |
| 144 |
} |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
mw.widgets.RowWidget.prototype.getIndex = function () { |
| 153 |
return this.model.getRowProperties().index; |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
mw.widgets.RowWidget.prototype.setIndex = function ( index ) { |
| 162 |
this.model.setIndex( index ); |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
mw.widgets.RowWidget.prototype.getLabel = function () { |
| 172 |
const props = this.model.getRowProperties(); |
| 173 |
|
| 174 |
if ( props.label === null ) { |
| 175 |
return ''; |
| 176 |
} else if ( !props.label ) { |
| 177 |
return props.index.toString(); |
| 178 |
} else { |
| 179 |
return props.label; |
| 180 |
} |
| 181 |
}; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
mw.widgets.RowWidget.prototype.setLabel = function ( label ) { |
| 195 |
this.model.setLabel( label ); |
| 196 |
}; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
mw.widgets.RowWidget.prototype.setValue = function ( index, value ) { |
| 205 |
this.model.setValue( index, value ); |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
mw.widgets.RowWidget.prototype.insertCell = function ( data, index, key ) { |
| 216 |
this.model.insertCell( data, index, key ); |
| 217 |
}; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
mw.widgets.RowWidget.prototype.removeCell = function ( index ) { |
| 225 |
this.model.removeCell( index ); |
| 226 |
}; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
mw.widgets.RowWidget.prototype.clear = function () { |
| 232 |
this.model.clear(); |
| 233 |
}; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
mw.widgets.RowWidget.prototype.onValueChange = function ( index, value ) { |
| 244 |
this.getItems()[ index ].setValue( value ); |
| 245 |
this.emit( 'inputChange', index, value ); |
| 246 |
}; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
mw.widgets.RowWidget.prototype.onInsertCell = function ( data, index ) { |
| 255 |
this.addItems( [ |
| 256 |
new OO.ui.TextInputWidget( { |
| 257 |
data: index, |
| 258 |
value: data, |
| 259 |
validate: this.model.getValidationPattern() |
| 260 |
} ) |
| 261 |
], index ); |
| 262 |
}; |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
mw.widgets.RowWidget.prototype.onRemoveCell = function ( index ) { |
| 270 |
this.removeItems( [ index ] ); |
| 271 |
}; |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
mw.widgets.RowWidget.prototype.onClear = function () { |
| 277 |
const cells = this.getItems(); |
| 278 |
|
| 279 |
for ( let i = 0, len = cells.length; i < len; i++ ) { |
| 280 |
cells[ i ].setValue( '' ); |
| 281 |
} |
| 282 |
}; |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
mw.widgets.RowWidget.prototype.onLabelUpdate = function () { |
| 288 |
this.labelCell.setLabel( this.getLabel() ); |
| 289 |
}; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
mw.widgets.RowWidget.prototype.onCellChange = function ( input, value ) { |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
input.getValidity().done( () => { |
| 307 |
this.model.setValue( input.getData(), value ); |
| 308 |
} ); |
| 309 |
}; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
mw.widgets.RowWidget.prototype.onDeleteButtonClick = function () { |
| 318 |
this.emit( 'deleteButtonClick' ); |
| 319 |
}; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
mw.widgets.RowWidget.prototype.setDisabled = function ( disabled ) { |
| 325 |
|
| 326 |
mw.widgets.RowWidget.super.prototype.setDisabled.call( this, disabled ); |
| 327 |
|
| 328 |
if ( !this.items ) { |
| 329 |
return; |
| 330 |
} |
| 331 |
|
| 332 |
if ( this.model.getRowProperties().isDeletable ) { |
| 333 |
this.deleteButton.setDisabled( disabled ); |
| 334 |
} |
| 335 |
|
| 336 |
this.getItems().forEach( ( cell ) => { |
| 337 |
cell.setDisabled( disabled ); |
| 338 |
} ); |
| 339 |
}; |