Difference between revisions of "MediaWiki:Common.js"

Jump to navigationJump to search
m (Common.js moved to MediaWiki:Common.js)
Line 1: Line 1:
 
/* <pre><nowiki> */
 
/* <pre><nowiki> */
 
if (window.location.href.indexOf("printable=yes") != -1) {
 
    document.write('<style type="text/css">/*<![CDATA[*/ @import "/index.php?title=MediaWiki:PrintableTweaks.css&action=raw&ctype=text/css"; /*]]>*/</style>');
 
}
 
  
 
  // global XHRs
 
  // global XHRs
Line 59: Line 55:
 
     rewriteHover();
 
     rewriteHover();
 
     addAlternatingRowColors();
 
     addAlternatingRowColors();
 +
 +
    if (window.location.href.indexOf("printable=yes") != -1)
 +
    {
 +
        document.write('<style type="text/css">/*<![CDATA[*/ @import "/index.php?title=MediaWiki:PrintableTweaks.css&action=raw&ctype=text/css"; /*]]>*/</style>');
 +
    }
  
 
     if(typeof onPageLoad != "undefined")
 
     if(typeof onPageLoad != "undefined")
Line 446: Line 447:
 
         for(var i = 0; i < rows.length; i++)
 
         for(var i = 0; i < rows.length; i++)
 
         {
 
         {
 +
            if(rows[i].className.indexOf('infoboxstopalt') != -1)
 +
                break;
 +
 
             var ths = rows[i].getElementsByTagName('th');
 
             var ths = rows[i].getElementsByTagName('th');
  
 
             if(ths.length > 0)
 
             if(ths.length > 0)
 
             {
 
             {
                // do not change userboxes
 
                if(ths[0].className.indexOf('infoboxstopalt') != -1)
 
                    break;
 
 
 
                 continue;
 
                 continue;
 
             }
 
             }

Revision as of 15:34, 9 March 2007

/* <pre><nowiki> */

 // global XHRs
 var xhrSummaries, xhrReasons, xhrPreloads;
 var storagePresent = (typeof(globalStorage) != 'undefined');
 var pageName;

 String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };

 // onload stuff

 function loadFunc()
 {
     document.getElementById('searchform').getElementsByTagName('a')[0].href = "http://starwars.wikia.com/index.php?title=Special:Search&adv=1";

     pageName = getElementsByClass('firstHeading', document.getElementById('content'), 'h1')[0].childNodes[0].nodeValue.trim();

     if(document.getElementById('infoboxinternal') != null)
     {
         document.getElementById('infoboxend').innerHTML = '<a id="infoboxtoggle" href="javascript:infoboxToggle()">[Hide]</a>';
     }

     if(document.getElementById('appearances-list') != null)
     {
         document.getElementById('appearances-control').style.display = 'block';
         document.getElementById('appearances-list').style.display = 'none';
         document.getElementById('appearances-control').innerHTML = '<a id="appearances-toggle" href="javascript:divToggle(\'appearances\')">[Show]</a>';
     }

     if(document.getElementById('sources-list') != null)
     {
         document.getElementById('sources-control').style.display = 'block';
         document.getElementById('sources-list').style.display = 'none';
         document.getElementById('sources-control').innerHTML = '<a id="sources-toggle" href="javascript:divToggle(\'sources\')">[Show]</a>';
     }

     if(document.getElementById('mp3-navlink') != null)
     {
         document.getElementById('mp3-navlink').onclick = onArticleNavClick;
         document.getElementById('mp3-navlink').getElementsByTagName('a')[0].href = 'javascript:void(0)';
     }

     if(storagePresent)
         initVisibility();

     fillEditSummaries();
     fillDeleteReasons();
     fillPreloads();

     bodyClassAdd();
     substUsername();
     rewriteTitle();
     showEras('title-eraicons');
     showEras('title-shortcut');
     rewriteHover();
     addAlternatingRowColors();

     if (window.location.href.indexOf("printable=yes") != -1)
     {
         document.write('<style type="text/css">/*<![CDATA[*/ @import "/index.php?title=MediaWiki:PrintableTweaks.css&action=raw&ctype=text/css"; /*]]>*/</style>');
     }

     if(typeof onPageLoad != "undefined")
     {
         onPageLoad();
     }
 }

 function infoboxToggle()
 {
    var page = pageName.replace(/\W/g,'_');
    var nowShown;

     if(document.getElementById('infoboxtoggle').innerHTML == '[Hide]')
     {
         document.getElementById('infoboxinternal').style.display = 'none';
         document.getElementById('infoboxtoggle').innerHTML = '[Show]';
         nowShown = false;
     }
     else
     {
         document.getElementById('infoboxinternal').style.display = 'block';
         document.getElementById('infoboxtoggle').innerHTML = '[Hide]';
         nowShown = true;
     }

    if(storagePresent)
    {
        var storage = globalStorage[window.location.hostname];
        storage.setItem('infoboxshow-' + page, nowShown);
    }
}

 function divToggle(line)
 {
     if(document.getElementById(line + '-toggle').innerHTML == '[Hide]')
     {
         document.getElementById(line + '-list').style.display = 'none';
         document.getElementById(line + '-toggle').innerHTML = '[Show]';
     }
     else
     {
         document.getElementById(line + '-list').style.display = 'block';
         document.getElementById(line + '-toggle').innerHTML = '[Hide]';
     }
 }

 function fillEditSummaries()
 {
    var label = document.getElementById("wpSummaryLabel");

    if(label == null)
    {
        return;
    }

    var comboString = "Standard summaries: <select id='stdSummaries' onchange='onStdSummaryChange()'>";
    comboString += "</select><br />";
    label.innerHTML = comboString + label.innerHTML;

    var request = getRequest();
    xhrSummaries = request;
    request.open("GET", "http://starwars.wikia.com/index.php?title=Template:Stdsummaries&action=raw&ctype=text/plain");
    request.onreadystatechange = onRSCSummaries;
    request.send(null);
 }

 function onStdSummaryChange()
 {
    var combo = document.getElementById("stdSummaries");
    var value = combo.options[combo.selectedIndex].value;

    if(value != "")
        document.getElementById("wpSummary").value = value;
 }

 function fillDeleteReasons()
 {
    var label = document.getElementById("wpReason");

    if(label == null)
    {
        return;
    }

    label = document.getElementById("contentSub");

    if(label == null)
    {
        return;
    }

    var comboString = "<br /><select id='stdReasons' onchange='onStdReasonChange()'>";
    comboString += "</select>";
    label.innerHTML += comboString;

    var request = getRequest();
    xhrReasons = request;
    request.open("GET", "http://starwars.wikia.com/index.php?title=Template:Stdreasons&action=raw&ctype=text/plain");
    request.onreadystatechange = onRSCReasons;
    request.send(null);
 }

 function onStdReasonChange()
 {
    var combo = document.getElementById("stdReasons");
    var value = combo.options[combo.selectedIndex].value;

    if(value != "")
        document.getElementById("wpReason").value = value;
 }

 function fillPreloads()
 {
    var div = document.getElementById("lf-preload");

    if(div == null)
    {
        return;
    }

    div.style.display = 'block';
    var span = document.getElementById('lf-preload-cbox');

    var comboString = "<select id='stdPreloads' onchange='onPreloadChange()'>";
    comboString += "</select>";
    span.innerHTML = comboString;

    var request = getRequest();
    xhrPreloads = request;
    request.open("GET", "http://starwars.wikia.com/index.php?title=Template:Stdpreloads&action=raw&ctype=text/plain");
    request.onreadystatechange = onRSCPreloads;
    request.send(null);
 }

 function onPreloadChange()
 {
    var combo = document.getElementById("stdPreloads");
    var value = combo.options[combo.selectedIndex].value;

    if(value == "")
        return;

    value = "Template:" + value + "/preload";
    value = value.replace(" ", "_");
    window.location.href += "&preload=" + value;
 }

 function getRequest()
 {
    var agt = navigator.userAgent.toLowerCase();

    if (agt.indexOf('msie')!=-1 && agt.indexOf('msie 7')==-1 && document.all && agt.indexOf('opera')==-1 && agt.indexOf('mac')==-1)
        return new ActiveXObject("Msxml2.XMLHTTP");

   var request = new XMLHttpRequest();
   return request;
 }

 function iAmAlive()
 {
     alert('I am alive!');
 }

 function onRSCSummaries()
 {
     fillCombo(xhrSummaries, 'stdSummaries');
 }

 function onRSCReasons()
 {
     fillCombo(xhrReasons, 'stdReasons');
 }

 function onRSCPreloads()
 {
     fillCombo(xhrPreloads, 'stdPreloads');
 }

 function fillCombo(request, comboid)
    {
        if(request.readyState == 4)
        {
            var combo = document.getElementById(comboid);
            var lines = request.responseText.split("\n");

            for(var i = 0; i < lines.length; i++)
            {
                var value = lines[i].indexOf("-- ") == 0 ? lines[i].substring(3) : "";
                //combo.innerHTML += "<option value='" + value + "'>" + lines[i] + "</option>";
                var option = document.createElement('option');
                option.setAttribute('value', value);
                option.appendChild(document.createTextNode(lines[i]));
                combo.appendChild(option);
            }
        }
    }

// BEGIN getElementsByClass - http://www.dustindiaz.com/getelementsbyclass/
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 pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
    
	return classElements;
}
// END getElementsByClass

