MediaWiki:Common.js

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

/* =================  Our common.js file owes a huge debt to user:Grunny's  work at starwars.wikia.com.

Which is to say we stole huge chunks of code from there. ================== */

// onload stuff var firstRun = true; function loadFunc { if( firstRun ) { firstRun = false; } else { return; }	window.pageName = wgPageName; window.storagePresent = (typeof(globalStorage) != 'undefined'); // Upload form - need to run before adding hide buttons if ( wgCanonicalSpecialPageName === 'Upload' ) { setupUploadForm; }	addHideButtons; if( document.getElementById('mp3-navlink') !== null ) { document.getElementById('mp3-navlink').onclick = onArticleNavClick; document.getElementById('mp3-navlink').getElementsByTagName('a')[0].href = 'javascript:void(0)'; }	if( window.storagePresent ) { initVisibility; }	fillEditSummaries; fillPreloads; substUsername; substUsernameTOC; rewriteTitle; showEras('title-eraicons'); showEras('title-shortcut'); rewriteHover; addAlternatingRowColors; // replaceSearchIcon; this is now called from MediaWiki:Monobook.js	fixSearch; var body = document.getElementsByTagName('body')[0]; var bodyClass = body.className; if( !bodyClass || (bodyClass.indexOf('page-') === -1) ) { var page = window.pageName.replace(/\W/g, '_'); body.className += ' page-' + page; }	if( typeof(onPageLoad) != "undefined" ) { onPageLoad; } }

//End onload stuff

/** * jQuery version of fillEditSummaries * @author Grunny */ function fillEditSummaries {

if( !$( '#wpSummaryLabel' ).length ) { return; }	var	summaryOptionsHtml = '', $summaryOptionsList;

$.get( wgScript, { title: 'Template:Stdsummaries', action: 'raw', ctype: 'text/plain' }, function( data ) {		var lines = data.split( '\n' );		for( var i = 0; i < lines.length; i++ ) {			var value = ( lines[i].indexOf( '-- ' ) === 0 ) ? lines[i].substring(3) : "";			summaryOptionsHtml += '' + lines[i] + ' ';		}		$summaryOptionsList = $( ' ' ).attr( 'id', 'stdEditSummaries' ).html( summaryOptionsHtml ).change( function { var value = $( this ).val; if ( value !== '' ) { if( skin === 'oasis' ) { $( '#wpSummary' ).text( value ); } else { $( '#wpSummary' ).val( value ); }			}		} );

$( '#wpSummaryLabel' ).prepend( 'Standard summaries: ', $summaryOptionsList, ' ' ); } );

}

/* ================= * jQuery version of fillPreloads * by Grunny at starwars.wikia.com * ================= */

function fillPreloads { if( !$( '#lf-preload' ).length ) { return; }	var	preloadOptionsHtml = '', $preloadOptionsList; $( '#lf-preload' ).attr( 'style', 'display: block' ); $.get( wgScript, { title: 'Template:Stdpreloads', action: 'raw', ctype: 'text/plain' }, function( data ) {		var lines = data.split( '\n' );		for( var i = 0; i < lines.length; i++ ) {			var value = ( lines[i].indexOf( '-- ' ) === 0 ) ? lines[i].substring(3) : "";			preloadOptionsHtml += '' + lines[i] + ' ';		}		$preloadOptionsList = $( ' ' ).attr( 'id', 'stdSummaries' ).html( preloadOptionsHtml ).change( function { var value = $( this ).val; if ( value !== '' ) { value = 'Template:' + value + '/preload'; value = value.replace( ' ', '_' ); $.get( wgScript, { title: value, action: 'raw', ctype: 'text/plain' }, function( data ) {					insertAtCursor( document.getElementById( 'wpTextbox1' ), data );				} ); }		} );		$( '#lf-preload-cbox' ).html( $preloadOptionsList );	} ); $( '#lf-preload-pagename' ).html( '' ); $( '#lf-preload-button' ).html( '' ); } function doCustomPreload { doPreload(document.getElementById('lf-preload-pagename').getElementsByTagName('input')[0].value); }

