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 ) {
|
|
|
|
Embed.super.call( this, $container );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formatter converting image data into formats needed for output
|
|
|
|
* @property {mw.mmv.EmbedFileFormatter}
|
|
|
|
*/
|
|
|
|
this.formatter = new mw.mmv.EmbedFileFormatter();
|
|
|
|
|
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 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently selected embed snippet, defaults to wikitext.
|
|
|
|
* @property {jQuery}
|
|
|
|
*/
|
|
|
|
this.$currentMainEmbedText = this.embedTextWikitext.$element;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently selected size menu.
|
|
|
|
* @property {OO.ui.MenuWidget}
|
|
|
|
*/
|
2014-03-21 20:29:55 +00:00
|
|
|
this.currentSizeMenu = this.embedSizeSwitchWikitext.getMenu();
|
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
|
|
|
|
*/
|
|
|
|
EP.createSnippetTextAreas = function( $container ) {
|
2014-03-19 03:05:36 +00:00
|
|
|
this.embedTextHtml = new oo.ui.TextInputWidget( {
|
2014-03-31 21:33:12 +00:00
|
|
|
classes: [ 'mw-mmv-embed-text-html' ],
|
2014-03-19 03:05:36 +00:00
|
|
|
multiline: true,
|
|
|
|
readOnly: true
|
|
|
|
} );
|
|
|
|
|
2014-03-19 22:35:55 +00:00
|
|
|
this.embedTextHtml.$element.find( 'textarea' )
|
|
|
|
.prop( 'placeholder', mw.message( 'multimediaviewer-reuse-loading-placeholder' ).text() );
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
this.embedTextWikitext = new oo.ui.TextInputWidget( {
|
2014-03-31 21:33:12 +00:00
|
|
|
classes: [ 'mw-mmv-embed-text-wikitext', 'active' ],
|
2014-03-19 02:00:26 +00:00
|
|
|
multiline: true,
|
|
|
|
readOnly: true
|
|
|
|
} );
|
|
|
|
|
2014-03-19 22:35:55 +00:00
|
|
|
this.embedTextWikitext.$element.find( 'textarea' )
|
|
|
|
.prop( 'placeholder', mw.message( 'multimediaviewer-reuse-loading-placeholder' ).text() );
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
$( '<p>' )
|
|
|
|
.append(
|
2014-03-19 03:05:36 +00:00
|
|
|
this.embedTextHtml.$element,
|
2014-03-19 02:00:26 +00:00
|
|
|
this.embedTextWikitext.$element
|
|
|
|
)
|
|
|
|
.appendTo( $container );
|
|
|
|
};
|
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
/**
|
|
|
|
* Creates snippet selection buttons.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $container
|
|
|
|
*/
|
|
|
|
EP.createSnippetSelectionButtons = function( $container ) {
|
|
|
|
var wikitextButtonOption,
|
|
|
|
htmlButtonOption;
|
|
|
|
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-03-21 20:29:55 +00:00
|
|
|
wikitextButtonOption = new oo.ui.ButtonOptionWidget( 'wikitext', {
|
|
|
|
label: mw.message( 'multimediaviewer-embed-wt' ).text()
|
2014-03-19 03:05:36 +00:00
|
|
|
} );
|
|
|
|
htmlButtonOption = new oo.ui.ButtonOptionWidget( 'html', {
|
|
|
|
label: mw.message( 'multimediaviewer-embed-html' ).text()
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.embedSwitch.addItems( [
|
|
|
|
wikitextButtonOption,
|
|
|
|
htmlButtonOption
|
|
|
|
] );
|
|
|
|
|
|
|
|
$( '<p>' )
|
|
|
|
.append( this.embedSwitch.$element )
|
|
|
|
.appendTo( $container );
|
|
|
|
|
|
|
|
// Default to 'wikitext'
|
|
|
|
this.embedSwitch.selectItem( wikitextButtonOption );
|
|
|
|
};
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
/**
|
|
|
|
* Creates pulldown menus to select file sizes.
|
|
|
|
*
|
|
|
|
* @param {jQuery} $container
|
|
|
|
*/
|
|
|
|
EP.createSizePulldownMenus = function( $container ) {
|
2014-03-24 14:44:41 +00:00
|
|
|
var placeholderDimensions = $( '<span>' )
|
2014-03-31 21:33:12 +00:00
|
|
|
.addClass( 'mw-mmv-embed-dimensions' )
|
2014-03-24 14:44:41 +00:00
|
|
|
.text( mw.message( 'multimediaviewer-embed-dimensions', 0, 0 ).text() ).get( 0 ).outerHTML;
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
// Wikitext sizes pulldown menu
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedSizeSwitchWikitext = new oo.ui.InlineMenuWidget( {
|
2014-03-31 21:33:12 +00:00
|
|
|
classes: [ 'mw-mmv-embed-size', 'active' ]
|
2014-03-19 02:00:26 +00:00
|
|
|
} );
|
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedSizeChoicesWikitext = {};
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedSizeSwitchWikitext.getMenu().addItems( [
|
|
|
|
this.embedSizeChoicesWikitext.default = new oo.ui.MenuItemWidget( { name: 'default' }, {
|
2014-03-24 14:44:41 +00:00
|
|
|
label: mw.message( 'multimediaviewer-default-embed-dimensions' ).text(),
|
|
|
|
autoFitLabel: false
|
2014-03-19 02:00:26 +00:00
|
|
|
} ),
|
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedSizeChoicesWikitext.small = new oo.ui.MenuItemWidget( {
|
2014-03-19 02:00:26 +00:00
|
|
|
name: 'small',
|
|
|
|
height: null,
|
|
|
|
width: null
|
|
|
|
},
|
|
|
|
{
|
2014-03-24 14:44:41 +00:00
|
|
|
label: $( '<span>' ).html( mw.message( 'multimediaviewer-small-embed-dimensions', placeholderDimensions ).text() ),
|
|
|
|
autoFitLabel: false
|
2014-03-19 02:00:26 +00:00
|
|
|
} ),
|
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedSizeChoicesWikitext.medium = new oo.ui.MenuItemWidget( {
|
2014-03-19 02:00:26 +00:00
|
|
|
name: 'medium',
|
|
|
|
height: null,
|
|
|
|
width: null
|
|
|
|
},
|
|
|
|
{
|
2014-03-24 14:44:41 +00:00
|
|
|
label: $( '<span>' ).html( mw.message( 'multimediaviewer-medium-embed-dimensions', placeholderDimensions ).text() ),
|
|
|
|
autoFitLabel: false
|
2014-03-19 02:00:26 +00:00
|
|
|
} ),
|
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedSizeChoicesWikitext.large = new oo.ui.MenuItemWidget( {
|
2014-03-19 02:00:26 +00:00
|
|
|
name: 'large',
|
|
|
|
height: null,
|
|
|
|
width: null
|
|
|
|
},
|
|
|
|
{
|
2014-03-24 14:44:41 +00:00
|
|
|
label: $( '<span>' ).html( mw.message( 'multimediaviewer-large-embed-dimensions', placeholderDimensions ).text() ),
|
|
|
|
autoFitLabel: false
|
2014-03-19 02:00:26 +00:00
|
|
|
} )
|
|
|
|
] );
|
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedSizeSwitchWikitext.getMenu().selectItem( this.embedSizeChoicesWikitext.default );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
// Html sizes pulldown menu
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedSizeSwitchHtml = new oo.ui.InlineMenuWidget( {
|
2014-03-31 21:33:12 +00:00
|
|
|
classes: [ 'mw-mmv-embed-size' ]
|
2014-03-19 03:05:36 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
this.embedHtmlSizeChoices = {};
|
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedSizeSwitchHtml.getMenu().addItems( [
|
2014-03-19 03:05:36 +00:00
|
|
|
this.embedHtmlSizeChoices.small = new oo.ui.MenuItemWidget( {
|
|
|
|
name: 'small',
|
|
|
|
height: null,
|
|
|
|
width: null
|
|
|
|
},
|
|
|
|
{
|
2014-03-24 14:44:41 +00:00
|
|
|
label: $( '<span>' ).html( mw.message( 'multimediaviewer-small-embed-dimensions', placeholderDimensions ).text() ),
|
|
|
|
autoFitLabel: false
|
2014-03-19 03:05:36 +00:00
|
|
|
} ),
|
|
|
|
|
|
|
|
this.embedHtmlSizeChoices.medium = new oo.ui.MenuItemWidget( {
|
|
|
|
name: 'medium',
|
|
|
|
height: null,
|
|
|
|
width: null
|
|
|
|
},
|
|
|
|
{
|
2014-03-24 14:44:41 +00:00
|
|
|
label: $( '<span>' ).html( mw.message( 'multimediaviewer-medium-embed-dimensions', placeholderDimensions ).text() ),
|
|
|
|
autoFitLabel: false
|
2014-03-19 03:05:36 +00:00
|
|
|
} ),
|
|
|
|
|
|
|
|
this.embedHtmlSizeChoices.large = new oo.ui.MenuItemWidget( {
|
|
|
|
name: 'large',
|
|
|
|
height: null,
|
|
|
|
width: null
|
|
|
|
},
|
|
|
|
{
|
2014-03-24 14:44:41 +00:00
|
|
|
label: $( '<span>' ).html( mw.message( 'multimediaviewer-large-embed-dimensions', placeholderDimensions ).text() ),
|
|
|
|
autoFitLabel: false
|
2014-03-19 03:05:36 +00:00
|
|
|
} ),
|
|
|
|
|
|
|
|
this.embedHtmlSizeChoices.original = new oo.ui.MenuItemWidget( {
|
|
|
|
name: 'original',
|
|
|
|
height: null,
|
|
|
|
width: null
|
|
|
|
},
|
|
|
|
{
|
2014-03-24 14:44:41 +00:00
|
|
|
label: $( '<span>' ).html( mw.message( 'multimediaviewer-original-embed-dimensions', placeholderDimensions ).text() ),
|
|
|
|
autoFitLabel: false
|
2014-03-19 03:05:36 +00:00
|
|
|
} )
|
|
|
|
] );
|
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedSizeSwitchHtml.getMenu().selectItem( this.embedHtmlSizeChoices.small );
|
2014-03-19 03:05:36 +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.
|
|
|
|
*/
|
|
|
|
EP.attach = function() {
|
|
|
|
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-03-19 03:05:36 +00:00
|
|
|
this.embedTextHtml.onDOMEvent( 'focus', $.proxy( this.selectAllOnEvent, $htmlTextarea ) );
|
2014-03-19 02:00:26 +00:00
|
|
|
this.embedTextWikitext.onDOMEvent( 'focus', $.proxy( this.selectAllOnEvent, $wikitextTextarea ) );
|
2014-03-19 22:35:55 +00:00
|
|
|
// Disable partial text selection inside the textboxes
|
2014-03-19 03:05:36 +00:00
|
|
|
this.embedTextHtml.onDOMEvent( 'mousedown click', $.proxy( this.onlyFocus, $htmlTextarea ) );
|
2014-03-19 02:00:26 +00:00
|
|
|
this.embedTextWikitext.onDOMEvent( 'mousedown click', $.proxy( this.onlyFocus, $wikitextTextarea ) );
|
|
|
|
|
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-26 01:33:38 +00:00
|
|
|
// workaround for bug 63094
|
|
|
|
this.proxiedHandleSizeSwitch = this.proxiedHandleSizeSwitch || $.proxy( this.handleSizeSwitch, this );
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
// Register handlers for switching between file sizes
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedSizeSwitchHtml.getMenu().on( 'select', this.proxiedHandleSizeSwitch );
|
|
|
|
this.embedSizeSwitchWikitext.getMenu().on( 'select', this.proxiedHandleSizeSwitch );
|
2014-03-19 02:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears listeners.
|
|
|
|
*/
|
|
|
|
EP.unattach = function() {
|
|
|
|
this.constructor.super.prototype.unattach.call( this );
|
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
this.embedTextHtml.offDOMEvent( 'focus mousedown click' );
|
2014-03-19 02:00:26 +00:00
|
|
|
this.embedTextWikitext.offDOMEvent( 'focus mousedown click' );
|
2014-03-19 03:05:36 +00:00
|
|
|
this.embedSwitch.off( 'select' );
|
2014-03-26 01:33:38 +00:00
|
|
|
// the noop is needed for some tests which call unattach before calling attach.
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedSizeSwitchHtml.getMenu().off( 'select', this.proxiedHandleSizeSwitch || $.noop );
|
|
|
|
this.embedSizeSwitchWikitext.getMenu().off( 'select', this.proxiedHandleSizeSwitch || $.noop );
|
2014-03-19 02:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles size menu change events.
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
EP.handleTypeSwitch = function ( item ) {
|
|
|
|
var value = item.getData();
|
|
|
|
|
|
|
|
if ( value === 'html' ) {
|
|
|
|
this.$currentMainEmbedText = this.embedTextHtml.$element;
|
2014-03-21 20:29:55 +00:00
|
|
|
this.currentSizeMenu = this.embedSizeSwitchHtml.getMenu();
|
|
|
|
this.embedSizeSwitchWikitext.getMenu().hide();
|
|
|
|
} else if ( value === 'wikitext' ) {
|
2014-03-19 03:05:36 +00:00
|
|
|
this.$currentMainEmbedText = this.embedTextWikitext.$element;
|
2014-03-21 20:29:55 +00:00
|
|
|
this.currentSizeMenu = this.embedSizeSwitchWikitext.getMenu();
|
|
|
|
this.embedSizeSwitchHtml.getMenu().hide();
|
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
|
|
|
|
|
|
|
this.select();
|
|
|
|
|
|
|
|
this.currentSizeMenu.selectItem( this.currentSizeMenu.getSelectedItem() );
|
|
|
|
};
|
|
|
|
|
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
|
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.
|
|
|
|
* @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 () {
|
|
|
|
this.constructor.super.prototype.show.call( this );
|
|
|
|
this.select();
|
|
|
|
};
|
|
|
|
|
2014-03-19 03:05:36 +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
|
|
|
|
* @returns {Object}
|
|
|
|
* @returns { {width: number, height: number} } return.small
|
|
|
|
* @returns { {width: number, height: number} } return.medium
|
|
|
|
* @returns { {width: number, height: number} } return.large
|
|
|
|
* @returns { {width: number, height: number} } return.original
|
|
|
|
*/
|
|
|
|
EP.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++ ) {
|
|
|
|
bucketName = bucketNames[i];
|
|
|
|
dimensions = buckets[bucketName];
|
|
|
|
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
|
|
|
|
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
|
|
|
|
sizes[bucketName] = {
|
|
|
|
width: Math.round( bucketHeight * heightToWidth ),
|
|
|
|
height: bucketHeight
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sizes.original = { width: width, height: height };
|
|
|
|
|
|
|
|
return sizes;
|
|
|
|
};
|
|
|
|
|
2014-03-19 02:00:26 +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
|
|
|
|
* @returns {Object}
|
|
|
|
* @returns { {width: number, height: number} } return.small
|
|
|
|
* @returns { {width: number, height: number} } return.medium
|
|
|
|
* @returns { {width: number, height: number} } return.large
|
|
|
|
*/
|
|
|
|
EP.getPossibleImageSizesForWikitext = function ( width, height ) {
|
|
|
|
var i, bucketName,
|
|
|
|
bucketWidth,
|
|
|
|
buckets = {
|
|
|
|
'small': 300,
|
|
|
|
'medium': 400,
|
|
|
|
'large': 500
|
|
|
|
},
|
|
|
|
sizes = {},
|
|
|
|
bucketNames = Object.keys( buckets ),
|
|
|
|
widthToHeight = height / width;
|
|
|
|
|
|
|
|
for ( i = 0; i < bucketNames.length; i++ ) {
|
|
|
|
bucketName = bucketNames[i];
|
|
|
|
bucketWidth = buckets[bucketName];
|
|
|
|
|
|
|
|
if ( width > bucketWidth ) {
|
|
|
|
sizes[bucketName] = {
|
|
|
|
width: bucketWidth,
|
|
|
|
height: Math.round( bucketWidth * widthToHeight )
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sizes;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets size options for html and wikitext snippets.
|
|
|
|
*
|
|
|
|
* @param {number} width
|
|
|
|
* @param {number} height
|
|
|
|
* @returns {Object}
|
|
|
|
* @returns {Object} return.html Collection of possible image sizes for html snippets
|
|
|
|
* @returns {Object} return.wikitext Collection of possible image sizes for wikitext snippets
|
|
|
|
*/
|
|
|
|
EP.getSizeOptions = function ( width, height ) {
|
|
|
|
var sizes = {};
|
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
sizes.html = this.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-03-19 02:00:26 +00:00
|
|
|
*/
|
2014-03-21 20:29:55 +00:00
|
|
|
EP.set = function ( image, repo, caption ) {
|
2014-03-19 03:05:36 +00:00
|
|
|
var embed = this,
|
2014-03-19 02:00:26 +00:00
|
|
|
sizes = this.getSizeOptions( image.width, image.height );
|
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
this.embedFileInfo = new mw.mmv.model.EmbedFileInfo( image, repo, caption );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
this.updateMenuOptions( sizes.html,
|
|
|
|
this.embedSizeSwitchHtml.getMenu().getItems() );
|
|
|
|
this.updateMenuOptions( sizes.wikitext,
|
|
|
|
this.embedSizeSwitchWikitext.getMenu().getItems() );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
|
|
|
this.currentSizeMenu.selectItem( this.currentSizeMenu.getSelectedItem() );
|
2014-03-19 03:05:36 +00:00
|
|
|
this.getThumbnailUrlPromise().done( function ( thumbnail ) {
|
2014-03-21 20:29:55 +00:00
|
|
|
embed.updateEmbedHtml( thumbnail );
|
2014-03-19 22:35:55 +00:00
|
|
|
embed.select();
|
2014-03-19 03:05:36 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* FIXME document this better - why is this only needed for the large thumbnail?
|
|
|
|
*
|
|
|
|
* @return {jQuery.Promise.<string>}
|
|
|
|
*/
|
|
|
|
EP.getThumbnailUrlPromise = function () {
|
|
|
|
return $( document ).triggerHandler( 'mmv-request-thumbnail',
|
|
|
|
this.LARGE_IMAGE_WIDTH_THRESHOLD ) || $.Deferred().reject();
|
2014-03-19 02:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* Updates the menu options based on calculated sizes.
|
|
|
|
*
|
|
|
|
* @param {Object} sizes
|
|
|
|
* @param {OO.ui.MenuItemWidget[]} options
|
|
|
|
*/
|
|
|
|
EP.updateMenuOptions = function ( sizes, options ) {
|
2014-03-24 14:44:41 +00:00
|
|
|
var i, option, data, dimensions, $label;
|
2014-03-19 02:00:26 +00:00
|
|
|
|
|
|
|
for ( i = 0; i < options.length; i++ ) {
|
|
|
|
option = options[i];
|
|
|
|
data = option.getData();
|
|
|
|
|
|
|
|
if ( sizes[data.name] ) {
|
|
|
|
option.setDisabled( false );
|
|
|
|
|
|
|
|
// These values are later used in the else if case below as flags
|
|
|
|
// to disable an option that is no longer pertinent. Ex: User went
|
|
|
|
// from a large image from which we have options(small, med, large) to
|
|
|
|
// a small image where the only pertinent option is small.
|
|
|
|
data.width = sizes[data.name].width;
|
|
|
|
data.height = sizes[data.name].height;
|
|
|
|
|
2014-03-24 14:44:41 +00:00
|
|
|
dimensions = $( '<span>' )
|
2014-03-31 21:33:12 +00:00
|
|
|
.addClass( 'mw-mmv-embed-dimensions' )
|
2014-03-24 14:44:41 +00:00
|
|
|
.text( mw.message( 'multimediaviewer-embed-dimensions', data.width, data.height ).text() ).get( 0 ).outerHTML;
|
|
|
|
|
|
|
|
$label = $( '<span>' ).html(
|
2014-03-19 02:00:26 +00:00
|
|
|
mw.message(
|
2014-03-24 14:44:41 +00:00
|
|
|
'multimediaviewer-' + data.name + '-embed-dimensions',
|
|
|
|
dimensions
|
2014-03-19 02:00:26 +00:00
|
|
|
).text()
|
|
|
|
);
|
2014-03-24 14:44:41 +00:00
|
|
|
|
|
|
|
option.setLabel( $label );
|
2014-03-19 02:00:26 +00:00
|
|
|
} else if ( data.width && data.height ) {
|
|
|
|
option.setDisabled( true );
|
|
|
|
|
|
|
|
data.width = null;
|
|
|
|
data.height = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-03-21 20:29:55 +00:00
|
|
|
this.embedSizeSwitchHtml.getMenu().hide();
|
|
|
|
this.embedSizeSwitchWikitext.getMenu().hide();
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
mw.mmv.ui.reuse.Embed = Embed;
|
|
|
|
}( mediaWiki, jQuery, OO ) );
|