User:Bloody-libu/common.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
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 = $();
 
	var launch = function($c) {
		$('.gallery > li.gallerybox', $c).each(function(i, el) {
			var $el = $(el),
				$th = $el.find('div.thumb').addClass('guThumb'),
				$gu = $('<div>', {
					'class': 'guGU',
					title: "GlobalUsage"
				}),
				$img = $th.find('img:first'),
				$vid = $th.find('video:first'),
				src,
				t;
 
			src = $img.attr('src') || $vid.attr('poster');
			if (!src) return;
			t = mw.libs.commons.titleFromImgSrc(src);
			if (!t) return;
			if($gu.badge) $th.append($gu.badge('?'));
			toQuery[mw.config.get('wgFormattedNamespaces')[6] + ':' + t] = $gu;
			$gus = $gus.add($gu);
 
			return $th.find('img:first');
		});
		_onClick = function() {
			$gus.unbind('click', _onClick);
			var gu = window.mw.libs.GlobalUsage(5, 20, 2, true);
			gu.tipsyGravity = ($('body').hasClass('rtl') ? 'sw' : 'se');
			gu.query(toQuery);
		};
		$gus.click(_onClick);
	};
	mw.hook( 'wikipage.content' ).add( launch );
}(jQuery, mediaWiki));