2013-10-15 12:18:11 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor ContentEditable MWImageNode class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2013-10-17 11:13:40 +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.ProtectedNode
|
|
|
|
* @mixins ve.ce.FocusableNode
|
|
|
|
* @mixins ve.ce.RelocatableNode
|
|
|
|
* @mixins ve.ce.MWResizableNode
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
2013-11-01 06:13:09 +00:00
|
|
|
ve.ce.MWImageNode = function VeCeMWImageNode( $figure, $image, 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.ProtectedNode.call( this, this.$figure, config );
|
|
|
|
ve.ce.FocusableNode.call( this, this.$figure, config );
|
|
|
|
ve.ce.RelocatableNode.call( this, this.$figure, config );
|
2013-10-15 12:18:11 +00:00
|
|
|
ve.ce.MWResizableNode.call( this, this.$image, config );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 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.ProtectedNode );
|
2013-10-15 12:18:11 +00:00
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.mixinClass( ve.ce.MWImageNode, ve.ce.FocusableNode );
|
2013-10-15 12:18:11 +00:00
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.mixinClass( ve.ce.MWImageNode, ve.ce.RelocatableNode );
|
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 );
|
2013-10-15 12:18:11 +00:00
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.mixinClass( ve.ce.MWImageNode, ve.ce.MWResizableNode );
|
2013-10-15 12:18:11 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/** */
|
|
|
|
ve.ce.MWImageNode.prototype.generateContents = function () {
|
2013-10-17 11:13:40 +00:00
|
|
|
var xhr, deferred = $.Deferred();
|
|
|
|
|
|
|
|
xhr = $.ajax( {
|
|
|
|
'url': mw.util.wikiScript( 'api' ),
|
|
|
|
'data': {
|
|
|
|
'action': 'query',
|
|
|
|
'prop': 'imageinfo',
|
|
|
|
'iiprop': 'url',
|
|
|
|
'iiurlwidth': this.model.getAttribute( 'width' ),
|
|
|
|
'iiurlheight': this.model.getAttribute( 'height' ),
|
|
|
|
'titles': this.model.getAttribute( 'resource' ).replace( /^(.+\/)*/, '' ),
|
|
|
|
'format': 'json'
|
|
|
|
},
|
2013-10-17 11:39:27 +00:00
|
|
|
'cache': 'false'
|
|
|
|
} )
|
|
|
|
.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();
|
|
|
|
};
|