MediaWiki:Common.js

From IDW Hasbro Wiki
Jump to navigationJump to search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* Any JavaScript here will be loaded for all users on every page load. */

/* ==========   Upload preview   ==========
   ==========      -BEGIN-       ==========
   ========== CODE IS UNREVIEWED ========== */


/**
 * Live preview script for MediaWiki
 *
 * 2007-04-25 – Nikerabbit:
 *   Worked around text cutoff in mozilla-based browsers
 *   Support for categories
 */


lpIdPreview = 'wikiPreview';
lpIdCategories = 'catlinks';
lpIdDiff = 'wikiDiff';

/*
 * Returns XMLHttpRequest based on browser support or null
 */
function openXMLHttpRequest() {
	if( window.XMLHttpRequest ) {
		return new XMLHttpRequest();
	} else if( window.ActiveXObject && navigator.platform != 'MacPPC' ) {
		// IE/Mac has an ActiveXObject but it doesn't work.
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return null;
	}
}

/**
 * Returns true if could open the request,
 * false otherwise (eg no browser support).
 */
function lpDoPreview(text, postUrl) {
	lpRequest = openXMLHttpRequest();
	if( !lpRequest ) return false;

	lpRequest.onreadystatechange = lpStatusUpdate;
	lpRequest.open("POST", postUrl, true);

	var postData = 'wpTextbox1=' + encodeURIComponent(text);
	lpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	lpRequest.send(postData);
	return true;
}

function lpStatusUpdate() {

	/* We are at some stage of loading */
	if (lpRequest.readyState > 0 && lpRequest.readyState < 4) {
		notify(i18n(wgLivepreviewMessageLoading));
	}

	/* Not loaded yet */
	if(lpRequest.readyState != 4) {
		return;
	}

	/* We got response, bug it not what we wanted */
	if( lpRequest.status != 200 ) {
		var keys = new Array();
		keys[0] = lpRequest.status;
		keys[1] = lpRequest.statusText;
		window.alert(i18n(wgLivepreviewMessageError, keys));
		lpShowNormalPreview();
		return;
	}

	/* All good */
	dismissNotify(i18n(wgLivepreviewMessageReady), 750);


	var XMLObject = lpRequest.responseXML.documentElement;


	/* Work around Firefox (Gecko?) limitation where it shows only the first 4096
	 * bytes of data. Ref: http://www.thescripts.com/forum/thread482760.html
	 */
	XMLObject.normalize();

	var previewElement = XMLObject.getElementsByTagName('preview')[0];
	var categoryElement = XMLObject.getElementsByTagName('category')[0];

	/* Hide the active diff if it exists */
	var diff = document.getElementById(lpIdDiff);
	if ( diff ) { diff.style.display = 'none'; }

	/* Inject preview */
	var previewContainer = document.getElementById( lpIdPreview );
	if ( previewContainer && previewElement ) {
		previewContainer.innerHTML = previewElement.firstChild.data;
		previewContainer.style.display = 'block';
	} else {
		/* Should never happen */
		window.alert(i18n(wgLivepreviewMessageFailed));
		lpShowNormalPreview();
		return;
	}


	/* Inject categories */
	var categoryContainer  = document.getElementById( lpIdCategories );
	if ( categoryElement && categoryElement.firstChild ) {
		if ( categoryContainer ) {
			categoryContainer.innerHTML = categoryElement.firstChild.data;
			/* May be hidden */
			categoryContainer.style.display = 'block';
		} else {
			/* Just dump them somewhere */
	/*		previewContainer.innerHTML += categoryElement.firstChild.data;*/
		}
	} else {
		/* Nothing to show, hide old data */
		if ( categoryContainer ) {
			categoryContainer.style.display = 'none';
		}
	}

}

function lpShowNormalPreview() {
	var fallback = document.getElementById('wpPreview');
	if ( fallback ) { fallback.style.display = 'inline'; }
}


// TODO: move elsewhere
/* Small non-intrusive popup which can be used for example to notify the user
 * about completed AJAX action. Supports only one notify at a time.
 */
function notify(message) {
	var notifyElement = document.getElementById('mw-js-notify');
	if ( !notifyElement ) {
		createNotify();
		var notifyElement = document.getElementById('mw-js-notify');
	}
	notifyElement.style.display = 'block';
	notifyElement.innerHTML = message;
}

function dismissNotify(message, timeout) {
	var notifyElement = document.getElementById('mw-js-notify');
	if ( notifyElement ) {
		if ( timeout == 0 ) {
			notifyElement.style.display = 'none';
		} else {
			notify(message);
			setTimeout("dismissNotify('', 0)", timeout);
		}
	}
}

