mediawiki-extensions-Visual.../modules/ve-mw/ce/nodes/ve.ce.MWImageNode.js
James D. Forrester 4be0218bbb Update VE core submodule to master (5f10e73)
New changes:
a53f101 Update OOjs UI to v0.1.0
1fa518b Update OOjs UI to v0.1.0-pre (15f4156bac)
257af53 Use ContextWidget instead of toolbar inside context menu
5f10e73 Follow-up Ia2076a42: Mark getDescription() as @inheritable for Annotation, Node

Local changes to adjust for the ContextWidget changes.

Change-Id: I47f68f5cf1b9583dab9bd0109fa6504481bdfc67
2014-06-09 18:35:08 -07:00

148 lines
3.8 KiB
JavaScript

/*!
* VisualEditor ContentEditable MWImageNode class.
*
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/*global mw */
/**
* 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 {jQuery} $figure Figure element
* @param {jQuery} $image Image element
* @param {Object} [config] Configuration options
*/
ve.ce.MWImageNode = function VeCeMWImageNode( $figure, $image, config ) {
config = ve.extendObject( {
'enforceMax': false,
'minDimensions': { 'width': 1, 'height': 1 }
}, config );
// Parent constructor
ve.ce.GeneratedContentNode.call( this );
this.$figure = $figure;
this.$image = $image;
// Mixin constructors
ve.ce.ProtectedNode.call( this, this.$figure, config );
ve.ce.FocusableNode.call( this, this.$figure, config );
ve.ce.RelocatableNode.call( this, this.$figure, config );
ve.ce.MWResizableNode.call( this, this.$image, config );
ve.ce.ClickableNode.call( this );
// Events
this.model.connect( this, { 'attributeChange': 'onAttributeChange' } );
};
/* Inheritance */
OO.inheritClass( ve.ce.MWImageNode, ve.ce.GeneratedContentNode );
OO.mixinClass( ve.ce.MWImageNode, ve.ce.ProtectedNode );
OO.mixinClass( ve.ce.MWImageNode, ve.ce.FocusableNode );
OO.mixinClass( ve.ce.MWImageNode, ve.ce.RelocatableNode );
// Need to mixin base class as well
OO.mixinClass( ve.ce.MWImageNode, ve.ce.ResizableNode );
OO.mixinClass( ve.ce.MWImageNode, ve.ce.MWResizableNode );
OO.mixinClass( ve.ce.MWImageNode, ve.ce.ClickableNode );
/* Static Properties */
ve.ce.MWImageNode.static.primaryCommandName = 'mediaEdit';
/* Static Methods */
/**
* @inheritdoc ve.ce.Node
*/
ve.ce.MWImageNode.static.getDescription = function ( model ) {
var title = new mw.Title( model.getFilename() );
return title.getMain();
};
/* Methods */
/**
* 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 () {};
/** */
ve.ce.MWImageNode.prototype.generateContents = function () {
var xhr, deferred = $.Deferred();
xhr = ve.init.mw.Target.static.apiRequest( {
'action': 'query',
'prop': 'imageinfo',
'iiprop': 'url',
'iiurlwidth': this.getModel().getAttribute( 'width' ),
'iiurlheight': this.getModel().getAttribute( 'height' ),
'titles': this.getModel().getFilename()
} )
.done( ve.bind( this.onParseSuccess, this, deferred ) )
.fail( ve.bind( this.onParseError, this, deferred ) );
return deferred.promise( { abort: xhr.abort } );
};
/**
* 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 );
} else {
deferred.reject();
}
};
/** */
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();
};