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/>.
|
|
|
|
*/
|
|
|
|
|
2023-05-19 20:26:45 +00:00
|
|
|
const { HtmlUtils } = require( 'mmv.bootstrap' );
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-03-31 16:08:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper class for reuse logic.
|
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
class Utils {
|
|
|
|
constructor() {
|
|
|
|
/** @property {HtmlUtils} htmlUtils - */
|
|
|
|
this.htmlUtils = new HtmlUtils();
|
2014-03-31 16:08:02 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 08:11:42 +00:00
|
|
|
/**
|
|
|
|
* Creates select menu from given options.
|
|
|
|
*
|
|
|
|
* @param {string[]} options
|
|
|
|
* @param {string} def
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
|
|
|
createSelectMenu( options, def ) {
|
|
|
|
const $select = $( '<select>' ).addClass( 'cdx-select mw-mmv-flex-grow-1' );
|
|
|
|
options.forEach( ( size ) =>
|
|
|
|
$( '<option>' )
|
|
|
|
.attr( 'value', size )
|
|
|
|
.attr( 'data-name', size )
|
|
|
|
.text( this.getDimensionsMessageHtml( size ) )
|
|
|
|
.appendTo( $select )
|
|
|
|
);
|
|
|
|
$select.val( def );
|
|
|
|
return $select;
|
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Creates pulldown menu from given options.
|
|
|
|
*
|
|
|
|
* @param {string[]} options
|
|
|
|
* @param {string[]} classes
|
|
|
|
* @param {string} def
|
|
|
|
* @return {OO.ui.DropdownWidget}
|
|
|
|
*/
|
|
|
|
createPulldownMenu( options, classes, def ) {
|
|
|
|
const items = [];
|
|
|
|
const choices = {};
|
|
|
|
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
|
|
|
const dropdown = new OO.ui.DropdownWidget( {
|
|
|
|
classes: classes
|
|
|
|
} );
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
for ( let i = 0; i < options.length; i++ ) {
|
|
|
|
const option = options[ i ];
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
choices[ option ] = new OO.ui.MenuOptionWidget( {
|
|
|
|
data: {
|
|
|
|
name: option,
|
|
|
|
height: null,
|
|
|
|
width: null
|
|
|
|
},
|
|
|
|
label: this.getDimensionsMessageHtml( option, 0, 0 ),
|
|
|
|
autoFitLabel: false
|
|
|
|
} );
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
items.push( choices[ option ] );
|
|
|
|
}
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
dropdown.getMenu()
|
|
|
|
.addItems( items )
|
|
|
|
.chooseItem( choices[ def ] );
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
return dropdown;
|
|
|
|
}
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2023-05-20 12:38:59 +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
|
|
|
|
*
|
2023-06-27 20:10:10 +00:00
|
|
|
* @fires MultimediaViewer#mmv-request-thumbnail
|
2023-05-20 12:38:59 +00:00
|
|
|
* @return {jQuery.Promise.<string>}
|
|
|
|
*/
|
|
|
|
getThumbnailUrlPromise( width ) {
|
|
|
|
return $( document ).triggerHandler( 'mmv-request-thumbnail', width ) ||
|
|
|
|
$.Deferred().reject();
|
|
|
|
}
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Updates the menu options based on calculated sizes.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Object} sizes
|
|
|
|
* @param {OO.ui.MenuOptionWidget[]} options
|
|
|
|
*/
|
|
|
|
updateMenuOptions( sizes, options ) {
|
2023-10-23 19:28:57 +00:00
|
|
|
for ( let i = 0; i < options.length; i++ ) {
|
|
|
|
const option = options[ i ];
|
|
|
|
const data = option.getData();
|
2023-05-20 12:38:59 +00:00
|
|
|
|
|
|
|
if ( sizes[ data.name ] ) {
|
|
|
|
option.setDisabled( false );
|
|
|
|
|
|
|
|
// These values are later used when the item is selected
|
|
|
|
data.width = sizes[ data.name ].width;
|
|
|
|
data.height = sizes[ data.name ].height;
|
|
|
|
|
2023-10-23 19:28:57 +00:00
|
|
|
const $label = $( '<span>' ).html( this.getDimensionsMessageHtml( data.name, data.width, data.height ) );
|
2023-05-20 12:38:59 +00:00
|
|
|
|
|
|
|
option.setLabel( $label );
|
|
|
|
} else {
|
|
|
|
option.setDisabled( true );
|
|
|
|
}
|
2014-03-31 16:08:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-04 08:11:42 +00:00
|
|
|
/**
|
|
|
|
* Updates the select options based on calculated sizes.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Object} sizes
|
|
|
|
* @param {jQuery} options
|
|
|
|
*/
|
|
|
|
updateSelectOptions( sizes, options ) {
|
|
|
|
for ( let i = 0; i < options.length; i++ ) {
|
|
|
|
const $option = $( options[ i ] );
|
|
|
|
const name = $option.attr( 'data-name' );
|
|
|
|
if ( sizes[ name ] ) {
|
|
|
|
$option.prop( 'disabled', false );
|
|
|
|
|
|
|
|
// These values are later used when the item is selected
|
|
|
|
$option.attr( 'data-width', sizes[ name ].width );
|
|
|
|
$option.attr( 'data-height', sizes[ name ].height );
|
|
|
|
|
|
|
|
$option.text( this.getDimensionsMessageHtml( name, sizes[ name ].width, sizes[ name ].height ) );
|
|
|
|
} else {
|
|
|
|
$option.prop( 'disabled', true );
|
|
|
|
$option.text( this.getDimensionsMessageHtml( name, undefined, undefined ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @return {Object}
|
|
|
|
* @return {Object} return.small
|
|
|
|
* @return {Object} return.medium
|
|
|
|
* @return {Object} return.large
|
|
|
|
* @return {Object} return.xl
|
|
|
|
* @return {Object} return.original
|
|
|
|
*/
|
|
|
|
getPossibleImageSizesForHtml( width, height ) {
|
|
|
|
const buckets = {
|
2019-05-18 17:23:46 +00:00
|
|
|
small: { width: 640, height: 480 },
|
|
|
|
medium: { width: 1280, height: 720 }, // HD ready = 720p
|
|
|
|
large: { width: 1920, height: 1080 }, // Full HD = 1080p
|
|
|
|
xl: { width: 3840, height: 2160 } // 4K = 2160p
|
2023-05-20 12:38:59 +00:00
|
|
|
};
|
|
|
|
const sizes = {};
|
|
|
|
const bucketNames = Object.keys( buckets );
|
|
|
|
const widthToHeight = height / width;
|
|
|
|
const heightToWidth = width / height;
|
|
|
|
|
|
|
|
for ( let i = 0; i < bucketNames.length; i++ ) {
|
|
|
|
const bucketName = bucketNames[ i ];
|
|
|
|
const dimensions = buckets[ bucketName ];
|
|
|
|
const bucketWidth = dimensions.width;
|
|
|
|
const bucketHeight = dimensions.height;
|
|
|
|
|
|
|
|
if ( width > bucketWidth ) {
|
|
|
|
// Width fits in the current bucket
|
|
|
|
const currentGuess = bucketWidth;
|
|
|
|
|
|
|
|
if ( currentGuess * widthToHeight > bucketHeight ) {
|
|
|
|
// Constrain in height, resize width accordingly
|
|
|
|
sizes[ bucketName ] = {
|
|
|
|
width: Math.round( bucketHeight * heightToWidth ),
|
|
|
|
height: bucketHeight
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
sizes[ bucketName ] = {
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
sizes.original = { width: width, height: height };
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
return sizes;
|
|
|
|
}
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Generates an i18n message for a label, given a size label and image dimensions
|
|
|
|
*
|
|
|
|
* @param {string} sizeLabel
|
|
|
|
* @param {number} width
|
|
|
|
* @param {number} height
|
|
|
|
* @return {string} i18n label html
|
|
|
|
*/
|
|
|
|
getDimensionsMessageHtml( sizeLabel, width, height ) {
|
2024-05-04 08:11:42 +00:00
|
|
|
const dimensions = !width || !height ? '' : mw.message(
|
|
|
|
'multimediaviewer-embed-dimensions-separated',
|
|
|
|
mw.message(
|
|
|
|
'multimediaviewer-embed-dimensions',
|
|
|
|
width, height ).text()
|
|
|
|
).text();
|
|
|
|
|
|
|
|
// The following messages are used here:
|
2023-05-20 12:38:59 +00:00
|
|
|
return mw.message(
|
|
|
|
// The following messages are used here:
|
|
|
|
// * multimediaviewer-default-embed-dimensions
|
|
|
|
// * multimediaviewer-original-embed-dimensions
|
|
|
|
// * multimediaviewer-xl-embed-dimensions
|
|
|
|
// * multimediaviewer-large-embed-dimensions
|
|
|
|
// * multimediaviewer-medium-embed-dimensions
|
|
|
|
// * multimediaviewer-small-embed-dimensions
|
2024-02-13 00:44:51 +00:00
|
|
|
`multimediaviewer-${ sizeLabel }-embed-dimensions`,
|
2023-05-20 12:38:59 +00:00
|
|
|
dimensions
|
|
|
|
).text();
|
|
|
|
}
|
|
|
|
}
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2023-05-19 14:47:34 +00:00
|
|
|
module.exports = Utils;
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|