| 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 |
ve.dm.Scalable = function VeDmScalable( config ) { |
| 26 |
config = ve.extendObject( { |
| 27 |
fixedRatio: true, |
| 28 |
enforceMin: true, |
| 29 |
enforceMax: true |
| 30 |
}, config ); |
| 31 |
|
| 32 |
|
| 33 |
OO.EventEmitter.call( this ); |
| 34 |
|
| 35 |
|
| 36 |
this.ratio = null; |
| 37 |
this.valid = null; |
| 38 |
this.defaultSize = false; |
| 39 |
|
| 40 |
|
| 41 |
this.currentDimensions = null; |
| 42 |
this.defaultDimensions = null; |
| 43 |
this.originalDimensions = null; |
| 44 |
this.minDimensions = null; |
| 45 |
this.maxDimensions = null; |
| 46 |
|
| 47 |
|
| 48 |
this.fixedRatio = config.fixedRatio; |
| 49 |
if ( config.currentDimensions ) { |
| 50 |
this.setCurrentDimensions( config.currentDimensions ); |
| 51 |
} |
| 52 |
if ( config.originalDimensions ) { |
| 53 |
this.setOriginalDimensions( config.originalDimensions ); |
| 54 |
} |
| 55 |
if ( config.defaultDimensions ) { |
| 56 |
this.setDefaultDimensions( config.defaultDimensions ); |
| 57 |
} |
| 58 |
if ( config.isDefault ) { |
| 59 |
this.toggleDefault( !!config.isDefault ); |
| 60 |
} |
| 61 |
if ( config.minDimensions ) { |
| 62 |
this.setMinDimensions( config.minDimensions ); |
| 63 |
} |
| 64 |
if ( config.maxDimensions ) { |
| 65 |
this.setMaxDimensions( config.maxDimensions ); |
| 66 |
} |
| 67 |
|
| 68 |
this.setEnforcedMin( config.enforceMin ); |
| 69 |
this.setEnforcedMax( config.enforceMax ); |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
OO.mixinClass( ve.dm.Scalable, OO.EventEmitter ); |
| 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 |
ve.dm.Scalable.static.getDimensionsFromValue = function ( dimensions, ratio ) { |
| 131 |
dimensions = ve.copy( dimensions ); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
if ( dimensions.width === '' ) { |
| 136 |
dimensions.width = 0; |
| 137 |
} |
| 138 |
if ( dimensions.height === '' ) { |
| 139 |
dimensions.height = 0; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
if ( !dimensions.height && ratio !== null && +dimensions.width ) { |
| 144 |
dimensions.height = Math.round( dimensions.width / ratio ); |
| 145 |
} |
| 146 |
if ( !dimensions.width && ratio !== null && +dimensions.height ) { |
| 147 |
dimensions.width = Math.round( dimensions.height * ratio ); |
| 148 |
} |
| 149 |
|
| 150 |
return dimensions; |
| 151 |
}; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
ve.dm.Scalable.static.isDimensionsObjectValid = function ( dimensions ) { |
| 161 |
if ( |
| 162 |
dimensions && |
| 163 |
!ve.isEmptyObject( dimensions ) && |
| 164 |
( |
| 165 |
dimensions.width !== undefined || |
| 166 |
dimensions.height !== undefined |
| 167 |
) |
| 168 |
) { |
| 169 |
return true; |
| 170 |
} |
| 171 |
return false; |
| 172 |
}; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
ve.dm.Scalable.prototype.clone = function () { |
| 182 |
const currentDimensions = this.getCurrentDimensions(), |
| 183 |
originalDimensions = this.getOriginalDimensions(), |
| 184 |
defaultDimensions = this.getDefaultDimensions(), |
| 185 |
minDimensions = this.getMinDimensions(), |
| 186 |
maxDimensions = this.getMaxDimensions(), |
| 187 |
config = { |
| 188 |
isDefault: !!this.isDefault(), |
| 189 |
enforceMin: !!this.isEnforcedMin(), |
| 190 |
enforceMax: !!this.isEnforcedMax() |
| 191 |
}; |
| 192 |
if ( currentDimensions ) { |
| 193 |
config.currentDimensions = ve.copy( currentDimensions ); |
| 194 |
} |
| 195 |
if ( originalDimensions ) { |
| 196 |
config.originalDimensions = ve.copy( originalDimensions ); |
| 197 |
} |
| 198 |
if ( defaultDimensions ) { |
| 199 |
config.defaultDimensions = ve.copy( defaultDimensions ); |
| 200 |
} |
| 201 |
if ( minDimensions ) { |
| 202 |
config.minDimensions = ve.copy( minDimensions ); |
| 203 |
} |
| 204 |
if ( maxDimensions ) { |
| 205 |
config.maxDimensions = ve.copy( maxDimensions ); |
| 206 |
} |
| 207 |
return new this.constructor( config ); |
| 208 |
}; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
ve.dm.Scalable.prototype.setRatioFromDimensions = function ( dimensions ) { |
| 216 |
if ( dimensions && dimensions.width && dimensions.height ) { |
| 217 |
this.ratio = dimensions.width / dimensions.height; |
| 218 |
} |
| 219 |
this.valid = null; |
| 220 |
}; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
ve.dm.Scalable.prototype.setCurrentDimensions = function ( dimensions ) { |
| 231 |
if ( |
| 232 |
this.constructor.static.isDimensionsObjectValid( dimensions ) && |
| 233 |
!ve.compare( dimensions, this.getCurrentDimensions() ) |
| 234 |
) { |
| 235 |
this.currentDimensions = ve.copy( dimensions ); |
| 236 |
|
| 237 |
if ( this.fixedRatio && !this.ratio ) { |
| 238 |
this.setRatioFromDimensions( this.getCurrentDimensions() ); |
| 239 |
} |
| 240 |
this.valid = null; |
| 241 |
this.emit( 'currentSizeChange', this.getCurrentDimensions() ); |
| 242 |
} |
| 243 |
}; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
ve.dm.Scalable.prototype.setOriginalDimensions = function ( dimensions ) { |
| 254 |
if ( |
| 255 |
this.constructor.static.isDimensionsObjectValid( dimensions ) && |
| 256 |
!ve.compare( dimensions, this.getOriginalDimensions() ) |
| 257 |
) { |
| 258 |
this.originalDimensions = ve.copy( dimensions ); |
| 259 |
|
| 260 |
if ( this.fixedRatio ) { |
| 261 |
this.setRatioFromDimensions( this.getOriginalDimensions() ); |
| 262 |
} |
| 263 |
this.valid = null; |
| 264 |
this.emit( 'originalSizeChange', this.getOriginalDimensions() ); |
| 265 |
} |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
ve.dm.Scalable.prototype.setDefaultDimensions = function ( dimensions ) { |
| 275 |
if ( |
| 276 |
this.constructor.static.isDimensionsObjectValid( dimensions ) && |
| 277 |
!ve.compare( dimensions, this.getDefaultDimensions() ) |
| 278 |
) { |
| 279 |
this.defaultDimensions = ve.copy( dimensions ); |
| 280 |
this.valid = null; |
| 281 |
this.emit( 'defaultSizeChange', this.isDefault() ); |
| 282 |
} |
| 283 |
}; |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
ve.dm.Scalable.prototype.clearDefaultDimensions = function () { |
| 291 |
if ( this.defaultDimensions !== null ) { |
| 292 |
this.defaultDimensions = null; |
| 293 |
this.valid = null; |
| 294 |
this.emit( 'defaultSizeChange', this.isDefault() ); |
| 295 |
} |
| 296 |
}; |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
ve.dm.Scalable.prototype.clearOriginalDimensions = function () { |
| 304 |
if ( this.originalDimensions !== null ) { |
| 305 |
this.originalDimensions = null; |
| 306 |
this.valid = null; |
| 307 |
this.emit( 'originalSizeChange', this.isDefault() ); |
| 308 |
} |
| 309 |
}; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
ve.dm.Scalable.prototype.toggleDefault = function ( isDefault ) { |
| 318 |
if ( isDefault === undefined ) { |
| 319 |
isDefault = !this.isDefault(); |
| 320 |
} |
| 321 |
if ( this.isDefault() !== isDefault ) { |
| 322 |
this.defaultSize = isDefault; |
| 323 |
if ( isDefault ) { |
| 324 |
this.setCurrentDimensions( |
| 325 |
this.getDefaultDimensions() |
| 326 |
); |
| 327 |
} |
| 328 |
this.emit( 'defaultSizeChange', this.isDefault() ); |
| 329 |
} |
| 330 |
}; |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
ve.dm.Scalable.prototype.setMinDimensions = function ( dimensions ) { |
| 339 |
if ( |
| 340 |
this.constructor.static.isDimensionsObjectValid( dimensions ) && |
| 341 |
!ve.compare( dimensions, this.getMinDimensions() ) |
| 342 |
) { |
| 343 |
this.minDimensions = ve.copy( dimensions ); |
| 344 |
this.valid = null; |
| 345 |
this.emit( 'minSizeChange', dimensions ); |
| 346 |
} |
| 347 |
}; |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
ve.dm.Scalable.prototype.setMaxDimensions = function ( dimensions ) { |
| 356 |
if ( |
| 357 |
this.constructor.static.isDimensionsObjectValid( dimensions ) && |
| 358 |
!ve.compare( dimensions, this.getMaxDimensions() ) |
| 359 |
) { |
| 360 |
this.maxDimensions = ve.copy( dimensions ); |
| 361 |
this.emit( 'maxSizeChange', dimensions ); |
| 362 |
this.valid = null; |
| 363 |
} |
| 364 |
}; |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
ve.dm.Scalable.prototype.clearMinDimensions = function () { |
| 372 |
if ( this.minDimensions !== null ) { |
| 373 |
this.minDimensions = null; |
| 374 |
this.valid = null; |
| 375 |
this.emit( 'minSizeChange', this.minDimensions ); |
| 376 |
} |
| 377 |
}; |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
ve.dm.Scalable.prototype.clearMaxDimensions = function () { |
| 385 |
if ( this.maxDimensions !== null ) { |
| 386 |
this.maxDimensions = null; |
| 387 |
this.valid = null; |
| 388 |
this.emit( 'maxSizeChange', this.maxDimensions ); |
| 389 |
} |
| 390 |
}; |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
ve.dm.Scalable.prototype.getCurrentDimensions = function () { |
| 398 |
return this.currentDimensions; |
| 399 |
}; |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
ve.dm.Scalable.prototype.getOriginalDimensions = function () { |
| 407 |
return this.originalDimensions; |
| 408 |
}; |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
ve.dm.Scalable.prototype.getDefaultDimensions = function () { |
| 416 |
return this.defaultDimensions; |
| 417 |
}; |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
ve.dm.Scalable.prototype.isDefault = function () { |
| 425 |
return this.defaultSize; |
| 426 |
}; |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
ve.dm.Scalable.prototype.getMinDimensions = function () { |
| 434 |
return this.minDimensions; |
| 435 |
}; |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
ve.dm.Scalable.prototype.getMaxDimensions = function () { |
| 443 |
return this.maxDimensions; |
| 444 |
}; |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
ve.dm.Scalable.prototype.isEnforcedMin = function () { |
| 452 |
return this.enforceMin; |
| 453 |
}; |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
ve.dm.Scalable.prototype.isEnforcedMax = function () { |
| 461 |
return this.enforceMax; |
| 462 |
}; |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
ve.dm.Scalable.prototype.setEnforcedMin = function ( enforceMin ) { |
| 470 |
this.valid = null; |
| 471 |
this.enforceMin = !!enforceMin; |
| 472 |
}; |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
ve.dm.Scalable.prototype.setEnforcedMax = function ( enforceMax ) { |
| 480 |
this.valid = null; |
| 481 |
this.enforceMax = !!enforceMax; |
| 482 |
}; |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
ve.dm.Scalable.prototype.getRatio = function () { |
| 490 |
return this.ratio; |
| 491 |
}; |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
ve.dm.Scalable.prototype.isFixedRatio = function () { |
| 499 |
return this.fixedRatio; |
| 500 |
}; |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
ve.dm.Scalable.prototype.getCurrentScale = function () { |
| 508 |
if ( !this.isFixedRatio() || !this.getCurrentDimensions() || !this.getOriginalDimensions() ) { |
| 509 |
return null; |
| 510 |
} |
| 511 |
return this.getCurrentDimensions().width / this.getOriginalDimensions().width; |
| 512 |
}; |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
ve.dm.Scalable.prototype.isTooSmall = function () { |
| 522 |
return !!( this.getCurrentDimensions() && this.getMinDimensions() && |
| 523 |
( |
| 524 |
this.getCurrentDimensions().width < this.getMinDimensions().width || |
| 525 |
this.getCurrentDimensions().height < this.getMinDimensions().height |
| 526 |
) |
| 527 |
); |
| 528 |
}; |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
ve.dm.Scalable.prototype.isTooLarge = function () { |
| 538 |
return !!( this.getCurrentDimensions() && this.getMaxDimensions() && |
| 539 |
( |
| 540 |
this.getCurrentDimensions().width > this.getMaxDimensions().width || |
| 541 |
this.getCurrentDimensions().height > this.getMaxDimensions().height |
| 542 |
) |
| 543 |
); |
| 544 |
}; |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
ve.dm.Scalable.prototype.getBoundedDimensions = function ( dimensions, grid ) { |
| 554 |
const minDimensions = this.isEnforcedMin() && this.getMinDimensions(), |
| 555 |
maxDimensions = this.isEnforcedMax() && this.getMaxDimensions(); |
| 556 |
|
| 557 |
dimensions = ve.copy( dimensions ); |
| 558 |
|
| 559 |
|
| 560 |
if ( minDimensions ) { |
| 561 |
dimensions.width = Math.max( dimensions.width, this.minDimensions.width ); |
| 562 |
dimensions.height = Math.max( dimensions.height, this.minDimensions.height ); |
| 563 |
} |
| 564 |
if ( maxDimensions ) { |
| 565 |
dimensions.width = Math.min( dimensions.width, this.maxDimensions.width ); |
| 566 |
dimensions.height = Math.min( dimensions.height, this.maxDimensions.height ); |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
if ( this.isFixedRatio() ) { |
| 571 |
const ratio = dimensions.width / dimensions.height; |
| 572 |
if ( ratio < this.getRatio() ) { |
| 573 |
dimensions.height = Math.round( dimensions.width / this.getRatio() ); |
| 574 |
} else { |
| 575 |
dimensions.width = Math.round( dimensions.height * this.getRatio() ); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
if ( grid ) { |
| 581 |
let snapMin = minDimensions ? Math.ceil( minDimensions.width / grid ) : -Infinity; |
| 582 |
let snapMax = maxDimensions ? Math.floor( maxDimensions.width / grid ) : Infinity; |
| 583 |
let snap = Math.round( dimensions.width / grid ); |
| 584 |
dimensions.width = Math.max( Math.min( snap, snapMax ), snapMin ) * grid; |
| 585 |
if ( this.isFixedRatio() ) { |
| 586 |
|
| 587 |
dimensions.height = Math.round( dimensions.width / this.getRatio() ); |
| 588 |
} else { |
| 589 |
snapMin = minDimensions ? Math.ceil( minDimensions.height / grid ) : -Infinity; |
| 590 |
snapMax = maxDimensions ? Math.floor( maxDimensions.height / grid ) : Infinity; |
| 591 |
snap = Math.round( dimensions.height / grid ); |
| 592 |
dimensions.height = Math.max( Math.min( snap, snapMax ), snapMin ) * grid; |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
return dimensions; |
| 597 |
}; |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
ve.dm.Scalable.prototype.isCurrentDimensionsValid = function () { |
| 605 |
const dimensions = this.getCurrentDimensions(), |
| 606 |
minDimensions = this.isEnforcedMin() && !ve.isEmptyObject( this.getMinDimensions() ) && this.getMinDimensions(), |
| 607 |
maxDimensions = this.isEnforcedMax() && !ve.isEmptyObject( this.getMaxDimensions() ) && this.getMaxDimensions(); |
| 608 |
|
| 609 |
this.valid = ( |
| 610 |
!!dimensions && |
| 611 |
|
| 612 |
+dimensions.width && |
| 613 |
+dimensions.height && |
| 614 |
( |
| 615 |
!minDimensions || ( |
| 616 |
dimensions.width >= minDimensions.width && |
| 617 |
dimensions.height >= minDimensions.height |
| 618 |
) |
| 619 |
) && |
| 620 |
( |
| 621 |
!maxDimensions || ( |
| 622 |
dimensions.width <= maxDimensions.width && |
| 623 |
dimensions.height <= maxDimensions.height |
| 624 |
) |
| 625 |
) |
| 626 |
); |
| 627 |
return this.valid; |
| 628 |
}; |