2 * MediaWiki Widgets - MediaResultWidget class.
4 * @copyright 2011-2016 VisualEditor Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
10 * Creates an mw.widgets.MediaResultWidget object.
13 * @extends OO.ui.OptionWidget
16 * @param {Object} [config] Configuration options
17 * @cfg {number} [rowHeight] Height of the row this result is part of
18 * @cfg {number} [maxRowWidth] A limit for the width of the row this
19 * result is a part of.
20 * @cfg {number} [minWidth] Minimum width for the result
21 * @cfg {number} [maxWidth] Maximum width for the result
23 mw
.widgets
.MediaResultWidget
= function MwWidgetsMediaResultWidget( config
) {
24 // Configuration initialization
25 config
= config
|| {};
28 mw
.widgets
.MediaResultWidget
.super.call( this, config
);
31 this.setRowHeight( config
.rowHeight
|| 150 );
32 this.maxRowWidth
= config
.maxRowWidth
|| 500;
33 this.minWidth
= config
.minWidth
|| this.maxRowWidth
/ 5;
34 this.maxWidth
= config
.maxWidth
|| this.maxRowWidth
* 2 / 3;
36 this.imageDimensions
= {};
38 this.isAudio
= this.data
.mediatype
=== 'AUDIO';
40 // Store the thumbnail url
41 this.thumbUrl
= this.data
.thumburl
;
45 this.$thumb
= $( '<img>' )
46 .addClass( 'mw-widget-mediaResultWidget-thumbnail' )
48 load
: this.onThumbnailLoad
.bind( this ),
49 error
: this.onThumbnailError
.bind( this )
51 this.$overlay
= $( '<div>' )
52 .addClass( 'mw-widget-mediaResultWidget-overlay' );
54 this.calculateSizing( this.data
);
56 // Get wiki default thumbnail size
57 this.defaultThumbSize
= mw
.config
.get( 'wgVisualEditorConfig' )
58 .defaultUserOptions
.defaultthumbsize
;
61 this.setLabel( new mw
.Title( this.data
.title
).getNameText() );
62 this.$label
.addClass( 'mw-widget-mediaResultWidget-nameLabel' );
65 .addClass( 'mw-widget-mediaResultWidget ve-ui-texture-pending' )
66 .prepend( this.$thumb
, this.$overlay
);
71 OO
.inheritClass( mw
.widgets
.MediaResultWidget
, OO
.ui
.OptionWidget
);
75 // Copied from ve.dm.MWImageNode
76 mw
.widgets
.MediaResultWidget
.static.resizeToBoundingBox = function ( imageDimensions
, boundingBox
) {
77 var newDimensions
= OO
.copy( imageDimensions
),
79 boundingBox
.height
/ imageDimensions
.height
,
80 boundingBox
.width
/ imageDimensions
.width
86 width
: Math
.floor( newDimensions
.width
* scale
),
87 height
: Math
.floor( newDimensions
.height
* scale
)
95 mw
.widgets
.MediaResultWidget
.prototype.onThumbnailLoad = function () {
96 this.$thumb
.first().addClass( 've-ui-texture-transparency' );
98 .addClass( 'mw-widget-mediaResultWidget-done' )
99 .removeClass( 've-ui-texture-pending' );
103 mw
.widgets
.MediaResultWidget
.prototype.onThumbnailError = function () {
105 .css( 'background-image', '' )
106 .addClass( 've-ui-texture-alert' );
108 .addClass( 'mw-widget-mediaResultWidget-error' )
109 .removeClass( 've-ui-texture-pending' );
113 * Resize the thumbnail and wrapper according to row height and bounding boxes, if given.
115 * @param {Object} originalDimensions Original image dimensions with width and height values
116 * @param {Object} [boundingBox] Specific bounding box, if supplied
118 mw
.widgets
.MediaResultWidget
.prototype.calculateSizing = function ( originalDimensions
, boundingBox
) {
120 imageDimensions
= {};
122 boundingBox
= boundingBox
|| {};
124 if ( this.isAudio
) {
125 // HACK: We are getting the wrong information from the
126 // API about audio files. Set their thumbnail to square 120px
132 // Get the image within the bounding box
133 imageDimensions
= this.constructor.static.resizeToBoundingBox(
134 // Image original dimensions
136 width
: originalDimensions
.width
|| originalDimensions
.thumbwidth
,
137 height
: originalDimensions
.height
|| originalDimensions
.thumbwidth
141 width
: boundingBox
.width
|| this.getImageMaxWidth(),
142 height
: boundingBox
.height
|| this.getRowHeight()
146 this.imageDimensions
= imageDimensions
;
147 // Set the thumbnail size
148 this.$thumb
.css( this.imageDimensions
);
151 wrapperPadding
= this.calculateWrapperPadding( this.imageDimensions
);
152 this.$element
.css( wrapperPadding
);
156 * Replace the empty .src attribute of the image with the
159 mw
.widgets
.MediaResultWidget
.prototype.lazyLoad = function () {
160 if ( !this.hasSrc() ) {
161 this.src
= this.thumbUrl
;
162 this.$thumb
.attr( 'src', this.thumbUrl
);
167 * Retrieve the store dimensions object
169 * @return {Object} Thumb dimensions
171 mw
.widgets
.MediaResultWidget
.prototype.getDimensions = function () {
172 return this.dimensions
;
176 * Resize thumbnail and element according to the resize factor
178 * @param {number} resizeFactor The resizing factor for the image
180 mw
.widgets
.MediaResultWidget
.prototype.resizeThumb = function ( resizeFactor
) {
182 imageOriginalWidth
= this.imageDimensions
.width
,
183 wrapperWidth
= this.$element
.width();
184 // Set the new row height
185 this.setRowHeight( Math
.ceil( this.getRowHeight() * resizeFactor
) );
188 width
: Math
.ceil( this.imageDimensions
.width
* resizeFactor
),
189 height
: this.getRowHeight()
192 this.calculateSizing( this.data
, boundingBox
);
194 // We need to adjust the wrapper this time to fit the "perfect"
195 // dimensions, regardless of how small the image is
196 if ( imageOriginalWidth
< wrapperWidth
) {
197 boundingBox
.width
= wrapperWidth
* resizeFactor
;
199 this.$element
.css( this.calculateWrapperPadding( boundingBox
) );
203 * Adjust the wrapper padding for small images
205 * @param {Object} thumbDimensions Thumbnail dimensions
206 * @return {Object} Css styling for the wrapper
208 mw
.widgets
.MediaResultWidget
.prototype.calculateWrapperPadding = function ( thumbDimensions
) {
210 height
: this.rowHeight
,
211 width
: thumbDimensions
.width
,
212 lineHeight
: this.getRowHeight() + 'px'
215 // Check if the image is too thin so we can make a bit of space around it
216 if ( thumbDimensions
.width
< this.minWidth
) {
217 css
.width
= this.minWidth
;
224 * Set the row height for all size calculations
226 * @return {number} rowHeight Row height
228 mw
.widgets
.MediaResultWidget
.prototype.getRowHeight = function () {
229 return this.rowHeight
;
233 * Set the row height for all size calculations
235 * @param {number} rowHeight Row height
237 mw
.widgets
.MediaResultWidget
.prototype.setRowHeight = function ( rowHeight
) {
238 this.rowHeight
= rowHeight
;
241 mw
.widgets
.MediaResultWidget
.prototype.setImageMaxWidth = function ( width
) {
242 this.maxWidth
= width
;
244 mw
.widgets
.MediaResultWidget
.prototype.getImageMaxWidth = function () {
245 return this.maxWidth
;
249 * Set the row this result is in.
251 * @param {number} row Row number
253 mw
.widgets
.MediaResultWidget
.prototype.setRow = function ( row
) {
258 * Get the row this result is in.
260 * @return {number} row Row number
262 mw
.widgets
.MediaResultWidget
.prototype.getRow = function () {
267 * Check if the image has a src attribute already
269 * @return {boolean} Thumbnail has its source attribute set
271 mw
.widgets
.MediaResultWidget
.prototype.hasSrc = function () {
274 }( jQuery
, mediaWiki
) );