User:Aroche/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.
// Gallery for subcategories
// Gives a snapshot of the content of subcategories in a category
// works with categoryTree

function activeGallery(nomCat, nfich) {
    $.getJSON('/w/api.php',
	{
	    action:'query',
	    generator: 'categorymembers',
	    gcmtitle: 'Category:'+nomCat,
	    gcmnamespace: 6,
	    gcmlimit: 10 + nfich,
	    prop: 'imageinfo',
	    iiprop: 'url',
	    iiurlheight: 120,
	    iiurlwidth: 120,
	    format: 'json'
	},
	function(data) {
	    $('#gall_main')
		.show()
		.find('#gall_content tr')
		    .empty();
            var t = 0;
	    $.each(data.query.pages, function(key, val) {
	    if (t <= 6	) {
            var snapshotAdress = val.imageinfo[0].thumburl;
		$('#gall_content tr').append('<td><a href="'+val.imageinfo[0].descriptionurl+'"><img src="'+snapshotAdress+'"/></a></td>');
            }
            t = t+1;
	    });
            if (t === 0) {
              $('#gall_content tr').append('<td>No image in this category</td>');
            }
	}
    );
   return false;
}

function gal_stz(stra) {
    var i = parseInt(stra);
    if(isNaN(i)) {
	return 0;
    } else {
	return i;
    }
}

function galleryAppendElement(elt) {
    elt.find('.CategoryTreeItem').each(function() {
        var res = $(this).children('span:eq(1)').text().match(/\(((\d+) [CK])?(, +)?((\d+) P)?(, +)?((\d+) [FD])?\)/);
	if (res && res[8] && !$(this).hasClass('commonsGalleryOK')) {
	    var nbfich = gal_stz(res[2]) + gal_stz(res[5]);
	    var elta = $(' <a href="javascript:void(0)">(G)</a>');
	    elta.click(function() {
		var nomcat = $(this).parent().find('a.CategoryTreeLabel').text();
		activeGallery(nomcat, nbfich);
	    });        
	    $(this).append(elta);
	    $(this).addClass('commonsGalleryOK');
        }
    });
    var nomCat = elt.siblings('.CategoryTreeItem').first().children('a.CategoryTreeLabel').text();

}

$(document).ready(function() {
   if (mw.config.get('wgNamespaceNumber') == 14) {
	   	var gt_container = $('<div id="gall_main"></div>');
	   	gt_container.css({
	   		width: "100%",
	   		"margin-left": "160px",
	   		height: "150px",
	   		position: "fixed",
	   		bottom: "5px",
	   		left: "1px",
	   		"background-color": "black"
	   	});
		galleryAppendElement($('#mw-subcategories'));
		$('#mw-subcategories').append(gt_container);
		gt_container
		    .hide()
		    .append('<div id="gall_close" style="color:grey">x close</div>')
		    .children('#gall_close')
			.click(function() {
			    $('#gall_main').hide();
			})
			.after('<table id="gall_content"><tr></tr></table>');
	        $('#gall_content').css({'margin-left':'auto','margin-right':'auto'});
        
        $('#mw-subcategories').ajaxComplete(function(evt, xhr, settings) {
        	if (/rs=efCategoryTreeAjaxWrapper/.test(settings.url)) {
        		galleryAppendElement($('#mw-subcategories'));
        	}
        });
    }
});

// END

// Check usages for the user imports page
$(document).ready(function() {
	if (mw.config.get('wgPageName').slice(0, 17) == 'Special:ListFiles') {
		var fileList = {};
		$('.TablePager_col_img_name').each(function(i, elt) {
			fileList[$(elt).find('a').attr('title')] = i;
		});
		$.getJSON('/w/api.php',
			{
			    action:'query',
			    prop: 'globalusage',
			    guprop: 'url',
			    gulimit: 100,
				titles: Object.keys(fileList).join('|'),
			    format: 'json'
			}, 
			function(data) {
				$('table.listfiles thead tr').append('<th>Global Uses</th>');
				$.each(data.query.pages, function(k, page) {
					var li = fileList[page.title];
					var col = $('<td><ul></ul></td>');
					$.each(page.globalusage, function(i, val) {
						col.find('ul').append('<li>'+ val.wiki + ': ' + '<a href="' + val.url + '">' + val.title.replace('_', ' ') + '</a></li>');
					});
					$('table.listfiles tbody tr').eq(li).append(col);
				});
			}
		);
	}
});