2014-03-31 16:08:02 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension MultimediaViewer.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
( function ( mw, $, oo ) {
|
|
|
|
var RUP;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper class for reuse logic.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @class mw.mmv.ui.Utils
|
2014-03-31 16:08:02 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function Utils() {
|
|
|
|
/** @property {mw.mmv.HtmlUtils} htmlUtils - */
|
|
|
|
this.htmlUtils = new mw.mmv.HtmlUtils();
|
|
|
|
}
|
|
|
|
|
|
|
|
RUP = Utils.prototype;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates pulldown menu from given options.
|
|
|
|
*
|
|
|
|
* @param {string[]} options
|
|
|
|
* @param {string[]} classes
|
|
|
|
* @param {string} def
|
2016-07-18 13:49:27 +00:00
|
|
|
* @return {OO.ui.DropdownWidget}
|
2014-03-31 16:08:02 +00:00
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
RUP.createPulldownMenu = function ( options, classes, def ) {
|
2014-10-30 21:54:52 +00:00
|
|
|
var dropdown, i, option,
|
2014-03-31 16:08:02 +00:00
|
|
|
items = [],
|
|
|
|
choices = {};
|
|
|
|
|
2014-10-30 21:54:52 +00:00
|
|
|
dropdown = new oo.ui.DropdownWidget( {
|
2014-03-31 16:08:02 +00:00
|
|
|
classes: classes
|
|
|
|
} );
|
|
|
|
|
|
|
|
for ( i = 0; i < options.length; i++ ) {
|
|
|
|
option = options[ i ];
|
|
|
|
|
2016-07-18 13:49:27 +00:00
|
|
|
choices[ option ] = new oo.ui.MenuOptionWidget( {
|
2014-11-22 00:01:29 +00:00
|
|
|
data: {
|
2014-03-31 16:08:02 +00:00
|
|
|
name: option,
|
|
|
|
height: null,
|
|
|
|
width: null
|
|
|
|
},
|
2014-11-22 00:01:29 +00:00
|
|
|
label: this.getDimensionsMessageHtml( option, 0, 0 ),
|
|
|
|
autoFitLabel: false
|
|
|
|
} );
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2016-07-18 13:49:27 +00:00
|
|
|
items.push( choices[ option ] );
|
2014-03-31 16:08:02 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 21:54:52 +00:00
|
|
|
dropdown.getMenu()
|
|
|
|
.addItems( items )
|
|
|
|
.chooseItem( choices[ def ] );
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2014-10-30 21:54:52 +00:00
|
|
|
return dropdown;
|
2014-03-31 16:08:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a promise for the large thumbnail URL. This is needed because thumbnail URLs cannot
|
|
|
|
* be reliably guessed, even if we know the full size of the image - most of the time replacing
|
|
|
|
* the size in another thumbnail URL works (as long as the new size is not larger than the full
|
|
|
|
* size), but if the file name is very long and with the larger size the URL length would
|
|
|
|
* exceed a certain threshold, a different schema is used instead.
|
|
|
|
*
|
|
|
|
* @param {number} width
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise.<string>}
|
|
|
|
*/
|
|
|
|
RUP.getThumbnailUrlPromise = function ( width ) {
|
|
|
|
return $( document ).triggerHandler( 'mmv-request-thumbnail', width ) ||
|
|
|
|
$.Deferred().reject();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the menu options based on calculated sizes.
|
|
|
|
*
|
2016-07-18 13:49:27 +00:00
|
|
|
* @private
|
2014-03-31 16:08:02 +00:00
|
|
|
* @param {Object} sizes
|
2014-11-17 22:18:23 +00:00
|
|
|
* @param {OO.ui.MenuOptionWidget[]} options
|
2014-03-31 16:08:02 +00:00
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
RUP.updateMenuOptions = function ( sizes, options ) {
|
2014-03-31 16:08:02 +00:00
|
|
|
var i, option, data, $label;
|
|
|
|
|
|
|
|
for ( i = 0; i < options.length; i++ ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
option = options[ i ];
|
2014-03-31 16:08:02 +00:00
|
|
|
data = option.getData();
|
|
|
|
|
2016-07-18 13:49:27 +00:00
|
|
|
if ( sizes[ data.name ] ) {
|
2014-03-31 16:08:02 +00:00
|
|
|
option.setDisabled( false );
|
|
|
|
|
2014-04-02 02:22:16 +00:00
|
|
|
// These values are later used when the item is selected
|
2016-07-18 13:49:27 +00:00
|
|
|
data.width = sizes[ data.name ].width;
|
|
|
|
data.height = sizes[ data.name ].height;
|
2014-03-31 16:08:02 +00:00
|
|
|
|
|
|
|
$label = $( '<span>' ).html( this.getDimensionsMessageHtml( data.name, data.width, data.height ) );
|
|
|
|
|
|
|
|
option.setLabel( $label );
|
2014-04-02 02:22:16 +00:00
|
|
|
} else {
|
2014-03-31 16:08:02 +00:00
|
|
|
option.setDisabled( true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates possible image sizes for html snippets. It returns up to
|
|
|
|
* three possible snippet frame sizes (small, medium, large) plus the
|
|
|
|
* original image size.
|
|
|
|
*
|
|
|
|
* @param {number} width
|
|
|
|
* @param {number} height
|
2016-07-18 13:49:27 +00:00
|
|
|
* @return {Object}
|
|
|
|
* @return {Object} return.small
|
|
|
|
* @return {Object} return.medium
|
|
|
|
* @return {Object} return.large
|
|
|
|
* @return {Object} return.original
|
2014-03-31 16:08:02 +00:00
|
|
|
*/
|
|
|
|
RUP.getPossibleImageSizesForHtml = function ( width, height ) {
|
|
|
|
var i, bucketName,
|
|
|
|
currentGuess, dimensions,
|
|
|
|
bucketWidth, bucketHeight,
|
|
|
|
buckets = {
|
|
|
|
small: { width: 220, height: 145 },
|
|
|
|
medium: { width: 640, height: 480 },
|
|
|
|
large: { width: 1200, height: 900 }
|
|
|
|
},
|
|
|
|
sizes = {},
|
|
|
|
bucketNames = Object.keys( buckets ),
|
|
|
|
widthToHeight = height / width,
|
|
|
|
heightToWidth = width / height;
|
|
|
|
|
|
|
|
for ( i = 0; i < bucketNames.length; i++ ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
bucketName = bucketNames[ i ];
|
|
|
|
dimensions = buckets[ bucketName ];
|
2014-03-31 16:08:02 +00:00
|
|
|
bucketWidth = dimensions.width;
|
|
|
|
bucketHeight = dimensions.height;
|
|
|
|
|
|
|
|
if ( width > bucketWidth ) {
|
|
|
|
// Width fits in the current bucket
|
|
|
|
currentGuess = bucketWidth;
|
|
|
|
|
|
|
|
if ( currentGuess * widthToHeight > bucketHeight ) {
|
|
|
|
// Constrain in height, resize width accordingly
|
2016-07-18 13:49:27 +00:00
|
|
|
sizes[ bucketName ] = {
|
2014-03-31 16:08:02 +00:00
|
|
|
width: Math.round( bucketHeight * heightToWidth ),
|
|
|
|
height: bucketHeight
|
|
|
|
};
|
|
|
|
} else {
|
2016-07-18 13:49:27 +00:00
|
|
|
sizes[ bucketName ] = {
|
2014-03-31 16:08:02 +00:00
|
|
|
width: currentGuess,
|
|
|
|
height: Math.round( currentGuess * widthToHeight )
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else if ( height > bucketHeight ) {
|
|
|
|
// Height fits in the current bucket, resize width accordingly
|
2016-07-18 13:49:27 +00:00
|
|
|
sizes[ bucketName ] = {
|
2014-03-31 16:08:02 +00:00
|
|
|
width: Math.round( bucketHeight * heightToWidth ),
|
|
|
|
height: bucketHeight
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sizes.original = { width: width, height: height };
|
|
|
|
|
|
|
|
return sizes;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an i18n message for a label, given a size label and image dimensions
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-03-31 16:08:02 +00:00
|
|
|
* @param {string} sizeLabel
|
|
|
|
* @param {number} width
|
|
|
|
* @param {number} height
|
2016-07-18 13:49:27 +00:00
|
|
|
* @return {string} i18n label html
|
2014-03-31 16:08:02 +00:00
|
|
|
*/
|
|
|
|
RUP.getDimensionsMessageHtml = function ( sizeLabel, width, height ) {
|
|
|
|
var dimensions = this.htmlUtils.jqueryToHtml( $( '<span>' )
|
|
|
|
.addClass( 'mw-mmv-embed-dimensions' )
|
|
|
|
.text(
|
|
|
|
mw.message(
|
|
|
|
'multimediaviewer-embed-dimensions-separated',
|
|
|
|
mw.message(
|
|
|
|
'multimediaviewer-embed-dimensions',
|
|
|
|
width, height ).text()
|
|
|
|
).text()
|
|
|
|
) );
|
|
|
|
|
|
|
|
return mw.message(
|
|
|
|
'multimediaviewer-' + sizeLabel + '-embed-dimensions',
|
|
|
|
dimensions
|
|
|
|
).text();
|
|
|
|
};
|
|
|
|
|
2014-09-16 20:33:05 +00:00
|
|
|
mw.mmv.ui.Utils = Utils;
|
2014-03-31 16:08:02 +00:00
|
|
|
}( mediaWiki, jQuery, OO ) );
|