Make MediaViewer ignore images which are in 'noviewer' class

This includes images where the <img> element has that class
(achievable with [[File:Foo.png|class=noviewer]]) and also those
where some parent element has it.

Change-Id: I666be026828ea9ecb6e8c93d3f5ad1e3c190f81e
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/511
This commit is contained in:
Gergő Tisza 2014-06-11 18:42:29 +00:00
parent 114890cc43
commit 92ea8edbc5
2 changed files with 39 additions and 8 deletions

View file

@ -148,6 +148,20 @@
} }
}; };
/**
* Check if this thumbnail should be handled by MediaViewer
* @param {jQuery} $thumb the thumbnail (an `<img>` element) in question
* @return {boolean}
*/
MMVB.isAllowedThumb = function ( $thumb ) {
// .metadata means this is inside an informational template like {{refimprove}} on enwiki.
// .noviewer means MediaViewer has been specifically disabled for this image
// .noarticletext means we are on an error page for a non-existing article, the image is part of some
// template // FIXME this should be handled by .metadata
return $thumb.closest( '.metadata, .noviewer, .noarticletext' ).length === 0;
};
/** /**
* Processes a thumb * Processes a thumb
* @param {Object} thumb * @param {Object} thumb
@ -168,14 +182,7 @@
return; return;
} }
if ( if ( !bs.isAllowedThumb( $thumb ) ) {
// This is almost certainly an icon for an informational template like
// {{refimprove}} on enwiki.
$thumb.closest( '.metadata' ).length > 0 ||
// This is an article with no text.
$thumb.closest( '.noarticletext' ).length > 0
) {
return; return;
} }

View file

@ -407,4 +407,28 @@
assert.ok( mw.loader.load.called, 'Dependencies should be preloaded if the thumb is hovered long enough' ); assert.ok( mw.loader.load.called, 'Dependencies should be preloaded if the thumb is hovered long enough' );
} ); } );
QUnit.test( 'isAllowedThumb', 5, function ( assert ) {
var $container = $( '<div>' ),
$thumb = $( '<img>' ).appendTo( $container ),
bootstrap = createBootstrap();
assert.ok( bootstrap.isAllowedThumb( $thumb ), 'Normal image in a div is allowed.' );
$container.addClass( 'metadata' );
assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), false, 'Image in a metadata container is disallowed.' );
$container.prop( 'class', '' );
$container.addClass( 'noviewer' );
assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), false, 'Image in a noviewer container is disallowed.' );
$container.prop( 'class', '' );
$container.addClass( 'noarticletext' );
assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), false, 'Image in an empty article is disallowed.' );
$container.prop( 'class', '' );
$thumb.addClass( 'noviewer' );
assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), false, 'Image with a noviewer class is disallowed.' );
} );
}( mediaWiki, jQuery ) ); }( mediaWiki, jQuery ) );