| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
mw.widgets.RowWidgetModel = function MwWidgetsRowWidgetModel( config ) { |
| 24 |
config = config || {}; |
| 25 |
|
| 26 |
|
| 27 |
OO.EventEmitter.call( this, config ); |
| 28 |
|
| 29 |
this.data = config.data || []; |
| 30 |
this.validate = config.validate; |
| 31 |
this.index = ( config.index !== undefined ) ? config.index : -1; |
| 32 |
this.label = ( config.label !== undefined ) ? config.label : ''; |
| 33 |
this.showLabel = ( config.showLabel !== undefined ) ? !!config.showLabel : true; |
| 34 |
this.isDeletable = ( config.deletable !== undefined ) ? !!config.deletable : true; |
| 35 |
|
| 36 |
this.initializeProps( config.keys ); |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
OO.mixinClass( mw.widgets.RowWidgetModel, OO.EventEmitter ); |
| 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 |
mw.widgets.RowWidgetModel.prototype.initializeProps = function ( props ) { |
| 91 |
let i, len; |
| 92 |
|
| 93 |
this.cells = []; |
| 94 |
|
| 95 |
if ( Array.isArray( props ) ) { |
| 96 |
for ( i = 0, len = props.length; i < len; i++ ) { |
| 97 |
this.cells.push( { |
| 98 |
index: i, |
| 99 |
key: props[ i ] |
| 100 |
} ); |
| 101 |
} |
| 102 |
} |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
mw.widgets.RowWidgetModel.prototype.setupRow = function () { |
| 111 |
this.verifyData(); |
| 112 |
this.buildRow(); |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
mw.widgets.RowWidgetModel.prototype.verifyData = function () { |
| 123 |
let i, len; |
| 124 |
|
| 125 |
for ( i = 0, len = this.cells.length; i < len; i++ ) { |
| 126 |
if ( this.data[ i ] === undefined ) { |
| 127 |
this.data.push( '' ); |
| 128 |
} |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
mw.widgets.RowWidgetModel.prototype.buildRow = function () { |
| 139 |
let i, len; |
| 140 |
|
| 141 |
for ( i = 0, len = this.cells.length; i < len; i++ ) { |
| 142 |
this.emit( 'insertCell', this.data[ i ], i ); |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
mw.widgets.RowWidgetModel.prototype.refreshRow = function () { |
| 153 |
|
| 154 |
|
| 155 |
this.buildRow(); |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
mw.widgets.RowWidgetModel.prototype.setValue = function ( handle, value ) { |
| 166 |
let index; |
| 167 |
|
| 168 |
if ( typeof handle === 'number' ) { |
| 169 |
index = handle; |
| 170 |
} else if ( typeof handle === 'string' ) { |
| 171 |
index = this.getCellProperties( handle ).index; |
| 172 |
} |
| 173 |
|
| 174 |
if ( typeof index === 'number' && this.data[ index ] !== undefined && |
| 175 |
this.data[ index ] !== value ) { |
| 176 |
|
| 177 |
this.data[ index ] = value; |
| 178 |
this.emit( 'valueChange', index, value ); |
| 179 |
} |
| 180 |
}; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
mw.widgets.RowWidgetModel.prototype.setData = function ( data ) { |
| 188 |
if ( Array.isArray( data ) ) { |
| 189 |
this.data = data; |
| 190 |
|
| 191 |
this.verifyData(); |
| 192 |
this.refreshRow(); |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
mw.widgets.RowWidgetModel.prototype.setIndex = function ( index ) { |
| 203 |
this.index = index; |
| 204 |
this.emit( 'labelUpdate' ); |
| 205 |
}; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
mw.widgets.RowWidgetModel.prototype.setLabel = function ( label ) { |
| 214 |
this.label = label; |
| 215 |
this.emit( 'labelUpdate' ); |
| 216 |
}; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
mw.widgets.RowWidgetModel.prototype.insertCell = function ( data, index, key ) { |
| 230 |
const insertIndex = ( typeof index === 'number' ) ? index : this.cells.length; |
| 231 |
|
| 232 |
|
| 233 |
this.cells.splice( insertIndex, 0, { |
| 234 |
index: insertIndex, |
| 235 |
key: key || undefined |
| 236 |
} ); |
| 237 |
|
| 238 |
|
| 239 |
const insertData = ( typeof data === 'string' || typeof data === 'number' ) ? data : ''; |
| 240 |
this.data.splice( insertIndex, 0, insertData ); |
| 241 |
|
| 242 |
|
| 243 |
for ( let i = insertIndex + 1, len = this.cells.length; i < len; i++ ) { |
| 244 |
this.cells[ i ].index++; |
| 245 |
} |
| 246 |
|
| 247 |
this.emit( 'insertCell', data, insertIndex ); |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
mw.widgets.RowWidgetModel.prototype.removeCell = function ( handle ) { |
| 258 |
const cellProps = this.getCellProperties( handle ); |
| 259 |
|
| 260 |
|
| 261 |
if ( cellProps === null ) { |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
this.cells.splice( cellProps.index, 1 ); |
| 266 |
this.data.splice( cellProps.index, 1 ); |
| 267 |
|
| 268 |
|
| 269 |
for ( let i = cellProps.index, len = this.cells.length; i < len; i++ ) { |
| 270 |
this.cells[ i ].index--; |
| 271 |
} |
| 272 |
|
| 273 |
this.emit( 'removeCell', cellProps.index ); |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
mw.widgets.RowWidgetModel.prototype.clear = function () { |
| 282 |
this.data = []; |
| 283 |
this.verifyData(); |
| 284 |
|
| 285 |
this.emit( 'clear', false ); |
| 286 |
}; |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
mw.widgets.RowWidgetModel.prototype.clearWithProperties = function () { |
| 294 |
this.data = []; |
| 295 |
this.cells = []; |
| 296 |
|
| 297 |
this.emit( 'clear', true ); |
| 298 |
}; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
mw.widgets.RowWidgetModel.prototype.getValidationPattern = function () { |
| 306 |
return this.validate; |
| 307 |
}; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
mw.widgets.RowWidgetModel.prototype.getRowProperties = function () { |
| 315 |
return { |
| 316 |
index: this.index, |
| 317 |
label: this.label, |
| 318 |
showLabel: this.showLabel, |
| 319 |
isDeletable: this.isDeletable |
| 320 |
}; |
| 321 |
}; |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
mw.widgets.RowWidgetModel.prototype.getCellProperties = function ( handle ) { |
| 331 |
let cell = null, |
| 332 |
i, len; |
| 333 |
|
| 334 |
if ( typeof handle === 'string' ) { |
| 335 |
for ( i = 0, len = this.cells.length; i < len; i++ ) { |
| 336 |
if ( this.cells[ i ].key === handle ) { |
| 337 |
cell = this.cells[ i ]; |
| 338 |
break; |
| 339 |
} |
| 340 |
} |
| 341 |
} else if ( typeof handle === 'number' ) { |
| 342 |
if ( handle < this.cells.length ) { |
| 343 |
cell = this.cells[ handle ]; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
return cell; |
| 348 |
}; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
mw.widgets.RowWidgetModel.prototype.getAllCellProperties = function () { |
| 356 |
return this.cells.slice(); |
| 357 |
}; |