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
|
|
|
*
|
2015-01-08 23:54:03 +00:00
|
|
|
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
|
2013-06-06 23:14:11 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-06-25 00:41:01 +00:00
|
|
|
* Creates an ve.ui.MWMediaResultWidget object.
|
2013-06-06 23:14:11 +00:00
|
|
|
*
|
|
|
|
* @class
|
2013-10-09 20:09:59 +00:00
|
|
|
* @extends OO.ui.OptionWidget
|
2013-06-06 23:14:11 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-06-06 23:14:11 +00:00
|
|
|
* @cfg {number} [size] Media thumbnail size
|
|
|
|
*/
|
2014-11-22 01:40:00 +00:00
|
|
|
ve.ui.MWMediaResultWidget = function VeUiMWMediaResultWidget( config ) {
|
2014-11-21 13:00:50 +00:00
|
|
|
// Configuration initialization
|
2013-06-06 23:14:11 +00:00
|
|
|
config = config || {};
|
|
|
|
|
|
|
|
// Parent constructor
|
2014-11-22 01:40:00 +00:00
|
|
|
OO.ui.OptionWidget.call( this, config );
|
2013-06-06 23:14:11 +00:00
|
|
|
|
|
|
|
// Properties
|
2014-09-18 19:49:22 +00:00
|
|
|
this.initialSize = config.size || 150;
|
|
|
|
this.maxSize = config.maxSize || this.initialSize * 2;
|
|
|
|
this.expanded = false;
|
|
|
|
this.dimensions = {};
|
2013-06-06 23:14:11 +00:00
|
|
|
this.$thumb = this.buildThumbnail();
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$overlay = this.$( '<div>' );
|
2014-09-18 19:49:22 +00:00
|
|
|
this.row = null;
|
2015-01-15 18:50:30 +00:00
|
|
|
// Store the thumbnail url
|
|
|
|
this.thumbUrl = ve.getProp( this.data.imageinfo, 0, 'thumburl' );
|
2014-09-18 19:49:22 +00:00
|
|
|
|
|
|
|
// Get wiki default thumbnail size
|
|
|
|
this.defaultThumbSize = mw.config.get( 'wgVisualEditorConfig' )
|
|
|
|
.defaultUserOptions.defaultthumbsize;
|
|
|
|
|
2013-06-06 23:14:11 +00:00
|
|
|
// Initialization
|
|
|
|
this.setLabel( new mw.Title( this.data.title ).getNameText() );
|
2014-09-18 19:49:22 +00:00
|
|
|
this.$label.addClass( 've-ui-mwMediaResultWidget-nameLabel' );
|
2013-06-25 00:41:01 +00:00
|
|
|
this.$overlay.addClass( 've-ui-mwMediaResultWidget-overlay' );
|
2014-09-18 19:49:22 +00:00
|
|
|
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element
|
2013-06-25 00:41:01 +00:00
|
|
|
.addClass( 've-ui-mwMediaResultWidget ve-ui-texture-pending' )
|
2013-06-06 23:14:11 +00:00
|
|
|
.prepend( this.$thumb, this.$overlay );
|
2014-09-18 19:49:22 +00:00
|
|
|
|
|
|
|
// Adjust wrapper padding
|
|
|
|
this.$element.css( $.extend( this.dimensions, this.calculateWrapperPadding( this.dimensions, this.initialSize ) ) );
|
|
|
|
|
|
|
|
// Select button
|
|
|
|
this.selectButton = new OO.ui.ButtonWidget( {
|
|
|
|
$: this.$,
|
|
|
|
label: ve.msg( 'visualeditor-dialog-media-searchselect' ),
|
|
|
|
icon: 'check'
|
|
|
|
} );
|
2015-01-29 23:09:47 +00:00
|
|
|
this.selectButton.toggle( false );
|
2014-09-18 19:49:22 +00:00
|
|
|
this.$element.prepend( this.selectButton.$element );
|
2013-06-06 23:14:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.inheritClass( ve.ui.MWMediaResultWidget, OO.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' );
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element
|
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' );
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element
|
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 () {
|
2014-09-18 19:49:22 +00:00
|
|
|
var imageDimensions,
|
|
|
|
info = this.data.imageinfo[0],
|
|
|
|
$thumb = this.$( '<img>' );
|
2013-06-06 23:14:11 +00:00
|
|
|
|
|
|
|
// Preload image
|
2014-09-18 19:49:22 +00:00
|
|
|
$thumb
|
2015-01-15 18:50:30 +00:00
|
|
|
.addClass( 've-ui-mwMediaResultWidget-thumbnail' )
|
|
|
|
.on( {
|
|
|
|
load: this.onThumbnailLoad.bind( this ),
|
|
|
|
error: this.onThumbnailError.bind( this )
|
|
|
|
} );
|
2013-06-06 23:14:11 +00:00
|
|
|
|
2014-09-18 19:49:22 +00:00
|
|
|
if ( info.mediatype === 'AUDIO' ) {
|
|
|
|
// HACK: We are getting the wrong information from the
|
|
|
|
// API about audio files. Set their thumbnail to square
|
|
|
|
imageDimensions = {
|
|
|
|
width: this.initialSize,
|
|
|
|
height: this.initialSize
|
|
|
|
};
|
2013-06-06 23:14:11 +00:00
|
|
|
} else {
|
2014-09-18 19:49:22 +00:00
|
|
|
if ( info.height < this.initialSize && info.width < this.maxSize ) {
|
|
|
|
// Define dimensions with original size
|
|
|
|
imageDimensions = {
|
|
|
|
width: info.width,
|
|
|
|
height: info.height
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// Resize dimensions to be a fixed height
|
|
|
|
imageDimensions = ve.dm.Scalable.static.getDimensionsFromValue(
|
|
|
|
{ height: this.initialSize },
|
|
|
|
info.thumbwidth / info.thumbheight
|
|
|
|
);
|
|
|
|
}
|
2013-06-06 23:14:11 +00:00
|
|
|
}
|
2014-09-18 19:49:22 +00:00
|
|
|
// Resize the wrapper
|
|
|
|
this.dimensions = this.calculateThumbDimensions( imageDimensions );
|
|
|
|
|
|
|
|
// Resize the image
|
|
|
|
$thumb.css( this.dimensions );
|
2013-06-06 23:14:11 +00:00
|
|
|
|
|
|
|
return $thumb;
|
|
|
|
};
|
2014-09-18 19:49:22 +00:00
|
|
|
|
2015-01-15 18:50:30 +00:00
|
|
|
/**
|
|
|
|
* Replace the empty .src attribute of the image with the
|
|
|
|
* actual src.
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaResultWidget.prototype.lazyLoad = function () {
|
|
|
|
this.$thumb.attr( 'src', this.thumbUrl );
|
|
|
|
};
|
|
|
|
|
2014-09-18 19:49:22 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the store dimensions object
|
|
|
|
* @return {Object} Thumb dimensions
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaResultWidget.prototype.getDimensions = function () {
|
|
|
|
return this.dimensions;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize thumbnail and element according to the resize factor
|
|
|
|
* @param {number} resizeFactor New resizing factor, multiplying the
|
|
|
|
* current dimensions of the thumbnail
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaResultWidget.prototype.resizeThumb = function ( resizeFactor ) {
|
|
|
|
var wrapperCss, imageDimensions,
|
|
|
|
currWidth = this.$thumb.width(),
|
|
|
|
currHeight = this.$thumb.height();
|
|
|
|
|
|
|
|
imageDimensions = {
|
|
|
|
width: currWidth * resizeFactor,
|
|
|
|
height: currHeight * resizeFactor
|
|
|
|
};
|
|
|
|
// Resize thumb wrapper
|
|
|
|
this.$thumb.css( imageDimensions );
|
|
|
|
// Adjust wrapper padding - this is done so the image is placed in the center
|
|
|
|
wrapperCss = $.extend( imageDimensions, this.calculateWrapperPadding( imageDimensions, imageDimensions.height ) );
|
|
|
|
this.$element.css( wrapperCss );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate thumbnail dimensions
|
|
|
|
* @param {Object} imageDimensions Image dimensions
|
|
|
|
* @return {Object} Thumbnail dimensions
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaResultWidget.prototype.calculateThumbDimensions = function ( imageDimensions ) {
|
|
|
|
var dimensions,
|
|
|
|
maxWidth = this.maxSize,
|
|
|
|
ratio = imageDimensions.width / imageDimensions.height;
|
|
|
|
// Rules of resizing:
|
|
|
|
// (1) Images must have height = this.initialSize
|
2015-01-15 18:50:30 +00:00
|
|
|
// (2) If after resize image width is larger than maxWidth
|
|
|
|
// the image is scaled down to width = 3*this.width
|
2014-09-18 19:49:22 +00:00
|
|
|
// (3) Smaller images do not scale up
|
|
|
|
// * If image height < this.initialSize, add padding and center
|
|
|
|
// the image vertically
|
|
|
|
// * If image width < this.initialSize, add a fixed padding to both
|
|
|
|
// sides.
|
2015-01-15 18:50:30 +00:00
|
|
|
// This is done in 'calculateWrapperPadding'
|
2014-09-18 19:49:22 +00:00
|
|
|
// First scale all images based on height = this.initialSize
|
|
|
|
dimensions = ve.dm.MWImageNode.static.resizeToBoundingBox(
|
|
|
|
// Image thumb size
|
|
|
|
imageDimensions,
|
|
|
|
// Bounding box
|
|
|
|
{
|
2015-01-15 18:50:30 +00:00
|
|
|
width: maxWidth,
|
2014-09-18 19:49:22 +00:00
|
|
|
height: this.initialSize
|
2015-01-15 18:50:30 +00:00
|
|
|
}
|
2014-09-18 19:49:22 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Check if image width is larger than maxWidth
|
|
|
|
if ( dimensions.width > maxWidth ) {
|
|
|
|
// Resize again to fit maxWidth
|
|
|
|
dimensions = ve.dm.Scalable.static.getDimensionsFromValue( { width: maxWidth }, ratio );
|
|
|
|
}
|
|
|
|
|
|
|
|
return dimensions;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adjust the wrapper padding for small images
|
|
|
|
* @param {Object} thumbDimensions Thumbnail dimensions
|
|
|
|
* @return {Object} Left and right padding
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaResultWidget.prototype.calculateWrapperPadding = function ( thumbDimensions, rowHeight ) {
|
|
|
|
var padding,
|
|
|
|
minWidthRatioForPadding = 2 / 3 * rowHeight,
|
|
|
|
paddingWidth = {},
|
|
|
|
paddingHeight = {},
|
|
|
|
minWidth = 0.5 * rowHeight;
|
|
|
|
|
|
|
|
// Check if the image fits the row height
|
|
|
|
if ( thumbDimensions.height < rowHeight ) {
|
|
|
|
// Set up top/bottom padding
|
|
|
|
paddingHeight = {
|
|
|
|
'padding-top': ( rowHeight - thumbDimensions.height ) / 2,
|
|
|
|
'padding-bottom': ( rowHeight - thumbDimensions.height ) / 2
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the image is too thin so we can make a bit of space around it
|
|
|
|
if ( thumbDimensions.width < minWidth ) {
|
|
|
|
// Make the padding so that the total width is a 1/3 of the line height
|
|
|
|
padding = rowHeight - minWidthRatioForPadding - thumbDimensions.width;
|
|
|
|
paddingWidth = {
|
|
|
|
'padding-left': padding / 2,
|
|
|
|
'padding-right': padding / 2
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return $.extend( {}, paddingWidth, paddingHeight );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the row this result is in.
|
|
|
|
* @param {number} row Row number
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaResultWidget.prototype.setRow = function ( row ) {
|
|
|
|
this.row = row;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the row this result is in.
|
|
|
|
* @return {number} row Row number
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaResultWidget.prototype.getRow = function () {
|
|
|
|
return this.row;
|
|
|
|
};
|
2015-01-15 18:50:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the image has a src attribute already
|
|
|
|
* @returns {boolean} Thumbnail has its source attribute set
|
|
|
|
*/
|
|
|
|
ve.ui.MWMediaResultWidget.prototype.hasSrc = function () {
|
|
|
|
return !!this.$thumb.attr( 'src' );
|
|
|
|
};
|