2013-10-15 12:18:11 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor ContentEditable 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-06-10 01:18:23 +00:00
|
|
|
/*global mw */
|
|
|
|
|
2013-10-15 12:18:11 +00:00
|
|
|
/**
|
|
|
|
* ContentEditable MediaWiki image node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @extends ve.ce.GeneratedContentNode
|
|
|
|
* @mixins ve.ce.FocusableNode
|
|
|
|
* @mixins ve.ce.MWResizableNode
|
|
|
|
*
|
|
|
|
* @constructor
|
2013-12-18 21:22:41 +00:00
|
|
|
* @param {jQuery} $figure Figure element
|
|
|
|
* @param {jQuery} $image Image element
|
2013-10-15 12:18:11 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
2013-11-01 06:13:09 +00:00
|
|
|
ve.ce.MWImageNode = function VeCeMWImageNode( $figure, $image, config ) {
|
2014-01-26 20:09:40 +00:00
|
|
|
config = ve.extendObject( {
|
|
|
|
'enforceMax': false,
|
|
|
|
'minDimensions': { 'width': 1, 'height': 1 }
|
|
|
|
}, config );
|
|
|
|
|
2013-10-15 12:18:11 +00:00
|
|
|
// Parent constructor
|
|
|
|
ve.ce.GeneratedContentNode.call( this );
|
|
|
|
|
2013-11-01 06:13:09 +00:00
|
|
|
this.$figure = $figure;
|
2013-10-15 12:18:11 +00:00
|
|
|
this.$image = $image;
|
|
|
|
|
|
|
|
// Mixin constructors
|
2013-11-01 06:13:09 +00:00
|
|
|
ve.ce.FocusableNode.call( this, this.$figure, config );
|
2013-10-15 12:18:11 +00:00
|
|
|
ve.ce.MWResizableNode.call( this, this.$image, config );
|
2014-01-26 20:09:40 +00:00
|
|
|
|
|
|
|
// Events
|
2014-04-10 00:26:48 +00:00
|
|
|
this.model.connect( this, { 'attributeChange': 'onAttributeChange' } );
|
2013-10-15 12:18:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.ce.MWImageNode, ve.ce.GeneratedContentNode );
|
2013-10-15 12:18:11 +00:00
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.mixinClass( ve.ce.MWImageNode, ve.ce.FocusableNode );
|
2014-04-10 00:26:48 +00:00
|
|
|
|
2013-10-15 12:18:11 +00:00
|
|
|
// Need to mixin base class as well
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.mixinClass( ve.ce.MWImageNode, ve.ce.ResizableNode );
|
2014-04-10 00:26:48 +00:00
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.mixinClass( ve.ce.MWImageNode, ve.ce.MWResizableNode );
|
2014-04-10 00:26:48 +00:00
|
|
|
|
2013-12-20 23:25:24 +00:00
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.ce.MWImageNode.static.primaryCommandName = 'mediaEdit';
|
2013-10-15 12:18:11 +00:00
|
|
|
|
2014-06-10 01:18:23 +00:00
|
|
|
/* Static Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc ve.ce.Node
|
|
|
|
*/
|
|
|
|
ve.ce.MWImageNode.static.getDescription = function ( model ) {
|
|
|
|
var title = new mw.Title( model.getFilename() );
|
|
|
|
return title.getMain();
|
|
|
|
};
|
|
|
|
|
2013-10-15 12:18:11 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2014-04-10 00:26:48 +00:00
|
|
|
/**
|
|
|
|
* 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.ce.MWImageNode.prototype.onAttributeChange = function () {};
|
|
|
|
|
2013-10-15 12:18:11 +00:00
|
|
|
/** */
|
|
|
|
ve.ce.MWImageNode.prototype.generateContents = function () {
|
2013-10-17 11:13:40 +00:00
|
|
|
var xhr, deferred = $.Deferred();
|
|
|
|
|
2014-01-05 04:44:13 +00:00
|
|
|
xhr = ve.init.mw.Target.static.apiRequest( {
|
2013-10-17 11:13:40 +00:00
|
|
|
'action': 'query',
|
|
|
|
'prop': 'imageinfo',
|
|
|
|
'iiprop': 'url',
|
2014-01-26 20:09:40 +00:00
|
|
|
'iiurlwidth': this.getModel().getAttribute( 'width' ),
|
|
|
|
'iiurlheight': this.getModel().getAttribute( 'height' ),
|
2014-05-28 19:21:06 +00:00
|
|
|
'titles': this.getModel().getFilename()
|
2013-10-17 11:39:27 +00:00
|
|
|
} )
|
|
|
|
.done( ve.bind( this.onParseSuccess, this, deferred ) )
|
|
|
|
.fail( ve.bind( this.onParseError, this, deferred ) );
|
2013-10-15 12:18:11 +00:00
|
|
|
|
2013-10-17 11:39:27 +00:00
|
|
|
return deferred.promise( { abort: xhr.abort } );
|
2013-10-15 12:18:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a successful response from the parser for the image src.
|
|
|
|
*
|
|
|
|
* @param {jQuery.Deferred} deferred The Deferred object created by generateContents
|
|
|
|
* @param {Object} response Response data
|
|
|
|
*/
|
|
|
|
ve.ce.MWImageNode.prototype.onParseSuccess = function ( deferred, response ) {
|
|
|
|
var id, src, pages = ve.getProp( response, 'query', 'pages' );
|
|
|
|
for ( id in pages ) {
|
|
|
|
if ( pages[id].imageinfo ) {
|
|
|
|
src = pages[id].imageinfo[0].thumburl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( src ) {
|
|
|
|
deferred.resolve( src );
|
2013-10-17 11:13:40 +00:00
|
|
|
} else {
|
|
|
|
deferred.reject();
|
2013-10-15 12:18:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** */
|
|
|
|
ve.ce.MWImageNode.prototype.render = function ( generateContents ) {
|
|
|
|
this.$image.attr( 'src', generateContents );
|
|
|
|
if ( this.live ) {
|
|
|
|
this.afterRender();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an unsuccessful response from the parser for the image src.
|
|
|
|
*
|
|
|
|
* @param {jQuery.Deferred} deferred The promise object created by generateContents
|
|
|
|
* @param {Object} response Response data
|
|
|
|
*/
|
|
|
|
ve.ce.MWImageNode.prototype.onParseError = function ( deferred ) {
|
|
|
|
deferred.reject();
|
|
|
|
};
|