Merge "Add more information to embed HTML"

This commit is contained in:
jenkins-bot 2014-03-28 13:46:10 +00:00 committed by Gerrit Code Review
commit 5d74fce836
15 changed files with 378 additions and 500 deletions

View file

@ -188,7 +188,9 @@ class MultimediaViewerHooks {
'tests/qunit/mmv/model/mmv.model.test.js',
'tests/qunit/mmv/model/mmv.model.TaskQueue.test.js',
'tests/qunit/mmv/model/mmv.model.License.test.js',
'tests/qunit/mmv/model/mmv.model.Image.test.js',
'tests/qunit/mmv/model/mmv.model.Repo.test.js',
'tests/qunit/mmv/model/mmv.model.EmbedFileInfo.test.js',
'tests/qunit/mmv/provider/mmv.provider.Api.test.js',
'tests/qunit/mmv/provider/mmv.provider.ImageUsage.test.js',
'tests/qunit/mmv/provider/mmv.provider.GlobalUsage.test.js',

View file

@ -16,15 +16,31 @@
*/
( function( mw, $ ) {
var AFP;
var EFFP;
/**
* Converts data in various formats needed by the Embed sub-dialog
* @class mw.mmv.EmbedFileFormatter
* @constructor
*/
function EmbedFileFormatter() {}
AFP = EmbedFileFormatter.prototype;
function EmbedFileFormatter() {
/** @property {mw.mmv.HtmlUtils} htmlUtils - */
this.htmlUtils = new mw.mmv.HtmlUtils();
}
EFFP = EmbedFileFormatter.prototype;
/**
* Returns the caption of the image (possibly a fallback generated from image metadata).
* @param {mw.mmv.model.EmbedFileInfo} info
* @return {string}
*/
EFFP.getCaption = function ( info ) {
if ( info.caption ) {
return this.htmlUtils.htmlToText( info.caption );
} else {
return info.imageInfo.title.getNameText();
}
};
/**
* Helper function to generate thumbnail wikicode
@ -33,9 +49,10 @@
* @param {string} [caption]
* @return {string}
*/
AFP.getThumbnailWikitext = function ( title, width, caption ) {
EFFP.getThumbnailWikitext = function ( title, width, caption ) {
var widthSection, captionSection;
widthSection = width ? '|' + width + 'px' : '';
captionSection = caption ? '|' + caption : '';
@ -48,44 +65,26 @@
* @param {number} [width]
* @return {string}
*/
AFP.getThumbnailWikitextFromEmbedFileInfo = function ( info, width ) {
var title = info.title,
caption = info.caption;
return this.getThumbnailWikitext( info.title, width,
caption ? caption.plain : title.getNameText() );
EFFP.getThumbnailWikitextFromEmbedFileInfo = function ( info, width ) {
return this.getThumbnailWikitext( info.imageInfo.title, width, this.getCaption( info ) );
};
/**
* Byline construction
* @param {{html: string, plain: string}} [author]
* @param {{html: string, plain: string}} [source]
* @return {{html: string, plain: string}} byline in plain text and html
* @param {string} [author] author name (can contain HTML)
* @param {string} [source] source name (can contain HTML)
* @return {string} byline (can contain HTML)
*/
AFP.getBylines = function ( author, source ) {
var bylines = {};
EFFP.getByline = function ( author, source ) {
if ( author && source) {
bylines.plain = mw.message(
return mw.message(
'multimediaviewer-credit',
author.plain,
source.plain
).text();
bylines.html = mw.message(
'multimediaviewer-credit',
author.html,
source.html
author,
source
).parse();
} else if ( author ) {
bylines.plain = author.plain;
bylines.html = author.html;
} else if ( source ) {
bylines.plain = source.plain;
bylines.html = source.html;
} else {
return author || source;
}
return bylines;
};
/**
@ -93,33 +92,50 @@
* @param {mw.mmv.model.EmbedFileInfo} info
* @return {string}
*/
AFP.getCreditHtml = function ( info ) {
EFFP.getCreditHtml = function ( info ) {
var creditText, creditFormat, creditParams,
titleText = info.title.getNameText(),
titleText = info.imageInfo.title.getNameText(),
titleUrl = this.getLinkUrl( info ),
$title = $( '<a>' ).text( titleText ).prop( 'href', titleUrl ),
bylines = this.getBylines( info.author, info.source );
byline = this.getByline( info.imageInfo.author, info.imageInfo.source );
creditFormat = 't';
creditParams = [ this.getOuterHtml( $title ) ];
if ( bylines.html ) {
creditParams = [ this.htmlUtils.jqueryToHtml( $title ) ];
if ( byline ) {
creditFormat += 'b';
creditParams.push( bylines.html );
creditParams.push( byline );
}
if ( info.license && info.license.plain.length ) {
if ( info.imageInfo.license ) {
creditFormat += 'l';
creditParams.push( info.license.plain );
}
if ( info.siteName ) {
creditFormat += 's';
creditParams.push( info.siteName );
creditParams.push( info.imageInfo.license.getShortLink() );
}
creditFormat += 's';
creditParams.push( this.getSiteLink( info ) );
creditParams.unshift( 'multimediaviewer-html-embed-credit-text-' + creditFormat );
creditText = mw.message.apply( mw, creditParams ).plain();
return creditText;
};
/**
* Returns HTML code for a link to the site of the image.
* @param {mw.mmv.model.EmbedFileInfo} info
* @return {string}
*/
EFFP.getSiteLink = function ( info ) {
var siteName = info.repoInfo.displayName,
siteUrl = info.repoInfo.getSiteLink();
if ( siteUrl ) {
return this.htmlUtils.jqueryToHtml(
$( '<a>' ).prop( 'href', siteUrl ).text( siteName )
);
} else {
return siteName;
}
};
/**
* Generates the HTML embed code for the image.
*
@ -129,22 +145,22 @@
* @param {number} [height] Height to put into the image element.
* @return {string} Embed code.
*/
AFP.getThumbnailHtml = function ( info, imgUrl, width, height ) {
return $( '<div>' ).append(
EFFP.getThumbnailHtml = function ( info, imgUrl, width, height ) {
return this.htmlUtils.jqueryToHtml(
$( '<p>' ).append(
$( '<a>' )
.attr( 'href', this.getLinkUrl( info ) )
.append(
$( '<img>' )
.attr( 'src', imgUrl )
.attr( 'alt', info.title.getMainText() )
.attr( 'alt', info.imageInfo.title.getMainText() )
.attr( 'height', height )
.attr( 'width', width )
),
$( '<br>' ),
this.getCreditHtml( info )
)
).html();
);
};
/**
@ -153,18 +169,8 @@
*
* @param {mw.mmv.model.EmbedFileInfo} info
*/
AFP.getLinkUrl = function ( info ) {
return info.url + '#mediaviewer/' + info.title.getMainText();
};
/**
* Returns the HTML code for a jQuery element.
* Unlike .html(), this includes code for the element itself.
* @param {jQuery} $jq
* @return {string}
*/
AFP.getOuterHtml = function ( $jq ) {
return $jq.get( 0 ).outerHTML;
EFFP.getLinkUrl = function ( info ) {
return info.imageInfo.descriptionUrl + '#mediaviewer/' + info.imageInfo.title.getMainText();
};
mw.mmv.EmbedFileFormatter = EmbedFileFormatter;

View file

@ -15,151 +15,33 @@
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
*/
( function ( mw, $ ) {
( function ( mw ) {
/**
* Contains information needed to embed and share files.
* @class mw.mmv.model.EmbedFileInfo
* @constructor
* @param {mw.Title} title
* @param {string} src
* @param {string} url
* @param {string} [siteName]
* @param {Object} [license]
* @param {string} [license.plain]
* @param {string} [license.html]
* @param {Object} [author]
* @param {string} [author.plain]
* @param {string} [author.html]
* @param {Object} [source]
* @param {string} [source.plain]
* @param {string} [source.html]
* @param {Object} [caption]
* @param {string} [caption.plain]
* @param {string} [caption.html]
* @param {mw.mmv.model.Image} imageInfo
* @param {mw.mmv.model.Repo} repoInfo
* @param {string} [caption]
*/
function EmbedFileInfo(
title,
src,
url,
siteName,
license,
author,
source,
caption
imageInfo,
repoInfo,
caption
) {
if ( !title || !src || !url ) {
throw 'title, src and url are required and must have a value';
if ( !imageInfo || !repoInfo ) {
throw 'imageInfo and repoInfo are required and must have a value';
}
/** @property {mw.Title} title The title of the file */
this.title = title;
/** @property {mw.mmv.model.Image} imageInfo The title of the file */
this.imageInfo = imageInfo;
/** @property {string} src The URL to the original file */
this.src = src;
/** @property {string} url The URL to the file description page */
this.url = url;
/** @property {string} [siteName] Human-readable name of the site the file is on */
this.siteName = siteName;
/** @property {Object} [license] Description of the license of the file - with links */
this.license = license;
/** @property {Object} [author] Author of the file - with links */
this.author = author;
/** @property {Object} [source] Source for the file - with links */
this.source = source;
/** @property {mw.mmv.model.Repo} repoInfo The URL to the original file */
this.repoInfo = repoInfo;
/** @property {Object} [caption] Image caption, if any */
this.caption = caption;
}
/**
* Helper function to turn HTML to plaintext
* @private
* @param {string} html
* @return {{plain: string, html: string}|null}
*/
function htmlToObject ( html ) {
if ( html && html.length ) {
return {
plain: $( '<div>' + html + '</div>' ).text(),
html: html
};
} else {
return null;
}
}
/**
* Factory method for creating an info object from html
* @param {mw.Title} title
* @param {string} src
* @param {string} url
* @param {string} [siteName]
* @param {string} [licenseHtml]
* @param {string} [authorHtml]
* @param {string} [sourceHtml]
* @param {string} [captionHtml]
* @return {mw.mmv.model.EmbedFileInfo}
*/
EmbedFileInfo.fromHtml = function (
title,
src,
url,
siteName,
licenseHtml,
authorHtml,
sourceHtml,
captionHtml
) {
return new EmbedFileInfo(
title,
src,
url,
siteName,
htmlToObject( licenseHtml ),
htmlToObject( authorHtml ),
htmlToObject( sourceHtml ),
htmlToObject( captionHtml )
);
};
/**
* Turns image info into EmbedFileInfo
* @param {mw.mmv.model.Image} imageInfo
* @param {string} [siteName]
* @param {string} [caption]
* @return {mw.mmv.model.EmbedFileInfo}
*/
EmbedFileInfo.fromImageInfo = function ( imageInfo, siteName, caption ) {
var title = imageInfo.title,
src = imageInfo.url,
url = imageInfo.descriptionUrl,
license = imageInfo.license && imageInfo.license.internalName,
author = imageInfo.author,
source = imageInfo.source;
return EmbedFileInfo.fromHtml( title, src, url, siteName, license, author, source, caption );
};
/**
* Turns a jQuery object into a plaintext/HTML pair
* @param $jq
* @return {{plain: string, html:string}|null}
*/
EmbedFileInfo.jqueryToObject = function ( $jq ) {
if ( $jq && $jq.length ) {
return {
plain: $jq.text(),
html: $jq.get( 0 ).outerHTML
};
} else {
return null;
}
};
mw.mmv.model.EmbedFileInfo = EmbedFileInfo;
}( mediaWiki, jQuery ) );
}( mediaWiki ) );

View file

@ -220,14 +220,14 @@
*/
Image.parseExtmeta = function ( data, type ) {
var value = data && data.value;
if ( !value ) {
if ( value === null || value === undefined ) {
return undefined;
} else if ( type === 'string' ) {
return value.toString();
} else if ( type === 'float' ) {
return parseFloat( value );
} else if ( type === 'list' ) {
return value.split( '|' );
return value === '' ? [] : value.split( '|' );
} else {
throw 'mw.mmv.model.Image.parseExtmeta: unknown type';
}

View file

@ -55,6 +55,24 @@
}
LP = License.prototype;
/**
* Returns the short name of the license:
* - if we have interface messages for this license (basically just CC and PD), use those
* - otherwise use the short name from the license template (might or might not be translated
* still, depending on how the template is set up)
* @return {string}
* FIXME a model should not depend on an i18n class. We should probably use view models.
*/
LP.getShortName = function() {
var message = 'multimediaviewer-license-' + ( this.internalName || '' );
if ( mw.messages.exists( message ) ) {
return mw.message( message ).text();
} else {
return this.shortName;
}
};
/**
* Returns a short HTML representation of the license.
* @return {string}
@ -65,7 +83,7 @@
$( '<a>' ).prop( {
href: this.deedUrl,
title: this.longName
} ).text( this.shortName )
} ).text( this.getShortName() )
);
} else {
return this.shortName;

View file

@ -458,12 +458,11 @@
/**
* Sets up the file reuse data in the DOM
* @param {mw.mmv.model.Image} image
* @param {string} siteName
* @param {mw.mmv.model.Repo} repo
* @param {string} caption
*/
MPP.setFileReuseData = function ( image, siteName, caption ) {
this.fileReuse.set( image,
mw.mmv.model.EmbedFileInfo.fromImageInfo( image, siteName, caption ) );
MPP.setFileReuseData = function ( image, repo, caption ) {
this.fileReuse.set( image, repo, caption );
};
/**
@ -689,7 +688,7 @@
}
// File reuse steals a bunch of information from the DOM, so do it last
this.setFileReuseData( imageData, repoData.displayName, image.caption );
this.setFileReuseData( imageData, repoData, image.caption );
};
/**

View file

@ -158,11 +158,12 @@
/**
* Sets data needed by contaned tabs and makes dialog launch link visible.
* @param {mw.mmv.model.Image} image
* @param {mw.mmv.model.EmbedFileInfo} info
* @param {mw.mmv.model.Repo} repo
* @param {string} caption
*/
DP.set = function ( image, info ) {
DP.set = function ( image, repo, caption) {
this.tabs.share.set( image );
this.tabs.embed.set( image, info );
this.tabs.embed.set( image, repo, caption );
this.$reuseLink.removeClass( 'empty' );
};

View file

@ -61,7 +61,7 @@
* Currently selected size menu.
* @property {OO.ui.MenuWidget}
*/
this.currentSizeMenu = this.embedWtSizeSwitch.getMenu();
this.currentSizeMenu = this.embedSizeSwitchWikitext.getMenu();
}
oo.inheritClass( Embed, mw.mmv.ui.reuse.Tab );
EP = Embed.prototype;
@ -89,7 +89,7 @@
.prop( 'placeholder', mw.message( 'multimediaviewer-reuse-loading-placeholder' ).text() );
this.embedTextWikitext = new oo.ui.TextInputWidget( {
classes: [ 'mw-mlb-embed-text-wt', 'active' ],
classes: [ 'mw-mlb-embed-text-wikitext', 'active' ],
multiline: true,
readOnly: true
} );
@ -117,8 +117,8 @@
classes: [ 'mw-mlb-embed-select' ]
} );
wikitextButtonOption = new oo.ui.ButtonOptionWidget( 'wt', {
label: mw.message( 'multimediaviewer-embed-wt' ).text(),
wikitextButtonOption = new oo.ui.ButtonOptionWidget( 'wikitext', {
label: mw.message( 'multimediaviewer-embed-wt' ).text()
} );
htmlButtonOption = new oo.ui.ButtonOptionWidget( 'html', {
label: mw.message( 'multimediaviewer-embed-html' ).text()
@ -135,7 +135,6 @@
// Default to 'wikitext'
this.embedSwitch.selectItem( wikitextButtonOption );
};
/**
@ -149,19 +148,19 @@
.text( mw.message( 'multimediaviewer-embed-dimensions', 0, 0 ).text() ).get( 0 ).outerHTML;
// Wikitext sizes pulldown menu
this.embedWtSizeSwitch = new oo.ui.InlineMenuWidget( {
this.embedSizeSwitchWikitext = new oo.ui.InlineMenuWidget( {
classes: [ 'mw-mlb-embed-size', 'active' ]
} );
this.embedWtSizeChoices = {};
this.embedSizeChoicesWikitext = {};
this.embedWtSizeSwitch.getMenu().addItems( [
this.embedWtSizeChoices.default = new oo.ui.MenuItemWidget( { name: 'default' }, {
this.embedSizeSwitchWikitext.getMenu().addItems( [
this.embedSizeChoicesWikitext.default = new oo.ui.MenuItemWidget( { name: 'default' }, {
label: mw.message( 'multimediaviewer-default-embed-dimensions' ).text(),
autoFitLabel: false
} ),
this.embedWtSizeChoices.small = new oo.ui.MenuItemWidget( {
this.embedSizeChoicesWikitext.small = new oo.ui.MenuItemWidget( {
name: 'small',
height: null,
width: null
@ -171,7 +170,7 @@
autoFitLabel: false
} ),
this.embedWtSizeChoices.medium = new oo.ui.MenuItemWidget( {
this.embedSizeChoicesWikitext.medium = new oo.ui.MenuItemWidget( {
name: 'medium',
height: null,
width: null
@ -181,7 +180,7 @@
autoFitLabel: false
} ),
this.embedWtSizeChoices.large = new oo.ui.MenuItemWidget( {
this.embedSizeChoicesWikitext.large = new oo.ui.MenuItemWidget( {
name: 'large',
height: null,
width: null
@ -192,16 +191,16 @@
} )
] );
this.embedWtSizeSwitch.getMenu().selectItem( this.embedWtSizeChoices.default );
this.embedSizeSwitchWikitext.getMenu().selectItem( this.embedSizeChoicesWikitext.default );
// Html sizes pulldown menu
this.embedHtmlSizeSwitch = new oo.ui.InlineMenuWidget( {
this.embedSizeSwitchHtml = new oo.ui.InlineMenuWidget( {
classes: [ 'mw-mlb-embed-size' ]
} );
this.embedHtmlSizeChoices = {};
this.embedHtmlSizeSwitch.getMenu().addItems( [
this.embedSizeSwitchHtml.getMenu().addItems( [
this.embedHtmlSizeChoices.small = new oo.ui.MenuItemWidget( {
name: 'small',
height: null,
@ -243,12 +242,12 @@
} )
] );
this.embedHtmlSizeSwitch.getMenu().selectItem( this.embedHtmlSizeChoices.small );
this.embedSizeSwitchHtml.getMenu().selectItem( this.embedHtmlSizeChoices.small );
$( '<p>' )
.append(
this.embedHtmlSizeSwitch.$element,
this.embedWtSizeSwitch.$element
this.embedSizeSwitchHtml.$element,
this.embedSizeSwitchWikitext.$element
)
.appendTo( $container );
};
@ -275,8 +274,8 @@
this.proxiedHandleSizeSwitch = this.proxiedHandleSizeSwitch || $.proxy( this.handleSizeSwitch, this );
// Register handlers for switching between file sizes
this.embedHtmlSizeSwitch.getMenu().on( 'select', this.proxiedHandleSizeSwitch );
this.embedWtSizeSwitch.getMenu().on( 'select', this.proxiedHandleSizeSwitch );
this.embedSizeSwitchHtml.getMenu().on( 'select', this.proxiedHandleSizeSwitch );
this.embedSizeSwitchWikitext.getMenu().on( 'select', this.proxiedHandleSizeSwitch );
};
/**
@ -289,8 +288,8 @@
this.embedTextWikitext.offDOMEvent( 'focus mousedown click' );
this.embedSwitch.off( 'select' );
// the noop is needed for some tests which call unattach before calling attach.
this.embedHtmlSizeSwitch.getMenu().off( 'select', this.proxiedHandleSizeSwitch || $.noop );
this.embedWtSizeSwitch.getMenu().off( 'select', this.proxiedHandleSizeSwitch || $.noop );
this.embedSizeSwitchHtml.getMenu().off( 'select', this.proxiedHandleSizeSwitch || $.noop );
this.embedSizeSwitchWikitext.getMenu().off( 'select', this.proxiedHandleSizeSwitch || $.noop );
};
/**
@ -310,21 +309,21 @@
if ( value === 'html' ) {
this.$currentMainEmbedText = this.embedTextHtml.$element;
this.currentSizeMenu = this.embedHtmlSizeSwitch.getMenu();
this.embedWtSizeSwitch.getMenu().hide();
} else if ( value === 'wt' ) {
this.currentSizeMenu = this.embedSizeSwitchHtml.getMenu();
this.embedSizeSwitchWikitext.getMenu().hide();
} else if ( value === 'wikitext' ) {
this.$currentMainEmbedText = this.embedTextWikitext.$element;
this.currentSizeMenu = this.embedWtSizeSwitch.getMenu();
this.embedHtmlSizeSwitch.getMenu().hide();
this.currentSizeMenu = this.embedSizeSwitchWikitext.getMenu();
this.embedSizeSwitchHtml.getMenu().hide();
}
this.embedTextHtml.$element
.add( this.embedHtmlSizeSwitch.$element )
.add( this.embedSizeSwitchHtml.$element )
.toggleClass( 'active', value === 'html' );
this.embedTextWikitext.$element
.add( this.embedWtSizeSwitch.$element )
.toggleClass( 'active', value === 'wt' );
.add( this.embedSizeSwitchWikitext.$element )
.toggleClass( 'active', value === 'wikitext' );
this.select();
@ -347,10 +346,10 @@
switch ( currentItem.getData() ) {
case 'html':
this.setThumbnailURL( {}, width, height );
this.updateEmbedHtml( {}, width, height );
break;
case 'wt':
this.updateWtEmbedText( width );
case 'wikitext':
this.updateEmbedWikitext( width );
break;
}
@ -358,25 +357,25 @@
};
/**
* Sets the value of the thumbnail URL to use for the HTML embed text.
* Sets the HTML embed text.
*
* Assumes that the set method has already been called.
* Assumes that the set() method has already been called to update this.embedFileInfo
* @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
*/
EP.setThumbnailURL = function ( thumbnail, width, height ) {
EP.updateEmbedHtml = function ( thumbnail, width, height ) {
var src;
if ( !this.embedFileInfo ) {
return;
}
src = thumbnail.url || this.embedFileInfo.src;
src = thumbnail.url || this.embedFileInfo.imageInfo.url;
// If the image dimension requested are "large", use the current image url
if ( width > EP.LARGE_IMAGE_WIDTH_THRESHOLD || height > EP.LARGE_IMAGE_HEIGHT_THRESHOLD ) {
src = this.embedFileInfo.src;
if ( width > EP.LARGE_IMAGE_WIDTH_THRESHOLD || height > EP.LARGE_IMAGE_HEIGHT_THRESHOLD ) {
src = this.embedFileInfo.imageInfo.url;
}
this.embedTextHtml.setValue(
@ -389,16 +388,14 @@
* Assumes that the set method has already been called.
* @param {number} width
*/
EP.updateWtEmbedText = function ( width ) {
EP.updateEmbedWikitext = function ( width ) {
if ( !this.embedFileInfo ) {
return;
}
var title = this.embedFileInfo.title,
caption = this.embedFileInfo.caption;
this.embedTextWikitext.setValue( this.formatter.getThumbnailWikitext(
title, width, caption ? caption.plain : title.getNameText() ) );
this.embedTextWikitext.setValue(
this.formatter.getThumbnailWikitextFromEmbedFileInfo( this.embedFileInfo, width )
);
};
/**
@ -532,24 +529,23 @@
* Sets the data on the element.
*
* @param {mw.mmv.model.Image} image
* @param {mw.mmv.model.EmbedFileInfo} embedFileInfo
* @param {mw.mmv.model.Repo} repo
* @param {string} caption
*/
EP.set = function ( image, embedFileInfo ) {
EP.set = function ( image, repo, caption ) {
var embed = this,
htmlSizeSwitch = this.embedHtmlSizeSwitch.getMenu(),
htmlSizeOptions = htmlSizeSwitch.getItems(),
wtSizeSwitch = this.embedWtSizeSwitch.getMenu(),
wtSizeOptions = wtSizeSwitch.getItems(),
sizes = this.getSizeOptions( image.width, image.height );
this.embedFileInfo = embedFileInfo;
this.embedFileInfo = new mw.mmv.model.EmbedFileInfo( image, repo, caption );
this.updateMenuOptions( sizes.html, htmlSizeOptions );
this.updateMenuOptions( sizes.wikitext, wtSizeOptions );
this.updateMenuOptions( sizes.html,
this.embedSizeSwitchHtml.getMenu().getItems() );
this.updateMenuOptions( sizes.wikitext,
this.embedSizeSwitchWikitext.getMenu().getItems() );
this.currentSizeMenu.selectItem( this.currentSizeMenu.getSelectedItem() );
this.getThumbnailUrlPromise().done( function ( thumbnail ) {
embed.setThumbnailURL( thumbnail );
embed.updateEmbedHtml( thumbnail );
embed.select();
} );
};
@ -625,8 +621,8 @@
this.embedTextHtml.setValue( '' );
this.embedTextWikitext.setValue( '' );
this.embedHtmlSizeSwitch.getMenu().hide();
this.embedWtSizeSwitch.getMenu().hide();
this.embedSizeSwitchHtml.getMenu().hide();
this.embedSizeSwitchWikitext.getMenu().hide();
};
/**

View file

@ -5,7 +5,7 @@
.mw-mlb-reuse-pane {
.mw-mlb-embed-text-html,
.mw-mlb-embed-text-wt {
.mw-mlb-embed-text-wikitext {
display: none;
width: auto;

View file

@ -1,96 +1,86 @@
( function ( mw, $ ) {
( function ( mw ) {
QUnit.module( 'mmv.EmbedFileFormatter', QUnit.newMwEnvironment() );
function createEmbedFileInfo( options ) {
var license = options.licenseShortName ? new mw.mmv.model.License( options.licenseShortName,
options.licenseInternalName, options.licenseLongName, options.licenseUrl ) : undefined,
imageInfo = new mw.mmv.model.Image( options.title, undefined, undefined, undefined,
undefined, options.imgUrl, options.filePageUrl, 'repo', undefined, undefined,
undefined, undefined, options.source, options.author, license ),
repoInfo = { displayName: options.siteName, getSiteLink:
function () { return options.siteUrl; } };
return new mw.mmv.model.EmbedFileInfo( imageInfo, repoInfo, options.caption );
}
QUnit.test( 'EmbedFileFormatter constructor sanity check', 1, function ( assert ) {
var formatter = new mw.mmv.EmbedFileFormatter();
assert.ok( formatter, 'constructor with no argument works');
} );
QUnit.test( 'getBylines():', 7, function ( assert ) {
QUnit.test( 'getByline():', 4, function ( assert ) {
var formatter = new mw.mmv.EmbedFileFormatter(),
$author = $( '<span class="mw-mlb-author">Homer</span>' ),
$source = $( '<span class="mw-mlb-source">Iliad</span>' ),
author = {
plain: $author.text(),
html: $author.get( 0 ).outerHTML
},
source = {
plain: $source.text(),
html: $source.get( 0 ).outerHTML
},
bylines;
author = '<span class="mw-mlb-author">Homer</span>',
source = '<span class="mw-mlb-source">Iliad</span>',
byline;
// Works with no arguments
bylines = formatter.getBylines();
assert.deepEqual( bylines, {}, 'No argument case handled correctly.' );
byline = formatter.getByline();
assert.strictEqual( byline, undefined, 'No argument case handled correctly.' );
// Author and source present
bylines = formatter.getBylines( author, source );
assert.ok( bylines.plain.match( /Homer|Iliad/ ), 'Author and source found in plain bylines' );
assert.ok( bylines.html.match ( /Homer|Iliad/ ), 'Author and source found in html bylines' );
byline = formatter.getByline( author, source );
assert.ok( byline.match ( /Homer|Iliad/ ), 'Author and source found in bylines' );
// Only author present
bylines = formatter.getBylines( author );
assert.ok( bylines.plain.match( /^Homer$/ ), 'Author found in plain bylines.' );
assert.ok( bylines.html.match ( /Homer/ ), 'Author found in html bylines.' );
byline = formatter.getByline( author );
assert.ok( byline.match ( /Homer/ ), 'Author found in bylines.' );
// Only source present
bylines = formatter.getBylines( undefined, source );
assert.ok( bylines.plain.match( /^Iliad$/ ), 'Source found in plain bylines.' );
assert.ok( bylines.html.match ( /Iliad/ ), 'Source found in html bylines.' );
byline = formatter.getByline( undefined, source );
assert.ok( byline.match( /Iliad/ ), 'Source found in bylines.' );
} );
QUnit.test( 'getThumbnailHtml():', 72, function ( assert ) {
QUnit.test( 'getSiteLink():', 2, function ( assert ) {
var repoInfo = new mw.mmv.model.Repo( 'Wikipedia', '//wikipedia.org/favicon.ico', true ),
info = new mw.mmv.model.EmbedFileInfo( {}, repoInfo ),
formatter = new mw.mmv.EmbedFileFormatter(),
siteUrl = repoInfo.getSiteLink(),
siteLink = formatter.getSiteLink( info );
assert.ok( siteLink.match( 'Wikipedia' ), 'Site name is present in site link' );
assert.ok( siteLink.match( siteUrl ), 'Site URL is present in site link' );
} );
QUnit.test( 'getThumbnailHtml():', 36, function ( assert ) {
var formatter = new mw.mmv.EmbedFileFormatter(),
title = 'Music Room',
titleText = 'Music Room',
title = mw.Title.newFromText( titleText ),
imgUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
filePageUrl = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg',
siteName = 'Site Name',
$license = $( '<a class="mw-mlb-license empty" href="/wiki/File:License.txt">Public License</a>' ),
$author = $( '<span class="mw-mlb-author">Homer</span>' ),
$source = $( '<span class="mw-mlb-source">Iliad</span>' ),
license = {
plain: $license && $license.text(),
html: $license && $license.get( 0 ).outerHTML
},
author = {
plain: $author && $author.text(),
html: $author && $author.get( 0 ).outerHTML
},
source = {
plain: $source && $source.text(),
html: $source && $source.get( 0 ).outerHTML
},
siteUrl = '//site.url/',
licenseShortName = 'Public License',
licenseInternalName = '-',
licenseLongName = 'Public Domain, copyrights have lapsed',
licenseUrl = '//example.com/pd',
author = '<span class="mw-mlb-author">Homer</span>',
source = '<span class="mw-mlb-source">Iliad</span>',
thumbUrl = 'https://upload.wikimedia.org/wikipedia/thumb/Foobar.jpg',
width = 700,
height = 500,
info,
generatedHtml;
// No bylines, no license and no site
info = new mw.mmv.model.EmbedFileInfo( new mw.Title( title ), imgUrl, filePageUrl );
generatedHtml = formatter.getThumbnailHtml( info, thumbUrl );
assert.ok( generatedHtml.match( title ), 'Title appears in generated HTML.');
assert.ok( generatedHtml.match( filePageUrl ), 'Page url appears in generated HTML.' );
assert.ok( generatedHtml.match( thumbUrl ), 'Thumbnail url appears in generated HTML' );
assert.ok( ! generatedHtml.match( siteName ), 'Site name should not appear in generated HTML' );
assert.ok( ! generatedHtml.match( 'Public License' ), 'License should not appear in generated HTML' );
assert.ok( ! generatedHtml.match( 'Homer' ), 'Author should not appear in generated HTML' );
assert.ok( ! generatedHtml.match( 'Iliad' ), 'Source should not appear in generated HTML' );
assert.ok( ! generatedHtml.match( width ), 'Width should not appear in generated HTML' );
assert.ok( ! generatedHtml.match( height ), 'Height should not appear in generated HTML' );
// Bylines, license and site
info = new mw.mmv.model.EmbedFileInfo( new mw.Title( title ), imgUrl, filePageUrl,
siteName, license, author, source );
info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl,
siteName: siteName, siteUrl: siteUrl, licenseShortName: licenseShortName,
licenseInternalName: licenseInternalName, licenseLongName: licenseLongName,
licenseUrl: licenseUrl, author: author, source: source } );
generatedHtml = formatter.getThumbnailHtml( info, thumbUrl, width, height);
assert.ok( generatedHtml.match( title ), 'Title appears in generated HTML.' );
assert.ok( generatedHtml.match( titleText ), 'Title appears in generated HTML.' );
assert.ok( generatedHtml.match( filePageUrl ), 'Page url appears in generated HTML.' );
assert.ok( generatedHtml.match( thumbUrl ), 'Thumbnail url appears in generated HTML' );
assert.ok( generatedHtml.match( siteName ), 'Site name appears in generated HTML' );
@ -100,27 +90,13 @@
assert.ok( generatedHtml.match( width ), 'Width appears in generated HTML' );
assert.ok( generatedHtml.match( height ), 'Height appears in generated HTML' );
// Bylines, license and no site
info = new mw.mmv.model.EmbedFileInfo( new mw.Title( title ), imgUrl, filePageUrl,
undefined, license, author, source );
generatedHtml = formatter.getThumbnailHtml( info, thumbUrl, width, height);
assert.ok( generatedHtml.match( title ), 'Title appears in generated HTML.' );
assert.ok( generatedHtml.match( filePageUrl ), 'Page url appears in generated HTML.' );
assert.ok( generatedHtml.match( thumbUrl ), 'Thumbnail url appears in generated HTML' );
assert.ok( ! generatedHtml.match( siteName ), 'Site name should not appear in generated HTML' );
assert.ok( generatedHtml.match( 'Public License' ), 'License appears in generated HTML' );
assert.ok( generatedHtml.match( 'Homer' ), 'Author appears in generated HTML' );
assert.ok( generatedHtml.match( 'Iliad' ), 'Source appears in generated HTML' );
assert.ok( generatedHtml.match( width ), 'Width appears in generated HTML' );
assert.ok( generatedHtml.match( height ), 'Height appears in generated HTML' );
// Bylines, no license and site
info = new mw.mmv.model.EmbedFileInfo( new mw.Title( title ), imgUrl, filePageUrl,
siteName, undefined, author, source );
info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl,
siteName: siteName, siteUrl: siteUrl,
author: author, source: source } );
generatedHtml = formatter.getThumbnailHtml( info, thumbUrl, width, height);
assert.ok( generatedHtml.match( title ), 'Title appears in generated HTML.' );
assert.ok( generatedHtml.match( titleText ), 'Title appears in generated HTML.' );
assert.ok( generatedHtml.match( filePageUrl ), 'Page url appears in generated HTML.' );
assert.ok( generatedHtml.match( thumbUrl ), 'Thumbnail url appears in generated HTML' );
assert.ok( generatedHtml.match( siteName ), 'Site name appears in generated HTML' );
@ -130,27 +106,14 @@
assert.ok( generatedHtml.match( width ), 'Width appears in generated HTML' );
assert.ok( generatedHtml.match( height ), 'Height appears in generated HTML' );
// Bylines, no license and no site
info = new mw.mmv.model.EmbedFileInfo( new mw.Title( title ), imgUrl, filePageUrl,
undefined, undefined, author, source );
generatedHtml = formatter.getThumbnailHtml( info, thumbUrl, width, height);
assert.ok( generatedHtml.match( title ), 'Title appears in generated HTML.' );
assert.ok( generatedHtml.match( filePageUrl ), 'Page url appears in generated HTML.' );
assert.ok( generatedHtml.match( thumbUrl ), 'Thumbnail url appears in generated HTML' );
assert.ok( ! generatedHtml.match( siteName ), 'Site name should not appear in generated HTML' );
assert.ok( ! generatedHtml.match( 'Public License' ), 'License should not appear in generated HTML' );
assert.ok( generatedHtml.match( 'Homer' ), 'Author appears in generated HTML' );
assert.ok( generatedHtml.match( 'Iliad' ), 'Source appears in generated HTML' );
assert.ok( generatedHtml.match( width ), 'Width appears in generated HTML');
assert.ok( generatedHtml.match( height ), 'Height appears in generated HTML');
// No bylines, license and site
info = new mw.mmv.model.EmbedFileInfo( new mw.Title( title ), imgUrl, filePageUrl,
siteName, license );
info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl,
siteName: siteName, siteUrl: siteUrl, licenseShortName: licenseShortName,
licenseInternalName: licenseInternalName, licenseLongName: licenseLongName,
licenseUrl: licenseUrl } );
generatedHtml = formatter.getThumbnailHtml( info, thumbUrl, width, height);
assert.ok( generatedHtml.match( title ), 'Title appears in generated HTML.');
assert.ok( generatedHtml.match( titleText ), 'Title appears in generated HTML.');
assert.ok( generatedHtml.match( filePageUrl ), 'Page url appears in generated HTML.' );
assert.ok( generatedHtml.match( thumbUrl ), 'Thumbnail url appears in generated HTML' );
assert.ok( generatedHtml.match( siteName ), 'Site name appears in generated HTML' );
@ -160,27 +123,12 @@
assert.ok( generatedHtml.match( width ), 'Width appears in generated HTML' );
assert.ok( generatedHtml.match( height ), 'Height appears in generated HTML' );
// No bylines, license and no site
info = new mw.mmv.model.EmbedFileInfo( new mw.Title( title ), imgUrl, filePageUrl,
undefined, license );
generatedHtml = formatter.getThumbnailHtml( info, thumbUrl, width, height);
assert.ok( generatedHtml.match( title ), 'Title appears in generated HTML.');
assert.ok( generatedHtml.match( filePageUrl ), 'Page url appears in generated HTML.' );
assert.ok( generatedHtml.match( thumbUrl ), 'Thumbnail url appears in generated HTML' );
assert.ok( ! generatedHtml.match( siteName ), 'Site name should not appear in generated HTML' );
assert.ok( generatedHtml.match( 'Public License' ), 'License appears in generated HTML' );
assert.ok( ! generatedHtml.match( 'Homer' ), 'Author should not appear in generated HTML' );
assert.ok( ! generatedHtml.match( 'Iliad' ), 'Source should not appear in generated HTML' );
assert.ok( generatedHtml.match( width ), 'Width appears in generated HTML' );
assert.ok( generatedHtml.match( height ), 'Height appears in generated HTML' );
// No bylines, no license and site
info = new mw.mmv.model.EmbedFileInfo( new mw.Title( title ), imgUrl, filePageUrl,
siteName );
info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl,
siteName: siteName, siteUrl: siteUrl } );
generatedHtml = formatter.getThumbnailHtml( info, thumbUrl, width, height);
assert.ok( generatedHtml.match( title ), 'Title appears in generated HTML.');
assert.ok( generatedHtml.match( titleText ), 'Title appears in generated HTML.');
assert.ok( generatedHtml.match( filePageUrl ), 'Page url appears in generated HTML.' );
assert.ok( generatedHtml.match( thumbUrl ), 'Thumbnail url appears in generated HTML' );
assert.ok( generatedHtml.match( siteName ), 'Site name should appear in generated HTML' );
@ -203,8 +151,8 @@
wikitext;
// Title, width and caption
info = new mw.mmv.model.EmbedFileInfo.fromHtml( title, imgUrl, filePageUrl, undefined,
undefined, undefined, undefined, caption );
info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl,
caption: caption } );
wikitext = formatter.getThumbnailWikitextFromEmbedFileInfo( info, width );
assert.strictEqual(
@ -213,7 +161,7 @@
'Wikitext generated correctly.' );
// Title, width and no caption
info = new mw.mmv.model.EmbedFileInfo.fromHtml( title, imgUrl, filePageUrl );
info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl } );
wikitext = formatter.getThumbnailWikitextFromEmbedFileInfo( info , width );
assert.strictEqual(
@ -222,7 +170,7 @@
'Wikitext generated correctly.' );
// Title, no width and no caption
info = new mw.mmv.model.EmbedFileInfo.fromHtml( title, imgUrl, filePageUrl );
info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl } );
wikitext = formatter.getThumbnailWikitextFromEmbedFileInfo( info );
assert.strictEqual(
@ -231,4 +179,4 @@
'Wikitext generated correctly.' );
} );
}( mediaWiki, jQuery ) );
}( mediaWiki ) );

View file

@ -0,0 +1,38 @@
/*
* This file is part of the MediaWiki extension MediaViewer.
*
* MediaViewer 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.
*
* MediaViewer 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 MediaViewer. If not, see <http://www.gnu.org/licenses/>.
*/
( function( mw ) {
QUnit.module( 'mmv.model.EmbedFileInfo', QUnit.newMwEnvironment() );
QUnit.test( 'EmbedFileInfo constructor sanity check', 4, function ( assert ) {
var imageInfo = {},
repoInfo = {},
caption = 'Foo',
embedFileInfo = new mw.mmv.model.EmbedFileInfo( imageInfo, repoInfo, caption );
assert.strictEqual( embedFileInfo.imageInfo, imageInfo, 'ImageInfo is set correctly' );
assert.strictEqual( embedFileInfo.repoInfo, repoInfo, 'ImageInfo is set correctly' );
assert.strictEqual( embedFileInfo.caption, caption, 'Caption is set correctly' );
try {
embedFileInfo = new mw.mmv.model.EmbedFileInfo( {} );
} catch (e) {
assert.ok( e, 'Exception is thrown when parameters are missing' );
}
} );
}( mediaWiki ) );

View file

@ -89,7 +89,8 @@
} );
QUnit.test( 'parseExtmeta()', 8, function ( assert ) {
var stringData = { value: 'foo' },
var Image = mw.mmv.model.Image,
stringData = { value: 'foo' },
floatData = { value: 1.23 },
floatStringData = { value: '1.23' },
listDataEmpty = {value: '' },

View file

@ -47,6 +47,36 @@
}
} );
QUnit.test( 'getShortName()', 3, function( assert ) {
var existingMessageKey = 'Internal name that does exist',
nonExistingMessageKey = 'Internal name that does not exist',
license1 = new mw.mmv.model.License( 'Shortname' ),
license2 = new mw.mmv.model.License( 'Shortname', nonExistingMessageKey ),
license3 = new mw.mmv.model.License( 'Shortname', existingMessageKey ),
oldMwMessage = mw.message,
oldMwMessagesExists = mw.messages.exists;
mw.message = function ( name ) {
return name === 'multimediaviewer-license-' + existingMessageKey
? { text: function () { return 'Translated name'; } }
: oldMwMessage.apply( mw, arguments );
};
mw.messages.exists = function ( name ) {
return name === 'multimediaviewer-license-' + existingMessageKey
? true : oldMwMessagesExists.apply( mw.messages, arguments );
};
assert.strictEqual( license1.getShortName(), 'Shortname',
'Short name is returned when there is no translated name' );
assert.strictEqual( license2.getShortName(), 'Shortname',
'Short name is returned when translated name is missing' );
assert.strictEqual( license3.getShortName(), 'Translated name',
'Translated name is returned when it exists' );
mw.message = oldMwMessage;
mw.messages.exists = oldMwMessagesExists;
} );
QUnit.test( 'getShortLink()', 6, function( assert ) {
var $html,
license1 = new mw.mmv.model.License( 'lorem ipsum' ),

View file

@ -15,7 +15,7 @@
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
*/
( function ( mw, $ ) {
( function ( mw ) {
QUnit.module( 'mmv.model', QUnit.newMwEnvironment() );
QUnit.test( 'File usage constructor sanity check', 5, function ( assert ) {
@ -103,47 +103,4 @@
}
} );
QUnit.test( 'EmbedFileInfo constructor sanity check', 9, function ( assert ) {
var title = mw.Title.newFromText( 'File:Foobar.jpg' ),
src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
url = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg',
siteName = 'Name of the web site',
$license = $( '<a class="mw-mlb-license empty" href="/wiki/File:License.txt">Public License</a>' ),
$author = $( '<span class="mw-mlb-author">Homer</span>' ),
$source = $( '<span class="mw-mlb-source">Iliad</span>' ),
license = {
plain: $license && $license.text(),
html: $license && $license.html()
},
author = {
plain: $author && $author.text(),
html: $author && $author.get( 0 ).outerHTML
},
source = {
plain: $source && $source.text(),
html: $source && $source.get( 0 ).outerHTML
},
caption = {
plain: 'Plain image caption',
html: '<b>HTML imgae caption</b>'
},
embedFileInfo = new mw.mmv.model.EmbedFileInfo(
title, src, url, siteName, license, author, source, caption );
assert.strictEqual( embedFileInfo.title, title, 'Title is set correctly' );
assert.strictEqual( embedFileInfo.src, src, 'Src is set correctly' );
assert.strictEqual( embedFileInfo.url, url, 'Url is set correctly' );
assert.strictEqual( embedFileInfo.siteName, siteName, 'Site name is set correctly' );
assert.strictEqual( embedFileInfo.license, license, 'License is set correctly' );
assert.strictEqual( embedFileInfo.author, author, 'Author is set correctly' );
assert.strictEqual( embedFileInfo.source, source, 'Source is set correctly' );
assert.strictEqual( embedFileInfo.caption, caption, 'Caption is set correctly' );
try {
embedFileInfo = new mw.mmv.model.EmbedFileInfo( title );
} catch (e) {
assert.ok( e, 'Exception is thrown when parameters are missing' );
}
} );
}( mediaWiki, jQuery ) );
}( mediaWiki ) );

View file

@ -28,8 +28,8 @@
assert.ok( embed.embedTextHtml, 'Html snipped text area created.' );
assert.ok( embed.embedTextWikitext, 'Wikitext snipped text area created.' );
assert.ok( embed.embedSwitch, 'Snipped selection buttons created.' );
assert.ok( embed.embedWtSizeSwitch, 'Size selection menu for wikitext created.' );
assert.ok( embed.embedHtmlSizeSwitch, 'Size selection menu for html created.' );
assert.ok( embed.embedSizeSwitchWikitext, 'Size selection menu for wikitext created.' );
assert.ok( embed.embedSizeSwitchHtml, 'Size selection menu for html created.' );
assert.strictEqual( embed.$currentMainEmbedText.length, 1, 'Size selection menu for html created.' );
assert.ok( embed.currentSizeMenu, 'Size selection menu for html created.' );
} );
@ -42,10 +42,10 @@
// deselect items
embed.embedSwitch.selectItem();
embed.setThumbnailURL = function() {
embed.updateEmbedHtml = function() {
assert.ok( false, 'No item selected, this should not have been called.' );
};
embed.updateWtEmbedText = function() {
embed.updateEmbedWikitext = function() {
assert.ok( false, 'No item selected, this should not have been called.' );
};
@ -60,12 +60,12 @@
embed.embedSwitch.getSelectedItem = function() {
return { getData: function() { return 'html'; } };
};
embed.setThumbnailURL = function( thumb, w, h ) {
embed.updateEmbedHtml = function( thumb, w, h ) {
assert.strictEqual( thumb.url, undefined, 'Empty thumbnail passed.' );
assert.strictEqual( w, width, 'Correct width passed.' );
assert.strictEqual( h, height, 'Correct height passed.' );
};
embed.updateWtEmbedText = function () {
embed.updateEmbedWikitext = function () {
assert.ok( false, 'Dealing with HTML menu, this should not have been called.' );
};
embed.select = function( ) {
@ -81,12 +81,12 @@
height = 20;
embed.embedSwitch.getSelectedItem = function() {
return { getData: function() { return 'wt'; } };
return { getData: function() { return 'wikitext'; } };
};
embed.setThumbnailURL = function() {
embed.updateEmbedHtml = function() {
assert.ok( false, 'Dealing with wikitext menu, this should not have been called.' );
};
embed.updateWtEmbedText = function( w ) {
embed.updateEmbedWikitext = function( w ) {
assert.strictEqual( w, width, 'Correct width passed.' );
};
embed.select = function( ) {
@ -96,7 +96,7 @@
embed.changeSize( width, height );
} );
QUnit.test( 'setThumbnailURL(): Do nothing if set() not called before.', 0, function ( assert ) {
QUnit.test( 'updateEmbedHtml(): Do nothing if set() not called before.', 0, function ( assert ) {
var embed = new mw.mmv.ui.reuse.Embed( $qf ),
width = 10,
height = 20;
@ -104,71 +104,70 @@
embed.formatter.getThumbnailHtml = function() {
assert.ok( false, 'formatter.getThumbnailHtml() should not have been called.' );
};
embed.setThumbnailURL( {}, width, height );
embed.updateEmbedHtml( {}, width, height );
} );
QUnit.test( 'setThumbnailURL():', 6, function ( assert ) {
QUnit.test( 'updateEmbedHtml():', 6, function ( assert ) {
var embed = new mw.mmv.ui.reuse.Embed( $qf ),
title = mw.Title.newFromText( 'File:Foobar.jpg' ),
src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
url = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg',
thumbUrl = 'https://upload.wikimedia.org/wikipedia/thumb/Foobar.jpg',
embedFileInfo = new mw.mmv.model.EmbedFileInfo( title, src, url ),
width = 10,
height = 20;
url = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
thumbUrl = 'https://upload.wikimedia.org/wikipedia/thumb/Foobar.jpg',
imageInfo = { url: url },
repoInfo = {},
caption = '-',
info = new mw.mmv.model.EmbedFileInfo( imageInfo, repoInfo, caption ),
width = 10,
height = 20;
embed.set( { width: width, height: height }, embedFileInfo );
embed.set( imageInfo, repoInfo, caption );
// Small image, no thumbnail info is passed
embed.formatter.getThumbnailHtml = function( info, url, w, h ) {
assert.strictEqual( info, embedFileInfo, 'Info passed correctly.' );
assert.strictEqual( url, src, 'Image src passed correctly.' );
embed.formatter.getThumbnailHtml = function( i, u, w, h ) {
assert.deepEqual( i, info, 'Info passed correctly.' );
assert.strictEqual( u, url, 'Image URL passed correctly.' );
assert.strictEqual( w, width, 'Correct width passed.' );
assert.strictEqual( h, height, 'Correct height passed.' );
};
embed.setThumbnailURL( {}, width, height );
embed.updateEmbedHtml( {}, width, height );
// Small image, thumbnail info present
embed.formatter.getThumbnailHtml = function( info, url ) {
assert.strictEqual( url, thumbUrl, 'Image src passed correctly.' );
embed.formatter.getThumbnailHtml = function( i, u ) {
assert.strictEqual( u, thumbUrl, 'Image src passed correctly.' );
};
embed.setThumbnailURL( { url: thumbUrl }, width, height );
embed.updateEmbedHtml( { url: thumbUrl }, width, height );
// Big image, thumbnail info present
embed.formatter.getThumbnailHtml = function( info, url ) {
assert.strictEqual( url, src, 'Image src passed correctly.' );
embed.formatter.getThumbnailHtml = function( i, u ) {
assert.strictEqual( u, url, 'Image src passed correctly.' );
};
width = 1300;
embed.setThumbnailURL( { url: thumbUrl }, width, height );
embed.updateEmbedHtml( { url: thumbUrl }, width, height );
} );
QUnit.test( 'updateWtEmbedText(): Do nothing if set() not called before.', 0, function ( assert ) {
QUnit.test( 'updateEmbedWikitext(): Do nothing if set() not called before.', 0, function ( assert ) {
var embed = new mw.mmv.ui.reuse.Embed( $qf ),
width = 10;
embed.formatter.getThumbnailWikitext = function() {
assert.ok( false, 'formatter.getThumbnailWikitext() should not have been called.');
};
embed.updateWtEmbedText( width );
embed.updateEmbedWikitext( width );
} );
QUnit.test( 'updateWtEmbedText():', 3, function ( assert ) {
QUnit.test( 'updateEmbedWikitext():', 2, function ( assert ) {
var embed = new mw.mmv.ui.reuse.Embed( $qf ),
title = mw.Title.newFromText( 'File:Foobar.jpg' ),
src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
url = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg',
embedFileInfo = new mw.mmv.model.EmbedFileInfo( title, src, url ),
width = 10,
height = 20;
imageInfo = {},
repoInfo = {},
caption = '-',
info = new mw.mmv.model.EmbedFileInfo( imageInfo, repoInfo, caption ),
width = 10;
embed.set( { width: width, height: height }, embedFileInfo );
embed.set( imageInfo, repoInfo, caption );
embed.formatter.getThumbnailWikitext = function( t, w, c ) {
assert.strictEqual( t, title, 'Title passed correctly.');
embed.formatter.getThumbnailWikitextFromEmbedFileInfo = function( i, w ) {
assert.deepEqual( i, info, 'EmbedFileInfo passed correctly.');
assert.strictEqual( w, width, 'Width passed correctly.');
assert.strictEqual( c, title.getNameText(), 'Caption passed correctly.');
};
embed.updateWtEmbedText( width );
embed.updateEmbedWikitext( width );
} );
QUnit.test( 'Size options are correct', 3, function ( assert ) {
@ -242,8 +241,8 @@
embed.getThumbnailUrlPromise = function () {
return $.Deferred().resolve().promise();
};
embed.setThumbnailURL = function () {
assert.ok( true, 'setThumbnailURL() is called after data is collected.' );
embed.updateEmbedHtml = function () {
assert.ok( true, 'updateEmbedHtml() is called after data is collected.' );
};
embed.select = function () {
assert.ok( true, 'select() is called after data is collected.' );
@ -259,7 +258,7 @@
QUnit.test( 'updateMenuOptions():', 6, function ( assert ) {
var embed = new mw.mmv.ui.reuse.Embed( $qf ),
options = embed.embedHtmlSizeSwitch.getMenu().getItems(),
options = embed.embedSizeSwitchHtml.getMenu().getItems(),
width = 700,
height = 500,
sizes = embed.getSizeOptions( width, height ),
@ -278,16 +277,17 @@
QUnit.test( 'empty():', 6, function ( assert ) {
var embed = new mw.mmv.ui.reuse.Embed( $qf ),
title = mw.Title.newFromText( 'File:Foobar.jpg' ),
src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
url = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg',
embedFileInfo = new mw.mmv.model.EmbedFileInfo( title, src, url ),
width = 15,
height = 20;
embed.set( { width: width, height: height }, embedFileInfo );
embed.setThumbnailURL( {}, width, height );
embed.updateWtEmbedText( width );
embed.formatter = {
getThumbnailWikitextFromEmbedFileInfo: function () { return 'wikitext'; },
getThumbnailHtml: function () { return 'html'; }
};
embed.set( {}, {} );
embed.updateEmbedHtml( { url: 'x' }, width, height );
embed.updateEmbedWikitext( width );
assert.notStrictEqual( embed.embedTextHtml.getValue(), '', 'embedTextHtml is not empty.' );
assert.notStrictEqual( embed.embedTextWikitext.getValue(), '', 'embedTextWikitext is not empty.' );
@ -296,8 +296,8 @@
assert.strictEqual( embed.embedTextHtml.getValue(), '', 'embedTextHtml is empty.' );
assert.strictEqual( embed.embedTextWikitext.getValue(), '', 'embedTextWikitext is empty.' );
assert.ok( ! embed.embedHtmlSizeSwitch.getMenu().isVisible(), 'Html size menu should be hidden.' );
assert.ok( ! embed.embedWtSizeSwitch.getMenu().isVisible(), 'Wikitext size menu should be hidden.' );
assert.ok( ! embed.embedSizeSwitchHtml.getMenu().isVisible(), 'Html size menu should be hidden.' );
assert.ok( ! embed.embedSizeSwitchWikitext.getMenu().isVisible(), 'Wikitext size menu should be hidden.' );
} );
QUnit.test( 'attach()/unattach():', 5, function ( assert ) {
@ -325,10 +325,10 @@
embed.embedTextHtml.$element.focus();
embed.embedTextWikitext.$element.focus();
embed.embedSwitch.emit( 'select' );
embed.embedHtmlSizeSwitch.getMenu().emit(
'select', embed.embedHtmlSizeSwitch.getMenu().getSelectedItem() );
embed.embedWtSizeSwitch.getMenu().emit(
'select', embed.embedWtSizeSwitch.getMenu().getSelectedItem() );
embed.embedSizeSwitchHtml.getMenu().emit(
'select', embed.embedSizeSwitchHtml.getMenu().getSelectedItem() );
embed.embedSizeSwitchWikitext.getMenu().emit(
'select', embed.embedSizeSwitchWikitext.getMenu().getSelectedItem() );
embed.selectAllOnEvent = function() {
assert.ok( true, 'selectAllOnEvent was called.' );
@ -346,10 +346,10 @@
embed.embedTextHtml.$element.focus();
embed.embedTextWikitext.$element.focus();
embed.embedSwitch.emit( 'select' );
embed.embedHtmlSizeSwitch.getMenu().emit(
'select', embed.embedHtmlSizeSwitch.getMenu().getSelectedItem() );
embed.embedWtSizeSwitch.getMenu().emit(
'select', embed.embedWtSizeSwitch.getMenu().getSelectedItem() );
embed.embedSizeSwitchHtml.getMenu().emit(
'select', embed.embedSizeSwitchHtml.getMenu().getSelectedItem() );
embed.embedSizeSwitchWikitext.getMenu().emit(
'select', embed.embedSizeSwitchWikitext.getMenu().getSelectedItem() );
// Test the unattach part
embed.selectAllOnEvent = function() {
@ -368,10 +368,10 @@
embed.embedTextHtml.$element.focus();
embed.embedTextWikitext.$element.focus();
embed.embedSwitch.emit( 'select' );
embed.embedHtmlSizeSwitch.getMenu().emit(
'select', embed.embedHtmlSizeSwitch.getMenu().getSelectedItem() );
embed.embedWtSizeSwitch.getMenu().emit(
'select', embed.embedWtSizeSwitch.getMenu().getSelectedItem() );
embed.embedSizeSwitchHtml.getMenu().emit(
'select', embed.embedSizeSwitchHtml.getMenu().getSelectedItem() );
embed.embedSizeSwitchWikitext.getMenu().emit(
'select', embed.embedSizeSwitchWikitext.getMenu().getSelectedItem() );
} );
QUnit.test( 'handleTypeSwitch():', 2, function ( assert ) {
@ -380,12 +380,12 @@
// HTML selected
embed.handleTypeSwitch( { getData: function() { return 'html'; } } );
assert.ok( ! embed.embedWtSizeSwitch.getMenu().isVisible(), 'Wikitext size menu should be hidden.' );
assert.ok( ! embed.embedSizeSwitchWikitext.getMenu().isVisible(), 'Wikitext size menu should be hidden.' );
// Wikitext selected
embed.handleTypeSwitch( { getData: function() { return 'wt'; } } );
embed.handleTypeSwitch( { getData: function() { return 'wikitext'; } } );
assert.ok( ! embed.embedHtmlSizeSwitch.getMenu().isVisible(), 'HTML size menu should be hidden.' );
assert.ok( ! embed.embedSizeSwitchHtml.getMenu().isVisible(), 'HTML size menu should be hidden.' );
} );
}( mediaWiki, jQuery ) );