| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
ve.ui.MediaSizeWidget = function VeUiMediaSizeWidget( scalable, config ) { |
| 21 |
|
| 22 |
config = config || {}; |
| 23 |
|
| 24 |
this.scalable = scalable; |
| 25 |
|
| 26 |
|
| 27 |
ve.ui.MediaSizeWidget.super.call( this, config ); |
| 28 |
|
| 29 |
|
| 30 |
this.ratio = {}; |
| 31 |
this.currentDimensions = {}; |
| 32 |
this.maxDimensions = {}; |
| 33 |
this.valid = null; |
| 34 |
this.noDefaultDimensions = !!config.noDefaultDimensions; |
| 35 |
this.dimensionsAlign = config.dimensionsAlign || 'right'; |
| 36 |
|
| 37 |
|
| 38 |
this.sizeTypeSelect = new OO.ui.ButtonSelectWidget( { |
| 39 |
classes: [ 've-ui-mediaSizeWidget-section-sizetype' ] |
| 40 |
} ); |
| 41 |
this.sizeTypeSelect.addItems( [ |
| 42 |
new OO.ui.ButtonOptionWidget( { |
| 43 |
data: 'default', |
| 44 |
label: ve.msg( 'visualeditor-mediasizewidget-sizeoptions-default' ) |
| 45 |
} ), |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
new OO.ui.ButtonOptionWidget( { |
| 52 |
data: 'custom', |
| 53 |
label: ve.msg( 'visualeditor-mediasizewidget-sizeoptions-custom' ) |
| 54 |
} ) |
| 55 |
] ); |
| 56 |
const sizeTypeField = new OO.ui.FieldLayout( this.sizeTypeSelect ); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
this.dimensions = new ve.ui.DimensionsWidget( { validate: this.isValid.bind( this ) } ); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
this.errorLabel = new OO.ui.LabelWidget( { |
| 72 |
label: ve.msg( 'visualeditor-mediasizewidget-label-defaulterror' ) |
| 73 |
} ); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
const dimensionsField = new OO.ui.FieldLayout( |
| 89 |
this.dimensions, { |
| 90 |
align: this.dimensionsAlign, |
| 91 |
classes: [ 've-ui-mediaSizeWidget-section-custom' ] |
| 92 |
} |
| 93 |
); |
| 94 |
|
| 95 |
|
| 96 |
this.$element.addClass( 've-ui-mediaSizeWidget' ); |
| 97 |
if ( !this.noDefaultDimensions ) { |
| 98 |
this.$element.append( sizeTypeField.$element ); |
| 99 |
} |
| 100 |
this.$element.append( dimensionsField.$element ); |
| 101 |
|
| 102 |
|
| 103 |
this.$element.append( |
| 104 |
$( '<div>' ) |
| 105 |
.addClass( 've-ui-mediaSizeWidget-label-error' ) |
| 106 |
.append( this.errorLabel.$element ) |
| 107 |
); |
| 108 |
|
| 109 |
|
| 110 |
this.dimensions.connect( this, { |
| 111 |
widthChange: [ 'onDimensionsChange', 'width' ], |
| 112 |
heightChange: [ 'onDimensionsChange', 'height' ] |
| 113 |
} ); |
| 114 |
|
| 115 |
|
| 116 |
this.sizeTypeSelect.connect( this, { choose: 'onSizeTypeChoose' } ); |
| 117 |
|
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
OO.inheritClass( ve.ui.MediaSizeWidget, OO.ui.Widget ); |
| 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 |
ve.ui.MediaSizeWidget.prototype.onScalableOriginalSizeChange = function () { |
| 150 |
|
| 151 |
this.updateDisabled(); |
| 152 |
this.validateDimensions(); |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
ve.ui.MediaSizeWidget.prototype.onScalableCurrentSizeChange = function ( dimensions ) { |
| 161 |
if ( !ve.isEmptyObject( dimensions ) ) { |
| 162 |
this.setCurrentDimensions( dimensions ); |
| 163 |
this.validateDimensions(); |
| 164 |
} |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
ve.ui.MediaSizeWidget.prototype.onScalableDefaultSizeChange = function ( isDefault ) { |
| 173 |
|
| 174 |
this.updateDefaultDimensions(); |
| 175 |
|
| 176 |
|
| 177 |
this.setSizeType( |
| 178 |
isDefault ? |
| 179 |
'default' : |
| 180 |
'custom' |
| 181 |
); |
| 182 |
this.validateDimensions(); |
| 183 |
}; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
ve.ui.MediaSizeWidget.prototype.onDimensionsChange = function ( type, value ) { |
| 203 |
if ( +value === 0 && !this.noDefaultDimensions ) { |
| 204 |
this.setSizeType( 'default' ); |
| 205 |
} else { |
| 206 |
this.setSizeType( 'custom' ); |
| 207 |
if ( !isNaN( +value ) ) { |
| 208 |
const dimensions = {}; |
| 209 |
dimensions[ type ] = +value; |
| 210 |
this.setCurrentDimensions( dimensions ); |
| 211 |
} else { |
| 212 |
this.validateDimensions(); |
| 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 |
ve.ui.MediaSizeWidget.prototype.onSizeTypeChoose = function ( item ) { |
| 239 |
const selectedType = item.getData(), |
| 240 |
wasDefault = this.scalable.isDefault(); |
| 241 |
|
| 242 |
this.scalable.toggleDefault( selectedType === 'default' ); |
| 243 |
|
| 244 |
if ( selectedType === 'default' ) { |
| 245 |
|
| 246 |
if ( !ve.isEmptyObject( this.dimensions.getDefaults() ) ) { |
| 247 |
this.dimensions.clear(); |
| 248 |
} |
| 249 |
} else if ( selectedType === 'custom' ) { |
| 250 |
|
| 251 |
if ( wasDefault && !ve.isEmptyObject( this.dimensions.getDefaults() ) ) { |
| 252 |
this.setCurrentDimensions( this.dimensions.getDefaults() ); |
| 253 |
} |
| 254 |
this.validateDimensions(); |
| 255 |
} |
| 256 |
|
| 257 |
this.emit( 'changeSizeType', selectedType ); |
| 258 |
this.updateDisabled(); |
| 259 |
this.validateDimensions(); |
| 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 |
ve.ui.MediaSizeWidget.prototype.setSizeType = function ( sizeType ) { |
| 295 |
if ( |
| 296 |
this.getSizeType() !== sizeType || |
| 297 |
|
| 298 |
|
| 299 |
+this.dimensions.getWidth() === 0 || |
| 300 |
+this.dimensions.getHeight() === 0 |
| 301 |
) { |
| 302 |
this.sizeTypeSelect.chooseItem( |
| 303 |
this.sizeTypeSelect.findItemFromData( sizeType ) |
| 304 |
); |
| 305 |
} |
| 306 |
return this; |
| 307 |
}; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
ve.ui.MediaSizeWidget.prototype.getSizeType = function () { |
| 314 |
return this.sizeTypeSelect.findSelectedItem() ? this.sizeTypeSelect.findSelectedItem().getData() : ''; |
| 315 |
}; |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
ve.ui.MediaSizeWidget.prototype.setScalable = function ( scalable ) { |
| 325 |
if ( this.scalable instanceof ve.dm.Scalable ) { |
| 326 |
this.scalable.disconnect( this ); |
| 327 |
} |
| 328 |
this.scalable = scalable; |
| 329 |
|
| 330 |
this.scalable.connect( this, { |
| 331 |
defaultSizeChange: 'onScalableDefaultSizeChange', |
| 332 |
originalSizeChange: 'onScalableOriginalSizeChange', |
| 333 |
currentSizeChange: 'onScalableCurrentSizeChange' |
| 334 |
} ); |
| 335 |
|
| 336 |
this.updateDefaultDimensions(); |
| 337 |
|
| 338 |
if ( !this.scalable.isDefault() ) { |
| 339 |
|
| 340 |
this.setCurrentDimensions( this.scalable.getCurrentDimensions() ); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
if ( this.scalable.getOriginalDimensions() ) { |
| 345 |
this.setSizeType( this.scalable.isDefault() ? 'default' : 'custom' ); |
| 346 |
} |
| 347 |
this.updateDisabled(); |
| 348 |
this.validateDimensions(); |
| 349 |
return this; |
| 350 |
}; |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
ve.ui.MediaSizeWidget.prototype.getScalable = function () { |
| 359 |
return this.scalable; |
| 360 |
}; |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
ve.ui.MediaSizeWidget.prototype.setRatio = function ( ratio ) { |
| 370 |
this.scalable.setRatio( ratio ); |
| 371 |
return this; |
| 372 |
}; |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
ve.ui.MediaSizeWidget.prototype.getRatio = function () { |
| 380 |
return this.scalable.getRatio(); |
| 381 |
}; |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
ve.ui.MediaSizeWidget.prototype.setMaxDimensions = function ( dimensions ) { |
| 392 |
|
| 393 |
const maxDimensions = ve.dm.Scalable.static.getDimensionsFromValue( dimensions, this.scalable.getRatio() ); |
| 394 |
this.scalable.setMaxDimensions( maxDimensions ); |
| 395 |
return this; |
| 396 |
}; |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
ve.ui.MediaSizeWidget.prototype.getMaxDimensions = function () { |
| 404 |
return this.scalable.getMaxDimensions(); |
| 405 |
}; |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
ve.ui.MediaSizeWidget.prototype.getCurrentDimensions = function () { |
| 413 |
return this.currentDimensions; |
| 414 |
}; |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
ve.ui.MediaSizeWidget.prototype.setDisabled = function ( disabled ) { |
| 420 |
|
| 421 |
ve.ui.MediaSizeWidget.super.prototype.setDisabled.call( this, disabled ); |
| 422 |
|
| 423 |
this.updateDisabled(); |
| 424 |
return this; |
| 425 |
}; |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
ve.ui.MediaSizeWidget.prototype.updateDisabled = function () { |
| 434 |
const disabled = this.isDisabled(); |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
if ( this.sizeTypeSelect && |
| 440 |
this.dimensions && |
| 441 |
this.scalable |
| 442 |
) { |
| 443 |
const sizeType = this.getSizeType(); |
| 444 |
|
| 445 |
|
| 446 |
this.sizeTypeSelect.setDisabled( disabled ); |
| 447 |
|
| 448 |
|
| 449 |
this.sizeTypeSelect.findItemFromData( 'default' ).setDisabled( |
| 450 |
ve.isEmptyObject( this.scalable.getDefaultDimensions() ) |
| 451 |
); |
| 452 |
|
| 453 |
|
| 454 |
this.dimensions.setDisabled( disabled || sizeType !== 'custom' ); |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
} |
| 459 |
return this; |
| 460 |
}; |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
ve.ui.MediaSizeWidget.prototype.setCurrentDimensions = function ( dimensions ) { |
| 469 |
|
| 470 |
if ( this.preventChangeRecursion ) { |
| 471 |
return; |
| 472 |
} |
| 473 |
this.preventChangeRecursion = true; |
| 474 |
|
| 475 |
if ( !this.scalable.isFixedRatio() ) { |
| 476 |
dimensions = ve.extendObject( {}, this.getCurrentDimensions(), dimensions ); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
const normalizedDimensions = ve.dm.Scalable.static.getDimensionsFromValue( dimensions, this.scalable.getRatio() ); |
| 481 |
|
| 482 |
if ( |
| 483 |
|
| 484 |
ve.dm.Scalable.static.isDimensionsObjectValid( normalizedDimensions ) && |
| 485 |
|
| 486 |
!this.scalable.isDefault() |
| 487 |
) { |
| 488 |
this.currentDimensions = normalizedDimensions; |
| 489 |
|
| 490 |
|
| 491 |
this.dimensions.setWidth( this.currentDimensions.width ); |
| 492 |
this.dimensions.setHeight( this.currentDimensions.height ); |
| 493 |
|
| 494 |
|
| 495 |
this.scalable.setCurrentDimensions( this.currentDimensions ); |
| 496 |
|
| 497 |
this.validateDimensions(); |
| 498 |
|
| 499 |
this.emit( 'change', this.currentDimensions ); |
| 500 |
} |
| 501 |
this.preventChangeRecursion = false; |
| 502 |
}; |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
ve.ui.MediaSizeWidget.prototype.validateDimensions = function () { |
| 515 |
const isValid = this.isValid(); |
| 516 |
|
| 517 |
if ( this.valid !== isValid ) { |
| 518 |
this.valid = isValid; |
| 519 |
this.errorLabel.toggle( !isValid ); |
| 520 |
this.dimensions.setValidityFlag(); |
| 521 |
|
| 522 |
this.emit( 'valid', this.valid ); |
| 523 |
} |
| 524 |
return isValid; |
| 525 |
}; |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
ve.ui.MediaSizeWidget.prototype.updateDefaultDimensions = function () { |
| 533 |
const defaultDimensions = this.scalable.getDefaultDimensions(); |
| 534 |
|
| 535 |
if ( !ve.isEmptyObject( defaultDimensions ) ) { |
| 536 |
this.dimensions.setDefaults( defaultDimensions ); |
| 537 |
} else { |
| 538 |
this.dimensions.removeDefaults(); |
| 539 |
} |
| 540 |
this.updateDisabled(); |
| 541 |
this.validateDimensions(); |
| 542 |
}; |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
ve.ui.MediaSizeWidget.prototype.isCustomEmpty = function () { |
| 550 |
return this.dimensions.isEmpty(); |
| 551 |
}; |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
ve.ui.MediaSizeWidget.prototype.isEmpty = function () { |
| 570 |
return this.isCustomEmpty(); |
| 571 |
|
| 572 |
}; |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
ve.ui.MediaSizeWidget.prototype.isValid = function () { |
| 584 |
const itemType = this.sizeTypeSelect.findSelectedItem() ? |
| 585 |
this.sizeTypeSelect.findSelectedItem().getData() : 'custom'; |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
if ( itemType === 'custom' ) { |
| 590 |
if ( |
| 591 |
this.dimensions.getDefaults() && |
| 592 |
this.dimensions.isEmpty() |
| 593 |
) { |
| 594 |
return true; |
| 595 |
} else if ( |
| 596 |
!isNaN( +this.dimensions.getWidth() ) && |
| 597 |
!isNaN( +this.dimensions.getHeight() ) |
| 598 |
) { |
| 599 |
return this.scalable.isCurrentDimensionsValid(); |
| 600 |
} else { |
| 601 |
return false; |
| 602 |
} |
| 603 |
} else { |
| 604 |
|
| 605 |
return true; |
| 606 |
} |
| 607 |
}; |