MediaWiki:Common.js

/* Any JavaScript here will be loaded for all users on every page load. */

/** Infobox collapsing script **/ $(".infoboxBlockImage img").each(function {	width = parseInt($(this).attr("width"), 10);	if(width > 240) {		$(this).attr("height", "");		$(this).attr("width", "240");	} });

$(".infobox:not(.infoboxNoCollapse) td").each(function {	if($(this).html.match(/}/)) {		$(this).parent("tr").hide;	} });

$(".infobox:not(.infoboxNoCollapse) .infoboxSubsectionBreak, .infobox:not(.infoboxNoCollapse) tr.infoboxSectionHeader").each(function {	flag = true;	next = $(this).next;	while(next && next !== undefined && next.html !== undefined && !next.hasClass("infoboxSubsectionBreak") && !next.hasClass("infoboxSectionHeader")) {		if(next.css("display") != "none") {			flag = false;		}		// console.info(next.html);		next = next.next;	}	// console.info(flag);	if(flag) {		$(this).hide;	} });

/** Ore dict testing script **/ if($(".OreDictTestingScriptsOutput").length) { notices = 0; errs = 0; warns = 0; $(".gridContainer .gridImage").each(function {		tgt = "";		if($(this).children("span").children("a.new").length) {			warns++;			tgt = $(this).children("span").children("a.new").attr("href").match(/\/index.php\?title=([^&]+)&action=edit&redlink=1/);			if(tgt !== null) {				tgt = tgt[1];				tgt = tgt.replace(/_/g, " ");				$(".OreDictTestingScriptsOutput").append("WARNING: Link to a non-existent article: "+tgt+"\n");			} else {				$(".OreDictTestingScriptsOutput").append("WARNING: Link to a non-existent article: "+$(this).children("span").children("a.new").attr("href")+"\n");				errs++;				$(".OreDictTestingScriptsOutput").append("ERROR: Unexpected error.\n");			}		} else {			if($(this).children("a:not(.new)").length) {				tgt = $(this).children("a:not(.new)").attr("href").match(/\/(.+)/); } else { errs++; $(".OreDictTestingScriptsOutput").append("ERROR: Unexpected error.\n"); }			if(tgt !== null) { tgt = tgt[1]; } else { notices++; $(".OreDictTestingScriptsOutput").append("NOTICE: Link overriden to none.\n"); }		}		itemName = $(this).find(".gridTooltipInner").html.match(/Variable 1: ([^<]+)/); itemMod = $(this).find(".gridTooltipInner").html.match(/Variable 3: ([^<]+)/); if(itemName !== null) { itemName = itemName[1]; }		if(itemMod !== null) { itemMod = itemMod[1]; }		if(itemName === null) { warns++; $(".OreDictTestingScriptsOutput").append("WARNING: Tooltip text overridden!\n"); }		if(itemMod === null) { expected = itemName; } else { expected = itemName+" ("+itemMod+")"; }		if(tgt !== null) { tgt = tgt.replace(/_/g, " "); }		if(expected != tgt) { notices++; $(".OreDictTestingScriptsOutput").append("NOTICE: Link overridden from "+expected+" to "+tgt+"!\n"); }		if($(this).children("a.new").length) { errs++; filename = $(this).children("a.new").attr("href").match(/\/index\.php\?title=Special:Upload&wpDestFile=(.+)/); $(".OreDictTestingScriptsOutput").append("ERROR: Missing file: File:"+filename[1]+"\n"); }		if(!$(this).hasClass("testingClass")) { warns++; $(".OreDictTestingScriptsOutput").append("WARNING: Image class overridden not appended!\n"); }		if(!$(this).find(".tooltip").hasClass("testingClass")) { warns++; $(".OreDictTestingScriptsOutput").append("WARNING: Image class overridden not appended!\n"); }		if($(this).find(".gridNumber a, .gridNumber span").html != "8") { errs++; $(".OreDictTestingScriptsOutput").append("ERROR: Item count overridden!\n"); }	});

if(errs + warns === 0) { $(".OreDictTestingScriptsOutput").append("Done. "+notices+" notices, no warnings, no errors ."); } else { $(".OreDictTestingScriptsOutput").append("Done. "+notices+" notices, "+errs+" errors , "+warns+" <span style=\"color:orange; font-weight:bold;\">warnings ."); } }

// Remove title from grid links $(".gridImage .gridNumber a").removeAttr("title");

/** Langbar **/ $(".langbarLink").children("a.new").parent(".langbarLink").hide;

/** Tanks **/ /** Resize clip parent **/ $(".tankClip").each(function {	$(this).parent("div").height($(this).children("img").height).width($(this).children("img").width); });

/** Clip tank image **/ $(".gridTankContainer").each(function {	// Tile liquid image	$(this).find(".tankLiquidImage").each(function { imglink = $(this).find("img").attr("src"); $(this).find("img").hide; $(this).css({			backgroundImage: 'url(' + imglink + ')',			backgroundRepeat: 'repeat'		}); });

$(this).find(".tankLiquidImage").height($(this).height); $(this).find(".tankLiquidImage").width($(this).width); $(this).find(".tankLiquidImageContainer").height($(this).height); $(this).find(".tankLiquidImageContainer").width($(this).width);

// Get tank info max = parseInt($(this).find(".tankMax").html, 10); $(this).find(".tankLiquidImage").each(function {		usage = parseInt($(this).find(".tankUsage").html, 10);		// Calculate stuff		if(max === 0) {			max = 10000;		}		height = $(this).height;		width = $(this).width;		cliptop = Math.ceil(height/2*(1-usage/max))*2;		$(this).css("clip", "rect(" + String(cliptop) + "px auto auto auto)");		// Replace vars		tooltip = $(this).parent.find(".gridTooltipInner");		text = tooltip.html;		text = text.replace("$2", usage);		text = text.replace("$3", max);		tooltip.html(text);	}); });

/* $(".CraftingGrid").each(function {	maxFrames = 0;	$(this).find(".CraftingGridCell").each(function { frames = $(this).children("span:not(.ignore), div.GridTank:not(.ignore)").length; if(frames > maxFrames) { maxFrames = frames; }		// Initialize cell states $(this).children("span:first-child:not(.ignore), div.GridTank:first-child:not(.ignore)").addClass("ActiveSlide"); });
 * Automatic slide creation in crafting grids.

if(maxFrames <= 1) { return; }

// Create crafting grid controls $(this).append(' <input type="button" value="<" class="prevPage"> 1 / '+String(maxFrames)+' <input type="button" value=">" class="nextPage"> '); $(this).height($(this).height + $(this).children(".CraftingGridControls").height);

// Implement controls $(this).find(".nextPage").click(function {		container = $(this).parents(".CraftingGrid");		container.find(".CraftingGridCell").each(function { if($(this).children(":not(.ignore)").length == 1) { $(this).removeClass(".CraftingGridCell"); return 0; }			cur = $(this).find(".ActiveSlide"); next = cur.next("span:not(.ignore), div.GridTank:not(.ignore)"); if(next.length === 0) { next = cur.siblings("span:not(.ignore), div.GridTank:not(.ignore)").first; }			cur.removeClass("ActiveSlide"); next.addClass("ActiveSlide"); });		pageNum = parseInt($(this).siblings("span.pageNum").html,10) + 1;		if(pageNum > parseInt($(this).siblings("span.pageCount").html, 10)) {			pageNum = 1;		}		$(this).siblings("span.pageNum").html(pageNum);	});

$(this).find(".prevPage").click(function {		container = $(this).parents(".CraftingGrid");		container.find(".CraftingGridCell").each(function { if($(this).children(":not(.ignore)").length == 1) { $(this).removeClass(".CraftingGridCell"); return 0; }			cur = $(this).find(".ActiveSlide"); next = cur.prev("span:not(.ignore), div.GridTank:not(.ignore)"); if(next.length === 0) { next = cur.siblings("span:not(.ignore), div.GridTank:not(.ignore)").last; }			cur.removeClass("ActiveSlide"); next.addClass("ActiveSlide"); });		pageNum = parseInt($(this).siblings("span.pageNum").html,10) - 1;		if(pageNum === 0) {			pageNum = parseInt($(this).siblings("span.pageCount").html, 10);		}		$(this).siblings("span.pageNum").html(pageNum);	}); });

/* $(".gallery > div, .gallery > span").addClass("page");
 * Script for flipping through galleries, intended to be used with crafting grids.

$(".gallery").append(' <input type="button" value="<" class="prevPage"> / <input type="button" value=">" class="nextPage"> ');

$(".gallery > .page:first-child").addClass("active");

$(".gallery span.controls span.pagenum").html(1);

$(".gallery").each(function {	$(this).find("span.pagecount").html($(this).children(".page").length); });

$(".gallery span.controls input.prevPage").click(function {	cur = $(this).parents(".gallery").children(".page.active");	next = cur.prev(".page");	if(next.length === 0) {		next = cur.siblings(".page").last;	} 	cur.removeClass("active");	next.addClass("active");	pageNum = parseInt($(this).siblings("span.pagenum").html,10) - 1;	if(pageNum === 0) {		pageNum = parseInt($(this).siblings("span.pagecount").html, 10);	}	$(this).siblings("span.pagenum").html(pageNum); });

$(".gallery span.controls input.nextPage").click(function {	cur = $(this).parents(".gallery").children(".page.active");	next = cur.next(".page");	if(next.length === 0) {		next = cur.siblings(".page").first;	} 	cur.removeClass("active");	next.addClass("active");	pageNum = parseInt($(this).siblings("span.pagenum").html,10) + 1;	if(pageNum > parseInt($(this).siblings("span.pagecount").html, 10)) {		pageNum = 1;	}	$(this).siblings("span.pagenum").html(pageNum); });

/* $(".tooltip").hide; $(".tooltip").parent.addClass("tooltipParent"); $(".tooltip").parent.mousemove(function(e) {	$(this).children(".tooltip").show;	var x = Math.min(e.pageX - $(this).offset.left + 5, $(window).width - $(this).offset.left - $(this).children(".tooltip").width - 15 + $(window).scrollLeft );	$(this).children(".tooltip").css({ position: "absolute", marginLeft: 0, marginTop: 0, top: e.pageY - $(this).offset.top - 5 - $(this).children(".tooltip").outerHeight(true), left: x}); });
 * Following tooltips

$(".tooltip").parent.mouseout(function {	$(this).children(".tooltip").hide; });

// Remove titles of links inside of siblings of tooltips $(".tooltipParent").children("a").attr('title', '');

/** * Element animator (generic animation) * * Will cycle the active class on any child elements within an element with the animated class. */ // Remove from animated class if only one child $('.animated').each(function {	if($(this).children("span, div").length == 1) {		$(this).removeClass("animated");	} });

// Add the active class to all of the first child of .animated $('.animated > span:first-child, .animated > div:first-child').addClass('active'); if ( $( '.animated' ).length ) { setInterval( function {		$( '.animated' ).each( function { var current = $( this ).find( '.active' ).removeClass( 'active' ), next = current.next; if ( !current.next.length ) { next = $( this ).children.eq( 0 ); }			next.addClass( 'active' ); } );	}, 2000 ); }

/** * Pause grid templates with lots of cells in them (e.g. Template:Grid/Crafting Table) on mouseover * * This is so people have a chance to look at each image on the cell * and click on pages they want to view. */ function pauseGrid( grid ) { $( grid ).hover( function { 		$( this ).find( '.grid .animated' ).removeClass( 'animated' ).addClass( 'paused' );	}, function {		$( this ).find( '.grid .paused' ).removeClass( 'paused' ).addClass( 'animated' );	} ); } pauseGrid( '.grid-Crafting_Table' ); pauseGrid( '.grid-Furnace' ); pauseGrid( '.grid-Brewing_Stand' );

$(document).ready( function {	var pops = function( elems ) {		for (i = 0; i < elems.length; i++) {			if ( !(' '+elems[i].className+' ').match( / pops / ) ) {				continue;			}			var anchs = elems[i].getElementsByTagName('a');			for (var j = 0; j < anchs.length; j++) anchs[j].target = '_blank';		}	};	bc = document.getElementById('bodyContent');	tags = ['span', 'div', 'table', 'td', 'th'];	for (i = 0; i < tags.length; i++) {		pops( bc.getElementsByTagName( tags[i] ) );	} } );

/** Unused function :? */ function ModifySidebar(action, section, name, link) { try { switch (section) { case "languages": target = "p-lang"; break; case "toolbox": target = "p-tb"; break; case "navigation": target = "p-navigation"; break; default: target = "p-" + section; break; }

if (action == "add") { node = document.getElementById(target) .getElementsByTagName('div')[0] .getElementsByTagName('ul')[0];

aNode = document.createElement('a'); liNode = document.createElement('li');

aNode.appendChild(document.createTextNode(name)); aNode.setAttribute('href', link); liNode.appendChild(aNode); liNode.className='plainlinks'; node.appendChild(liNode); }

if (action == "remove") { list = document.getElementById(target) .getElementsByTagName('div')[0] .getElementsByTagName('ul')[0];

listelements = list.getElementsByTagName('li');

for (i = 0; i < listelements.length; i++) { if (listelements[i].getElementsByTagName('a')[0].innerHTML == name ||					listelements[i].getElementsByTagName('a')[0].href == link) {

list.removeChild(listelements[i]); }			}		}	} catch(e) { // lets just ignore what's happened return; } }

/** Collapsible tables ********************************************************* * * Description: Allows tables to be collapsed, showing only the header. See *						 http://www.mediawiki.org/wiki/Manual:Collapsible_tables. * Maintainers: en:User:R. Koot */

var autoCollapse = 2; var collapseCaption = 'hide'; var expandCaption = 'show';

function collapseTable( tableIndex ) { Button = document.getElementById( 'collapseButton' + tableIndex ); Table = document.getElementById( 'collapsibleTable' + tableIndex ); if ( !Table || !Button ) { return false; }	Rows = Table.rows; if ( Button.firstChild.data == collapseCaption ) { for ( i = 1; i < Rows.length; i++ ) { Rows[i].style.display = 'none'; }		Button.firstChild.data = expandCaption; } else { for ( i = 1; i < Rows.length; i++ ) { Rows[i].style.display = Rows[0].style.display; }		Button.firstChild.data = collapseCaption; } } function createClickHandler( tableIndex ) { return function ( e ) { e.preventDefault; collapseTable( tableIndex ); }; }

function createCollapseButtons($content) { tableIndex = 0; NavigationBoxes = {}; Tables = $content[0].getElementsByTagName( 'table' ); for ( i = 0; i < Tables.length; i++ ) { if ( hasClass( Tables[i], 'collapsible' ) ) { /* only add button and increment count if there is a header row to work with */ HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0]; if ( !HeaderRow ) { continue; }			Header = HeaderRow.getElementsByTagName( 'th' )[0]; if ( !Header ) { continue; }			NavigationBoxes[tableIndex] = Tables[i]; Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );

var Button = document.createElement( 'span' ); var ButtonLink = document.createElement( 'a' ); var ButtonText = document.createTextNode( collapseCaption );

Button.className = 'collapseButton'; // Styles are declared in MediaWiki:Common.css

ButtonLink.style.color = Header.style.color; ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex ); ButtonLink.setAttribute( 'href', '#' ); $( ButtonLink ).on( 'click', createClickHandler( tableIndex ) ); Button.setAttribute( 'onClick', "collapseTable(" + tableIndex + ");" ); ButtonLink.appendChild( ButtonText );

Button.appendChild( document.createTextNode( '[' ) ); Button.appendChild( ButtonLink ); Button.appendChild( document.createTextNode( ']' ) );

Header.insertBefore( Button, Header.childNodes[0] ); Header.setAttribute( 'onClick', "collapseTable(" + tableIndex + ");" ); tableIndex++; }	}

for ( i = 0; i < tableIndex; i++ ) { if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) ||			( tableIndex >= autoCollapse && $( NavigationBoxes[i] ).hasClass( 'autocollapse' ) )		) { collapseTable( i ); }	} }

mw.hook('wikipage.content').add(function($content) {	(function { if (!$content.find('.load-page').length) { console.log('Nothing to add hooks for!'); return; }		console.log('Adding hooks!'); $content.on('click', '.load-page-button > a', function (e) {			e.preventDefault;			var $button = $(this).parent;			var $body = $button.closest('.load-page');			new mw.Api.get({ action: 'parse', prop: 'text', page: $body.data('page') }).done(function (data) { console.log('Loaded data!'); $body.html(data.parse.text['*']); mw.hook('wikipage.content').fire($body); }).fail(function { console.log('Failed to load data!'); });			console.log('Firing request to load data!');		}); });	createCollapseButtons($content); });

/** Test if an element has a certain class **************************************
 * Description: Uses regular expressions and caching for better performance.
 * Maintainers: User:Mike Dillon, User:R. Koot, User:SG
 * Maintainers: User:Mike Dillon, User:R. Koot, User:SG

function hasClass(element, className) { return new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ).test(element.className); }