// BEGIN {{USERNAME}} - Uncyclopedia
function substUsername() {
    for(var i=0; UserName = document.getElementsByTagName("span")[i]; i++) {
        if ((document.getElementById('pt-userpage'))&&(UserName.getAttribute('id') == "insertusername")) {
            var ViewerName = document.getElementById('pt-userpage').firstChild.innerHTML;
            UserName.innerHTML = ViewerName;
        }
    }
}
// END {{USERNAME}}

// ============================================================
// BEGIN JavaScript title rewrite

function rewriteTitle()
{
    if(typeof(SKIP_TITLE_REWRITE) != 'undefined' && SKIP_TITLE_REWRITE)
        return;

    var titleDiv = document.getElementById('title-meta');

    if(titleDiv == null || titleDiv == undefined)
        return;

    var cloneNode = titleDiv.cloneNode(true);
    var firstHeading = getElementsByClass('firstHeading', document.getElementById('content'), 'h1')[0];
    var node = firstHeading.childNodes[0];

    // new, then old!
    firstHeading.replaceChild(cloneNode, node);
    cloneNode.style.display = "inline";

    var titleAlign = document.getElementById('title-align');
    firstHeading.style.textAlign = titleAlign.childNodes[0].nodeValue;
}

function showEras(className)
{
    if(typeof(SKIP_ERAS) != 'undefined' && SKIP_ERAS)
        return;

    var titleDiv = document.getElementById(className);

    if(titleDiv == null || titleDiv == undefined)
        return;

    var cloneNode = titleDiv.cloneNode(true);
    var firstHeading = getElementsByClass('firstHeading', document.getElementById('content'), 'h1')[0];
    firstHeading.insertBefore(cloneNode, firstHeading.childNodes[0]);
    cloneNode.style.display = "block";
}
// END JavaScript title rewrite

