2014-03-19 02:00:26 +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/>.
|
|
|
|
*/
|
|
|
|
|
2024-05-22 19:49:26 +00:00
|
|
|
const EmbedFileFormatter = require( './mmv.EmbedFileFormatter.js' );
|
|
|
|
const Utils = require( './mmv.ui.utils.js' );
|
2024-05-04 14:00:17 +00:00
|
|
|
const { UiElement } = require( 'mmv' );
|
2023-05-19 14:56:40 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* UI component that provides the user html/wikitext snippets needed to share
|
|
|
|
* and/or embed a media asset.
|
|
|
|
*/
|
|
|
|
class Embed extends UiElement {
|
2014-03-19 02:00:26 +00:00
|
|
|
/**
|
2024-05-21 20:12:08 +00:00
|
|
|
* @param {jQuery} $container
|
2014-03-19 02:00:26 +00:00
|
|
|
*/
|
2024-05-21 20:12:08 +00:00
|
|
|
constructor( $container ) {
|
|
|
|
super( $container );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
|
|
|
/**
|
2024-05-21 20:12:08 +00:00
|
|
|
* Formatter converting image data into formats needed for output
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2024-05-21 20:12:08 +00:00
|
|
|
* @property {EmbedFileFormatter}
|
2014-03-19 02:00:26 +00:00
|
|
|
*/
|
2024-05-21 20:12:08 +00:00
|
|
|
this.formatter = new EmbedFileFormatter();
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2024-05-28 18:04:50 +00:00
|
|
|
Utils.createHeader( mw.msg( 'multimediaviewer-embed-tab' ) )
|
2024-05-21 20:12:08 +00:00
|
|
|
.appendTo( $container );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
const $body = $( '<div>' )
|
|
|
|
.addClass( 'cdx-dialog__body mw-mmv-pt-0 mw-mmv-pb-150' )
|
|
|
|
.appendTo( $container );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
this.createSnippetSelectionButtons( $body );
|
|
|
|
this.createSizePulldownMenus( $body );
|
|
|
|
this.createSnippetTextAreas( $body );
|
|
|
|
}
|
2023-05-20 12:38:59 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Creates text areas for html and wikitext snippets.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $container
|
|
|
|
*/
|
|
|
|
createSnippetTextAreas( $container ) {
|
|
|
|
[ this.$embedTextHtml, this.$embedTextHtmlDiv ] = Utils.createInputWithCopy(
|
2024-05-28 18:04:50 +00:00
|
|
|
mw.msg( 'multimediaviewer-reuse-copy-embed' ),
|
|
|
|
mw.msg( 'multimediaviewer-reuse-loading-placeholder' )
|
2024-05-21 20:12:08 +00:00
|
|
|
);
|
2024-05-28 18:04:50 +00:00
|
|
|
this.$embedTextHtml.attr( 'title', mw.msg( 'multimediaviewer-embed-explanation' ) );
|
2024-05-21 20:12:08 +00:00
|
|
|
|
|
|
|
[ this.$embedTextWikitext, this.$embedTextWikitextDiv ] = Utils.createInputWithCopy(
|
2024-05-28 18:04:50 +00:00
|
|
|
mw.msg( 'multimediaviewer-reuse-copy-embed' ),
|
|
|
|
mw.msg( 'multimediaviewer-reuse-loading-placeholder' )
|
2024-05-21 20:12:08 +00:00
|
|
|
);
|
2024-05-28 18:04:50 +00:00
|
|
|
this.$embedTextWikitext.attr( 'title', mw.msg( 'multimediaviewer-embed-explanation' ) );
|
2024-05-21 20:12:08 +00:00
|
|
|
|
|
|
|
$container.append(
|
|
|
|
this.$embedTextHtmlDiv,
|
|
|
|
this.$embedTextWikitextDiv
|
|
|
|
);
|
|
|
|
}
|
2014-04-02 02:22:16 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Creates snippet selection buttons.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $container
|
|
|
|
*/
|
|
|
|
createSnippetSelectionButtons( $container ) {
|
|
|
|
const $embedSwitch = $( '<div>' ).addClass( 'cdx-tabs mw-mmv-mb-75' ).appendTo( $container );
|
|
|
|
const $embedSwitchHeader = $( '<div>' ).addClass( 'cdx-tabs__header' ).appendTo( $embedSwitch );
|
|
|
|
this.$embedSwitchList = $( '<div>' ).addClass( 'cdx-tabs__list' ).attr( 'role', 'tablist' ).appendTo( $embedSwitchHeader );
|
|
|
|
[ 'wikitext', 'html' ].forEach( ( name ) => $( '<button>' )
|
|
|
|
.addClass( 'cdx-tabs__list__item' )
|
|
|
|
.attr( 'role', 'tab' )
|
|
|
|
.data( 'name', name )
|
2024-05-28 18:04:50 +00:00
|
|
|
.text( mw.msg( name === 'wikitext' ? 'multimediaviewer-embed-wt' : 'multimediaviewer-embed-html' ) )
|
2024-05-21 20:12:08 +00:00
|
|
|
.on( 'click', () => this.handleTypeSwitch( name ) )
|
|
|
|
.appendTo( this.$embedSwitchList )
|
|
|
|
);
|
|
|
|
}
|
2023-05-20 12:38:59 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Creates pulldown menus to select file sizes.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $container
|
|
|
|
*/
|
|
|
|
createSizePulldownMenus( $container ) {
|
|
|
|
// Wikitext sizes pulldown menu
|
|
|
|
this.$embedSizeSwitchWikitext = Utils.createSelectMenu(
|
|
|
|
[ 'default', 'small', 'medium', 'large' ],
|
|
|
|
'default'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Html sizes pulldown menu
|
|
|
|
this.$embedSizeSwitchHtml = Utils.createSelectMenu(
|
|
|
|
[ 'small', 'medium', 'large', 'original' ],
|
|
|
|
'original'
|
|
|
|
);
|
|
|
|
|
|
|
|
$( '<div>' ).addClass( 'mw-mmv-flex mw-mmv-mb-75' ).append(
|
|
|
|
this.$embedSizeSwitchHtml,
|
|
|
|
this.$embedSizeSwitchWikitext
|
|
|
|
).appendTo( $container );
|
|
|
|
}
|
2014-04-02 02:22:16 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Registers listeners.
|
|
|
|
*/
|
|
|
|
attach() {
|
|
|
|
// Logged-out defaults to 'html', logged-in to 'wikitext'
|
|
|
|
this.handleTypeSwitch( mw.user.isAnon() ? 'html' : 'wikitext' );
|
2014-04-02 02:22:16 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
// Register handlers for switching between file sizes
|
|
|
|
this.$embedSizeSwitchHtml.on( 'change', () => this.handleSizeSwitch() );
|
|
|
|
this.$embedSizeSwitchWikitext.on( 'change', () => this.handleSizeSwitch() );
|
|
|
|
}
|
2014-03-19 03:05:36 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Clears listeners.
|
|
|
|
*/
|
|
|
|
unattach() {
|
|
|
|
super.unattach();
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
this.$embedSizeSwitchHtml.off( 'change' );
|
|
|
|
this.$embedSizeSwitchWikitext.off( 'change' );
|
|
|
|
}
|
2019-05-23 14:27:43 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Handles size menu change events.
|
|
|
|
*/
|
|
|
|
handleSizeSwitch() {
|
|
|
|
// eslint-disable-next-line no-jquery/no-sizzle
|
|
|
|
const $html = this.$embedSizeSwitchHtml.find( ':selected' );
|
|
|
|
if ( $html.length ) {
|
|
|
|
this.updateEmbedHtml( {}, $html.data( 'width' ), $html.data( 'height' ) );
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line no-jquery/no-sizzle
|
|
|
|
const $wikitext = this.$embedSizeSwitchWikitext.find( ':selected' );
|
|
|
|
if ( $wikitext.length ) {
|
|
|
|
this.updateEmbedWikitext( $wikitext.data( 'width' ) );
|
2023-05-20 12:38:59 +00:00
|
|
|
}
|
2024-05-21 20:12:08 +00:00
|
|
|
}
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Handles snippet type switch.
|
|
|
|
*
|
|
|
|
* @param {string} value 'html' or 'wikitext'
|
|
|
|
*/
|
|
|
|
handleTypeSwitch( value ) {
|
|
|
|
this.$embedSwitchList.children().each( ( _i, element ) => {
|
|
|
|
const $element = $( element );
|
|
|
|
$element.attr( 'aria-selected', $element.data( 'name' ) === value );
|
|
|
|
} );
|
2014-03-19 03:05:36 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
this.$embedTextHtmlDiv.toggle( value === 'html' );
|
|
|
|
this.$embedSizeSwitchHtml.toggle( value === 'html' );
|
2014-04-02 02:22:16 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
this.$embedTextWikitextDiv.toggle( value === 'wikitext' );
|
|
|
|
this.$embedSizeSwitchWikitext.toggle( value === 'wikitext' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset current menu selection to default item.
|
|
|
|
*/
|
|
|
|
resetCurrentSizeMenuToDefault() {
|
|
|
|
this.$embedSizeSwitchWikitext.val( 'default' );
|
|
|
|
this.$embedSizeSwitchHtml.val( 'original' );
|
|
|
|
this.handleSizeSwitch();
|
|
|
|
}
|
2014-04-02 02:22:16 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the HTML embed text.
|
|
|
|
*
|
|
|
|
* Assumes that the set() method has already been called to update this.embedFileInfo
|
|
|
|
*
|
|
|
|
* @param {Thumbnail} thumbnail (can be just an empty object)
|
|
|
|
* @param {number} width New width to set
|
|
|
|
* @param {number} height New height to set
|
|
|
|
*/
|
|
|
|
updateEmbedHtml( thumbnail, width, height ) {
|
|
|
|
if ( !this.embedFileInfo ) {
|
|
|
|
return;
|
2014-03-19 03:05:36 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
let src = thumbnail.url || this.embedFileInfo.imageInfo.url;
|
2014-03-19 03:05:36 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
// If the image dimension requested are "large", use the current image url
|
|
|
|
if ( width > Embed.LARGE_IMAGE_WIDTH_THRESHOLD || height > Embed.LARGE_IMAGE_HEIGHT_THRESHOLD ) {
|
|
|
|
src = this.embedFileInfo.imageInfo.url;
|
2014-04-02 02:22:16 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
this.$embedTextHtml.val(
|
|
|
|
this.formatter.getThumbnailHtml( this.embedFileInfo, src, width, height )
|
|
|
|
);
|
|
|
|
}
|
2014-03-19 03:05:36 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Updates the wikitext embed text with a new value for width.
|
|
|
|
*
|
|
|
|
* Assumes that the set method has already been called.
|
|
|
|
*
|
|
|
|
* @param {number} width
|
|
|
|
*/
|
|
|
|
updateEmbedWikitext( width ) {
|
|
|
|
if ( !this.embedFileInfo ) {
|
|
|
|
return;
|
2014-03-19 03:05:36 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
this.$embedTextWikitext.val(
|
|
|
|
this.formatter.getThumbnailWikitextFromEmbedFileInfo( this.embedFileInfo, width )
|
|
|
|
);
|
|
|
|
}
|
2023-05-20 12:38:59 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Gets size options for html and wikitext snippets.
|
|
|
|
*
|
|
|
|
* @param {number} width
|
|
|
|
* @param {number} height
|
|
|
|
* @return {Object}
|
|
|
|
* @return {Object} return.html Collection of possible image sizes for html snippets
|
|
|
|
* @return {Object} return.wikitext Collection of possible image sizes for wikitext snippets
|
|
|
|
*/
|
|
|
|
getSizeOptions( width, height ) {
|
|
|
|
const sizes = {};
|
2023-05-20 12:38:59 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
sizes.html = Utils.getPossibleImageSizesForHtml( width, height );
|
|
|
|
sizes.wikitext = this.getPossibleImageSizesForWikitext( width, height );
|
2014-03-19 03:05:36 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
return sizes;
|
|
|
|
}
|
2014-03-19 03:05:36 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the data on the element.
|
|
|
|
*
|
2024-05-29 08:54:16 +00:00
|
|
|
* @param {ImageModel} imageInfo
|
2024-05-21 20:12:08 +00:00
|
|
|
* @param {string} [caption]
|
|
|
|
* @param {string} [alt]
|
|
|
|
*/
|
2024-05-29 08:54:16 +00:00
|
|
|
set( imageInfo, caption, alt ) {
|
|
|
|
const sizes = this.getSizeOptions( imageInfo.width, imageInfo.height );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2024-05-29 08:54:16 +00:00
|
|
|
this.embedFileInfo = { imageInfo, caption, alt };
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
Utils.updateSelectOptions( sizes.html, this.$embedSizeSwitchHtml.children() );
|
|
|
|
Utils.updateSelectOptions( sizes.wikitext, this.$embedSizeSwitchWikitext.children() );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
// Reset defaults
|
|
|
|
this.resetCurrentSizeMenuToDefault();
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
Utils.getThumbnailUrlPromise( this.LARGE_IMAGE_WIDTH_THRESHOLD )
|
|
|
|
.done( ( thumbnail ) => {
|
|
|
|
this.updateEmbedHtml( thumbnail );
|
|
|
|
} );
|
2023-05-20 12:38:59 +00:00
|
|
|
}
|
2014-03-19 02:00:26 +00:00
|
|
|
|
|
|
|
/**
|
2024-05-21 20:12:08 +00:00
|
|
|
* @inheritdoc
|
2014-03-19 02:00:26 +00:00
|
|
|
*/
|
2024-05-21 20:12:08 +00:00
|
|
|
empty() {
|
|
|
|
this.$embedTextHtml.val( '' );
|
|
|
|
this.$embedTextWikitext.val( '' );
|
|
|
|
|
|
|
|
this.$embedSizeSwitchHtml.toggle( false );
|
|
|
|
this.$embedSizeSwitchWikitext.toggle( false );
|
|
|
|
}
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2014-03-31 16:08:02 +00:00
|
|
|
/**
|
2024-05-21 20:12:08 +00:00
|
|
|
* Calculates possible image sizes for wikitext snippets. It returns up to
|
|
|
|
* three possible snippet frame sizes (small, medium, large).
|
|
|
|
*
|
|
|
|
* @param {number} width
|
|
|
|
* @param {number} height
|
|
|
|
* @return {Object}
|
|
|
|
* @return {Object} return.small
|
|
|
|
* @return {Object} return.medium
|
|
|
|
* @return {Object} return.large
|
2014-03-31 16:08:02 +00:00
|
|
|
*/
|
2024-05-21 20:12:08 +00:00
|
|
|
getPossibleImageSizesForWikitext( width, height ) {
|
|
|
|
const buckets = {
|
|
|
|
small: 300,
|
|
|
|
medium: 400,
|
|
|
|
large: 500
|
|
|
|
};
|
|
|
|
const sizes = {};
|
|
|
|
const widthToHeight = height / width;
|
|
|
|
|
|
|
|
for ( const bucketName in buckets ) {
|
|
|
|
const bucketWidth = buckets[ bucketName ];
|
|
|
|
|
|
|
|
if ( width > bucketWidth ) {
|
|
|
|
sizes[ bucketName ] = {
|
|
|
|
width: bucketWidth,
|
|
|
|
height: Math.round( bucketWidth * widthToHeight )
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sizes.default = { width: null, height: null };
|
|
|
|
|
|
|
|
return sizes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} Width threshold at which an image is to be considered "large"
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
Embed.LARGE_IMAGE_WIDTH_THRESHOLD = 1200;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} Height threshold at which an image is to be considered "large"
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
Embed.LARGE_IMAGE_HEIGHT_THRESHOLD = 900;
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2024-05-21 20:12:08 +00:00
|
|
|
module.exports = Embed;
|