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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
( function ( mw, $, oo ) {
|
|
|
|
// Shortcut for prototype later
|
|
|
|
var EP;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UI component that provides the user html/wikitext snippets needed to share
|
|
|
|
* and/or embed a media asset.
|
|
|
|
*
|
|
|
|
* @class mw.mmv.ui.reuse.Embed
|
|
|
|
* @extends mw.mmv.ui.reuse.Tab
|
|
|
|
* @constructor
|
|
|
|
* @param {jQuery} $container
|
|
|
|
*/
|
|
|
|
function Embed( $container ) {
|
2014-09-16 20:33:05 +00:00
|
|
|
mw.mmv.ui.reuse.Tab.call( this, $container );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Formatter converting image data into formats needed for output
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-03-19 02:00:26 +00:00
|
|
|
* @property {mw.mmv.EmbedFileFormatter}
|
|
|
|
*/
|
|
|
|
this.formatter = new mw.mmv.EmbedFileFormatter();
|
|
|
|
|
2014-09-16 20:33:05 +00:00
|
|
|
/** @property {mw.mmv.ui.Utils} utils - */
|
|
|
|
this.utils = new mw.mmv.ui.Utils();
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2014-04-02 02:22:16 +00:00
|
|
|
/**
|
|
|
|
* Indicates whether or not the default option has been reset for both size menus.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-04-02 02:22:16 +00:00
|
|
|
* @property {boolean}
|
|
|
|
*/
|
|
|
|
this.isSizeMenuDefaultReset = false;
|
|
|
|
|
2014-03-31 21:33:12 +00:00
|
|
|
this.$pane.addClass( 'mw-mmv-embed-pane' );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
|
|
|
this.$pane.appendTo( this.$container );
|
|
|
|
|
|
|
|
this.createSnippetTextAreas( this.$pane );
|
2014-03-26 13:34:44 +00:00
|
|
|
|
|
|
|
this.$explanation = $( '<div>' )
|
2014-03-31 21:33:12 +00:00
|
|
|
.addClass( 'mw-mmv-shareembed-explanation mw-mmv-embed-explanation' )
|
2014-03-26 13:34:44 +00:00
|
|
|
.text( mw.message( 'multimediaviewer-embed-explanation' ).text() )
|
|
|
|
.appendTo( this.$pane );
|
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
this.createSnippetSelectionButtons( this.$pane );
|
2014-03-19 02:00:26 +00:00
|
|
|
this.createSizePulldownMenus( this.$pane );
|
|
|
|
|
|
|
|
/**
|
2014-04-08 14:25:27 +00:00
|
|
|
* Currently selected embed snippet.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-03-19 02:00:26 +00:00
|
|
|
* @property {jQuery}
|
|
|
|
*/
|
2014-04-08 14:25:27 +00:00
|
|
|
this.$currentMainEmbedText = mw.user.isAnon() ? this.embedTextHtml.$element : this.embedTextWikitext.$element;
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2014-04-02 02:22:16 +00:00
|
|
|
/**
|
|
|
|
* Default item for the html size menu.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-11-17 22:18:23 +00:00
|
|
|
* @property {OO.ui.MenuOptionWidget}
|
2014-04-02 02:22:16 +00:00
|
|
|
*/
|
2014-04-08 23:01:02 +00:00
|
|
|
this.defaultHtmlItem = this.embedSizeSwitchHtml.getMenu().getSelectedItem();
|
2014-04-02 02:22:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default item for the wikitext size menu.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-11-17 22:18:23 +00:00
|
|
|
* @property {OO.ui.MenuOptionWidget}
|
2014-04-02 02:22:16 +00:00
|
|
|
*/
|
2014-04-08 23:01:02 +00:00
|
|
|
this.defaultWikitextItem = this.embedSizeSwitchWikitext.getMenu().getSelectedItem();
|
2014-04-02 02:22:16 +00:00
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
/**
|
|
|
|
* Currently selected size menu.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-11-17 22:18:23 +00:00
|
|
|
* @property {OO.ui.MenuSelectWidget}
|
2014-03-19 02:00:26 +00:00
|
|
|
*/
|
2014-04-08 14:25:27 +00:00
|
|
|
this.currentSizeMenu = mw.user.isAnon() ? this.embedSizeSwitchHtml.getMenu() : this.embedSizeSwitchWikitext.getMenu();
|
2014-04-02 02:22:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current default item.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-11-17 22:18:23 +00:00
|
|
|
* @property {OO.ui.MenuOptionWidget}
|
2014-04-02 02:22:16 +00:00
|
|
|
*/
|
2014-04-08 14:25:27 +00:00
|
|
|
this.currentDefaultItem = mw.user.isAnon() ? this.defaultHtmlItem : this.defaultWikitextItem;
|
2014-03-19 02:00:26 +00:00
|
|
|
}
|
|
|
|
oo.inheritClass( Embed, mw.mmv.ui.reuse.Tab );
|
|
|
|
EP = Embed.prototype;
|
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
/** @property {number} Width threshold at which an image is to be considered "large" */
|
|
|
|
EP.LARGE_IMAGE_WIDTH_THRESHOLD = 1200;
|
|
|
|
|
|
|
|
/** @property {number} Height threshold at which an image is to be considered "large" */
|
|
|
|
EP.LARGE_IMAGE_HEIGHT_THRESHOLD = 900;
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
/**
|
|
|
|
* Creates text areas for html and wikitext snippets.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $container
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
EP.createSnippetTextAreas = function ( $container ) {
|
2014-04-08 14:25:27 +00:00
|
|
|
var wikitextClasses = [ 'mw-mmv-embed-text-wikitext' ],
|
|
|
|
htmlClasses = [ 'mw-mmv-embed-text-html' ];
|
|
|
|
|
|
|
|
( mw.user.isAnon() ? htmlClasses : wikitextClasses ).push( 'active' );
|
|
|
|
|
2017-07-12 08:27:57 +00:00
|
|
|
this.embedTextHtml = new oo.ui.MultilineTextInputWidget( {
|
2014-04-08 14:25:27 +00:00
|
|
|
classes: htmlClasses,
|
2014-03-19 03:05:36 +00:00
|
|
|
readOnly: true
|
|
|
|
} );
|
|
|
|
|
2014-03-19 22:35:55 +00:00
|
|
|
this.embedTextHtml.$element.find( 'textarea' )
|
|
|
|
.prop( 'placeholder', mw.message( 'multimediaviewer-reuse-loading-placeholder' ).text() );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
this.embedTextHtml.$input.on( 'copy', function () {
|
2014-09-05 12:52:53 +00:00
|
|
|
mw.mmv.actionLogger.log( 'embed-html-copied' );
|
|
|
|
} );
|
|
|
|
|
2017-07-12 08:27:57 +00:00
|
|
|
this.embedTextWikitext = new oo.ui.MultilineTextInputWidget( {
|
2014-04-08 14:25:27 +00:00
|
|
|
classes: wikitextClasses,
|
2014-03-19 02:00:26 +00:00
|
|
|
readOnly: true
|
|
|
|
} );
|
|
|
|
|
2014-03-19 22:35:55 +00:00
|
|
|
this.embedTextWikitext.$element.find( 'textarea' )
|
|
|
|
.prop( 'placeholder', mw.message( 'multimediaviewer-reuse-loading-placeholder' ).text() );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
this.embedTextWikitext.$input.on( 'copy', function () {
|
2014-09-05 12:52:53 +00:00
|
|
|
mw.mmv.actionLogger.log( 'embed-wikitext-copied' );
|
|
|
|
} );
|
|
|
|
|
2016-12-22 17:59:07 +00:00
|
|
|
this.$copyButton = $( '<a>' )
|
2016-12-28 16:32:28 +00:00
|
|
|
.addClass( 'mw-mmv-dialog-copy' )
|
2016-12-22 17:59:07 +00:00
|
|
|
.click( function () {
|
|
|
|
// Select the text, and then try to copy the text.
|
|
|
|
// If the copy fails or is not supported, continue as if nothing had happened.
|
|
|
|
$( this ).parent().find( '.active > textarea' ).select();
|
|
|
|
try {
|
|
|
|
if ( document.queryCommandSupported &&
|
|
|
|
document.queryCommandSupported( 'copy' ) ) {
|
|
|
|
document.execCommand( 'copy' );
|
|
|
|
}
|
|
|
|
} catch ( e ) {
|
|
|
|
// queryCommandSupported in Firefox pre-41 can throw errors when used with
|
|
|
|
// clipboard commands. We catch and ignore these and other copy-command-related
|
|
|
|
// errors here.
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
.prop( 'title', mw.msg( 'multimediaviewer-reuse-copy-embed' ) )
|
|
|
|
.tipsy( {
|
|
|
|
delayIn: mw.config.get( 'wgMultimediaViewer' ).tooltipDelay,
|
|
|
|
gravity: this.correctEW( 'se' )
|
|
|
|
} );
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
$( '<p>' )
|
|
|
|
.append(
|
2014-03-19 03:05:36 +00:00
|
|
|
this.embedTextHtml.$element,
|
2016-12-22 17:59:07 +00:00
|
|
|
this.embedTextWikitext.$element,
|
|
|
|
this.$copyButton
|
2014-03-19 02:00:26 +00:00
|
|
|
)
|
|
|
|
.appendTo( $container );
|
|
|
|
};
|
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
/**
|
|
|
|
* Creates snippet selection buttons.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $container
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
EP.createSnippetSelectionButtons = function ( $container ) {
|
2014-03-19 03:05:36 +00:00
|
|
|
var wikitextButtonOption,
|
|
|
|
htmlButtonOption;
|
2014-04-08 14:25:27 +00:00
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
this.embedSwitch = new oo.ui.ButtonSelectWidget( {
|
2014-03-31 21:33:12 +00:00
|
|
|
classes: [ 'mw-mmv-embed-select' ]
|
2014-03-19 03:05:36 +00:00
|
|
|
} );
|
|
|
|
|
2014-11-22 00:01:29 +00:00
|
|
|
wikitextButtonOption = new oo.ui.ButtonOptionWidget( {
|
|
|
|
data: 'wikitext',
|
|
|
|
label: mw.message( 'multimediaviewer-embed-wt' ).text()
|
|
|
|
} );
|
|
|
|
htmlButtonOption = new oo.ui.ButtonOptionWidget( {
|
|
|
|
data: 'html',
|
|
|
|
label: mw.message( 'multimediaviewer-embed-html' ).text()
|
|
|
|
} );
|
2014-03-19 03:05:36 +00:00
|
|
|
|
|
|
|
this.embedSwitch.addItems( [
|
|
|
|
wikitextButtonOption,
|
|
|
|
htmlButtonOption
|
|
|
|
] );
|
|
|
|
|
|
|
|
$( '<p>' )
|
|
|
|
.append( this.embedSwitch.$element )
|
|
|
|
.appendTo( $container );
|
|
|
|
|
2014-04-08 14:25:27 +00:00
|
|
|
// Logged-out defaults to 'html', logged-in to 'wikitext'
|
|
|
|
this.embedSwitch.selectItem( mw.user.isAnon() ? htmlButtonOption : wikitextButtonOption );
|
2014-03-19 03:05:36 +00:00
|
|
|
};
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
/**
|
|
|
|
* Creates pulldown menus to select file sizes.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $container
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
EP.createSizePulldownMenus = function ( $container ) {
|
2014-04-08 14:25:27 +00:00
|
|
|
var wikitextClasses = [ 'mw-mmv-embed-size' ],
|
|
|
|
htmlClasses = [ 'mw-mmv-embed-size' ];
|
|
|
|
|
|
|
|
( mw.user.isAnon() ? htmlClasses : wikitextClasses ).push( 'active' );
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
// Wikitext sizes pulldown menu
|
2014-03-31 16:08:02 +00:00
|
|
|
this.embedSizeSwitchWikitext = this.utils.createPulldownMenu(
|
|
|
|
[ 'default', 'small', 'medium', 'large' ],
|
2014-04-08 14:25:27 +00:00
|
|
|
wikitextClasses,
|
2014-03-31 16:08:02 +00:00
|
|
|
'default'
|
|
|
|
);
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
this.embedSizeSwitchWikitext.getMenu().on( 'select', function ( item ) {
|
2014-09-05 12:52:53 +00:00
|
|
|
mw.mmv.actionLogger.log( 'embed-select-menu-wikitext-' + item.data.name );
|
2014-09-17 20:33:56 +00:00
|
|
|
} );
|
2014-09-05 12:52:53 +00:00
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
// Html sizes pulldown menu
|
2014-03-31 16:08:02 +00:00
|
|
|
this.embedSizeSwitchHtml = this.utils.createPulldownMenu(
|
|
|
|
[ 'small', 'medium', 'large', 'original' ],
|
2014-04-08 14:25:27 +00:00
|
|
|
htmlClasses,
|
2014-04-02 02:22:16 +00:00
|
|
|
'original'
|
2014-03-31 16:08:02 +00:00
|
|
|
);
|
2014-03-19 03:05:36 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
this.embedSizeSwitchHtml.getMenu().on( 'select', function ( item ) {
|
2014-09-05 12:52:53 +00:00
|
|
|
mw.mmv.actionLogger.log( 'embed-select-menu-html-' + item.data.name );
|
2014-09-17 20:33:56 +00:00
|
|
|
} );
|
2014-04-08 14:25:27 +00:00
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
$( '<p>' )
|
|
|
|
.append(
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedSizeSwitchHtml.$element,
|
|
|
|
this.embedSizeSwitchWikitext.$element
|
2014-03-19 02:00:26 +00:00
|
|
|
)
|
|
|
|
.appendTo( $container );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers listeners.
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
EP.attach = function () {
|
2014-03-19 02:00:26 +00:00
|
|
|
var embed = this,
|
2014-03-19 03:05:36 +00:00
|
|
|
$htmlTextarea = this.embedTextHtml.$element.find( 'textarea' ),
|
2014-03-19 02:00:26 +00:00
|
|
|
$wikitextTextarea = this.embedTextWikitext.$element.find( 'textarea' );
|
|
|
|
|
|
|
|
// Select all text once element gets focus
|
2014-05-21 21:41:39 +00:00
|
|
|
$htmlTextarea.on( 'focus', this.selectAllOnEvent );
|
|
|
|
$wikitextTextarea.on( 'focus', this.selectAllOnEvent );
|
2014-03-19 22:35:55 +00:00
|
|
|
// Disable partial text selection inside the textboxes
|
2014-05-21 21:41:39 +00:00
|
|
|
$htmlTextarea.on( 'mousedown click', this.onlyFocus );
|
|
|
|
$wikitextTextarea.on( 'mousedown click', this.onlyFocus );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
// Register handler for switching between wikitext/html snippets
|
|
|
|
this.embedSwitch.on( 'select', $.proxy( embed.handleTypeSwitch, embed ) );
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
// Register handlers for switching between file sizes
|
2014-04-08 23:01:02 +00:00
|
|
|
this.embedSizeSwitchHtml.getMenu().on( 'choose', $.proxy( this.handleSizeSwitch, this ) );
|
|
|
|
this.embedSizeSwitchWikitext.getMenu().on( 'choose', $.proxy( this.handleSizeSwitch, this ) );
|
2014-03-19 02:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears listeners.
|
|
|
|
*/
|
2015-01-23 12:48:27 +00:00
|
|
|
EP.unattach = function () {
|
2014-05-21 21:41:39 +00:00
|
|
|
var $htmlTextarea = this.embedTextHtml.$element.find( 'textarea' ),
|
|
|
|
$wikitextTextarea = this.embedTextWikitext.$element.find( 'textarea' );
|
|
|
|
|
2014-09-16 20:33:05 +00:00
|
|
|
mw.mmv.ui.reuse.Tab.prototype.unattach.call( this );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2014-05-21 21:41:39 +00:00
|
|
|
$htmlTextarea.off( 'focus mousedown click' );
|
|
|
|
$wikitextTextarea.off( 'focus mousedown click' );
|
2014-03-19 03:05:36 +00:00
|
|
|
this.embedSwitch.off( 'select' );
|
2014-04-08 23:01:02 +00:00
|
|
|
this.embedSizeSwitchHtml.getMenu().off( 'choose' );
|
|
|
|
this.embedSizeSwitchWikitext.getMenu().off( 'choose' );
|
2014-03-19 02:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles size menu change events.
|
2014-04-02 02:22:16 +00:00
|
|
|
*
|
2014-11-17 22:18:23 +00:00
|
|
|
* @param {OO.ui.MenuOptionWidget} item
|
2014-03-19 02:00:26 +00:00
|
|
|
*/
|
|
|
|
EP.handleSizeSwitch = function ( item ) {
|
|
|
|
var value = item.getData();
|
|
|
|
|
|
|
|
this.changeSize( value.width, value.height );
|
|
|
|
};
|
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
/**
|
|
|
|
* Handles snippet type switch.
|
2014-04-02 02:22:16 +00:00
|
|
|
*
|
2014-11-17 22:18:23 +00:00
|
|
|
* @param {OO.ui.MenuOptionWidget} item
|
2014-03-19 03:05:36 +00:00
|
|
|
*/
|
|
|
|
EP.handleTypeSwitch = function ( item ) {
|
|
|
|
var value = item.getData();
|
|
|
|
|
2014-09-05 12:52:53 +00:00
|
|
|
mw.mmv.actionLogger.log( 'embed-switched-to-' + value );
|
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
if ( value === 'html' ) {
|
|
|
|
this.$currentMainEmbedText = this.embedTextHtml.$element;
|
2014-07-15 22:02:05 +00:00
|
|
|
this.embedSizeSwitchWikitext.getMenu().toggle( false );
|
2014-04-02 02:22:16 +00:00
|
|
|
|
|
|
|
this.currentSizeMenu = this.embedSizeSwitchHtml.getMenu();
|
|
|
|
this.currentDefaultItem = this.defaultHtmlItem;
|
2014-03-21 20:29:55 +00:00
|
|
|
} else if ( value === 'wikitext' ) {
|
2014-03-19 03:05:36 +00:00
|
|
|
this.$currentMainEmbedText = this.embedTextWikitext.$element;
|
2014-07-15 22:02:05 +00:00
|
|
|
this.embedSizeSwitchHtml.getMenu().toggle( false );
|
2014-04-02 02:22:16 +00:00
|
|
|
|
|
|
|
this.currentSizeMenu = this.embedSizeSwitchWikitext.getMenu();
|
|
|
|
this.currentDefaultItem = this.defaultWikitextItem;
|
2014-03-19 03:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.embedTextHtml.$element
|
2014-03-21 20:29:55 +00:00
|
|
|
.add( this.embedSizeSwitchHtml.$element )
|
2014-03-19 03:05:36 +00:00
|
|
|
.toggleClass( 'active', value === 'html' );
|
|
|
|
|
|
|
|
this.embedTextWikitext.$element
|
2014-03-21 20:29:55 +00:00
|
|
|
.add( this.embedSizeSwitchWikitext.$element )
|
|
|
|
.toggleClass( 'active', value === 'wikitext' );
|
2014-03-19 03:05:36 +00:00
|
|
|
|
2014-04-02 02:22:16 +00:00
|
|
|
// Reset current selection to default when switching the first time
|
2015-01-23 12:48:27 +00:00
|
|
|
if ( !this.isSizeMenuDefaultReset ) {
|
2014-04-02 02:22:16 +00:00
|
|
|
this.resetCurrentSizeMenuToDefault();
|
|
|
|
this.isSizeMenuDefaultReset = true;
|
|
|
|
}
|
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
this.select();
|
2014-04-02 02:22:16 +00:00
|
|
|
};
|
2014-03-19 03:05:36 +00:00
|
|
|
|
2014-04-02 02:22:16 +00:00
|
|
|
/**
|
|
|
|
* Reset current menu selection to default item.
|
|
|
|
*/
|
|
|
|
EP.resetCurrentSizeMenuToDefault = function () {
|
2014-04-08 23:01:02 +00:00
|
|
|
this.currentSizeMenu.chooseItem( this.currentDefaultItem );
|
|
|
|
// Force select logic to update the selected item bar, otherwise we end up
|
|
|
|
// with the wrong label. This is implementation dependent and maybe it should
|
|
|
|
// be done via a to flag to OO.ui.SelectWidget.prototype.chooseItem()?
|
|
|
|
this.currentSizeMenu.emit( 'select', this.currentDefaultItem );
|
2014-03-19 03:05:36 +00:00
|
|
|
};
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
/**
|
|
|
|
* Changes the size, takes different actions based on which sort of
|
|
|
|
* embed is currently chosen.
|
|
|
|
*
|
|
|
|
* @param {number} width New width to set
|
2014-03-19 03:05:36 +00:00
|
|
|
* @param {number} height New height to set
|
2014-03-19 02:00:26 +00:00
|
|
|
*/
|
2014-03-19 03:05:36 +00:00
|
|
|
EP.changeSize = function ( width, height ) {
|
|
|
|
var currentItem = this.embedSwitch.getSelectedItem();
|
|
|
|
|
|
|
|
if ( currentItem === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( currentItem.getData() ) {
|
|
|
|
case 'html':
|
2014-03-21 20:29:55 +00:00
|
|
|
this.updateEmbedHtml( {}, width, height );
|
2014-03-19 03:05:36 +00:00
|
|
|
break;
|
2014-03-21 20:29:55 +00:00
|
|
|
case 'wikitext':
|
|
|
|
this.updateEmbedWikitext( width );
|
2014-03-19 03:05:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
this.select();
|
|
|
|
};
|
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
/**
|
2014-03-21 20:29:55 +00:00
|
|
|
* Sets the HTML embed text.
|
2014-03-19 03:05:36 +00:00
|
|
|
*
|
2014-03-21 20:29:55 +00:00
|
|
|
* Assumes that the set() method has already been called to update this.embedFileInfo
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-03-19 03:05:36 +00:00
|
|
|
* @param {mw.mmv.model.Thumbnail} thumbnail (can be just an empty object)
|
|
|
|
* @param {number} width New width to set
|
|
|
|
* @param {number} height New height to set
|
|
|
|
*/
|
2014-03-21 20:29:55 +00:00
|
|
|
EP.updateEmbedHtml = function ( thumbnail, width, height ) {
|
2014-03-19 03:05:36 +00:00
|
|
|
var src;
|
|
|
|
|
|
|
|
if ( !this.embedFileInfo ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
src = thumbnail.url || this.embedFileInfo.imageInfo.url;
|
2014-03-19 03:05:36 +00:00
|
|
|
|
|
|
|
// If the image dimension requested are "large", use the current image url
|
2014-03-21 20:29:55 +00:00
|
|
|
if ( width > EP.LARGE_IMAGE_WIDTH_THRESHOLD || height > EP.LARGE_IMAGE_HEIGHT_THRESHOLD ) {
|
|
|
|
src = this.embedFileInfo.imageInfo.url;
|
2014-03-19 03:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.embedTextHtml.setValue(
|
|
|
|
this.formatter.getThumbnailHtml( this.embedFileInfo, src, width, height ) );
|
|
|
|
};
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
/**
|
|
|
|
* Updates the wikitext embed text with a new value for width.
|
|
|
|
*
|
|
|
|
* Assumes that the set method has already been called.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-03-19 02:00:26 +00:00
|
|
|
* @param {number} width
|
|
|
|
*/
|
2014-03-21 20:29:55 +00:00
|
|
|
EP.updateEmbedWikitext = function ( width ) {
|
2014-03-19 02:00:26 +00:00
|
|
|
if ( !this.embedFileInfo ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedTextWikitext.setValue(
|
|
|
|
this.formatter.getThumbnailWikitextFromEmbedFileInfo( this.embedFileInfo, width )
|
|
|
|
);
|
2014-03-19 02:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows the pane.
|
|
|
|
*/
|
|
|
|
EP.show = function () {
|
2014-09-16 20:33:05 +00:00
|
|
|
mw.mmv.ui.reuse.Tab.prototype.show.call( this );
|
2014-03-19 02:00:26 +00:00
|
|
|
this.select();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets size options for html and wikitext snippets.
|
|
|
|
*
|
|
|
|
* @param {number} width
|
|
|
|
* @param {number} height
|
2016-07-18 13:49:27 +00:00
|
|
|
* @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
|
2014-03-19 02:00:26 +00:00
|
|
|
*/
|
|
|
|
EP.getSizeOptions = function ( width, height ) {
|
|
|
|
var sizes = {};
|
|
|
|
|
2014-03-31 16:08:02 +00:00
|
|
|
sizes.html = this.utils.getPossibleImageSizesForHtml( width, height );
|
2014-03-19 02:00:26 +00:00
|
|
|
sizes.wikitext = this.getPossibleImageSizesForWikitext( width, height );
|
|
|
|
|
|
|
|
return sizes;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the data on the element.
|
|
|
|
*
|
|
|
|
* @param {mw.mmv.model.Image} image
|
2014-03-21 20:29:55 +00:00
|
|
|
* @param {mw.mmv.model.Repo} repo
|
|
|
|
* @param {string} caption
|
2014-12-30 05:10:27 +00:00
|
|
|
* @param {string} alt
|
2014-03-19 02:00:26 +00:00
|
|
|
*/
|
2014-12-30 05:10:27 +00:00
|
|
|
EP.set = function ( image, repo, caption, alt ) {
|
2014-03-19 03:05:36 +00:00
|
|
|
var embed = this,
|
2014-03-31 16:08:02 +00:00
|
|
|
htmlSizeSwitch = this.embedSizeSwitchHtml.getMenu(),
|
|
|
|
htmlSizeOptions = htmlSizeSwitch.getItems(),
|
|
|
|
wikitextSizeSwitch = this.embedSizeSwitchWikitext.getMenu(),
|
|
|
|
wikitextSizeOptions = wikitextSizeSwitch.getItems(),
|
2014-03-19 02:00:26 +00:00
|
|
|
sizes = this.getSizeOptions( image.width, image.height );
|
|
|
|
|
2014-12-30 05:10:27 +00:00
|
|
|
this.embedFileInfo = new mw.mmv.model.EmbedFileInfo( image, repo, caption, alt );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2014-03-31 16:08:02 +00:00
|
|
|
this.utils.updateMenuOptions( sizes.html, htmlSizeOptions );
|
|
|
|
this.utils.updateMenuOptions( sizes.wikitext, wikitextSizeOptions );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2014-04-02 02:22:16 +00:00
|
|
|
// Reset defaults
|
|
|
|
this.isSizeMenuDefaultReset = false;
|
|
|
|
this.resetCurrentSizeMenuToDefault();
|
2014-03-19 03:05:36 +00:00
|
|
|
|
2014-03-31 16:08:02 +00:00
|
|
|
this.utils.getThumbnailUrlPromise( this.LARGE_IMAGE_WIDTH_THRESHOLD )
|
|
|
|
.done( function ( thumbnail ) {
|
|
|
|
embed.updateEmbedHtml( thumbnail );
|
|
|
|
embed.select();
|
|
|
|
} );
|
2014-03-19 02:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
EP.empty = function () {
|
2014-03-19 03:05:36 +00:00
|
|
|
this.embedTextHtml.setValue( '' );
|
2014-03-19 02:00:26 +00:00
|
|
|
this.embedTextWikitext.setValue( '' );
|
|
|
|
|
2014-07-15 22:02:05 +00:00
|
|
|
this.embedSizeSwitchHtml.getMenu().toggle( false );
|
|
|
|
this.embedSizeSwitchWikitext.getMenu().toggle( false );
|
2014-03-19 02:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-03-19 22:35:55 +00:00
|
|
|
* Selects the text in the current textbox by triggering a focus event.
|
2014-03-19 02:00:26 +00:00
|
|
|
*/
|
|
|
|
EP.select = function () {
|
|
|
|
this.$currentMainEmbedText.focus();
|
|
|
|
};
|
|
|
|
|
2014-03-31 16:08:02 +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
|
2016-07-18 13:49:27 +00:00
|
|
|
* @return {Object}
|
|
|
|
* @return {Object} return.small
|
|
|
|
* @return {Object} return.medium
|
|
|
|
* @return {Object} return.large
|
2014-03-31 16:08:02 +00:00
|
|
|
*/
|
|
|
|
EP.getPossibleImageSizesForWikitext = function ( width, height ) {
|
|
|
|
var i, bucketName,
|
|
|
|
bucketWidth,
|
|
|
|
buckets = {
|
2016-07-18 13:49:27 +00:00
|
|
|
small: 300,
|
|
|
|
medium: 400,
|
|
|
|
large: 500
|
2014-03-31 16:08:02 +00:00
|
|
|
},
|
|
|
|
sizes = {},
|
|
|
|
bucketNames = Object.keys( buckets ),
|
|
|
|
widthToHeight = height / width;
|
|
|
|
|
|
|
|
for ( i = 0; i < bucketNames.length; i++ ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
bucketName = bucketNames[ i ];
|
|
|
|
bucketWidth = buckets[ bucketName ];
|
2014-03-31 16:08:02 +00:00
|
|
|
|
|
|
|
if ( width > bucketWidth ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
sizes[ bucketName ] = {
|
2014-03-31 16:08:02 +00:00
|
|
|
width: bucketWidth,
|
|
|
|
height: Math.round( bucketWidth * widthToHeight )
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 13:09:10 +00:00
|
|
|
sizes.default = { width: null, height: null };
|
2014-04-02 02:22:16 +00:00
|
|
|
|
2014-03-31 16:08:02 +00:00
|
|
|
return sizes;
|
|
|
|
};
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
mw.mmv.ui.reuse.Embed = Embed;
|
|
|
|
}( mediaWiki, jQuery, OO ) );
|