function initVisibility()
{
    var storage = globalStorage[window.location.hostname];

    var page = pageName.replace(/\W/g,'_');
    var show = storage.getItem('infoboxshow-' + page);

    if(show == 'false')
    {
        infoboxToggle();
    }
}

 addOnloadHook(loadFunc);

//[[User:Editcount/editcount.js]]
if (document.title.indexOf('User:Editcount/editcount.js') != -1) {
  document.write('<script type="text/javascript" src="' 
    + 'http://starwars.wikia.com/index.php?title=User:Editcount/editcount.js' 
    + '&action=raw&ctype=text/javascript&dontcountme=s"></script>'); }

// ==================================================
//   Begin temporary per-page unique CSS (Splarka)
// ==================================================
// The following script sort of emulates the per-page
// unique class functionality that is added to future
// version of MediaWiki.
//
// This takes <h1 class="firstHeading"> text.Content
// and makes it a class in the format:
//  page-Page_Title_Here 
// with all spaces and non-alphanumeric characters
// converted to underscores.
//
// So one can style Special:Contributions with a css
//  command like: body.page-User_Contributions {}
//
// Query string debugging tools:
//  &useclass=xxx     lets you overwrite the class
//  &debug=true         popup tells you the pageclass

function bodyClassAdd() {
    var bodyclass = document.getElementsByTagName('body')[0].className;
    var page = pageName;

    if(queryString('useclass')) page = queryString('useclass')
    page = page.replace(/\W/g,'_');   //
    page = 'page-' + page;
    if(queryString('debug')) alert('This page has an extra CSS body class:\n' + page+ '\nIt will be applied as soon as you close this alert')
    document.getElementsByTagName('body')[0].className = bodyclass + ' ' + page;
}

function queryString(p) {
	var re = RegExp('[&?]' + p + '=([^&]*)');
	var matches;
	if (matches = re.exec(document.location)) {
		try { 
			return decodeURI(matches[1]);
		} catch (e) {
		}
	}
	return null;
}
// ==================================================
//    End temporary per-page unique CSS (Splarka)
// ==================================================

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"), "");
    }
  }
}

function onArticleNavClick()
{
    var div = document.getElementById('mp3-nav');

    if(div.style.display == 'block')
        div.style.display = 'none';
    else
        div.style.display = 'block';
}

function addAlternatingRowColors()
{
    var infoboxes = getElementsByClass('infobox', document.getElementById('content'));

    if(infoboxes.length == 0)
        return;

    for(var k = 0; k < infoboxes.length; k++)
    {
        var infobox = infoboxes[k];

        var rows = infobox.getElementsByTagName('tr');
        var changeColor = false;

        for(var i = 0; i < rows.length; i++)
        {
            if(rows[i].className.indexOf('infoboxstopalt') != -1)
                break;

            var ths = rows[i].getElementsByTagName('th');

            if(ths.length > 0)
            {
                continue;
            }

            if(changeColor)
                rows[i].style.backgroundColor = '#f9f9f9';

            changeColor = !changeColor;
        }
    }
}

// </nowiki></pre>