/* =============  Title rewrite jQuery version and new wikia skin fixes by Grunny ==============  This is what powers template:title, principally allowing for italic page titles ============== */ function rewriteTitle { if( typeof( window.SKIP_TITLE_REWRITE ) != 'undefined' && window.SKIP_TITLE_REWRITE ) { return; }	if( $('#title-meta').length == 0 ) { return; }	var newTitle = $('#title-meta').html; if( skin == "oasis" ) { $('header.WikiaPageHeader > h1').html(' ' + newTitle + ' '); $('header.WikiaPageHeader > h1').attr('style','text-align:' + $('#title-align').html + ';'); } else { $('.firstHeading').html(' ' + newTitle + ' '); $('.firstHeading').attr('style','text-align:' + $('#title-align').html + ';'); } }

/************************************************************ * Functions.js stuff * Deprecated, most of these functions will be removed slowly ************************************************************/

/*   Source: http://www.dustindiaz.com/getelementsbyclass/ getElementsByClass, which complements getElementById and getElementsByTagName, returns an array of all subelements of node that are tagged with a specific CSS class (searchClass) and are of the tag name tag. If tag is null, it searches for any suitable elements regardless of the tag name. Example: getElementsByClass('infobox', document.getElementById('content'), 'div') selects the same elements as the CSS declaration #content div.infobox function getElementsByClass(searchClass, node, tag) {	var classElements = new Array;

if(node == null) node = document;

if(tag == null) tag = '*';

var els = node.getElementsByTagName(tag); var elsLen = els.length; var tester = new ClassTester(searchClass);

for(i = 0, j = 0; i < elsLen; i++) {		if(tester.isMatch(els[i])) {			classElements[j] = els[i]; j++; }	}	return classElements; }

function ClassTester(className) {	this.regex = new RegExp("(^|\\s)" + className + "(\\s|$)"); }

ClassTester.prototype.isMatch = function(element) {	return this.regex.test(element.className); } /*   end getElementsByClass

function insertAtCursor(myField, myValue) { //IE support if (document.selection) {		myField.focus; sel = document.selection.createRange; sel.text = myValue; }	//MOZILLA/NETSCAPE support else if(myField.selectionStart || myField.selectionStart == '0') {		var startPos = myField.selectionStart; var endPos = myField.selectionEnd; myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length); }	else {		myField.value += myValue; } }

function getFirstHeading { var elements = getElementsByClass('firstHeading', document.getElementById('content'), 'h1'); return (elements != null && elements.length > 0) ? elements[0] : null; }

/*   Returns the element's nearest parent that has the specified CSS class. function getParentByClass(className, element) { var tester = new ClassTester(className); var node = element.parentNode;

while(node != null && node != document) {		if(tester.isMatch(node)) return node;

node = node.parentNode; }

return null; }

/*   Performs dynamic hover class rewriting to work around the IE6 :hover bug (needs CSS changes as well) function rewriteHover { var gbl = document.getElementById("hover-global");

if(gbl == null) return;

var nodes = getElementsByClass("hoverable", gbl);

for (var i = 0; i < nodes.length; i++) { nodes[i].onmouseover = function { this.className += " over"; }		nodes[i].onmouseout = function { this.className = this.className.replace(new RegExp(" over\\b"), ""); }	} } /************************************************************ * End old Functions.js stuff * Deprecated, most of these functions will be removed slowly ************************************************************/

$( loadFunc );

///////////////////////////////////////////////////////////////////////////////////////////////////////////

// ADVANCED AJAX AUTO-REFRESHING ARTICLES // Code originally by "pcj" of Wowpedia // Maintenance, cleanup, style and bug fixes by Grunny (http://starwars.wikia.com/wiki/User:Grunny)

///////////////////////////////////////////////////////////////////////////////////////////////////////////

var	ajaxIndicator = 'http://images2.wikia.nocookie.net/dev/images/8/82/Facebook_throbber.gif', ajaxTimer, ajaxRefresh = 60000, refreshText = 'Automatically refresh', refreshHover = 'Enable auto-refreshing page loads', doRefresh = true;

if ( !window.ajaxPages ) { var ajaxPages = new Array( 'Special:RecentChanges', 'Special:Watchlist', 'Special:Log', 'Special:NewFiles', 'Special:AbuseLog' ); } if ( !window.ajaxCallAgain ) { var ajaxCallAgain = []; } if( typeof AjaxRCRefreshText == "string" ) { refreshText = AjaxRCRefreshText; } if( typeof AjaxRCRefreshHoverText == "string" ) { refreshHover = AjaxRCRefreshHoverText; }

