| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
ve.dm.MWImageModel = function VeDmMWImageModel( parentDoc, config ) { |
| 23 |
config = config || {}; |
| 24 |
|
| 25 |
|
| 26 |
OO.EventEmitter.call( this ); |
| 27 |
|
| 28 |
|
| 29 |
this.attributesCache = {}; |
| 30 |
|
| 31 |
|
| 32 |
this.parentDoc = parentDoc; |
| 33 |
this.captionDoc = null; |
| 34 |
this.caption = null; |
| 35 |
this.mediaType = null; |
| 36 |
this.altText = ''; |
| 37 |
this.type = null; |
| 38 |
this.alignment = null; |
| 39 |
this.scalable = null; |
| 40 |
this.sizeType = null; |
| 41 |
this.border = false; |
| 42 |
this.borderable = false; |
| 43 |
this.defaultDimensions = null; |
| 44 |
this.changedImageSource = false; |
| 45 |
|
| 46 |
this.imageSrc = ''; |
| 47 |
this.imageResourceName = ''; |
| 48 |
this.imageHref = ''; |
| 49 |
this.imageClassAttr = null; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
this.imgWrapperClassAttr = null; |
| 55 |
|
| 56 |
this.boundingBox = null; |
| 57 |
this.initialHash = {}; |
| 58 |
|
| 59 |
|
| 60 |
this.defaultThumbSize = mw.config.get( 'wgVisualEditorConfig' ) |
| 61 |
.thumbLimits[ mw.user.options.get( 'thumbsize' ) ]; |
| 62 |
|
| 63 |
if ( config.resourceName ) { |
| 64 |
this.setImageResourceName( config.resourceName ); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
const currentDimensions = config.currentDimensions || {}; |
| 69 |
const minDimensions = config.minDimensions || {}; |
| 70 |
|
| 71 |
const scalable = new ve.dm.Scalable( { |
| 72 |
currentDimensions: { |
| 73 |
width: currentDimensions.width, |
| 74 |
height: currentDimensions.height |
| 75 |
}, |
| 76 |
minDimensions: { |
| 77 |
width: minDimensions.width || 1, |
| 78 |
height: minDimensions.height || 1 |
| 79 |
}, |
| 80 |
defaultSize: !!config.isDefaultSize |
| 81 |
} ); |
| 82 |
|
| 83 |
|
| 84 |
this.attachScalable( scalable ); |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
OO.mixinClass( ve.dm.MWImageModel, OO.EventEmitter ); |
| 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 |
ve.dm.MWImageModel.static.infoCache = {}; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
ve.dm.MWImageModel.static.createImageNode = function ( attributes, imageType ) { |
| 129 |
const defaultThumbSize = mw.config.get( 'wgVisualEditorConfig' ) |
| 130 |
.thumbLimits[ mw.user.options.get( 'thumbsize' ) ]; |
| 131 |
|
| 132 |
const attrs = ve.extendObject( { |
| 133 |
mediaClass: 'File', |
| 134 |
mediaTag: 'img', |
| 135 |
type: 'thumb', |
| 136 |
align: 'default', |
| 137 |
width: defaultThumbSize, |
| 138 |
mediaType: 'BITMAP', |
| 139 |
defaultSize: true, |
| 140 |
imageClassAttr: 'mw-file-element' |
| 141 |
}, attributes ); |
| 142 |
|
| 143 |
if ( attrs.defaultSize ) { |
| 144 |
const newDimensions = ve.dm.MWImageNode.static.scaleToThumbnailSize( attrs, attrs.mediaType ); |
| 145 |
if ( newDimensions ) { |
| 146 |
attrs.width = newDimensions.width; |
| 147 |
attrs.height = newDimensions.height; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
imageType = imageType || 'mwBlockImage'; |
| 152 |
|
| 153 |
const newNode = ve.dm.nodeFactory.createFromElement( { |
| 154 |
type: imageType, |
| 155 |
attributes: attrs |
| 156 |
} ); |
| 157 |
|
| 158 |
ve.dm.MWImageNode.static.syncScalableToType( attrs.type, attrs.mediaType, newNode.getScalable() ); |
| 159 |
|
| 160 |
return newNode; |
| 161 |
}; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
ve.dm.MWImageModel.static.newFromImageAttributes = function ( attrs, parentDoc ) { |
| 171 |
const imgModel = new ve.dm.MWImageModel( |
| 172 |
parentDoc, |
| 173 |
{ |
| 174 |
resourceName: attrs.resource, |
| 175 |
currentDimensions: { |
| 176 |
width: attrs.width, |
| 177 |
height: attrs.height |
| 178 |
}, |
| 179 |
defaultSize: !!attrs.defaultSize |
| 180 |
} |
| 181 |
); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
imgModel.cacheOriginalImageAttributes( attrs ); |
| 186 |
|
| 187 |
imgModel.setImageSource( attrs.src ); |
| 188 |
imgModel.setFilename( new mw.Title( mw.libs.ve.normalizeParsoidResourceName( attrs.resource ) ).getMainText() ); |
| 189 |
imgModel.setImageHref( attrs.href ); |
| 190 |
imgModel.setImageClassAttr( attrs.imageClassAttr ); |
| 191 |
imgModel.setImgWrapperClassAttr( attrs.imgWrapperClassAttr ); |
| 192 |
|
| 193 |
|
| 194 |
imgModel.setBoundingBox( { |
| 195 |
width: attrs.width, |
| 196 |
height: attrs.height |
| 197 |
} ); |
| 198 |
|
| 199 |
|
| 200 |
imgModel.toggleBorder( !!attrs.borderImage ); |
| 201 |
imgModel.setAltText( attrs.alt || '' ); |
| 202 |
|
| 203 |
imgModel.setType( attrs.type ); |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
imgModel.setAlignment( attrs.align || 'default' ); |
| 210 |
|
| 211 |
|
| 212 |
imgModel.toggleDefaultSize( !!attrs.defaultSize ); |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
imgModel.setSizeType( imgModel.isDefaultSize() ? 'default' : 'custom' ); |
| 217 |
|
| 218 |
return imgModel; |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
ve.dm.MWImageModel.static.newFromImageNode = function ( node ) { |
| 228 |
return ve.dm.MWImageModel.static.newFromImageAttributes( node.getAttributes(), node.getDocument() ); |
| 229 |
}; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
ve.dm.MWImageModel.prototype.getHashObject = function () { |
| 239 |
const hash = { |
| 240 |
filename: this.getFilename(), |
| 241 |
altText: this.getAltText(), |
| 242 |
type: this.getType(), |
| 243 |
alignment: this.getAlignment(), |
| 244 |
sizeType: this.getSizeType(), |
| 245 |
border: this.hasBorder(), |
| 246 |
borderable: this.isBorderable() |
| 247 |
}; |
| 248 |
|
| 249 |
if ( this.getScalable() ) { |
| 250 |
hash.scalable = { |
| 251 |
currentDimensions: ve.copy( this.getScalable().getCurrentDimensions() ), |
| 252 |
isDefault: this.getScalable().isDefault() |
| 253 |
}; |
| 254 |
} |
| 255 |
return hash; |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
ve.dm.MWImageModel.prototype.getNormalizedImageSource = function () { |
| 270 |
|
| 271 |
return this.getImageSource().replace( /^https?:\/\//, '//' ); |
| 272 |
}; |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
ve.dm.MWImageModel.prototype.changeImageSource = function ( attrs, APIinfo ) { |
| 282 |
this.changedImageSource = true; |
| 283 |
|
| 284 |
if ( attrs.mediaType ) { |
| 285 |
this.setMediaType( attrs.mediaType ); |
| 286 |
} |
| 287 |
if ( attrs.href ) { |
| 288 |
this.setImageHref( attrs.href ); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
if ( attrs.imageClassAttr ) { |
| 293 |
this.setImageClassAttr( attrs.imageClassAttr ); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
if ( attrs.imgWrapperClassAttr ) { |
| 298 |
this.setImgWrapperClassAttr( attrs.imgWrapperClassAttr ); |
| 299 |
} |
| 300 |
|
| 301 |
if ( attrs.resource ) { |
| 302 |
this.setImageResourceName( attrs.resource ); |
| 303 |
this.setFilename( new mw.Title( mw.libs.ve.normalizeParsoidResourceName( attrs.resource ) ).getMainText() ); |
| 304 |
} |
| 305 |
|
| 306 |
if ( attrs.src ) { |
| 307 |
this.setImageSource( attrs.src ); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
this.scalable.clearOriginalDimensions(); |
| 312 |
this.scalable.clearDefaultDimensions(); |
| 313 |
this.scalable.clearMaxDimensions(); |
| 314 |
this.scalable.clearMinDimensions(); |
| 315 |
|
| 316 |
delete this.attributesCache.originalWidth; |
| 317 |
delete this.attributesCache.originalHeight; |
| 318 |
|
| 319 |
|
| 320 |
if ( APIinfo ) { |
| 321 |
this.scalable.setOriginalDimensions( { |
| 322 |
width: APIinfo.width, |
| 323 |
height: APIinfo.height |
| 324 |
} ); |
| 325 |
|
| 326 |
this.setMediaType( APIinfo.mediatype ); |
| 327 |
|
| 328 |
ve.dm.MWImageNode.static.syncScalableToType( |
| 329 |
this.getType(), |
| 330 |
APIinfo.mediatype, |
| 331 |
this.scalable |
| 332 |
); |
| 333 |
this.updateScalableDetails( { |
| 334 |
width: APIinfo.width, |
| 335 |
height: APIinfo.height |
| 336 |
} ); |
| 337 |
} else { |
| 338 |
|
| 339 |
if ( this.getFilename() ) { |
| 340 |
|
| 341 |
ve.dm.MWImageNode.static.getScalablePromise( this.getFilename() ).done( ( info ) => { |
| 342 |
this.scalable.setOriginalDimensions( { |
| 343 |
width: info.width, |
| 344 |
height: info.height |
| 345 |
} ); |
| 346 |
|
| 347 |
this.setMediaType( info.mediatype ); |
| 348 |
|
| 349 |
ve.dm.MWImageNode.static.syncScalableToType( |
| 350 |
this.getType(), |
| 351 |
info.mediatype, |
| 352 |
this.scalable |
| 353 |
); |
| 354 |
this.updateScalableDetails( { |
| 355 |
width: info.width, |
| 356 |
height: info.height |
| 357 |
} ); |
| 358 |
} ); |
| 359 |
} else { |
| 360 |
throw new Error( 'Cannot compute details for an image without remote filename and without sizing info.' ); |
| 361 |
} |
| 362 |
} |
| 363 |
}; |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
ve.dm.MWImageModel.prototype.getImageNodeType = function ( imageType, align ) { |
| 376 |
imageType = imageType || this.getType(); |
| 377 |
|
| 378 |
if ( |
| 379 |
( this.getType() === 'frameless' || this.getType() === 'none' ) && |
| 380 |
( !this.isAligned( align ) || this.isDefaultAligned( imageType, align ) ) |
| 381 |
) { |
| 382 |
return 'mwInlineImage'; |
| 383 |
} else { |
| 384 |
return 'mwBlockImage'; |
| 385 |
} |
| 386 |
}; |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
ve.dm.MWImageModel.prototype.getBoundingBox = function () { |
| 394 |
return this.boundingBox; |
| 395 |
}; |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
ve.dm.MWImageModel.prototype.updateImageNode = function ( node, surfaceModel ) { |
| 404 |
const doc = surfaceModel.getDocument(); |
| 405 |
|
| 406 |
|
| 407 |
if ( node.getType() === 'mwBlockImage' ) { |
| 408 |
let captionNode = node.getCaptionNode(); |
| 409 |
if ( !captionNode ) { |
| 410 |
|
| 411 |
surfaceModel.getFragment() |
| 412 |
.adjustLinearSelection( 1 ) |
| 413 |
.collapseToStart() |
| 414 |
.insertContent( [ { type: 'mwImageCaption' }, { type: '/mwImageCaption' } ] ); |
| 415 |
|
| 416 |
captionNode = node.getCaptionNode(); |
| 417 |
} |
| 418 |
|
| 419 |
const captionRange = captionNode.getRange(); |
| 420 |
|
| 421 |
|
| 422 |
surfaceModel.change( |
| 423 |
ve.dm.TransactionBuilder.static.newFromRemoval( |
| 424 |
doc, |
| 425 |
captionRange, |
| 426 |
true |
| 427 |
) |
| 428 |
); |
| 429 |
|
| 430 |
|
| 431 |
surfaceModel.change( |
| 432 |
ve.dm.TransactionBuilder.static.newFromDocumentInsertion( |
| 433 |
doc, |
| 434 |
captionRange.start, |
| 435 |
this.getCaptionDocument() |
| 436 |
) |
| 437 |
); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
surfaceModel.change( |
| 442 |
ve.dm.TransactionBuilder.static.newFromAttributeChanges( |
| 443 |
doc, |
| 444 |
node.getOffset(), |
| 445 |
this.getUpdatedAttributes() |
| 446 |
) |
| 447 |
); |
| 448 |
}; |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
ve.dm.MWImageModel.prototype.insertImageNode = function ( fragment ) { |
| 460 |
const nodeType = this.getImageNodeType(), |
| 461 |
surfaceModel = fragment.getSurface(); |
| 462 |
|
| 463 |
if ( !( fragment.getSelection() instanceof ve.dm.LinearSelection ) ) { |
| 464 |
return fragment; |
| 465 |
} |
| 466 |
|
| 467 |
const selectedNode = fragment.getSelectedNode(); |
| 468 |
|
| 469 |
|
| 470 |
if ( selectedNode ) { |
| 471 |
|
| 472 |
fragment.removeContent(); |
| 473 |
} |
| 474 |
|
| 475 |
const contentToInsert = this.getData(); |
| 476 |
|
| 477 |
let offset; |
| 478 |
switch ( nodeType ) { |
| 479 |
case 'mwInlineImage': |
| 480 |
if ( selectedNode && selectedNode.type === 'mwBlockImage' ) { |
| 481 |
|
| 482 |
fragment.insertContent( [ { type: 'paragraph', internal: { generated: 'wrapper' } }, { type: '/paragraph' } ] ); |
| 483 |
offset = fragment.getSelection().getRange().start + 1; |
| 484 |
} else { |
| 485 |
|
| 486 |
offset = fragment.getDocument().data.getNearestContentOffset( fragment.getSelection().getRange().start ); |
| 487 |
} |
| 488 |
if ( offset > -1 ) { |
| 489 |
fragment = fragment.clone( new ve.dm.LinearSelection( new ve.Range( offset ) ) ); |
| 490 |
} |
| 491 |
fragment.insertContent( contentToInsert ); |
| 492 |
return fragment; |
| 493 |
|
| 494 |
case 'mwBlockImage': |
| 495 |
|
| 496 |
offset = fragment.getDocument().data.getNearestStructuralOffset( fragment.getSelection().getRange().start, -1 ); |
| 497 |
if ( offset > -1 ) { |
| 498 |
fragment = fragment.clone( new ve.dm.LinearSelection( new ve.Range( offset ) ) ); |
| 499 |
} |
| 500 |
fragment.insertContent( contentToInsert ); |
| 501 |
|
| 502 |
surfaceModel.change( |
| 503 |
ve.dm.TransactionBuilder.static.newFromDocumentInsertion( |
| 504 |
surfaceModel.getDocument(), |
| 505 |
fragment.getSelection().getRange().start + 2, |
| 506 |
this.getCaptionDocument() |
| 507 |
) |
| 508 |
); |
| 509 |
return fragment; |
| 510 |
|
| 511 |
default: |
| 512 |
throw new Error( 'Unknown image node type ' + nodeType ); |
| 513 |
} |
| 514 |
}; |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
ve.dm.MWImageModel.prototype.getData = function () { |
| 522 |
const originalAttrs = ve.copy( this.getOriginalImageAttributes() ), |
| 523 |
editAttributes = ve.extendObject( originalAttrs, this.getUpdatedAttributes() ), |
| 524 |
nodeType = this.getImageNodeType(); |
| 525 |
|
| 526 |
|
| 527 |
delete editAttributes.originalClasses; |
| 528 |
delete editAttributes.unrecognizedClasses; |
| 529 |
|
| 530 |
if ( this.isChangedImageSource() ) { |
| 531 |
delete editAttributes.isError; |
| 532 |
} |
| 533 |
|
| 534 |
const data = [ |
| 535 |
{ |
| 536 |
type: nodeType, |
| 537 |
attributes: editAttributes |
| 538 |
}, |
| 539 |
{ type: '/' + nodeType } |
| 540 |
]; |
| 541 |
|
| 542 |
if ( nodeType === 'mwBlockImage' ) { |
| 543 |
data.splice( 1, 0, { type: 'mwImageCaption' }, { type: '/mwImageCaption' } ); |
| 544 |
} |
| 545 |
return data; |
| 546 |
}; |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
ve.dm.MWImageModel.prototype.getUpdatedAttributes = function () { |
| 554 |
const origAttrs = this.getOriginalImageAttributes(); |
| 555 |
|
| 556 |
let currentDimensions; |
| 557 |
|
| 558 |
if ( this.scalable.isDefault() && this.scalable.getDefaultDimensions() ) { |
| 559 |
currentDimensions = this.scalable.getDefaultDimensions(); |
| 560 |
} else { |
| 561 |
currentDimensions = this.getCurrentDimensions(); |
| 562 |
} |
| 563 |
|
| 564 |
const attrs = { |
| 565 |
mediaClass: 'File', |
| 566 |
mediaTag: this.getMediaTag(), |
| 567 |
type: this.getType(), |
| 568 |
width: currentDimensions.width, |
| 569 |
height: currentDimensions.height, |
| 570 |
defaultSize: this.isDefaultSize(), |
| 571 |
borderImage: this.hasBorder() |
| 572 |
}; |
| 573 |
|
| 574 |
if ( this.getAltText() || typeof origAttrs.alt === 'string' ) { |
| 575 |
attrs.alt = this.getAltText(); |
| 576 |
} |
| 577 |
|
| 578 |
if ( this.isDefaultAligned() ) { |
| 579 |
attrs.align = 'default'; |
| 580 |
} else if ( !this.isAligned() ) { |
| 581 |
attrs.align = 'none'; |
| 582 |
} else { |
| 583 |
attrs.align = this.getAlignment(); |
| 584 |
} |
| 585 |
|
| 586 |
attrs.src = this.getImageSource(); |
| 587 |
attrs.href = this.getImageHref(); |
| 588 |
attrs.imageClassAttr = this.getImageClassAttr(); |
| 589 |
attrs.imgWrapperClassAttr = this.getImgWrapperClassAttr(); |
| 590 |
attrs.resource = this.getImageResourceName(); |
| 591 |
|
| 592 |
return attrs; |
| 593 |
}; |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
ve.dm.MWImageModel.prototype.onScalableDefaultSizeChange = function ( isDefault ) { |
| 601 |
this.toggleDefaultSize( isDefault ); |
| 602 |
}; |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
ve.dm.MWImageModel.prototype.setImageSource = function ( src ) { |
| 610 |
this.imageSrc = src; |
| 611 |
}; |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
ve.dm.MWImageModel.prototype.setImageResourceName = function ( resourceName ) { |
| 619 |
this.imageResourceName = resourceName; |
| 620 |
}; |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
ve.dm.MWImageModel.prototype.setImageHref = function ( href ) { |
| 628 |
this.imageHref = href; |
| 629 |
}; |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
ve.dm.MWImageModel.prototype.setBoundingBox = function ( box ) { |
| 637 |
this.boundingBox = box; |
| 638 |
}; |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
ve.dm.MWImageModel.prototype.storeInitialHash = function ( hash ) { |
| 647 |
this.initialHash = hash; |
| 648 |
}; |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
ve.dm.MWImageModel.prototype.setMediaType = function ( type ) { |
| 658 |
this.mediaType = type; |
| 659 |
}; |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
ve.dm.MWImageModel.prototype.isDefaultSize = function () { |
| 667 |
|
| 668 |
return this.scalable.isDefault() || this.getType() === 'frame'; |
| 669 |
}; |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
ve.dm.MWImageModel.prototype.hasBorder = function () { |
| 677 |
return this.border; |
| 678 |
}; |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
ve.dm.MWImageModel.prototype.isChangedImageSource = function () { |
| 686 |
return this.changedImageSource; |
| 687 |
}; |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
ve.dm.MWImageModel.prototype.isAligned = function ( align ) { |
| 696 |
align = align || this.alignment; |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
return align && align !== 'none'; |
| 701 |
}; |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
ve.dm.MWImageModel.prototype.isDefaultAligned = function ( imageType, align ) { |
| 715 |
const alignment = align || this.getAlignment(), |
| 716 |
defaultAlignment = ( this.parentDoc.getDir() === 'rtl' ) ? 'left' : 'right'; |
| 717 |
|
| 718 |
imageType = imageType || this.getType(); |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
if ( !alignment ) { |
| 726 |
return true; |
| 727 |
} |
| 728 |
|
| 729 |
if ( |
| 730 |
( |
| 731 |
( imageType === 'frameless' || imageType === 'none' ) && |
| 732 |
alignment === 'none' |
| 733 |
) || |
| 734 |
( |
| 735 |
( imageType === 'thumb' || imageType === 'frame' ) && |
| 736 |
alignment === defaultAlignment |
| 737 |
) |
| 738 |
) { |
| 739 |
return true; |
| 740 |
} |
| 741 |
|
| 742 |
return false; |
| 743 |
}; |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
ve.dm.MWImageModel.prototype.isBorderable = function () { |
| 751 |
return this.borderable; |
| 752 |
}; |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
ve.dm.MWImageModel.prototype.getResourceName = function () { |
| 760 |
return this.imageResourceName; |
| 761 |
}; |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
ve.dm.MWImageModel.prototype.getAltText = function () { |
| 769 |
return this.altText || ''; |
| 770 |
}; |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
ve.dm.MWImageModel.prototype.getType = function () { |
| 778 |
return this.type; |
| 779 |
}; |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
ve.dm.MWImageModel.prototype.getSizeType = function () { |
| 787 |
return this.sizeType; |
| 788 |
}; |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
ve.dm.MWImageModel.prototype.getMediaType = function () { |
| 798 |
return this.mediaType; |
| 799 |
}; |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
ve.dm.MWImageModel.prototype.getMediaTag = function () { |
| 807 |
const mediaType = this.getMediaType(); |
| 808 |
|
| 809 |
if ( mediaType === 'VIDEO' ) { |
| 810 |
return 'video'; |
| 811 |
} |
| 812 |
if ( mediaType === 'AUDIO' ) { |
| 813 |
return 'audio'; |
| 814 |
} |
| 815 |
return 'img'; |
| 816 |
}; |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
ve.dm.MWImageModel.prototype.getAlignment = function () { |
| 825 |
return this.alignment; |
| 826 |
}; |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
ve.dm.MWImageModel.prototype.getVerticalAlignment = function () { |
| 835 |
return this.verticalAlignment; |
| 836 |
}; |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
ve.dm.MWImageModel.prototype.getScalable = function () { |
| 845 |
return this.scalable; |
| 846 |
}; |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
ve.dm.MWImageModel.prototype.getCurrentDimensions = function () { |
| 861 |
return this.scalable.getCurrentDimensions(); |
| 862 |
}; |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
ve.dm.MWImageModel.prototype.getCaptionDocument = function () { |
| 872 |
if ( !this.captionDoc ) { |
| 873 |
this.captionDoc = this.parentDoc.cloneWithData( [ |
| 874 |
{ type: 'paragraph', internal: { generated: 'wrapper' } }, |
| 875 |
{ type: '/paragraph' }, |
| 876 |
{ type: 'internalList' }, |
| 877 |
{ type: '/internalList' } |
| 878 |
] ); |
| 879 |
} |
| 880 |
return this.captionDoc; |
| 881 |
}; |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
ve.dm.MWImageModel.prototype.toggleBorderable = function ( borderable ) { |
| 891 |
borderable = borderable !== undefined ? !!borderable : !this.isBorderable(); |
| 892 |
|
| 893 |
this.borderable = borderable; |
| 894 |
}; |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
ve.dm.MWImageModel.prototype.toggleBorder = function ( hasBorder ) { |
| 902 |
hasBorder = hasBorder !== undefined ? !!hasBorder : !this.hasBorder(); |
| 903 |
|
| 904 |
this.border = !!hasBorder; |
| 905 |
}; |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
ve.dm.MWImageModel.prototype.toggleDefaultSize = function ( isDefault ) { |
| 914 |
isDefault = isDefault !== undefined ? !!isDefault : !this.isDefaultSize(); |
| 915 |
|
| 916 |
if ( this.isDefaultSize() !== isDefault ) { |
| 917 |
this.scalable.toggleDefault( !!isDefault ); |
| 918 |
this.resetDefaultDimensions(); |
| 919 |
this.emit( 'sizeDefaultChange', !!isDefault ); |
| 920 |
} |
| 921 |
}; |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
ve.dm.MWImageModel.prototype.cacheOriginalImageAttributes = function ( attrs ) { |
| 929 |
this.attributesCache = attrs; |
| 930 |
}; |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
ve.dm.MWImageModel.prototype.getOriginalImageAttributes = function () { |
| 938 |
return this.attributesCache; |
| 939 |
}; |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
ve.dm.MWImageModel.prototype.setCurrentDimensions = function ( dimensions ) { |
| 950 |
const normalizedDimensions = ve.dm.Scalable.static.getDimensionsFromValue( dimensions, this.scalable.getRatio() ); |
| 951 |
this.scalable.setCurrentDimensions( normalizedDimensions ); |
| 952 |
}; |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
ve.dm.MWImageModel.prototype.setAltText = function ( text ) { |
| 960 |
this.altText = text; |
| 961 |
}; |
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
ve.dm.MWImageModel.prototype.setType = function ( type ) { |
| 972 |
const isDefaultAligned = this.isDefaultAligned( this.imageCurrentType ); |
| 973 |
|
| 974 |
this.type = type; |
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
if ( isDefaultAligned && this.imageCurrentType !== this.type ) { |
| 979 |
if ( this.type === 'none' || this.type === 'frameless' ) { |
| 980 |
|
| 981 |
this.setAlignment( 'none' ); |
| 982 |
} else { |
| 983 |
|
| 984 |
this.setAlignment( 'default' ); |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
this.imageCurrentType = type; |
| 990 |
|
| 991 |
if ( type === 'frame' || type === 'thumb' ) { |
| 992 |
|
| 993 |
this.toggleBorderable( false ); |
| 994 |
} else { |
| 995 |
|
| 996 |
this.toggleBorderable( true ); |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
if ( type === 'frame' ) { |
| 1001 |
this.toggleDefaultSize( true ); |
| 1002 |
} |
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
ve.dm.MWImageNode.static.syncScalableToType( type, this.getMediaType(), this.getScalable() ); |
| 1007 |
|
| 1008 |
this.emit( 'typeChange', type ); |
| 1009 |
}; |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
ve.dm.MWImageModel.prototype.resetDefaultDimensions = function () { |
| 1017 |
const originalDimensions = this.scalable.getOriginalDimensions(); |
| 1018 |
|
| 1019 |
if ( !ve.isEmptyObject( originalDimensions ) ) { |
| 1020 |
if ( this.getType() === 'thumb' || this.getType() === 'frameless' ) { |
| 1021 |
|
| 1022 |
if ( originalDimensions.width <= this.defaultThumbSize ) { |
| 1023 |
this.scalable.setDefaultDimensions( originalDimensions ); |
| 1024 |
} else { |
| 1025 |
this.scalable.setDefaultDimensions( |
| 1026 |
ve.dm.Scalable.static.getDimensionsFromValue( { |
| 1027 |
width: this.defaultThumbSize |
| 1028 |
}, this.scalable.getRatio() ) |
| 1029 |
); |
| 1030 |
} |
| 1031 |
} else { |
| 1032 |
|
| 1033 |
this.scalable.setDefaultDimensions( originalDimensions ); |
| 1034 |
} |
| 1035 |
} else { |
| 1036 |
this.scalable.clearDefaultDimensions(); |
| 1037 |
} |
| 1038 |
}; |
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
ve.dm.MWImageModel.prototype.getDefaultDimensions = function () { |
| 1047 |
return this.scalable.getDefaultDimensions(); |
| 1048 |
}; |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
ve.dm.MWImageModel.prototype.setSizeType = function ( type ) { |
| 1056 |
if ( this.sizeType !== type ) { |
| 1057 |
this.sizeType = type; |
| 1058 |
this.toggleDefaultSize( type === 'default' ); |
| 1059 |
} |
| 1060 |
}; |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
ve.dm.MWImageModel.prototype.setAlignment = function ( align ) { |
| 1071 |
if ( align === 'default' ) { |
| 1072 |
|
| 1073 |
align = this.getDefaultDir(); |
| 1074 |
} |
| 1075 |
|
| 1076 |
this.alignment = align; |
| 1077 |
this.emit( 'alignmentChange', align ); |
| 1078 |
}; |
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
ve.dm.MWImageModel.prototype.setVerticalAlignment = function ( valign ) { |
| 1089 |
this.verticalAlignment = valign; |
| 1090 |
this.emit( 'alignmentChange', valign ); |
| 1091 |
}; |
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
ve.dm.MWImageModel.prototype.getDefaultDir = function ( imageNodeType ) { |
| 1104 |
imageNodeType = imageNodeType || this.getImageNodeType(); |
| 1105 |
|
| 1106 |
if ( this.parentDoc.getDir() === 'rtl' ) { |
| 1107 |
|
| 1108 |
return ( imageNodeType === 'mwBlockImage' ) ? 'left' : 'none'; |
| 1109 |
} else { |
| 1110 |
|
| 1111 |
return ( imageNodeType === 'mwBlockImage' ) ? 'right' : 'none'; |
| 1112 |
} |
| 1113 |
}; |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
ve.dm.MWImageModel.prototype.getImageSource = function () { |
| 1124 |
return this.imageSrc; |
| 1125 |
}; |
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
ve.dm.MWImageModel.prototype.getImageResourceName = function () { |
| 1136 |
return this.imageResourceName; |
| 1137 |
}; |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
ve.dm.MWImageModel.prototype.getImageHref = function () { |
| 1149 |
return this.imageHref; |
| 1150 |
}; |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
ve.dm.MWImageModel.prototype.setImageClassAttr = function ( classAttr ) { |
| 1156 |
this.imageClassAttr = classAttr; |
| 1157 |
}; |
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
ve.dm.MWImageModel.prototype.getImageClassAttr = function () { |
| 1163 |
return this.imageClassAttr; |
| 1164 |
}; |
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
ve.dm.MWImageModel.prototype.setImgWrapperClassAttr = function ( classAttr ) { |
| 1170 |
this.imgWrapperClassAttr = classAttr; |
| 1171 |
}; |
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
ve.dm.MWImageModel.prototype.getImgWrapperClassAttr = function () { |
| 1177 |
return this.imgWrapperClassAttr; |
| 1178 |
}; |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
ve.dm.MWImageModel.prototype.attachScalable = function ( scalable ) { |
| 1187 |
const imageName = mw.libs.ve.normalizeParsoidResourceName( this.getResourceName() ); |
| 1188 |
|
| 1189 |
if ( this.scalable instanceof ve.dm.Scalable ) { |
| 1190 |
this.scalable.disconnect( this ); |
| 1191 |
} |
| 1192 |
this.scalable = scalable; |
| 1193 |
|
| 1194 |
|
| 1195 |
this.scalable.connect( this, { defaultSizeChange: 'onScalableDefaultSizeChange' } ); |
| 1196 |
|
| 1197 |
|
| 1198 |
if ( imageName ) { |
| 1199 |
ve.dm.MWImageNode.static.getScalablePromise( imageName ).done( ( info ) => { |
| 1200 |
this.scalable.setOriginalDimensions( { |
| 1201 |
width: info.width, |
| 1202 |
height: info.height |
| 1203 |
} ); |
| 1204 |
|
| 1205 |
this.setMediaType( info.mediatype ); |
| 1206 |
|
| 1207 |
ve.dm.MWImageNode.static.syncScalableToType( |
| 1208 |
this.getType(), |
| 1209 |
this.getMediaType(), |
| 1210 |
this.getScalable() |
| 1211 |
); |
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
if ( !ve.isEmptyObject( this.initialHash ) && this.initialHash.scalable.isDefault ) { |
| 1217 |
this.initialHash.scalable.currentDimensions = this.scalable.getDefaultDimensions(); |
| 1218 |
} |
| 1219 |
|
| 1220 |
} ); |
| 1221 |
} |
| 1222 |
}; |
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
ve.dm.MWImageModel.prototype.setFilename = function ( filename ) { |
| 1230 |
this.filename = filename; |
| 1231 |
}; |
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
ve.dm.MWImageModel.prototype.getFilename = function () { |
| 1239 |
return this.filename; |
| 1240 |
}; |
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
ve.dm.MWImageModel.prototype.updateScalableDetails = function ( originalDimensions ) { |
| 1248 |
let newDimensions; |
| 1249 |
|
| 1250 |
|
| 1251 |
if ( this.isDefaultSize() ) { |
| 1252 |
|
| 1253 |
newDimensions = ve.dm.MWImageNode.static.scaleToThumbnailSize( originalDimensions ); |
| 1254 |
} else { |
| 1255 |
if ( this.getBoundingBox() ) { |
| 1256 |
|
| 1257 |
newDimensions = ve.dm.MWImageNode.static.resizeToBoundingBox( |
| 1258 |
originalDimensions, |
| 1259 |
{ |
| 1260 |
width: this.boundingBox.width, |
| 1261 |
height: Infinity |
| 1262 |
} |
| 1263 |
); |
| 1264 |
} else { |
| 1265 |
newDimensions = originalDimensions; |
| 1266 |
} |
| 1267 |
} |
| 1268 |
|
| 1269 |
if ( newDimensions ) { |
| 1270 |
this.getScalable().setCurrentDimensions( newDimensions ); |
| 1271 |
} |
| 1272 |
}; |
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
ve.dm.MWImageModel.prototype.setCaptionDocument = function ( doc ) { |
| 1280 |
this.captionDoc = doc; |
| 1281 |
}; |
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
ve.dm.MWImageModel.prototype.hasBeenModified = function () { |
| 1290 |
if ( this.initialHash ) { |
| 1291 |
return !ve.compare( this.initialHash, this.getHashObject() ); |
| 1292 |
} |
| 1293 |
return true; |
| 1294 |
}; |