MediaWiki:Newgallery.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.
/*
    Copyright (C) 2011 Magnus Manske <magnusmanske@googlemail.com>
    GPL V2+
*/

var newgallery = {

	// Thumb height in pixel
	h: 180,

	init: function () {
		var self = this;
		$('ul.gallery').css('text-align', 'center');
		$('li.gallerybox').each( function () {
			var o = $(this) ;
			o.find('div.gallerytext').appendTo(o.find('div.thumb').get(0));
			o.css({width:'',height:self.h+'px',overflow:'hidden'});
			o.find('div').css({width:'',margin:''});
			$(o.find('img').get(0)).attr('width','').attr('height',self.h+'px');
			var t = o.find('div.gallerytext');
			var th = t.height();
			t.css({position:'relative',bottom:th,'z-index':1,'background-color':'white',opacity:0.7}).hide();
			o.find('div.thumb').hover ( function(){ t.show(); } , function(){ t.hide(); } );
			var d = $(o.children('div').get(0));
			d.css({width:d.width(),overflow:'hidden'});
			
			var a = $(o.find('a').get(0)) ;
			var m = a.attr('href') ;
			if ( undefined === m ) return ;
			m = m.match(/^\/wiki\/(.+)$/) ;
			if ( m == null ) {
				m = a.attr('href') ;
				m = m.match(/^\/\/upload.wikimedia.org\/.+\/([^/]+)$/) ;
			}
			if ( m == null ) { console.log( a.attr('href') ); return; }
			var fn = m[1] ;
			if ( $.trim(o.find('div.gallerytext').text()) === '' ) {
				var h = "<a href='/wiki/"+fn+"'>" + fn + "</a>" ;
				o.find('div.gallerytext').html ( h ) ;
			} else console.log( o.find('div.gallerytext').text() );
			
			$.get ( '//commons.wikimedia.org/w/api.php', {
				format: 'json',
				action: 'query',
				prop: 'imageinfo',
				titles: fn,
				iiprop: 'url',
				iiurlheight: self.h
			} , function ( d ) {
				$.each ( d.query.pages , function ( id , page ) {
					if ( !page.imageinfo ) return ;
					$(o.find('img').get(0)).attr ( 'src' , page.imageinfo[0].thumburl ) ;
				} ) ;
			} ) ;
			
		} ) ;
	} ,

	fin : ''
} ;


$( function() {
	newgallery.init() ;
} );