MediaWiki:Common.js

Revision as of 11:10, 26 October 2014 by 127.0.0.1 (talk) (Test -- Wikia wikis should already support jquery)

Note: After publishing, 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: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

// Duplicate image list
importScript('User:Arkalor/dupimages.js');

/* --- Tooltips --- */

// default setting to turn tooltips on
var tooltipsOn = true;

var $tfb;
var activeHoverLink = null;
var tipCache = new Object();

// hides the tooltip
function hideTip() {
  $tfb.html("").removeClass("tooltip-ready").addClass("hidden").css("visibility","hidden"); 
  activeHoverLink = null;
}

// displays the tooltip
function displayTip(e) {
  $tfb.not(":empty").removeClass("hidden").addClass("tooltip-ready");
  moveTip(e);
  $tfb.not(":empty").css("visibility","visible");
  moveTip(e);
}

// moves the tooltip
function moveTip(e) {
  $ct = $tfb.not(":empty");
  var newTop = e.clientY + ((e.clientY > ($(window).height()/2)) ? -($ct.innerHeight()+20):20);
  var newLeft = e.clientX + ((e.clientX > ($(window).width()/2)) ? -($ct.innerWidth()+20):20);
  $ct.css({"position":"fixed","top":newTop + "px","left":newLeft + "px"});
}

// AJAX tooltips
function showTip(e) {
$tfb.html('<div class="tooltip-error" style="position:fixed; top: '+e.clientY+'px; left: '+e.clientX+'px; visibility: visible;">Loading...</div>');
  var $t=$(this);
  activeHoverLink = $t;
  $p=$t.parent();
  if ($p.hasClass("selflink")==false) {
    $t.removeAttr("title");
    $p.removeAttr("title");
    var url = "/w/index.php?title="+$t.data("tt").replace(/ /g,"_").replace(/\?/g,"%3F")+"&action=render .tooltip-content";
    if (tipCache[url] != null) {
      $tfb.html(tipCache[url]);
      displayTip(e);
      return;
    }
    $tfb.load(url,function () {
      if ($t != activeHoverLink) return;
      if ($tfb.html() == "") $tfb.html('<div class="tooltip-content"><b>Error</b><br />This target either has no tooltip<br />or was not intended to have one.</div>');
      $tfb.find(".tooltip-content").css("display","");
      tipCache[url] = $tfb.html();
      displayTip(e);
    });
  }
}

function bindTT() {
  $t=$(this);
  $p=$t.parent();
  if ($p.hasClass("selflink") == false) {
    $t.data("tt", $p.attr("title").replace(" (page does not exist)","").replace("?","%3F")).hover(showTip,hideTip).mousemove(moveTip);
  }
}

// check to see if it is active then do it
function ttMouseOver() {
  if (tooltipsOn) {
    $("#bodyContent").append('<div id="tfb" class="htt"></div>');
    $tfb = $("#tfb");
    $("#bodyContent span.ajaxttlink").each(bindTT);
  }
}
addOnloadHook(ttMouseOver);

/* --- Calculate PXP --- */

function xp_at_level(level)
{
    if (level < 2)
        return 0;

    if (level == 2)
        return 1000;

    if (level <= 126)
        return Math.ceil((Math.pow((level + 5), 5) - Math.pow(6, 5)) / 9.0);

    return Math.round((Math.pow((level + 5), 5) - Math.pow(6, 5)) / 9.0);
}

function xp_to_next_level(level)
{
    if (level < 0 || level > 274)
        return 0;

    return xp_at_level(level + 1) - xp_at_level(level);
}

function percent_xp_to_next_level(level, percent)
{
    return Math.floor(xp_to_next_level(level) * percent / 100.0);
}

function format_xp(mystring)
{
    var result;
    var pos;

    mystring += '';
    if (mystring.length <= 3) {
        result = mystring;
    } else {
        var count = 0;
        result = "";
        for (pos = mystring.length - 3; pos >= 0 && count < 20; pos -= 3, count++) {
            if (result.length == 0)
                result = mystring.substr(pos, 3);
            else
                result = mystring.substr(pos, 3) + "," + result;
        }
        if (pos > -3)
            result = mystring.substr(0, 3+pos) + "," + result;
    }

    return result;
}