/** * Sets the cookie * @param c_name string Name of the cookie * @param value string 'on' or 'off' * @param expiredays integer Expiry time of the cookie in days */ function setCookie( c_name, value, expiredays ) { var exdate = new Date; exdate.setDate( exdate.getDate + expiredays); document.cookie = c_name + "=" + escape(value) + ( ( expiredays === null ) ? "" : ";expires=" + exdate.toGMTString ); }

/** * Gets the cookie * @param c_name string Cookie name * @return The cookie name or empty string */ function getCookie( c_name ) { if ( document.cookie.length > 0 ) { var c_start = document.cookie.indexOf( c_name + "=" ) if ( c_start !== -1 ) { c_start = c_start + c_name.length + 1; var c_end = document.cookie.indexOf( ";", c_start ); if ( c_end === -1 ) { c_end = document.cookie.length; }			return unescape( document.cookie.substring( c_start, c_end ) ); } 	}	return ""; }

/** * Main function to start the Auto-refresh process */ function preloadAJAXRL { var	ajaxRLCookie = ( getCookie( "ajaxload-" + wgPageName ) == "on" ) ? true : false, appTo = ( $( '#WikiaPageHeader' ).length ) ? $( '#WikiaPageHeader' ) : ( $( '#AdminDashboardHeader' ).length ? $( '#AdminDashboardHeader > h1' ) : $( '.firstHeading' ) ); appTo.append( ' ' + refreshText + ':   ' ); $( '#ajaxLoadProgress' ).ajaxSend( function ( event, xhr, settings ) {		if ( location.href == settings.url ) {			$( this ).show;		}	} ).ajaxComplete ( function ( event, xhr, settings ) {		if ( location.href == settings.url ) {			$( this ).hide;			for( i in ajaxCallAgain ) {				ajaxCallAgain[i];			}		}	} ); $( '#ajaxToggle' ).click( toggleAjaxReload ); $( '#ajaxToggle' ).attr( 'checked', ajaxRLCookie); if ( getCookie( "ajaxload-" + wgPageName ) == "on" ) { loadPageData; } }

/** * Turn refresh on and off by toggling the checkbox */ function toggleAjaxReload { if ( $( '#ajaxToggle' ).prop( 'checked' ) == true ) { setCookie( "ajaxload-" + wgPageName, "on", 30 ); doRefresh = true; loadPageData; } else { setCookie( "ajaxload-" + wgPageName, "off", 30 ); doRefresh = false; clearTimeout( ajaxTimer ); } }

/** * Does the actual refresh */ function loadPageData { var cC = ( $( '#WikiaArticle' ).length ) ? '#WikiaArticle' : '#bodyContent'; $( cC ).load( location.href + " " + cC + " > *", function ( data ) {		if ( doRefresh ) {			ajaxTimer = setTimeout( "loadPageData;", ajaxRefresh );		}	} ); }

/** * Load the script on specific pages */ $( function { 	for ( x in ajaxPages ) {		if ( wgPageName == ajaxPages[x] && $( '#ajaxToggle' ).length === 0 ) {			preloadAJAXRL;		}	} } );

///////////////////////////////////////////////////////////////////////////////////////////////////////////

// END OF AJAX AUTO-REFRESH

///////////////////////////////////////////////////////////////////////////////////////////////////////////

/* ======================  Script imports from dev.wikia.com ====================== */

importScriptPage('ShowHide/code.js', 'dev'); importScriptPage('EditIntroButton/code.js', 'dev'); //importScriptPage('DisplayClock/code.js', 'dev'); importScriptPage('DupImageList/code.js', 'dev');

var ArchiveToolConfig = { archiveListTemplate: 'ArchCat', archivePageTemplate: 'ArchPage', archiveSubpage: 'Archive', userLang: true }; importScriptPage('ArchiveTool/code.js', 'dev');

/* =============  MOS box from Brickipedia ==============  This is the little box underneath the search bar and article tally, which has the Seal of Rassilon in it. ===============  Keep at end of common.js, but before any addOnloadHooks. ================ */

importScript('MediaWiki:Common.js/mosbox.js');

/* ================    addOnloadHook area ================   Necessary for calling to life functions specified elsewhere in this document. ================= */

addOnloadHook( rewriteTitle );