function createNotify() {
	var div = document.createElement("div");
	var txt = '###PLACEHOLDER###'
	var txtNode = document.createTextNode(txt);
	div.appendChild(txtNode);
	div.id = 'mw-js-notify';
	// TODO: move styles to css
	div.setAttribute('style',
		'display: none; position: fixed; bottom: 0px; right: 0px; color: white; background-color: DarkRed; z-index: 5; padding: 0.1em 1em 0.1em 1em; font-size: 120%;');
	var body = document.getElementsByTagName('body')[0];
	body.appendChild(div);
}



/* Helper function similar to wfMsgReplaceArgs() */
function i18n(message, keys) {
	var localMessage = message;
	if ( !keys ) { return localMessage; }
	for( var i = 0; i < keys.length; i++) {
		var myregexp = new RegExp("\\$"+(i+1), 'g');
		localMessage = localMessage.replace(myregexp, keys[i]);
	}
	return localMessage;
}


	if (wgCanonicalNamespace == 'Special' && wgCanonicalSpecialPageName == 'Upload'){
	 addOnloadHook(uploadPreviewInit)
	 //prepare Mediawiki Live Preview
	 wgLivepreviewMessageLoading = 'Wait...'
	 wgLivepreviewMessageReady = 'Done'
	 document.write('<script type= "text/javascript" src="/skins-1.5/common/preview.js"></script>')
	}

	function uploadPreviewInit(){
	 wpUploadDescription = document.getElementById('wpUploadDescription') //global var
	 var but = document.createElement('input')
	 but.type = 'button'
	 but.value='Preview'
	 but.onclick=uploadPreviewDo
	 wpUploadDescription.parentNode.insertBefore(but, wpUploadDescription.nextSibling)
	}

	function uploadPreviewDo(){
	 if (!window.wikiPreview) //create preview div
	    wikiPreview = document.createElement('div')
	    wikiPreview.id = 'wikiPreview'
	    wpUploadDescription.parentNode.insertBefore(wikiPreview, wpUploadDescription.nextSibling.nextSibling)
	 lpDoPreview(wpUploadDescription.value, '/w2/index.php?action=submit&live')
	}

/* ==========   Upload preview   ==========
   ==========      -End-       ==========
   ========== CODE IS UNREVIEWED ========== */


/* Blatantly based on Wookiepedia's ShowEras */
/* Used for the faction icon template*/
/* That template generates a div that contains another div, which itself contains */
/* the image and link which make up the icon. The id of the main div is */
/* which is what then gets passed into this function, is 'title-factionicons' */ 
/* This function copies a div tag with the passed in id, puts the copy */
/* before the page title bar h1 tag, and makes the copy visible. */
/* The main div is expected to have it's 'display' CSS attribute set to none to hide it */
/* in the main portion of the page. */

function showTitles(className) { 

     //This check seems to be a holdover from Wookiepedia and not functional for us as SKIP_ERAS is not defined anywhere in our code (that I've found so far). Can probably be removed. 
     if(typeof(SKIP_ERAS) != 'undefined' && SKIP_ERAS) 
          return; 
 
     //Get the HTML element with the id passed in. This element is the div that contains the faction icon.  
     var titleDiv = document.getElementById(className); 
 
     if(titleDiv == null || titleDiv == undefined) 
          return; 
 
     //Make a copy of the Title bar div
     var cloneNode = titleDiv.cloneNode(true);

     //Get the first h1 tag with a class of 'firstHeading' inside of the div that has the id 'content'.
     var firstHeading = $('#content h1.firstHeading')[0];
     
     //Insert the cloned node before the heading.
     firstHeading.insertBefore(cloneNode, firstHeading.childNodes[0]);

     //Make the cloned node visible
     cloneNode.style.display = "block"; 
}// END showTitles 

/* Code for hiding segments */
function hide_block(id) {
     document.getElementById(id).style.display = "inline";
     document.getElementById(id + "-link").style.display = "none";
}

/* Used for the Hide template, this function inserts the */
/* 'Show Hidden Content' links used by the template and hides */
/* the list markup generated by the template */

function replace_hides() {
     $('div.hide').each(
          function(blockToHide){

               //Create div for the 'Show Hidden Content' link
               var hide_link = document.createElement("div");

               // Create link 
               hide_link.innerHTML = "<a href='javascript:hide_block(\"" + blockToHide.getAttribute("id") + "\");' id='" + blockToHide.getAttribute("id") + "-link'><i>Show hidden content.</i></a>";

               //Insert the link before the list to be hidden
               blockToHide.parentNode.insertBefore(hide_link, blockToHide);

               //Hide the list
               blockToHide.style.display = "none"; 
          }
     );
}


function tfwiki_hooks() {
     showTitles('title-factionicons'); //Move faction and other icons to the header bar
     replace_hides(); //Insert 'Show Hidden Content' links for the Hide template
}

/* Run on page load */
$(document).ready(tfwiki_hooks);