2014-03-25 16:01:04 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWImageModel class.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2014-03-25 16:01:04 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MediaWiki image model.
|
|
|
|
*
|
|
|
|
* @class
|
2024-04-29 17:51:41 +00:00
|
|
|
* @mixes OO.EventEmitter
|
2014-03-25 16:01:04 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2015-08-27 06:49:26 +00:00
|
|
|
* @param {ve.dm.Document} parentDoc Document that contains or will contain the image
|
2014-07-24 23:50:22 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2024-05-27 04:59:02 +00:00
|
|
|
* @param {string} [config.resourceName] The resource name of the given media file
|
|
|
|
* @param {Object} [config.currentDimensions={}] Current dimensions, width & height
|
|
|
|
* @param {Object} [config.minDimensions={}] Minimum dimensions, width & height
|
|
|
|
* @param {boolean} [config.isDefaultSize=false] Object is using its default size dimensions
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
2015-08-27 06:49:26 +00:00
|
|
|
ve.dm.MWImageModel = function VeDmMWImageModel( parentDoc, config ) {
|
2014-07-24 23:50:22 +00:00
|
|
|
config = config || {};
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
// Mixin constructors
|
|
|
|
OO.EventEmitter.call( this );
|
|
|
|
|
|
|
|
// Properties
|
2022-01-17 15:32:30 +00:00
|
|
|
this.attributesCache = {};
|
2014-03-25 16:01:04 +00:00
|
|
|
|
|
|
|
// Image properties
|
2015-08-27 06:49:26 +00:00
|
|
|
this.parentDoc = parentDoc;
|
2014-03-25 16:01:04 +00:00
|
|
|
this.captionDoc = null;
|
|
|
|
this.caption = null;
|
2014-07-17 19:55:24 +00:00
|
|
|
this.mediaType = null;
|
2014-07-25 02:28:07 +00:00
|
|
|
this.altText = '';
|
2014-03-25 16:01:04 +00:00
|
|
|
this.type = null;
|
|
|
|
this.alignment = null;
|
|
|
|
this.scalable = null;
|
|
|
|
this.sizeType = null;
|
|
|
|
this.border = false;
|
|
|
|
this.borderable = false;
|
|
|
|
this.defaultDimensions = null;
|
2015-06-16 16:45:35 +00:00
|
|
|
this.changedImageSource = false;
|
2014-03-25 16:01:04 +00:00
|
|
|
|
2014-07-25 02:28:07 +00:00
|
|
|
this.imageSrc = '';
|
|
|
|
this.imageResourceName = '';
|
|
|
|
this.imageHref = '';
|
2022-03-21 21:03:00 +00:00
|
|
|
this.imageClassAttr = null;
|
2014-07-25 02:28:07 +00:00
|
|
|
|
2022-03-09 19:33:42 +00:00
|
|
|
// FIXME: This is blindly being preserved but may not apply if, say,
|
|
|
|
// a link is no longer pointing to a file description page. When support
|
|
|
|
// for editing the |link= media option is added, take it into account.
|
|
|
|
this.imgWrapperClassAttr = null;
|
|
|
|
|
2014-07-25 02:28:07 +00:00
|
|
|
this.boundingBox = null;
|
2014-08-07 17:02:52 +00:00
|
|
|
this.initialHash = {};
|
2014-07-25 02:28:07 +00:00
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
// Get wiki default thumbnail size
|
|
|
|
this.defaultThumbSize = mw.config.get( 'wgVisualEditorConfig' )
|
2019-03-26 21:15:26 +00:00
|
|
|
.thumbLimits[ mw.user.options.get( 'thumbsize' ) ];
|
2014-07-24 23:50:22 +00:00
|
|
|
|
|
|
|
if ( config.resourceName ) {
|
2014-07-25 02:28:07 +00:00
|
|
|
this.setImageResourceName( config.resourceName );
|
2014-07-24 23:50:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create scalable
|
2024-05-21 14:22:56 +00:00
|
|
|
const currentDimensions = config.currentDimensions || {};
|
|
|
|
const minDimensions = config.minDimensions || {};
|
2014-07-24 23:50:22 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const scalable = new ve.dm.Scalable( {
|
2014-08-22 20:50:48 +00:00
|
|
|
currentDimensions: {
|
|
|
|
width: currentDimensions.width,
|
|
|
|
height: currentDimensions.height
|
2014-07-24 23:50:22 +00:00
|
|
|
},
|
2014-08-22 20:50:48 +00:00
|
|
|
minDimensions: {
|
|
|
|
width: minDimensions.width || 1,
|
|
|
|
height: minDimensions.height || 1
|
2014-07-24 23:50:22 +00:00
|
|
|
},
|
2014-08-22 20:50:48 +00:00
|
|
|
defaultSize: !!config.isDefaultSize
|
2014-07-24 23:50:22 +00:00
|
|
|
} );
|
|
|
|
// Set the initial scalable, connect it to events
|
|
|
|
// and request an update from the API
|
2014-07-25 02:28:07 +00:00
|
|
|
this.attachScalable( scalable );
|
2014-03-25 16:01:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.mixinClass( ve.dm.MWImageModel, OO.EventEmitter );
|
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change of image alignment or of having alignment at all
|
2014-06-26 10:41:18 +00:00
|
|
|
*
|
2024-04-29 11:52:40 +00:00
|
|
|
* @event ve.dm.MWImageModel#alignmentChange
|
2014-03-25 16:01:04 +00:00
|
|
|
* @param {string} Alignment 'left', 'right', 'center' or 'none'
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change in size type between default and custom
|
2014-06-26 10:41:18 +00:00
|
|
|
*
|
2024-04-29 11:52:40 +00:00
|
|
|
* @event ve.dm.MWImageModel#sizeDefaultChange
|
2014-03-25 16:01:04 +00:00
|
|
|
* @param {boolean} Image is default size
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change in the image type
|
2014-06-26 10:41:18 +00:00
|
|
|
*
|
2024-04-29 11:52:40 +00:00
|
|
|
* @event ve.dm.MWImageModel#typeChange
|
2014-03-25 16:01:04 +00:00
|
|
|
* @param {string} Image type 'thumb', 'frame', 'frameless' or 'none'
|
|
|
|
*/
|
2014-06-19 15:44:05 +00:00
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.dm.MWImageModel.static.infoCache = {};
|
|
|
|
|
|
|
|
/* Static Methods */
|
|
|
|
|
2014-06-19 15:44:05 +00:00
|
|
|
/**
|
|
|
|
* Create a new image node based on given parameters.
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-06-19 15:44:05 +00:00
|
|
|
* @param {Object} attributes Image attributes
|
|
|
|
* @param {string} [imageType] Image node type 'mwInlineImage' or 'mwBlockImage'.
|
|
|
|
* Defaults to 'mwBlockImage'
|
2015-03-07 04:43:53 +00:00
|
|
|
* @return {ve.dm.MWImageNode} An image node
|
2014-06-19 15:44:05 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.static.createImageNode = function ( attributes, imageType ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const defaultThumbSize = mw.config.get( 'wgVisualEditorConfig' )
|
2021-10-13 12:57:45 +00:00
|
|
|
.thumbLimits[ mw.user.options.get( 'thumbsize' ) ];
|
2014-06-19 15:44:05 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const attrs = ve.extendObject( {
|
2022-05-30 23:17:46 +00:00
|
|
|
mediaClass: 'File',
|
|
|
|
mediaTag: 'img',
|
2014-08-22 20:50:48 +00:00
|
|
|
type: 'thumb',
|
|
|
|
align: 'default',
|
|
|
|
width: defaultThumbSize,
|
|
|
|
mediaType: 'BITMAP',
|
2023-05-11 00:19:47 +00:00
|
|
|
defaultSize: true,
|
|
|
|
imageClassAttr: 'mw-file-element'
|
2014-06-19 15:44:05 +00:00
|
|
|
}, attributes );
|
|
|
|
|
|
|
|
if ( attrs.defaultSize ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const newDimensions = ve.dm.MWImageNode.static.scaleToThumbnailSize( attrs, attrs.mediaType );
|
2014-07-25 02:28:07 +00:00
|
|
|
if ( newDimensions ) {
|
2014-06-19 15:44:05 +00:00
|
|
|
attrs.width = newDimensions.width;
|
|
|
|
attrs.height = newDimensions.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
imageType = imageType || 'mwBlockImage';
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const newNode = ve.dm.nodeFactory.createFromElement( {
|
2014-08-22 20:50:48 +00:00
|
|
|
type: imageType,
|
|
|
|
attributes: attrs
|
2014-06-19 15:44:05 +00:00
|
|
|
} );
|
|
|
|
|
2014-07-15 18:43:54 +00:00
|
|
|
ve.dm.MWImageNode.static.syncScalableToType( attrs.type, attrs.mediaType, newNode.getScalable() );
|
2014-06-19 15:44:05 +00:00
|
|
|
|
|
|
|
return newNode;
|
|
|
|
};
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
/**
|
|
|
|
* Load from image data with scalable information.
|
|
|
|
*
|
2014-07-17 19:55:24 +00:00
|
|
|
* @param {Object} attrs Image node attributes
|
2015-08-27 06:49:26 +00:00
|
|
|
* @param {ve.dm.Document} parentDoc Document that contains or will contain the image
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {ve.dm.MWImageModel} Image model
|
|
|
|
*/
|
2015-08-27 06:49:26 +00:00
|
|
|
ve.dm.MWImageModel.static.newFromImageAttributes = function ( attrs, parentDoc ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const imgModel = new ve.dm.MWImageModel(
|
2016-10-28 00:22:30 +00:00
|
|
|
parentDoc,
|
|
|
|
{
|
|
|
|
resourceName: attrs.resource,
|
|
|
|
currentDimensions: {
|
|
|
|
width: attrs.width,
|
|
|
|
height: attrs.height
|
|
|
|
},
|
|
|
|
defaultSize: !!attrs.defaultSize
|
|
|
|
}
|
|
|
|
);
|
2014-03-25 16:01:04 +00:00
|
|
|
|
|
|
|
// Cache the attributes so we can create a new image without
|
|
|
|
// losing any existing information
|
|
|
|
imgModel.cacheOriginalImageAttributes( attrs );
|
|
|
|
|
2014-07-25 02:28:07 +00:00
|
|
|
imgModel.setImageSource( attrs.src );
|
2020-03-20 17:08:24 +00:00
|
|
|
imgModel.setFilename( new mw.Title( mw.libs.ve.normalizeParsoidResourceName( attrs.resource ) ).getMainText() );
|
2014-07-25 02:28:07 +00:00
|
|
|
imgModel.setImageHref( attrs.href );
|
2022-03-21 21:03:00 +00:00
|
|
|
imgModel.setImageClassAttr( attrs.imageClassAttr );
|
2022-03-09 19:33:42 +00:00
|
|
|
imgModel.setImgWrapperClassAttr( attrs.imgWrapperClassAttr );
|
2014-07-25 02:28:07 +00:00
|
|
|
|
2015-01-17 01:27:35 +00:00
|
|
|
// Set bounding box
|
|
|
|
imgModel.setBoundingBox( {
|
|
|
|
width: attrs.width,
|
|
|
|
height: attrs.height
|
|
|
|
} );
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
// Collect all the information
|
|
|
|
imgModel.toggleBorder( !!attrs.borderImage );
|
2014-07-25 02:28:07 +00:00
|
|
|
imgModel.setAltText( attrs.alt || '' );
|
2014-03-25 16:01:04 +00:00
|
|
|
|
|
|
|
imgModel.setType( attrs.type );
|
|
|
|
|
|
|
|
// Fix cases where alignment is undefined
|
|
|
|
// Inline images have no 'align' (they have 'valign' instead)
|
|
|
|
// But we do want an alignment case for these in case they
|
|
|
|
// are transformed to block images
|
2014-06-03 04:20:57 +00:00
|
|
|
imgModel.setAlignment( attrs.align || 'default' );
|
2014-03-25 16:01:04 +00:00
|
|
|
|
|
|
|
// Default size
|
|
|
|
imgModel.toggleDefaultSize( !!attrs.defaultSize );
|
2014-07-25 02:28:07 +00:00
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
// TODO: When scale/upright is available, set the size
|
|
|
|
// type accordingly
|
2017-07-18 11:55:33 +00:00
|
|
|
imgModel.setSizeType( imgModel.isDefaultSize() ? 'default' : 'custom' );
|
2014-09-18 19:49:22 +00:00
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
return imgModel;
|
|
|
|
};
|
|
|
|
|
2015-08-27 06:49:26 +00:00
|
|
|
/**
|
|
|
|
* Load from existing image node.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MWImageNode} node Image node
|
|
|
|
* @return {ve.dm.MWImageModel} Image model
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.static.newFromImageNode = function ( node ) {
|
|
|
|
return ve.dm.MWImageModel.static.newFromImageAttributes( node.getAttributes(), node.getDocument() );
|
|
|
|
};
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2014-07-25 02:28:07 +00:00
|
|
|
/**
|
|
|
|
* Get the hash object of the current image model state.
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2021-06-04 11:58:18 +00:00
|
|
|
* @return {Object}
|
2014-07-25 02:28:07 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getHashObject = function () {
|
2024-05-21 14:22:56 +00:00
|
|
|
const hash = {
|
2015-01-08 23:40:12 +00:00
|
|
|
filename: this.getFilename(),
|
2014-07-25 02:28:07 +00:00
|
|
|
altText: this.getAltText(),
|
|
|
|
type: this.getType(),
|
|
|
|
alignment: this.getAlignment(),
|
|
|
|
sizeType: this.getSizeType(),
|
|
|
|
border: this.hasBorder(),
|
|
|
|
borderable: this.isBorderable()
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( this.getScalable() ) {
|
|
|
|
hash.scalable = {
|
|
|
|
currentDimensions: ve.copy( this.getScalable().getCurrentDimensions() ),
|
|
|
|
isDefault: this.getScalable().isDefault()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
};
|
|
|
|
|
2014-08-07 17:02:52 +00:00
|
|
|
/**
|
|
|
|
* Normalize the source url by stripping the protocol off.
|
|
|
|
* This is done so when an image is replaced with the same image,
|
|
|
|
* the imageModel can recognize that nothing has actually changed.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* 'http://upload.wikimedia.org/wikipedia/commons/0/Foo.png'
|
|
|
|
* to '//upload.wikimedia.org/wikipedia/commons/0/Foo.png'
|
|
|
|
*
|
|
|
|
* @return {string} Normalized image source
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getNormalizedImageSource = function () {
|
|
|
|
// Strip the url prefix 'http' / 'https' etc
|
|
|
|
return this.getImageSource().replace( /^https?:\/\//, '//' );
|
|
|
|
};
|
|
|
|
|
2014-07-25 02:28:07 +00:00
|
|
|
/**
|
|
|
|
* Adjust the model parameters based on a new image
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-07-25 02:28:07 +00:00
|
|
|
* @param {Object} attrs New image source attributes
|
2015-01-17 01:27:35 +00:00
|
|
|
* @param {Object} [APIinfo] The image's API info
|
|
|
|
* @throws {Error} Image has insufficient details to compute the imageModel details.
|
2014-07-25 02:28:07 +00:00
|
|
|
*/
|
2015-01-17 01:27:35 +00:00
|
|
|
ve.dm.MWImageModel.prototype.changeImageSource = function ( attrs, APIinfo ) {
|
2015-06-16 16:45:35 +00:00
|
|
|
this.changedImageSource = true;
|
|
|
|
|
2014-07-25 02:28:07 +00:00
|
|
|
if ( attrs.mediaType ) {
|
|
|
|
this.setMediaType( attrs.mediaType );
|
|
|
|
}
|
|
|
|
if ( attrs.href ) {
|
|
|
|
this.setImageHref( attrs.href );
|
|
|
|
}
|
2022-03-09 19:33:42 +00:00
|
|
|
|
2022-03-21 21:03:00 +00:00
|
|
|
// FIXME: Account for falsey but present values
|
|
|
|
if ( attrs.imageClassAttr ) {
|
|
|
|
this.setImageClassAttr( attrs.imageClassAttr );
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Account for falsey but present values
|
2022-03-09 19:33:42 +00:00
|
|
|
if ( attrs.imgWrapperClassAttr ) {
|
|
|
|
this.setImgWrapperClassAttr( attrs.imgWrapperClassAttr );
|
|
|
|
}
|
|
|
|
|
2014-07-25 02:28:07 +00:00
|
|
|
if ( attrs.resource ) {
|
|
|
|
this.setImageResourceName( attrs.resource );
|
2020-03-20 17:08:24 +00:00
|
|
|
this.setFilename( new mw.Title( mw.libs.ve.normalizeParsoidResourceName( attrs.resource ) ).getMainText() );
|
2014-07-25 02:28:07 +00:00
|
|
|
}
|
2014-08-07 17:02:52 +00:00
|
|
|
|
2014-07-25 02:28:07 +00:00
|
|
|
if ( attrs.src ) {
|
|
|
|
this.setImageSource( attrs.src );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the scalable default and original dimensions
|
|
|
|
this.scalable.clearOriginalDimensions();
|
|
|
|
this.scalable.clearDefaultDimensions();
|
|
|
|
this.scalable.clearMaxDimensions();
|
|
|
|
this.scalable.clearMinDimensions();
|
2022-01-17 15:32:30 +00:00
|
|
|
// This is a different image so clear the attributes cache
|
|
|
|
delete this.attributesCache.originalWidth;
|
|
|
|
delete this.attributesCache.originalHeight;
|
2014-07-25 02:28:07 +00:00
|
|
|
|
2015-01-17 01:27:35 +00:00
|
|
|
// If we already have dimensions from the API, use them
|
|
|
|
if ( APIinfo ) {
|
2022-02-14 09:07:10 +00:00
|
|
|
this.scalable.setOriginalDimensions( {
|
2015-01-17 01:27:35 +00:00
|
|
|
width: APIinfo.width,
|
|
|
|
height: APIinfo.height
|
|
|
|
} );
|
|
|
|
// Update media type
|
2022-02-14 09:07:10 +00:00
|
|
|
this.setMediaType( APIinfo.mediatype );
|
2015-01-17 01:27:35 +00:00
|
|
|
// Update defaults
|
|
|
|
ve.dm.MWImageNode.static.syncScalableToType(
|
2022-02-14 09:07:10 +00:00
|
|
|
this.getType(),
|
2015-01-17 01:27:35 +00:00
|
|
|
APIinfo.mediatype,
|
2022-02-14 09:07:10 +00:00
|
|
|
this.scalable
|
2015-01-17 01:27:35 +00:00
|
|
|
);
|
2022-02-14 09:07:10 +00:00
|
|
|
this.updateScalableDetails( {
|
2015-01-17 01:27:35 +00:00
|
|
|
width: APIinfo.width,
|
|
|
|
height: APIinfo.height
|
2014-10-29 01:43:03 +00:00
|
|
|
} );
|
2014-07-25 02:28:07 +00:00
|
|
|
} else {
|
2015-01-17 01:27:35 +00:00
|
|
|
// Call for updated scalable if we don't have dimensions from the API info
|
|
|
|
if ( this.getFilename() ) {
|
2015-12-09 16:47:13 +00:00
|
|
|
// Update anyway
|
2024-04-30 16:44:25 +00:00
|
|
|
ve.dm.MWImageNode.static.getScalablePromise( this.getFilename() ).done( ( info ) => {
|
2024-05-01 12:32:49 +00:00
|
|
|
this.scalable.setOriginalDimensions( {
|
2015-01-17 01:27:35 +00:00
|
|
|
width: info.width,
|
|
|
|
height: info.height
|
|
|
|
} );
|
|
|
|
// Update media type
|
2024-05-01 12:32:49 +00:00
|
|
|
this.setMediaType( info.mediatype );
|
2015-01-17 01:27:35 +00:00
|
|
|
// Update defaults
|
|
|
|
ve.dm.MWImageNode.static.syncScalableToType(
|
2024-05-01 12:32:49 +00:00
|
|
|
this.getType(),
|
2015-01-17 01:27:35 +00:00
|
|
|
info.mediatype,
|
2024-05-01 12:32:49 +00:00
|
|
|
this.scalable
|
2015-01-17 01:27:35 +00:00
|
|
|
);
|
2024-05-01 12:32:49 +00:00
|
|
|
this.updateScalableDetails( {
|
2015-01-17 01:27:35 +00:00
|
|
|
width: info.width,
|
|
|
|
height: info.height
|
|
|
|
} );
|
|
|
|
} );
|
2014-07-25 02:28:07 +00:00
|
|
|
} else {
|
2015-01-17 01:27:35 +00:00
|
|
|
throw new Error( 'Cannot compute details for an image without remote filename and without sizing info.' );
|
2014-07-25 02:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
/**
|
|
|
|
* Get the current image node type according to the attributes.
|
2014-05-27 19:47:14 +00:00
|
|
|
* If either of the parameters are given, the node type is tested
|
|
|
|
* against them, otherwise, it is tested against the current image
|
|
|
|
* parameters.
|
2014-03-25 16:01:04 +00:00
|
|
|
*
|
2014-05-27 19:47:14 +00:00
|
|
|
* @param {string} [imageType] Optional. Image type.
|
|
|
|
* @param {string} [align] Optional. Image alignment.
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {string} Node type 'mwInlineImage' or 'mwBlockImage'
|
|
|
|
*/
|
2014-05-27 19:47:14 +00:00
|
|
|
ve.dm.MWImageModel.prototype.getImageNodeType = function ( imageType, align ) {
|
|
|
|
imageType = imageType || this.getType();
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
if (
|
2014-05-21 03:58:08 +00:00
|
|
|
( this.getType() === 'frameless' || this.getType() === 'none' ) &&
|
2014-05-27 19:47:14 +00:00
|
|
|
( !this.isAligned( align ) || this.isDefaultAligned( imageType, align ) )
|
2014-03-25 16:01:04 +00:00
|
|
|
) {
|
|
|
|
return 'mwInlineImage';
|
|
|
|
} else {
|
|
|
|
return 'mwBlockImage';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-25 02:28:07 +00:00
|
|
|
/**
|
|
|
|
* Get the original bounding box
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2015-03-07 04:43:53 +00:00
|
|
|
* @return {Object} Bounding box with width and height
|
2014-07-25 02:28:07 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getBoundingBox = function () {
|
|
|
|
return this.boundingBox;
|
|
|
|
};
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
/**
|
|
|
|
* Update an existing image node by changing its attributes
|
|
|
|
*
|
2014-07-17 19:55:24 +00:00
|
|
|
* @param {ve.dm.MWImageNode} node Image node to update
|
2014-03-25 16:01:04 +00:00
|
|
|
* @param {ve.dm.Surface} surfaceModel Surface model of main document
|
|
|
|
*/
|
2014-07-17 19:55:24 +00:00
|
|
|
ve.dm.MWImageModel.prototype.updateImageNode = function ( node, surfaceModel ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const doc = surfaceModel.getDocument();
|
2014-03-25 16:01:04 +00:00
|
|
|
|
|
|
|
// Update the caption
|
2014-05-21 03:58:08 +00:00
|
|
|
if ( node.getType() === 'mwBlockImage' ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
let captionNode = node.getCaptionNode();
|
2014-05-21 03:58:08 +00:00
|
|
|
if ( !captionNode ) {
|
|
|
|
// There was no caption before, so insert one now
|
|
|
|
surfaceModel.getFragment()
|
2014-09-30 16:34:33 +00:00
|
|
|
.adjustLinearSelection( 1 )
|
|
|
|
.collapseToStart()
|
2014-08-22 20:50:48 +00:00
|
|
|
.insertContent( [ { type: 'mwImageCaption' }, { type: '/mwImageCaption' } ] );
|
2014-05-21 03:58:08 +00:00
|
|
|
// Update the caption node
|
2014-07-17 19:55:24 +00:00
|
|
|
captionNode = node.getCaptionNode();
|
2014-05-21 03:58:08 +00:00
|
|
|
}
|
2014-03-25 16:01:04 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const captionRange = captionNode.getRange();
|
2014-05-21 03:58:08 +00:00
|
|
|
|
|
|
|
// Remove contents of old caption
|
|
|
|
surfaceModel.change(
|
2016-11-30 22:03:03 +00:00
|
|
|
ve.dm.TransactionBuilder.static.newFromRemoval(
|
2014-05-21 03:58:08 +00:00
|
|
|
doc,
|
|
|
|
captionRange,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add contents of new caption
|
|
|
|
surfaceModel.change(
|
2016-11-30 22:03:03 +00:00
|
|
|
ve.dm.TransactionBuilder.static.newFromDocumentInsertion(
|
2014-05-21 03:58:08 +00:00
|
|
|
doc,
|
|
|
|
captionRange.start,
|
|
|
|
this.getCaptionDocument()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2014-03-25 16:01:04 +00:00
|
|
|
|
|
|
|
// Update attributes
|
|
|
|
surfaceModel.change(
|
2016-11-30 22:03:03 +00:00
|
|
|
ve.dm.TransactionBuilder.static.newFromAttributeChanges(
|
2014-03-25 16:01:04 +00:00
|
|
|
doc,
|
2014-05-21 03:58:08 +00:00
|
|
|
node.getOffset(),
|
2014-03-25 16:01:04 +00:00
|
|
|
this.getUpdatedAttributes()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert image into a surface.
|
|
|
|
*
|
|
|
|
* Image is inserted at the current fragment position.
|
|
|
|
*
|
2014-06-19 11:02:41 +00:00
|
|
|
* @param {ve.dm.SurfaceFragment} fragment Fragment covering range to insert at
|
|
|
|
* @return {ve.dm.SurfaceFragment} Fragment covering inserted image
|
|
|
|
* @throws {Error} Unknown image node type
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.insertImageNode = function ( fragment ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const nodeType = this.getImageNodeType(),
|
2014-03-25 16:01:04 +00:00
|
|
|
surfaceModel = fragment.getSurface();
|
|
|
|
|
2014-09-30 16:34:33 +00:00
|
|
|
if ( !( fragment.getSelection() instanceof ve.dm.LinearSelection ) ) {
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const selectedNode = fragment.getSelectedNode();
|
2017-08-24 14:56:22 +00:00
|
|
|
|
|
|
|
// If there was a previous node, remove it first
|
|
|
|
if ( selectedNode ) {
|
|
|
|
// Remove the old image
|
|
|
|
fragment.removeContent();
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const contentToInsert = this.getData();
|
2014-03-25 16:01:04 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
let offset;
|
2014-06-19 11:02:41 +00:00
|
|
|
switch ( nodeType ) {
|
|
|
|
case 'mwInlineImage':
|
2018-01-08 15:31:13 +00:00
|
|
|
if ( selectedNode && selectedNode.type === 'mwBlockImage' ) {
|
2017-08-24 14:56:22 +00:00
|
|
|
// If converting from a block image, create a wrapper paragraph for the inline image to go in.
|
|
|
|
fragment.insertContent( [ { type: 'paragraph', internal: { generated: 'wrapper' } }, { type: '/paragraph' } ] );
|
|
|
|
offset = fragment.getSelection().getRange().start + 1;
|
|
|
|
} else {
|
|
|
|
// Try to put the image inside the nearest content node
|
|
|
|
offset = fragment.getDocument().data.getNearestContentOffset( fragment.getSelection().getRange().start );
|
|
|
|
}
|
2014-06-19 11:02:41 +00:00
|
|
|
if ( offset > -1 ) {
|
2018-11-05 20:44:57 +00:00
|
|
|
fragment = fragment.clone( new ve.dm.LinearSelection( new ve.Range( offset ) ) );
|
2014-06-19 11:02:41 +00:00
|
|
|
}
|
|
|
|
fragment.insertContent( contentToInsert );
|
|
|
|
return fragment;
|
|
|
|
|
|
|
|
case 'mwBlockImage':
|
|
|
|
// Try to put the image in front of the structural node
|
2014-09-30 16:34:33 +00:00
|
|
|
offset = fragment.getDocument().data.getNearestStructuralOffset( fragment.getSelection().getRange().start, -1 );
|
2014-06-19 11:02:41 +00:00
|
|
|
if ( offset > -1 ) {
|
2018-11-05 20:44:57 +00:00
|
|
|
fragment = fragment.clone( new ve.dm.LinearSelection( new ve.Range( offset ) ) );
|
2014-06-19 11:02:41 +00:00
|
|
|
}
|
|
|
|
fragment.insertContent( contentToInsert );
|
2018-08-01 02:35:49 +00:00
|
|
|
// Add contents of new caption
|
|
|
|
surfaceModel.change(
|
|
|
|
ve.dm.TransactionBuilder.static.newFromDocumentInsertion(
|
|
|
|
surfaceModel.getDocument(),
|
|
|
|
fragment.getSelection().getRange().start + 2,
|
|
|
|
this.getCaptionDocument()
|
|
|
|
)
|
|
|
|
);
|
2014-06-19 11:02:41 +00:00
|
|
|
return fragment;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error( 'Unknown image node type ' + nodeType );
|
2014-03-25 16:01:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-02 18:25:35 +00:00
|
|
|
/**
|
|
|
|
* Get linear data representation of the image
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2015-08-02 18:25:35 +00:00
|
|
|
* @return {Array} Linear data
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getData = function () {
|
2024-05-21 14:22:56 +00:00
|
|
|
const originalAttrs = ve.copy( this.getOriginalImageAttributes() ),
|
2018-11-07 16:51:39 +00:00
|
|
|
editAttributes = ve.extendObject( originalAttrs, this.getUpdatedAttributes() ),
|
2015-08-02 18:25:35 +00:00
|
|
|
nodeType = this.getImageNodeType();
|
|
|
|
|
|
|
|
// Remove old classes
|
|
|
|
delete editAttributes.originalClasses;
|
|
|
|
delete editAttributes.unrecognizedClasses;
|
|
|
|
// Newly created images must have valid URLs, so remove the error attribute
|
|
|
|
if ( this.isChangedImageSource() ) {
|
|
|
|
delete editAttributes.isError;
|
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const data = [
|
2015-08-02 18:25:35 +00:00
|
|
|
{
|
|
|
|
type: nodeType,
|
|
|
|
attributes: editAttributes
|
|
|
|
},
|
|
|
|
{ type: '/' + nodeType }
|
|
|
|
];
|
|
|
|
|
|
|
|
if ( nodeType === 'mwBlockImage' ) {
|
|
|
|
data.splice( 1, 0, { type: 'mwImageCaption' }, { type: '/mwImageCaption' } );
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
/**
|
|
|
|
* Return all updated attributes that belong to the node.
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {Object} Updated attributes
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getUpdatedAttributes = function () {
|
2024-05-21 14:22:56 +00:00
|
|
|
const origAttrs = this.getOriginalImageAttributes();
|
2014-06-03 07:32:47 +00:00
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
let currentDimensions;
|
2014-06-03 07:32:47 +00:00
|
|
|
// Adjust default dimensions if size is set to default
|
|
|
|
if ( this.scalable.isDefault() && this.scalable.getDefaultDimensions() ) {
|
2014-06-09 23:35:20 +00:00
|
|
|
currentDimensions = this.scalable.getDefaultDimensions();
|
|
|
|
} else {
|
|
|
|
currentDimensions = this.getCurrentDimensions();
|
2014-06-03 07:32:47 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
const attrs = {
|
2022-05-30 23:17:46 +00:00
|
|
|
mediaClass: 'File',
|
|
|
|
mediaTag: this.getMediaTag(),
|
2014-08-22 20:50:48 +00:00
|
|
|
type: this.getType(),
|
|
|
|
width: currentDimensions.width,
|
|
|
|
height: currentDimensions.height,
|
|
|
|
defaultSize: this.isDefaultSize(),
|
|
|
|
borderImage: this.hasBorder()
|
2014-03-25 16:01:04 +00:00
|
|
|
};
|
|
|
|
|
2020-10-04 16:33:03 +00:00
|
|
|
if ( this.getAltText() || typeof origAttrs.alt === 'string' ) {
|
2014-03-25 16:01:04 +00:00
|
|
|
attrs.alt = this.getAltText();
|
|
|
|
}
|
|
|
|
|
2014-05-24 02:42:14 +00:00
|
|
|
if ( this.isDefaultAligned() ) {
|
2014-03-25 16:01:04 +00:00
|
|
|
attrs.align = 'default';
|
|
|
|
} else if ( !this.isAligned() ) {
|
|
|
|
attrs.align = 'none';
|
|
|
|
} else {
|
|
|
|
attrs.align = this.getAlignment();
|
|
|
|
}
|
|
|
|
|
2014-07-25 02:28:07 +00:00
|
|
|
attrs.src = this.getImageSource();
|
|
|
|
attrs.href = this.getImageHref();
|
2022-03-21 21:03:00 +00:00
|
|
|
attrs.imageClassAttr = this.getImageClassAttr();
|
2022-03-09 19:33:42 +00:00
|
|
|
attrs.imgWrapperClassAttr = this.getImgWrapperClassAttr();
|
2014-07-25 02:28:07 +00:00
|
|
|
attrs.resource = this.getImageResourceName();
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
return attrs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deal with default change on the scalable object
|
|
|
|
*
|
|
|
|
* @param {boolean} isDefault
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.onScalableDefaultSizeChange = function ( isDefault ) {
|
|
|
|
this.toggleDefaultSize( isDefault );
|
|
|
|
};
|
|
|
|
|
2014-07-25 02:28:07 +00:00
|
|
|
/**
|
|
|
|
* Set the image file source
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-07-25 02:28:07 +00:00
|
|
|
* @param {string} src The source of the given media file
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.setImageSource = function ( src ) {
|
|
|
|
this.imageSrc = src;
|
|
|
|
};
|
|
|
|
|
2014-07-24 23:50:22 +00:00
|
|
|
/**
|
|
|
|
* Set the image file resource name
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-07-25 02:28:07 +00:00
|
|
|
* @param {string} resourceName The resource name of the given image file
|
2014-07-24 23:50:22 +00:00
|
|
|
*/
|
2014-07-25 02:28:07 +00:00
|
|
|
ve.dm.MWImageModel.prototype.setImageResourceName = function ( resourceName ) {
|
2014-07-24 23:50:22 +00:00
|
|
|
this.imageResourceName = resourceName;
|
|
|
|
};
|
|
|
|
|
2014-07-25 02:28:07 +00:00
|
|
|
/**
|
|
|
|
* Set the image href value
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-07-25 02:28:07 +00:00
|
|
|
* @param {string} href The destination href of the given media file
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.setImageHref = function ( href ) {
|
|
|
|
this.imageHref = href;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the original bounding box
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-07-25 02:28:07 +00:00
|
|
|
* @param {Object} box Bounding box with width and height
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.setBoundingBox = function ( box ) {
|
|
|
|
this.boundingBox = box;
|
|
|
|
};
|
|
|
|
|
2014-08-07 17:02:52 +00:00
|
|
|
/**
|
|
|
|
* Set the initial hash object of the image to be compared to when
|
|
|
|
* checking if the model is modified.
|
|
|
|
*
|
|
|
|
* @param {Object} hash The initial hash object
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.storeInitialHash = function ( hash ) {
|
|
|
|
this.initialHash = hash;
|
|
|
|
};
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
/**
|
2014-07-17 19:55:24 +00:00
|
|
|
* Set symbolic name of media type.
|
|
|
|
*
|
|
|
|
* Example values: "BITMAP" for JPEG or PNG images; "DRAWING" for SVG graphics
|
|
|
|
*
|
2015-08-19 18:33:59 +00:00
|
|
|
* @param {string|undefined} type Symbolic media type name, or undefined if empty
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
2014-07-17 19:55:24 +00:00
|
|
|
ve.dm.MWImageModel.prototype.setMediaType = function ( type ) {
|
|
|
|
this.mediaType = type;
|
2014-03-25 16:01:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the image is set to default size
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {boolean} Default size flag on or off
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.isDefaultSize = function () {
|
2018-09-24 14:16:20 +00:00
|
|
|
// An image with 'frame' always ignores the size specification
|
|
|
|
return this.scalable.isDefault() || this.getType() === 'frame';
|
2014-03-25 16:01:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the image has the border flag set
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {boolean} Border flag on or off
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.hasBorder = function () {
|
|
|
|
return this.border;
|
|
|
|
};
|
|
|
|
|
2015-06-16 16:45:35 +00:00
|
|
|
/**
|
|
|
|
* Check whether the image source is changed
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2015-06-16 16:45:35 +00:00
|
|
|
* @return {boolean} changedImageSource flag on or off
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.isChangedImageSource = function () {
|
|
|
|
return this.changedImageSource;
|
|
|
|
};
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
/**
|
|
|
|
* Check whether the image has floating alignment set
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-05-27 19:47:14 +00:00
|
|
|
* @param {string} [align] Optional. Alignment value to test against.
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {boolean} hasAlignment flag on or off
|
|
|
|
*/
|
2014-05-27 19:47:14 +00:00
|
|
|
ve.dm.MWImageModel.prototype.isAligned = function ( align ) {
|
|
|
|
align = align || this.alignment;
|
|
|
|
// The image is aligned if it has alignment (not undefined and not null)
|
|
|
|
// and if its alignment is not 'none'.
|
|
|
|
// Inline images initially have null alignment value (and are not aligned)
|
|
|
|
return align && align !== 'none';
|
2014-03-25 16:01:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the image is set to default alignment
|
2014-05-24 02:42:14 +00:00
|
|
|
* We explicitly repeat tests so to avoid recursively calling
|
|
|
|
* the other methods.
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2015-08-19 18:33:59 +00:00
|
|
|
* @param {string} [imageType] Type of the image.
|
2014-05-27 19:47:14 +00:00
|
|
|
* @param {string} [align] Optional alignment value to test against.
|
|
|
|
* Supplying this parameter would test whether this align parameter
|
|
|
|
* would mean the image is aligned to its default position.
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {boolean} defaultAlignment flag on or off
|
|
|
|
*/
|
2014-05-27 19:47:14 +00:00
|
|
|
ve.dm.MWImageModel.prototype.isDefaultAligned = function ( imageType, align ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const alignment = align || this.getAlignment(),
|
2015-08-27 06:49:26 +00:00
|
|
|
defaultAlignment = ( this.parentDoc.getDir() === 'rtl' ) ? 'left' : 'right';
|
2014-05-24 02:42:14 +00:00
|
|
|
|
2014-05-27 19:47:14 +00:00
|
|
|
imageType = imageType || this.getType();
|
2015-12-09 16:47:13 +00:00
|
|
|
// No alignment specified means default alignment always
|
2014-05-27 19:47:14 +00:00
|
|
|
// Inline images have no align attribute; during the initialization
|
|
|
|
// stage of the model we have to account for that option. Later the
|
|
|
|
// model creates a faux alignment for inline images ('none' for default)
|
|
|
|
// but if initially the alignment is null or undefined, it means the image
|
|
|
|
// is inline without explicit alignment (which makes it default aligned)
|
|
|
|
if ( !alignment ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-24 02:42:14 +00:00
|
|
|
if (
|
|
|
|
(
|
|
|
|
( imageType === 'frameless' || imageType === 'none' ) &&
|
|
|
|
alignment === 'none'
|
|
|
|
) ||
|
|
|
|
(
|
|
|
|
( imageType === 'thumb' || imageType === 'frame' ) &&
|
|
|
|
alignment === defaultAlignment
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-03-25 16:01:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the image can have a border set on it
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {boolean} Border possible or not
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.isBorderable = function () {
|
|
|
|
return this.borderable;
|
|
|
|
};
|
|
|
|
|
2014-07-24 23:50:22 +00:00
|
|
|
/**
|
|
|
|
* Get the image file resource name
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2015-03-07 04:43:53 +00:00
|
|
|
* @return {string} resourceName The resource name of the given media file
|
2014-07-24 23:50:22 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getResourceName = function () {
|
|
|
|
return this.imageResourceName;
|
|
|
|
};
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
/**
|
|
|
|
* Get the image alternate text
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {string} Alternate text
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getAltText = function () {
|
2014-07-25 02:28:07 +00:00
|
|
|
return this.altText || '';
|
2014-03-25 16:01:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get image wikitext type; 'thumb', 'frame', 'frameless' or 'none/inline'
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {string} Image type
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getType = function () {
|
|
|
|
return this.type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the image size type of the image
|
2019-04-16 15:17:29 +00:00
|
|
|
*
|
|
|
|
* @return {string} Size type
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getSizeType = function () {
|
|
|
|
return this.sizeType;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get symbolic name of media type.
|
|
|
|
*
|
|
|
|
* Example values: "BITMAP" for JPEG or PNG images; "DRAWING" for SVG graphics
|
|
|
|
*
|
2014-06-04 18:20:37 +00:00
|
|
|
* @return {string|undefined} Symbolic media type name, or undefined if empty
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getMediaType = function () {
|
2014-07-17 19:55:24 +00:00
|
|
|
return this.mediaType;
|
2014-03-25 16:01:04 +00:00
|
|
|
};
|
|
|
|
|
2017-04-27 18:08:35 +00:00
|
|
|
/**
|
2022-05-30 23:17:46 +00:00
|
|
|
* Get media tag: img, video or audio
|
2019-04-16 15:17:29 +00:00
|
|
|
*
|
2022-05-30 23:17:46 +00:00
|
|
|
* @return {string} Tag name
|
2017-04-27 18:08:35 +00:00
|
|
|
*/
|
2022-05-30 23:17:46 +00:00
|
|
|
ve.dm.MWImageModel.prototype.getMediaTag = function () {
|
2024-05-21 14:22:56 +00:00
|
|
|
const mediaType = this.getMediaType();
|
2017-04-27 18:08:35 +00:00
|
|
|
|
|
|
|
if ( mediaType === 'VIDEO' ) {
|
2022-05-30 23:17:46 +00:00
|
|
|
return 'video';
|
2017-04-27 18:08:35 +00:00
|
|
|
}
|
|
|
|
if ( mediaType === 'AUDIO' ) {
|
2022-05-30 23:17:46 +00:00
|
|
|
return 'audio';
|
2017-04-27 18:08:35 +00:00
|
|
|
}
|
2022-05-30 23:17:46 +00:00
|
|
|
return 'img';
|
2017-04-27 18:08:35 +00:00
|
|
|
};
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
/**
|
|
|
|
* Get image alignment 'left', 'right', 'center', 'none' or 'default'
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-05-27 19:47:14 +00:00
|
|
|
* @return {string|null} Image alignment. Inline images have initial alignment
|
|
|
|
* value of null.
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getAlignment = function () {
|
|
|
|
return this.alignment;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get image vertical alignment
|
|
|
|
* 'middle', 'baseline', 'sub', 'super', 'top', 'text-top', 'bottom', 'text-bottom' or 'default'
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {string} Image alignment
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getVerticalAlignment = function () {
|
|
|
|
return this.verticalAlignment;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the scalable object responsible for size manipulations
|
|
|
|
* for the given image
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2021-06-04 11:58:18 +00:00
|
|
|
* @return {ve.dm.Scalable}
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getScalable = function () {
|
|
|
|
return this.scalable;
|
|
|
|
};
|
|
|
|
|
2024-04-29 18:14:26 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Object} Dimensions
|
|
|
|
* @memberof ve.ui.DimensionsWidget
|
|
|
|
* @property {number} width The value of the width input
|
|
|
|
* @property {number} height The value of the height input
|
|
|
|
*/
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
/**
|
|
|
|
* Get the image current dimensions
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2024-04-29 18:14:26 +00:00
|
|
|
* @return {ve.ui.DimensionsWidget.Dimensions} Current dimensions width/height
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getCurrentDimensions = function () {
|
|
|
|
return this.scalable.getCurrentDimensions();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get image caption document.
|
|
|
|
*
|
|
|
|
* Auto-generates a blank document if no document exists.
|
|
|
|
*
|
|
|
|
* @return {ve.dm.Document} Caption document
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getCaptionDocument = function () {
|
|
|
|
if ( !this.captionDoc ) {
|
2016-05-30 20:40:07 +00:00
|
|
|
this.captionDoc = this.parentDoc.cloneWithData( [
|
|
|
|
{ type: 'paragraph', internal: { generated: 'wrapper' } },
|
|
|
|
{ type: '/paragraph' },
|
|
|
|
{ type: 'internalList' },
|
|
|
|
{ type: '/internalList' }
|
|
|
|
] );
|
2014-03-25 16:01:04 +00:00
|
|
|
}
|
|
|
|
return this.captionDoc;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle the option of whether this image can or cannot have
|
|
|
|
* a border set on it.
|
|
|
|
*
|
|
|
|
* @param {boolean} [borderable] Set or unset borderable. If not
|
|
|
|
* specified, the current state is toggled.
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.toggleBorderable = function ( borderable ) {
|
|
|
|
borderable = borderable !== undefined ? !!borderable : !this.isBorderable();
|
|
|
|
|
|
|
|
this.borderable = borderable;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle the border flag of the image
|
|
|
|
*
|
|
|
|
* @param {boolean} [hasBorder] Border flag. Omit to toggle current value.
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.toggleBorder = function ( hasBorder ) {
|
|
|
|
hasBorder = hasBorder !== undefined ? !!hasBorder : !this.hasBorder();
|
|
|
|
|
|
|
|
this.border = !!hasBorder;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle the default size flag of the image
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @param {boolean} [isDefault] Default size flag. Omit to toggle current value.
|
2024-04-29 18:14:26 +00:00
|
|
|
* @fires ve.dm.MWImageModel#sizeDefaultChange
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.toggleDefaultSize = function ( isDefault ) {
|
|
|
|
isDefault = isDefault !== undefined ? !!isDefault : !this.isDefaultSize();
|
|
|
|
|
|
|
|
if ( this.isDefaultSize() !== isDefault ) {
|
|
|
|
this.scalable.toggleDefault( !!isDefault );
|
|
|
|
this.resetDefaultDimensions();
|
|
|
|
this.emit( 'sizeDefaultChange', !!isDefault );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache all image attributes
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @param {Object} attrs Image attributes
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.cacheOriginalImageAttributes = function ( attrs ) {
|
|
|
|
this.attributesCache = attrs;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the cache of all image attributes
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {Object} attrs Image attributes
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getOriginalImageAttributes = function () {
|
|
|
|
return this.attributesCache;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current dimensions of the image.
|
|
|
|
* Normalize in case only one dimension is available.
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @param {Object} dimensions Dimensions width and height
|
|
|
|
* @param {number} dimensions.width The width of the image
|
|
|
|
* @param {number} dimensions.height The height of the image
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.setCurrentDimensions = function ( dimensions ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const normalizedDimensions = ve.dm.Scalable.static.getDimensionsFromValue( dimensions, this.scalable.getRatio() );
|
2014-03-25 16:01:04 +00:00
|
|
|
this.scalable.setCurrentDimensions( normalizedDimensions );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set alternate text
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @param {string} text Alternate text
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.setAltText = function ( text ) {
|
|
|
|
this.altText = text;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set image type
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @see #getType
|
|
|
|
*
|
|
|
|
* @param {string} type Image type
|
2024-04-29 18:14:26 +00:00
|
|
|
* @fires ve.dm.MWImageModel#typeChange
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.setType = function ( type ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const isDefaultAligned = this.isDefaultAligned( this.imageCurrentType );
|
2014-05-27 19:47:14 +00:00
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
this.type = type;
|
|
|
|
|
2015-12-09 16:47:13 +00:00
|
|
|
// If we're switching between inline and block or vice versa,
|
2014-06-03 04:20:57 +00:00
|
|
|
// check if the old type image was default aligned
|
|
|
|
if ( isDefaultAligned && this.imageCurrentType !== this.type ) {
|
|
|
|
if ( this.type === 'none' || this.type === 'frameless' ) {
|
|
|
|
// Reset default alignment for switching to inline images
|
|
|
|
this.setAlignment( 'none' );
|
|
|
|
} else {
|
|
|
|
// Reset default alignment for all other images
|
|
|
|
this.setAlignment( 'default' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the current type for next check
|
|
|
|
this.imageCurrentType = type;
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
if ( type === 'frame' || type === 'thumb' ) {
|
|
|
|
// Disable border option
|
|
|
|
this.toggleBorderable( false );
|
|
|
|
} else {
|
|
|
|
// Enable border option
|
|
|
|
this.toggleBorderable( true );
|
|
|
|
}
|
|
|
|
|
2014-06-09 18:47:22 +00:00
|
|
|
// If type is frame, set to 'default' size
|
|
|
|
if ( type === 'frame' ) {
|
|
|
|
this.toggleDefaultSize( true );
|
|
|
|
}
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
// Let the image node update scalable considerations
|
|
|
|
// for default and max dimensions as per the new type.
|
2014-07-15 18:43:54 +00:00
|
|
|
ve.dm.MWImageNode.static.syncScalableToType( type, this.getMediaType(), this.getScalable() );
|
2014-03-25 16:01:04 +00:00
|
|
|
|
|
|
|
this.emit( 'typeChange', type );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the default dimensions of the image based on its type
|
|
|
|
* and on whether we have the originalDimensions object from
|
|
|
|
* the API
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.resetDefaultDimensions = function () {
|
2024-05-21 14:22:56 +00:00
|
|
|
const originalDimensions = this.scalable.getOriginalDimensions();
|
2014-03-25 16:01:04 +00:00
|
|
|
|
2018-05-20 10:00:09 +00:00
|
|
|
if ( !ve.isEmptyObject( originalDimensions ) ) {
|
2014-03-25 16:01:04 +00:00
|
|
|
if ( this.getType() === 'thumb' || this.getType() === 'frameless' ) {
|
|
|
|
// Default is thumb size
|
|
|
|
if ( originalDimensions.width <= this.defaultThumbSize ) {
|
|
|
|
this.scalable.setDefaultDimensions( originalDimensions );
|
|
|
|
} else {
|
|
|
|
this.scalable.setDefaultDimensions(
|
2014-07-15 18:43:54 +00:00
|
|
|
ve.dm.Scalable.static.getDimensionsFromValue( {
|
2014-08-22 20:50:48 +00:00
|
|
|
width: this.defaultThumbSize
|
2014-07-15 18:43:54 +00:00
|
|
|
}, this.scalable.getRatio() )
|
2014-06-26 10:41:18 +00:00
|
|
|
);
|
2014-03-25 16:01:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Default is original size
|
|
|
|
this.scalable.setDefaultDimensions( originalDimensions );
|
|
|
|
}
|
|
|
|
} else {
|
2016-02-06 19:12:47 +00:00
|
|
|
this.scalable.clearDefaultDimensions();
|
2014-03-25 16:01:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the currently set default dimensions from the scalable
|
|
|
|
* object attached to the image.
|
|
|
|
*
|
|
|
|
* @return {Object} Image default dimensions
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getDefaultDimensions = function () {
|
|
|
|
return this.scalable.getDefaultDimensions();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change size type of the image
|
|
|
|
*
|
|
|
|
* @param {string} type Size type 'default', 'custom' or 'scale'
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.setSizeType = function ( type ) {
|
|
|
|
if ( this.sizeType !== type ) {
|
|
|
|
this.sizeType = type;
|
|
|
|
this.toggleDefaultSize( type === 'default' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set image alignment
|
|
|
|
*
|
|
|
|
* @see #getAlignment
|
|
|
|
*
|
|
|
|
* @param {string} align Alignment
|
2024-04-29 18:14:26 +00:00
|
|
|
* @fires ve.dm.MWImageModel#alignmentChange
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.setAlignment = function ( align ) {
|
|
|
|
if ( align === 'default' ) {
|
|
|
|
// If default, set the alignment to language dir default
|
|
|
|
align = this.getDefaultDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.alignment = align;
|
|
|
|
this.emit( 'alignmentChange', align );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set image vertical alignment
|
|
|
|
*
|
|
|
|
* @see #getVerticalAlignment
|
|
|
|
*
|
|
|
|
* @param {string} valign Alignment
|
2024-04-29 18:14:26 +00:00
|
|
|
* @fires ve.dm.MWImageModel#alignmentChange
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.setVerticalAlignment = function ( valign ) {
|
|
|
|
this.verticalAlignment = valign;
|
|
|
|
this.emit( 'alignmentChange', valign );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default alignment according to the document direction
|
|
|
|
*
|
2014-06-05 02:15:42 +00:00
|
|
|
* @param {string} [imageNodeType] Optional. The image node type that we would
|
|
|
|
* like to get the default direction for. Supplying this parameter allows us
|
|
|
|
* to check what the default alignment of a specific type of node would be.
|
|
|
|
* If the parameter is not supplied, the default alignment will be calculated
|
|
|
|
* based on the current node type.
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {string} Node alignment based on document direction
|
|
|
|
*/
|
2014-06-05 02:15:42 +00:00
|
|
|
ve.dm.MWImageModel.prototype.getDefaultDir = function ( imageNodeType ) {
|
|
|
|
imageNodeType = imageNodeType || this.getImageNodeType();
|
|
|
|
|
2015-08-27 06:49:26 +00:00
|
|
|
if ( this.parentDoc.getDir() === 'rtl' ) {
|
2014-03-25 16:01:04 +00:00
|
|
|
// Assume position is 'left'
|
2014-06-05 02:15:42 +00:00
|
|
|
return ( imageNodeType === 'mwBlockImage' ) ? 'left' : 'none';
|
2014-03-25 16:01:04 +00:00
|
|
|
} else {
|
|
|
|
// Assume position is 'right'
|
2014-06-05 02:15:42 +00:00
|
|
|
return ( imageNodeType === 'mwBlockImage' ) ? 'right' : 'none';
|
2014-03-25 16:01:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-17 19:55:24 +00:00
|
|
|
/**
|
2014-07-25 02:28:07 +00:00
|
|
|
* Get the image file source
|
|
|
|
* The image file source that points to the location of the
|
2015-08-19 18:21:01 +00:00
|
|
|
* file on the Web.
|
2014-07-25 02:28:07 +00:00
|
|
|
* For instance, '//upload.wikimedia.org/wikipedia/commons/0/0f/Foo.jpg'
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2015-03-07 04:43:53 +00:00
|
|
|
* @return {string} The source of the given media file
|
2014-07-17 19:55:24 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getImageSource = function () {
|
2014-07-25 02:28:07 +00:00
|
|
|
return this.imageSrc;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the image file resource name.
|
|
|
|
* The resource name represents the filename without the full
|
|
|
|
* source url.
|
|
|
|
* For example, './File:Foo.jpg'
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2015-03-07 04:43:53 +00:00
|
|
|
* @return {string} The resource name of the given media file
|
2014-07-25 02:28:07 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getImageResourceName = function () {
|
|
|
|
return this.imageResourceName;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the image href value.
|
|
|
|
* This is the link that the image leads to. It usually contains
|
|
|
|
* the link to the source of the image in commons or locally, but
|
|
|
|
* may hold an alternative link if link= is supplied in the wikitext.
|
|
|
|
* For example, './File:Foo.jpg' or 'http://www.wikipedia.org'
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2015-03-07 04:43:53 +00:00
|
|
|
* @return {string} The destination href of the given media file
|
2014-07-25 02:28:07 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getImageHref = function () {
|
|
|
|
return this.imageHref;
|
2014-07-17 19:55:24 +00:00
|
|
|
};
|
|
|
|
|
2022-03-21 21:03:00 +00:00
|
|
|
/**
|
|
|
|
* @param {string|null} classAttr
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.setImageClassAttr = function ( classAttr ) {
|
|
|
|
this.imageClassAttr = classAttr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string|null}
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getImageClassAttr = function () {
|
|
|
|
return this.imageClassAttr;
|
|
|
|
};
|
|
|
|
|
2022-03-09 19:33:42 +00:00
|
|
|
/**
|
|
|
|
* @param {string|null} classAttr
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.setImgWrapperClassAttr = function ( classAttr ) {
|
|
|
|
this.imgWrapperClassAttr = classAttr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string|null}
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getImgWrapperClassAttr = function () {
|
|
|
|
return this.imgWrapperClassAttr;
|
|
|
|
};
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
/**
|
2014-07-25 02:28:07 +00:00
|
|
|
* Attach a new scalable object to the model and request the
|
|
|
|
* information from the API.
|
2014-03-25 16:01:04 +00:00
|
|
|
*
|
2021-06-04 11:58:18 +00:00
|
|
|
* @param {ve.dm.Scalable} scalable
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
2014-07-25 02:28:07 +00:00
|
|
|
ve.dm.MWImageModel.prototype.attachScalable = function ( scalable ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const imageName = mw.libs.ve.normalizeParsoidResourceName( this.getResourceName() );
|
2014-07-17 19:55:24 +00:00
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
if ( this.scalable instanceof ve.dm.Scalable ) {
|
|
|
|
this.scalable.disconnect( this );
|
|
|
|
}
|
|
|
|
this.scalable = scalable;
|
2014-07-17 19:55:24 +00:00
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
// Events
|
2014-08-22 20:50:48 +00:00
|
|
|
this.scalable.connect( this, { defaultSizeChange: 'onScalableDefaultSizeChange' } );
|
2014-07-17 19:55:24 +00:00
|
|
|
|
|
|
|
// Call for updated scalable
|
2014-07-24 23:50:22 +00:00
|
|
|
if ( imageName ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
ve.dm.MWImageNode.static.getScalablePromise( imageName ).done( ( info ) => {
|
2024-05-01 12:32:49 +00:00
|
|
|
this.scalable.setOriginalDimensions( {
|
2014-08-22 20:50:48 +00:00
|
|
|
width: info.width,
|
|
|
|
height: info.height
|
2014-07-24 23:50:22 +00:00
|
|
|
} );
|
|
|
|
// Update media type
|
2024-05-01 12:32:49 +00:00
|
|
|
this.setMediaType( info.mediatype );
|
2014-07-24 23:50:22 +00:00
|
|
|
// Update according to type
|
|
|
|
ve.dm.MWImageNode.static.syncScalableToType(
|
2024-05-01 12:32:49 +00:00
|
|
|
this.getType(),
|
|
|
|
this.getMediaType(),
|
|
|
|
this.getScalable()
|
2014-07-24 23:50:22 +00:00
|
|
|
);
|
2014-10-21 22:15:44 +00:00
|
|
|
|
|
|
|
// We have to adjust the details in the initial hash if the original
|
|
|
|
// image was 'default' since we didn't have default until now and the
|
|
|
|
// default dimensions that were 'recorded' were wrong
|
2024-05-01 12:32:49 +00:00
|
|
|
if ( !ve.isEmptyObject( this.initialHash ) && this.initialHash.scalable.isDefault ) {
|
|
|
|
this.initialHash.scalable.currentDimensions = this.scalable.getDefaultDimensions();
|
2014-10-21 22:15:44 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 01:43:03 +00:00
|
|
|
} );
|
2014-07-24 23:50:22 +00:00
|
|
|
}
|
2014-03-25 16:01:04 +00:00
|
|
|
};
|
|
|
|
|
2015-01-08 23:40:12 +00:00
|
|
|
/**
|
|
|
|
* Set the filename of the current image
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2018-02-28 02:01:05 +00:00
|
|
|
* @param {string} filename Image filename (without namespace)
|
2015-01-08 23:40:12 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.setFilename = function ( filename ) {
|
|
|
|
this.filename = filename;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the filename of the current image
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2018-02-28 02:01:05 +00:00
|
|
|
* @return {string} filename Image filename (without namespace)
|
2015-01-08 23:40:12 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getFilename = function () {
|
|
|
|
return this.filename;
|
|
|
|
};
|
|
|
|
|
2015-01-17 01:27:35 +00:00
|
|
|
/**
|
|
|
|
* If the image changed, update scalable definitions.
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2015-01-17 01:27:35 +00:00
|
|
|
* @param {Object} originalDimensions Image original dimensions
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.updateScalableDetails = function ( originalDimensions ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
let newDimensions;
|
2015-01-17 01:27:35 +00:00
|
|
|
|
|
|
|
// Resize the new image's current dimensions to default or based on the bounding box
|
|
|
|
if ( this.isDefaultSize() ) {
|
|
|
|
// Scale to default
|
|
|
|
newDimensions = ve.dm.MWImageNode.static.scaleToThumbnailSize( originalDimensions );
|
|
|
|
} else {
|
|
|
|
if ( this.getBoundingBox() ) {
|
|
|
|
// Scale the new image by its width
|
|
|
|
newDimensions = ve.dm.MWImageNode.static.resizeToBoundingBox(
|
|
|
|
originalDimensions,
|
|
|
|
{
|
|
|
|
width: this.boundingBox.width,
|
|
|
|
height: Infinity
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
newDimensions = originalDimensions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( newDimensions ) {
|
|
|
|
this.getScalable().setCurrentDimensions( newDimensions );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
/**
|
|
|
|
* Set image caption document.
|
|
|
|
*
|
2015-08-19 18:33:59 +00:00
|
|
|
* @param {ve.dm.Document} doc Image caption document
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.setCaptionDocument = function ( doc ) {
|
|
|
|
this.captionDoc = doc;
|
|
|
|
};
|
2014-08-07 17:02:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the model attributes and parameters have been modified by
|
|
|
|
* comparing the current hash to the new hash object.
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-08-07 17:02:52 +00:00
|
|
|
* @return {boolean} Model has been modified
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.hasBeenModified = function () {
|
|
|
|
if ( this.initialHash ) {
|
|
|
|
return !ve.compare( this.initialHash, this.getHashObject() );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|