MediaWiki:Gadget-CatTreeGallery.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.
 /**
 * Embed a category as a gallery (with the help of the categorytree extension)
 * @author [[User:Dschwen]], 2013
 */
/* jshint laxcomma: true, smarttabs: true */
/* global mw,$ */

$(function(){
	var d = 300;
	function resize() {
		var t=$(this).css( {width:'',height:''} ), h=t.height(), w=t.width();
		if(!h || !w) return;
		if (!t.hasClass('cg-final')) {
			// need to load an even large thumbnail (wide panos)
			if (h<d) t.parent().append( $('<img>').on('load', resize).addClass('cg-final').attr('src', '/w/thumb.php?w='+Math.ceil(w*d/h)+'&f='+t.parent().parent().attr('href').split(':')[1]) );			
		} else t.parent().find('.cg-initial').remove(); 
		if (w>h) { 
			if (h<d||h>(d+2)) { w=Math.round(w*d/h); h=d; } // tolerance, because we are unlikely to match h exactly for large panos
		} else { h=Math.round(h*d/w); w=d; }
		t.css( {height:h+'px', width:w+'px', position:'absolute', left:Math.round(-(w-d)/2)+'px', top:Math.round(-(h-d)/2)+'px'} );
	}
	$('.categorygallery').each(function(i,e){
		$('.CategoryTreeLabelNs6',e).each(function(j,f){
			$(f).empty()
				.append(
					$('<div></div>')
						.css( {width:d+'px', height:d+'px', overflow:'hidden', display: 'inline-block', position:'relative', margin:'5px', 'box-shadow':'3px 3px 15px rgba(0,0,0,0.3)'} )
						.append(
							$('<img>')
								.on('load', resize).addClass('cg-initial')
								.attr('src', '/w/thumb.php?w='+(d*1.5)+'&f='+$(f).attr('href').split(':')[1]) // loading a thumbnail 1.5x wider than the square dimensions (avoids double loading of regular 3x2 images)
						)
				)
				.parent().parent().replaceWith(f);
		});
	}).find('.CategoryTreeTag').css('line-height',0);
});