function calc_and_display_pxp(index)
{
    var level = parseInt(document.getElementById("pxp_level_input_" + index).value);
    var percent = parseFloat(document.getElementById("pxp_percent_input_" + index).value);

    document.getElementById("pxp_result_" + index).innerHTML = " = " + format_xp(percent_xp_to_next_level(level, percent));

    return false;
}

function hasClass(element, className)
{
    return element.className.indexOf(className) != -1;
}

function createPXPCalculationForms()
{
    var paras = document.getElementsByTagName("p");
    var offset = 0;

    for (var index = 0; index < paras.length; index++) {
        if (hasClass(paras[index], "calc-pxp")) {
            var form = document.createElement("form");
            var input1 = document.createElement("input");
            var input2 = document.createElement("input");
            var input3 = document.createElement("input");
            var span = document.createElement("span");

            form.setAttribute("onSubmit", "return calc_and_display_pxp(" + offset + ");");
            input1.setAttribute("size", "6");
            input1.setAttribute("value", "200");
            input1.setAttribute("id", "pxp_level_input_" + offset);

            input2.setAttribute("size", "6");
            input2.setAttribute("value", "10");
            input2.setAttribute("id", "pxp_percent_input_" + offset);

            input3.setAttribute("type", "button");
            input3.setAttribute("value", "Calculate PXP");
            input3.setAttribute("onclick", "javascript:calc_and_display_pxp(" + offset + ");");

            span.setAttribute("id", "pxp_result_" + offset);
            span.innerHTML = "??? xp";

            form.appendChild(document.createTextNode("Level: "));
            form.appendChild(input1);
            form.appendChild(document.createTextNode(" Percent: "));
            form.appendChild(input2);
            form.appendChild(input3);
            form.appendChild(span);

            paras[index].appendChild(form);
            offset++;
        }
    }
}

addOnloadHook(createPXPCalculationForms);

/* --- Collapsible Tables --- */
 
var autoCollapse = 2;
var collapseCaption = 'hide';
var expandCaption = 'show';
 
function hasClass( element, className ) {
	var Classes = element.className.split( " " );
	for ( var i = 0; i < Classes.length; i++ ) {
		if ( Classes[i] == className ) {
			return true;
		}
	}
	return false;
}
 
function collapseTable( tableIndex ) {
	var i;
	var Button = document.getElementById( 'collapseButton' + tableIndex );
	var Table = document.getElementById( 'collapsibleTable' + tableIndex );
 
	if ( !Table || !Button ) {
		return false;
	}
 
	var Rows = Table.getElementsByTagName( 'tr' );
 
	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 createCollapseButtons() {
	var i;
	var tableIndex = 0;
	var NavigationBoxes = {};
	var Tables = document.getElementsByTagName( 'table' );
 
	for ( i = 0; i < Tables.length; i++ ) {
		if ( hasClass( Tables[i], 'collapsible' ) ) {
			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.style.styleFloat = 'right';
			Button.style.cssFloat = 'right';
			Button.style.fontWeight = 'normal';
			Button.style.textAlign = 'right';
			Button.style.width = '6em';
 
			ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
			ButtonLink.setAttribute( 'href', 'javascript:collapseTable(' + tableIndex + ');' );
			ButtonLink.appendChild( ButtonText );
 
			Button.appendChild( document.createTextNode( '[' ) );
			Button.appendChild( ButtonLink );
			Button.appendChild( document.createTextNode( ']' ) );
 
			var Header = Tables[i].getElementsByTagName( 'tr' )[0].getElementsByTagName( 'th' )[0];
			/* only add button and increment count if there is a header row to work with */
			if (Header) {
				Header.insertBefore( Button, Header.childNodes[0] );
				tableIndex++;
			}
		}
	}
 
	for ( i = 0; i < tableIndex; i++ ) {
		if ( hasClass( NavigationBoxes[i], 'collapsed' ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], 'autocollapse' ) ) ) {
			collapseTable( i );
		}
	}
}
 
addOnloadHook( createCollapseButtons );