MediaWiki:Gadget-GlobalUsageUI.js

Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/**
 * GlobalUsage badges for Wikimedia Commons
 * Displays a red label in the corners of
 * gallery boxes
 * This is an UI gadget. It's not a library.
 *
 * @rev 1 (2012-12-12)
 * @author Rillke, 2012
 */
// List the global variables for jsHint-Validation. Please make sure that it passes http://jshint.com/
// Scheme: globalVariable:allowOverwriting[, globalVariable:allowOverwriting][, globalVariable:allowOverwriting]
/*global jQuery:false, mediaWiki:false*/

// Set jsHint-options. You should not set forin or undef to false if your script does not validate.
/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true, strict:true, undef:true, curly:false, browser:true*/

(function($, mw) {
	"use strict";

	var toQuery = {},
		_onClick,
		gus = [],
		$gus;

	var launch = function($c) {
		$c.find('.gallery > li.gallerybox').each(function(i, el) {
			var $el = $(el),
				$th = $el.find('div.thumb').addClass('guThumb'),
				$gu = $('<div>').attr({
					'class': 'guGU',
					title: "GlobalUsage"
				}),
				src = $th.find('img:first, video:first'),
				t = src[0] && mw.Title.newFromImg(src);

			if (!t) return;
			if($gu.badge)$th.append($gu.badge('?', 'bottom'));
			toQuery[t.toText()] = $gu;
			gus = gus.concat($gu.get());

			return $th.find('img:first');
		});
		$gus = $(gus);
		_onClick = function() {
			$gus.off('click', _onClick);
			var gu = mw.libs.GlobalUsage(5, 20, 2, true);
			gu.tipsyGravity = ($('body').hasClass('rtl') ? 'sw' : 'se');
			gu.query(toQuery);
		};
		$gus.on('click', _onClick);
	};
	mw.hook( 'wikipage.content' ).add( launch );
}(jQuery, mediaWiki));