2013-10-15 12:18:11 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWImageNode class.
|
|
|
|
*
|
2014-01-05 12:05:05 +00:00
|
|
|
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
|
2013-10-15 12:18:11 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
2014-04-10 00:26:48 +00:00
|
|
|
/*global mw */
|
2013-10-15 12:18:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DataModel generated content node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @extends ve.dm.GeneratedContentNode
|
2014-04-10 00:26:48 +00:00
|
|
|
* @mixins ve.dm.ResizableNode
|
|
|
|
*
|
2013-10-15 12:18:11 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageNode = function VeDmMWImageNode() {
|
|
|
|
// Parent constructor
|
|
|
|
ve.dm.GeneratedContentNode.call( this );
|
2014-04-10 00:26:48 +00:00
|
|
|
// Mixin constructor
|
|
|
|
ve.dm.ResizableNode.call( this );
|
|
|
|
|
|
|
|
this.scalablePromise = null;
|
|
|
|
|
|
|
|
// Use 'bitmap' as default media type until we can
|
|
|
|
// fetch the actual media type from the API
|
|
|
|
this.mediaType = 'BITMAP';
|
|
|
|
// Get wiki defaults
|
|
|
|
this.svgMaxSize = mw.config.get( 'wgVisualEditor' ).svgMaxSize;
|
|
|
|
|
|
|
|
// Initialize
|
2014-07-15 18:43:54 +00:00
|
|
|
this.constructor.static.syncScalableToType(
|
|
|
|
this.getAttribute( 'type' ),
|
|
|
|
this.mediaType,
|
|
|
|
this.getScalable()
|
|
|
|
);
|
2014-04-10 00:26:48 +00:00
|
|
|
|
|
|
|
// Events
|
|
|
|
this.connect( this, { 'attributeChange': 'onAttributeChange' } );
|
2013-10-15 12:18:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.dm.MWImageNode, ve.dm.GeneratedContentNode );
|
2013-10-15 12:18:11 +00:00
|
|
|
|
2014-04-10 00:26:48 +00:00
|
|
|
OO.mixinClass( ve.dm.MWImageNode, ve.dm.ResizableNode );
|
|
|
|
|
2014-01-26 15:02:07 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
2014-06-04 18:20:37 +00:00
|
|
|
* Update image scalable properties according to the image type.
|
2014-01-26 15:02:07 +00:00
|
|
|
*
|
2014-04-10 00:26:48 +00:00
|
|
|
* @param {string} type The new image type
|
2014-07-15 18:43:54 +00:00
|
|
|
* @param {string} mediaType Image media type 'DRAWING' or 'BITMAP'
|
|
|
|
* @param {ve.dm.Scalable} scalable The scalable object to update
|
2014-01-26 15:02:07 +00:00
|
|
|
*/
|
2014-07-15 18:43:54 +00:00
|
|
|
ve.dm.MWImageNode.static.syncScalableToType = function ( type, mediaType, scalable ) {
|
2014-04-10 00:26:48 +00:00
|
|
|
var originalDimensions, dimensions,
|
2014-07-15 18:43:54 +00:00
|
|
|
width = scalable.getCurrentDimensions().width,
|
|
|
|
defaultThumbSize = mw.config.get( 'wgVisualEditorConfig' ).defaultUserOptions.defaultthumbsize;
|
2014-03-25 16:01:04 +00:00
|
|
|
|
2014-04-10 00:26:48 +00:00
|
|
|
originalDimensions = scalable.getOriginalDimensions();
|
|
|
|
|
|
|
|
// Deal with the different default sizes
|
|
|
|
if ( type === 'thumb' || type === 'frameless' ) {
|
2014-06-04 18:20:37 +00:00
|
|
|
// Set the default size to that in the wiki configuration if
|
|
|
|
// 1. The image width is not smaller than the default
|
|
|
|
// 2. If the image is an SVG drawing
|
2014-07-15 18:43:54 +00:00
|
|
|
if ( width >= defaultThumbSize || mediaType === 'DRAWING' ) {
|
|
|
|
dimensions = ve.dm.Scalable.static.getDimensionsFromValue( {
|
|
|
|
'width': defaultThumbSize
|
|
|
|
}, scalable.getRatio() );
|
2014-04-10 00:26:48 +00:00
|
|
|
} else {
|
2014-07-15 18:43:54 +00:00
|
|
|
dimensions = ve.dm.Scalable.static.getDimensionsFromValue(
|
|
|
|
scalable.getCurrentDimensions(),
|
|
|
|
scalable.getRatio()
|
|
|
|
);
|
2014-04-10 00:26:48 +00:00
|
|
|
}
|
|
|
|
scalable.setDefaultDimensions( dimensions );
|
|
|
|
} else {
|
|
|
|
if ( originalDimensions ) {
|
|
|
|
scalable.setDefaultDimensions( originalDimensions );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deal with maximum dimensions for images and drawings
|
2014-07-15 18:43:54 +00:00
|
|
|
if ( mediaType !== 'DRAWING' ) {
|
2014-04-10 00:26:48 +00:00
|
|
|
if ( originalDimensions ) {
|
|
|
|
scalable.setMaxDimensions( originalDimensions );
|
|
|
|
scalable.setEnforcedMax( true );
|
|
|
|
} else {
|
|
|
|
scalable.setEnforcedMax( false );
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 19:40:20 +00:00
|
|
|
// TODO: Some day, when svgMaxSize works properly in MediaWiki
|
|
|
|
// we can add it back as max dimension consideration.
|
2014-04-10 00:26:48 +00:00
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Respond to attribute change.
|
|
|
|
* Update the rendering of the 'align', src', 'width' and 'height' attributes
|
|
|
|
* when they change in the model.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} key Attribute key
|
|
|
|
* @param {string} from Old value
|
|
|
|
* @param {string} to New value
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageNode.prototype.onAttributeChange = function ( key, from, to ) {
|
|
|
|
if ( key === 'type' ) {
|
2014-07-15 18:43:54 +00:00
|
|
|
this.constructor.static.syncScalableToType( to, this.mediaType, this.getScalable() );
|
2014-01-26 15:02:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the normalised filename of the image
|
|
|
|
*
|
|
|
|
* @returns {string} Filename
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageNode.prototype.getFilename = function () {
|
2014-05-28 19:21:06 +00:00
|
|
|
// Strip ./ stuff and decode URI encoding
|
|
|
|
var resource = this.getAttribute( 'resource' ),
|
|
|
|
filename = resource.replace( /^(.+\/)*/, '' );
|
|
|
|
|
|
|
|
// Protect against decodeURIComponent() throwing exceptions
|
|
|
|
try {
|
|
|
|
filename = decodeURIComponent( filename );
|
|
|
|
} catch ( e ) {
|
|
|
|
ve.log( 'URI decoding exception', e );
|
|
|
|
}
|
|
|
|
return filename;
|
2014-01-26 15:02:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the store hash for the original dimensions of the image
|
|
|
|
*
|
|
|
|
* @returns {string} Store hash
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageNode.prototype.getSizeHash = function () {
|
|
|
|
return 'MWImageOriginalSize:' + this.getFilename();
|
|
|
|
};
|
|
|
|
|
2013-10-15 12:18:11 +00:00
|
|
|
/* Static methods */
|
|
|
|
|
|
|
|
ve.dm.MWImageNode.static.getHashObject = function ( dataElement ) {
|
|
|
|
return {
|
|
|
|
'type': dataElement.type,
|
|
|
|
'resource': dataElement.attributes.resource,
|
|
|
|
'width': dataElement.attributes.width,
|
|
|
|
'height': dataElement.attributes.height
|
|
|
|
};
|
|
|
|
};
|
2014-04-10 00:26:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageNode.prototype.getScalable = function () {
|
|
|
|
this.getScalablePromise();
|
|
|
|
// Parent method
|
|
|
|
return ve.dm.ResizableNode.prototype.getScalable.call( this );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the scalable promise which fetches original dimensions from the API
|
|
|
|
*
|
|
|
|
* @returns {jQuery.Promise} Promise which resolves setOriginalDimensions has been called (if required)
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageNode.prototype.getScalablePromise = function () {
|
|
|
|
// On the first call set off an async call to update the scalable's
|
|
|
|
// original dimensions from the API.
|
2014-06-24 17:15:48 +00:00
|
|
|
if ( ve.init.target && !this.scalablePromise ) {
|
2014-05-23 09:57:58 +00:00
|
|
|
this.scalablePromise = ve.init.target.constructor.static.apiRequest(
|
2014-05-14 22:54:36 +00:00
|
|
|
{
|
|
|
|
'action': 'query',
|
|
|
|
'prop': 'imageinfo',
|
|
|
|
'indexpageids': '1',
|
|
|
|
'iiprop': 'size|mediatype',
|
|
|
|
'titles': this.getFilename()
|
|
|
|
},
|
|
|
|
{ 'type': 'POST' }
|
2014-07-08 22:33:32 +00:00
|
|
|
).then( function ( response ) {
|
2014-05-14 22:54:36 +00:00
|
|
|
var page = response.query && response.query.pages[response.query.pageids[0]],
|
|
|
|
info = page && page.imageinfo && page.imageinfo[0];
|
|
|
|
|
|
|
|
if ( info ) {
|
|
|
|
this.getScalable().setOriginalDimensions( {
|
|
|
|
'width': info.width,
|
|
|
|
'height': info.height
|
|
|
|
} );
|
|
|
|
// Update media type
|
|
|
|
this.mediaType = info.mediatype;
|
2014-03-25 16:01:04 +00:00
|
|
|
// Update according to type
|
2014-07-15 18:43:54 +00:00
|
|
|
this.constructor.static.syncScalableToType(
|
|
|
|
this.getAttribute( 'type' ),
|
|
|
|
this.mediaType,
|
|
|
|
this.getScalable()
|
|
|
|
);
|
2014-05-14 22:54:36 +00:00
|
|
|
}
|
2014-07-08 22:33:32 +00:00
|
|
|
}.bind( this ) ).promise();
|
2014-04-10 00:26:48 +00:00
|
|
|
}
|
|
|
|
return this.scalablePromise;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageNode.prototype.createScalable = function () {
|
|
|
|
return new ve.dm.Scalable( {
|
|
|
|
'currentDimensions': {
|
|
|
|
'width': this.getAttribute( 'width' ),
|
|
|
|
'height': this.getAttribute( 'height' )
|
|
|
|
},
|
|
|
|
'minDimensions': {
|
|
|
|
'width': 1,
|
|
|
|
'height': 1
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
2014-06-04 18:20:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get symbolic name of media type.
|
|
|
|
*
|
|
|
|
* Example values: "BITMAP" for JPEG or PNG images; "DRAWING" for SVG graphics
|
|
|
|
*
|
|
|
|
* @return {string|undefined} Symbolic media type name, or undefined if empty
|
|
|
|
*/
|
|
|
|
ve.dm.MWImageNode.prototype.getMediaType = function () {
|
|
|
|
return this.mediaType;
|
|
|
|
};
|