mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-15 10:35:48 +00:00
8a40585847
Using the MW APIs get a resized version of the image and use GeneratedContentNode to cache the url. Bug: 55697 Change-Id: I418f7e1464663f447d46de7ffc29aa5f52d23b12
123 lines
3.5 KiB
JavaScript
123 lines
3.5 KiB
JavaScript
/*!
|
|
* VisualEditor ContentEditable MWImageNode class.
|
|
*
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
ve.ce.MWImageNode = function VeCeMWImageNode( $inner, $image, config ) {
|
|
// Parent constructor
|
|
ve.ce.GeneratedContentNode.call( this );
|
|
|
|
this.$inner = $inner;
|
|
this.$image = $image;
|
|
|
|
// Mixin constructors
|
|
ve.ce.ProtectedNode.call( this, this.$inner, config );
|
|
ve.ce.FocusableNode.call( this, this.$inner, config );
|
|
ve.ce.RelocatableNode.call( this, this.$inner, config );
|
|
ve.ce.MWResizableNode.call( this, this.$image, config );
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
ve.inheritClass( ve.ce.MWImageNode, ve.ce.GeneratedContentNode );
|
|
|
|
ve.mixinClass( ve.ce.MWImageNode, ve.ce.ProtectedNode );
|
|
|
|
ve.mixinClass( ve.ce.MWImageNode, ve.ce.FocusableNode );
|
|
|
|
ve.mixinClass( ve.ce.MWImageNode, ve.ce.RelocatableNode );
|
|
|
|
// Need to mixin base class as well
|
|
ve.mixinClass( ve.ce.MWImageNode, ve.ce.ResizableNode );
|
|
|
|
ve.mixinClass( ve.ce.MWImageNode, ve.ce.MWResizableNode );
|
|
|
|
/* Methods */
|
|
|
|
/** */
|
|
ve.ce.MWImageNode.prototype.generateContents = function () {
|
|
var i, len, source,
|
|
sources = ve.copy( ve.init.platform.getMediaSources() ),
|
|
deferred = $.Deferred();
|
|
|
|
for ( i = 0, len = sources.length; i < len; i++ ) {
|
|
source = sources[i];
|
|
source.request = $.ajax( {
|
|
'url': source.url,
|
|
'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'
|
|
},
|
|
// This request won't be cached since the JSON-P callback is unique. However make sure
|
|
// to allow jQuery to cache otherwise so it won't e.g. add "&_=(random)" which will
|
|
// trigger a MediaWiki API error for invalid parameter "_".
|
|
'cache': true,
|
|
// TODO: Only use JSON-P for cross-domain.
|
|
// jQuery has this logic built-in (if url is not same-origin ..)
|
|
// but isn't working for some reason.
|
|
'dataType': 'jsonp',
|
|
'success': ve.bind( this.onParseSuccess, this, deferred ),
|
|
'error': ve.bind( this.onParseError, this, deferred )
|
|
} );
|
|
}
|
|
|
|
return deferred.promise();
|
|
};
|
|
|
|
/**
|
|
* 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 );
|
|
}
|
|
};
|
|
|
|
/** */
|
|
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();
|
|
};
|