2013-06-06 23:14:11 +00:00
|
|
|
/*!
|
2013-06-25 00:41:01 +00:00
|
|
|
* VisualEditor UserInterface MWMediaResultWidget class.
|
2013-06-06 23:14:11 +00:00
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*global mw */
|
|
|
|
|
|
|
|
/**
|
2013-06-25 00:41:01 +00:00
|
|
|
* Creates an ve.ui.MWMediaResultWidget object.
|
2013-06-06 23:14:11 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ui.OptionWidget
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Mixed} data Item data
|
|
|
|
* @param {Object} [config] Config options
|
|
|
|
* @cfg {number} [size] Media thumbnail size
|
|
|
|
*/
|
2013-06-25 00:41:01 +00:00
|
|
|
ve.ui.MWMediaResultWidget = function VeUiMWMediaResultWidget( data, config ) {
|
2013-06-06 23:14:11 +00:00
|
|
|
// Configuration intialization
|
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Parent constructor
|
|
|
|
ve.ui.OptionWidget.call( this, data, config );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.size = config.size || 150;
|
|
|
|
this.$thumb = this.buildThumbnail();
|
|
|
|
this.$overlay = this.$$( '<div>' );
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.setLabel( new mw.Title( this.data.title ).getNameText() );
|
2013-06-25 00:41:01 +00:00
|
|
|
this.$overlay.addClass( 've-ui-mwMediaResultWidget-overlay' );
|
2013-06-06 23:14:11 +00:00
|
|
|
this.$
|
2013-06-25 00:41:01 +00:00
|
|
|
.addClass( 've-ui-mwMediaResultWidget ve-ui-texture-pending' )
|
2013-06-06 23:14:11 +00:00
|
|
|
.css( { 'width': this.size, 'height': this.size } )
|
|
|
|
.prepend( this.$thumb, this.$overlay );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-06-25 00:41:01 +00:00
|
|
|
ve.inheritClass( ve.ui.MWMediaResultWidget, ve.ui.OptionWidget );
|
2013-06-06 23:14:11 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2013-07-31 22:53:29 +00:00
|
|
|
/** */
|
2013-06-25 00:41:01 +00:00
|
|
|
ve.ui.MWMediaResultWidget.prototype.onThumbnailLoad = function () {
|
2013-06-06 23:14:11 +00:00
|
|
|
this.$thumb.first().addClass( 've-ui-texture-transparency' );
|
|
|
|
this.$
|
2013-06-25 00:41:01 +00:00
|
|
|
.addClass( 've-ui-mwMediaResultWidget-done' )
|
2013-06-06 23:14:11 +00:00
|
|
|
.removeClass( 've-ui-texture-pending' );
|
|
|
|
};
|
|
|
|
|
2013-07-31 22:53:29 +00:00
|
|
|
/** */
|
2013-06-25 00:41:01 +00:00
|
|
|
ve.ui.MWMediaResultWidget.prototype.onThumbnailError = function () {
|
2013-06-06 23:14:11 +00:00
|
|
|
this.$thumb.last()
|
|
|
|
.css( 'background-image', '' )
|
|
|
|
.addClass( 've-ui-texture-alert' );
|
|
|
|
this.$
|
2013-06-25 00:41:01 +00:00
|
|
|
.addClass( 've-ui-mwMediaResultWidget-error' )
|
2013-06-06 23:14:11 +00:00
|
|
|
.removeClass( 've-ui-texture-pending' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a thumbnail.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {jQuery} Thumbnail element
|
|
|
|
*/
|
2013-06-25 00:41:01 +00:00
|
|
|
ve.ui.MWMediaResultWidget.prototype.buildThumbnail = function () {
|
2013-06-06 23:14:11 +00:00
|
|
|
var info = this.data.imageinfo[0],
|
|
|
|
image = new Image(),
|
|
|
|
$image = this.$$( image ),
|
|
|
|
$back = this.$$( '<div>' ),
|
|
|
|
$front = this.$$( '<div>' ),
|
|
|
|
$thumb = $back.add( $front );
|
|
|
|
|
|
|
|
// Preload image
|
|
|
|
$image
|
|
|
|
.load( ve.bind( this.onThumbnailLoad, this ) )
|
|
|
|
.error( ve.bind( this.onThumbnailError, this ) );
|
|
|
|
image.src = info.thumburl;
|
|
|
|
|
2013-06-25 00:41:01 +00:00
|
|
|
$thumb.addClass( 've-ui-mwMediaResultWidget-thumbnail' );
|
2013-06-06 23:14:11 +00:00
|
|
|
$thumb.last().css( 'background-image', 'url(' + info.thumburl + ')' );
|
|
|
|
if ( info.width >= this.size && info.height >= this.size ) {
|
2013-06-25 00:41:01 +00:00
|
|
|
$front.addClass( 've-ui-mwMediaResultWidget-crop' );
|
2013-06-06 23:14:11 +00:00
|
|
|
$thumb.css( { 'width': '100%', 'height': '100%' } );
|
|
|
|
} else {
|
|
|
|
$thumb.css( {
|
|
|
|
'width': info.thumbwidth,
|
|
|
|
'height': info.thumbheight,
|
|
|
|
'left': '50%',
|
|
|
|
'top': '50%',
|
|
|
|
'margin-left': Math.round( -info.thumbwidth / 2 ),
|
|
|
|
'margin-top': Math.round( -info.thumbheight / 2 )
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $thumb;
|
|
|
|
};
|