2014-03-25 16:01:04 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWImageModel class.
|
|
|
|
*
|
2018-01-03 00:54:47 +00:00
|
|
|
* @copyright 2011-2018 VisualEditor Team and others; see AUTHORS.txt
|
2014-03-25 16:01:04 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MediaWiki image model.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @mixins OO.EventEmitter
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* @cfg {string} [resourceName] The resource name of the given media file
|
|
|
|
* @cfg {Object} [currentDimensions] Current dimensions, width & height
|
|
|
|
* @cfg {Object} [minDimensions] Minimum dimensions, width & height
|
|
|
|
* @cfg {boolean} [isDefaultSize] 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
|
|
|
var scalable, currentDimensions, minDimensions;
|
|
|
|
|
|
|
|
config = config || {};
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
// Mixin constructors
|
|
|
|
OO.EventEmitter.call( this );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.attributesCache = null;
|
|
|
|
|
|
|
|
// 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 = '';
|
|
|
|
|
|
|
|
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' )
|
|
|
|
.defaultUserOptions.defaultthumbsize;
|
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
|
|
|
|
currentDimensions = config.currentDimensions || {};
|
|
|
|
minDimensions = config.minDimensions || {};
|
|
|
|
|
|
|
|
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
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @event alignmentChange
|
|
|
|
* @param {string} Alignment 'left', 'right', 'center' or 'none'
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change in size type between default and custom
|
2014-06-26 10:41:18 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @event sizeDefaultChange
|
|
|
|
* @param {boolean} Image is default size
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change in the image type
|
2014-06-26 10:41:18 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @event typeChange
|
|
|
|
* @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 ) {
|
2014-07-15 18:43:54 +00:00
|
|
|
var attrs, newNode, newDimensions,
|
2014-06-19 15:44:05 +00:00
|
|
|
defaultThumbSize = mw.config.get( 'wgVisualEditorConfig' ).defaultUserOptions.defaultthumbsize;
|
|
|
|
|
|
|
|
attrs = ve.extendObject( {
|
2017-04-27 18:08:35 +00:00
|
|
|
mediaClass: 'Image',
|
2014-08-22 20:50:48 +00:00
|
|
|
type: 'thumb',
|
|
|
|
align: 'default',
|
|
|
|
width: defaultThumbSize,
|
|
|
|
mediaType: 'BITMAP',
|
|
|
|
defaultSize: true
|
2014-06-19 15:44:05 +00:00
|
|
|
}, attributes );
|
|
|
|
|
|
|
|
if ( attrs.defaultSize ) {
|
2014-07-25 02:28:07 +00:00
|
|
|
newDimensions = ve.dm.MWImageNode.static.scaleToThumbnailSize( attrs, attrs.mediaType );
|
|
|
|
if ( newDimensions ) {
|
2014-06-19 15:44:05 +00:00
|
|
|
attrs.width = newDimensions.width;
|
|
|
|
attrs.height = newDimensions.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
imageType = imageType || 'mwBlockImage';
|
|
|
|
|
2015-03-22 18:26:09 +00:00
|
|
|
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 ) {
|
|
|
|
var 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 );
|
2018-02-28 02:01:05 +00:00
|
|
|
imgModel.setFilename( new mw.Title( ve.normalizeParsoidResourceName( attrs.resource ) ).getMainText() );
|
2014-07-25 02:28:07 +00:00
|
|
|
imgModel.setImageHref( attrs.href );
|
|
|
|
|
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
|
|
|
*
|
2015-03-07 04:43:53 +00:00
|
|
|
* @return {Object} Hash object
|
2014-07-25 02:28:07 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getHashObject = function () {
|
|
|
|
var 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 ) {
|
|
|
|
var imageModel = this;
|
2014-07-25 02:28:07 +00:00
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
if ( attrs.resource ) {
|
|
|
|
this.setImageResourceName( attrs.resource );
|
2018-02-28 02:01:05 +00:00
|
|
|
this.setFilename( new mw.Title( 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();
|
|
|
|
|
2015-01-17 01:27:35 +00:00
|
|
|
// If we already have dimensions from the API, use them
|
|
|
|
if ( APIinfo ) {
|
|
|
|
imageModel.scalable.setOriginalDimensions( {
|
|
|
|
width: APIinfo.width,
|
|
|
|
height: APIinfo.height
|
|
|
|
} );
|
|
|
|
// Update media type
|
|
|
|
imageModel.setMediaType( APIinfo.mediatype );
|
|
|
|
// Update defaults
|
|
|
|
ve.dm.MWImageNode.static.syncScalableToType(
|
|
|
|
imageModel.getType(),
|
|
|
|
APIinfo.mediatype,
|
|
|
|
imageModel.scalable
|
|
|
|
);
|
|
|
|
imageModel.updateScalableDetails( {
|
|
|
|
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
|
2015-01-17 01:27:35 +00:00
|
|
|
ve.dm.MWImageNode.static.getScalablePromise( this.getFilename() ).done( function ( info ) {
|
|
|
|
imageModel.scalable.setOriginalDimensions( {
|
|
|
|
width: info.width,
|
|
|
|
height: info.height
|
|
|
|
} );
|
|
|
|
// Update media type
|
|
|
|
imageModel.setMediaType( info.mediatype );
|
|
|
|
// Update defaults
|
|
|
|
ve.dm.MWImageNode.static.syncScalableToType(
|
|
|
|
imageModel.getType(),
|
|
|
|
info.mediatype,
|
|
|
|
imageModel.scalable
|
|
|
|
);
|
|
|
|
imageModel.updateScalableDetails( {
|
|
|
|
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 ) {
|
2014-05-21 03:58:08 +00:00
|
|
|
var captionRange, captionNode,
|
2014-07-17 19:55:24 +00:00
|
|
|
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' ) {
|
|
|
|
captionNode = node.getCaptionNode();
|
|
|
|
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
|
|
|
|
2014-05-21 03:58:08 +00:00
|
|
|
captionRange = captionNode.getRange();
|
|
|
|
|
|
|
|
// 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 ) {
|
2017-08-24 14:56:22 +00:00
|
|
|
var captionDoc, offset, contentToInsert, selectedNode,
|
2014-03-25 16:01:04 +00:00
|
|
|
nodeType = this.getImageNodeType(),
|
|
|
|
surfaceModel = fragment.getSurface();
|
|
|
|
|
2014-09-30 16:34:33 +00:00
|
|
|
if ( !( fragment.getSelection() instanceof ve.dm.LinearSelection ) ) {
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
2017-08-24 14:56:22 +00:00
|
|
|
selectedNode = fragment.getSelectedNode();
|
|
|
|
|
|
|
|
// If there was a previous node, remove it first
|
|
|
|
if ( selectedNode ) {
|
|
|
|
// Remove the old image
|
|
|
|
fragment.removeContent();
|
|
|
|
}
|
|
|
|
|
2015-08-02 18:25:35 +00:00
|
|
|
contentToInsert = this.getData();
|
2014-03-25 16:01:04 +00:00
|
|
|
|
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 ) {
|
2014-09-30 16:34:33 +00:00
|
|
|
fragment = fragment.clone( new ve.dm.LinearSelection( fragment.getDocument(), 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 ) {
|
2014-09-30 16:34:33 +00:00
|
|
|
fragment = fragment.clone( new ve.dm.LinearSelection( fragment.getDocument(), new ve.Range( offset ) ) );
|
2014-06-19 11:02:41 +00:00
|
|
|
}
|
|
|
|
fragment.insertContent( contentToInsert );
|
|
|
|
// Check if there is caption document and insert it
|
|
|
|
captionDoc = this.getCaptionDocument();
|
2015-06-30 08:45:24 +00:00
|
|
|
if ( captionDoc.data.hasContent() ) {
|
2014-06-19 11:02:41 +00:00
|
|
|
// Add contents of new caption
|
|
|
|
surfaceModel.change(
|
2016-11-30 22:03:03 +00:00
|
|
|
ve.dm.TransactionBuilder.static.newFromDocumentInsertion(
|
2014-06-19 11:02:41 +00:00
|
|
|
surfaceModel.getDocument(),
|
2014-09-30 16:34:33 +00:00
|
|
|
fragment.getSelection().getRange().start + 2,
|
2014-06-19 11:02:41 +00:00
|
|
|
this.getCaptionDocument()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
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 () {
|
|
|
|
var data,
|
|
|
|
originalAttrs = ve.copy( this.getOriginalImageAttributes() ),
|
|
|
|
editAttributes = $.extend( originalAttrs, this.getUpdatedAttributes() ),
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = [
|
|
|
|
{
|
|
|
|
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 () {
|
2014-06-03 07:32:47 +00:00
|
|
|
var attrs, currentDimensions,
|
|
|
|
origAttrs = this.getOriginalImageAttributes();
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2014-03-25 16:01:04 +00:00
|
|
|
attrs = {
|
2017-04-27 18:08:35 +00:00
|
|
|
mediaClass: this.getMediaClass(),
|
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
|
|
|
};
|
|
|
|
|
|
|
|
if ( origAttrs.alt !== undefined || this.getAltText() !== '' ) {
|
|
|
|
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();
|
|
|
|
attrs.resource = this.getImageResourceName();
|
|
|
|
|
2014-05-21 22:55:56 +00:00
|
|
|
// If converting from block to inline, set isLinked=true to avoid |link=
|
|
|
|
if ( origAttrs.isLinked === undefined && this.getImageNodeType() === 'mwInlineImage' ) {
|
|
|
|
attrs.isLinked = true;
|
|
|
|
}
|
|
|
|
|
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 () {
|
|
|
|
return this.scalable.isDefault();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) {
|
|
|
|
var 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
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Get Parsoid media class: Image, Video or Audio
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getMediaClass = function () {
|
|
|
|
var mediaType = this.getMediaType();
|
|
|
|
|
|
|
|
if ( mediaType === 'VIDEO' ) {
|
|
|
|
return 'Video';
|
|
|
|
}
|
|
|
|
if ( mediaType === 'AUDIO' ) {
|
|
|
|
return 'Audio';
|
|
|
|
}
|
|
|
|
return 'Image';
|
|
|
|
};
|
|
|
|
|
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
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {ve.dm.Scalable} Scalable object
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.getScalable = function () {
|
|
|
|
return this.scalable;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the image current dimensions
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2014-03-25 16:01:04 +00:00
|
|
|
* @return {Object} Current dimensions width/height
|
|
|
|
* @return {number} dimensions.width The width of the image
|
|
|
|
* @return {number} dimensions.height The height of the image
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
* @fires sizeDefaultChange
|
|
|
|
*/
|
|
|
|
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 ) {
|
2014-07-15 18:43:54 +00:00
|
|
|
var 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
|
|
|
|
* @fires typeChange
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageModel.prototype.setType = function ( type ) {
|
2014-06-03 04:20:57 +00:00
|
|
|
var 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 () {
|
|
|
|
var originalDimensions = this.scalable.getOriginalDimensions();
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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
|
|
|
};
|
|
|
|
|
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
|
|
|
*
|
2015-08-19 18:33:59 +00:00
|
|
|
* @param {ve.dm.Scalable} scalable Scalable object
|
2014-03-25 16:01:04 +00:00
|
|
|
*/
|
2014-07-25 02:28:07 +00:00
|
|
|
ve.dm.MWImageModel.prototype.attachScalable = function ( scalable ) {
|
2018-02-28 02:01:05 +00:00
|
|
|
var imageName = ve.normalizeParsoidResourceName( this.getResourceName() ),
|
2014-10-29 01:43:03 +00:00
|
|
|
imageModel = this;
|
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 ) {
|
2014-10-29 01:43:03 +00:00
|
|
|
ve.dm.MWImageNode.static.getScalablePromise( imageName ).done( function ( info ) {
|
|
|
|
imageModel.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
|
2014-10-29 01:43:03 +00:00
|
|
|
imageModel.setMediaType( info.mediatype );
|
2014-07-24 23:50:22 +00:00
|
|
|
// Update according to type
|
|
|
|
ve.dm.MWImageNode.static.syncScalableToType(
|
2014-10-29 01:43:03 +00:00
|
|
|
imageModel.getType(),
|
|
|
|
imageModel.getMediaType(),
|
|
|
|
imageModel.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
|
2018-05-20 10:00:09 +00:00
|
|
|
if ( !ve.isEmptyObject( imageModel.initialHash ) && imageModel.initialHash.scalable.isDefault ) {
|
2014-10-29 01:43:03 +00:00
|
|
|
imageModel.initialHash.scalable.currentDimensions = imageModel.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 ) {
|
|
|
|
var newDimensions;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
};
|