Difference between revisions of "MediaWiki:Common.js"

Jump to navigationJump to search
Line 1: Line 1:
 
/* <pre><nowiki> */
 
/* <pre><nowiki> */
  
// global XHRs
+
document.write('<script type="text/javascript" src="'
var xhrSummaries, xhrReasons, xhrPreloads;
+
    + '/index.php?title=MediaWiki:Functions.js&action=raw&ctype=text/javascript"></script>');
var storagePresent = (typeof(globalStorage) != 'undefined');
+
   
var pageName;
 
  
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
+
// onload stuff
 +
var firstRun = true;
  
// onload stuff
+
function loadFunc()
 +
{
 +
    if(firstRun)
 +
        firstRun = false;
 +
    else
 +
        return;
  
function loadFunc()
+
    initFunctionsJS();
{
 
    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();
+
    // DEPRECATED
 +
    if(document.getElementById('infoboxinternal') != null && document.getElementById('infoboxend') != null)
 +
    {
 +
        document.getElementById('infoboxend').innerHTML = '<a id="infoboxtoggle" href="javascript:infoboxToggle()">[Hide]</a>';
 +
    }
  
    if(document.getElementById('infoboxinternal') != null)
+
    addHideButtons();
    {
 
        document.getElementById('infoboxend').innerHTML = '<a id="infoboxtoggle" href="javascript:infoboxToggle()">[Hide]</a>';
 
    }
 
  
    if(document.getElementById('appearances-list') != null)
+
    if(document.getElementById('mp3-navlink') != null)
    {
+
    {
        document.getElementById('appearances-control').style.display = 'block';
+
        document.getElementById('mp3-navlink').onclick = onArticleNavClick;
        document.getElementById('appearances-list').style.display = 'none';
+
        document.getElementById('mp3-navlink').getElementsByTagName('a')[0].href = 'javascript:void(0)';
        document.getElementById('appearances-control').innerHTML = '<a id="appearances-toggle" href="javascript:divToggle(\'appearances\')">[Show]</a>';
+
    }
    }
 
  
    if(document.getElementById('sources-list') != null)
+
    if(window.storagePresent)
    {
+
        initVisibility();
        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)
+
    rewriteSearchFormLink();
    {
+
    fillEditSummaries();
        document.getElementById('mp3-navlink').onclick = onArticleNavClick;
+
    fillDeleteReasons();
        document.getElementById('mp3-navlink').getElementsByTagName('a')[0].href = 'javascript:void(0)';
+
    fillPreloads();
    }
 
  
    if(storagePresent)
+
    substUsername();
        initVisibility();
+
    substUsernameTOC();
 +
    rewriteTitle();
 +
    showEras('title-eraicons');
 +
    showEras('title-shortcut');
 +
    rewriteHover();
 +
    addAlternatingRowColors();
 +
    replaceSearchIcon();
 +
    fixSearch();
  
    fillEditSummaries();
+
    var body = document.getElementsByTagName('body')[0];
    fillDeleteReasons();
+
    var bodyClass = body.className;
    fillPreloads();
 
  
    bodyClassAdd();
+
    if(!bodyClass || (bodyClass.indexOf('page-') == -1))
    substUsername();
+
    {
    rewriteTitle();
+
        var page = window.pageName.replace(/\W/g, '_');
    showEras('title-eraicons');
+
        body.className += ' page-' + page;
    showEras('title-shortcut');
+
    }
    rewriteHover();
 
    addAlternatingRowColors();
 
  
    if (window.location.href.indexOf("printable=yes") != -1)
+
    if(typeof(onPageLoad) != "undefined")
    {
+
    {
        document.write('<style type="text/css">/*<![CDATA[*/ @import "/index.php?title=MediaWiki:PrintableTweaks.css&action=raw&ctype=text/css"; /*]]>*/</style>');
+
        onPageLoad();
    }
+
    }
 +
}
  
    if(typeof onPageLoad != "undefined")
+
function infoboxToggle()
    {
+
{
        onPageLoad();
+
     var page = window.pageName.replace(/\W/g, '_');
    }
 
}
 
 
 
function infoboxToggle()
 
{
 
     var page = pageName.replace(/\W/g,'_');
 
 
     var nowShown;
 
     var nowShown;
  
    if(document.getElementById('infoboxtoggle').innerHTML == '[Hide]')
+
    if(document.getElementById('infoboxtoggle').innerHTML == '[Hide]')
    {
+
    {
        document.getElementById('infoboxinternal').style.display = 'none';
+
        document.getElementById('infoboxinternal').style.display = 'none';
        document.getElementById('infoboxtoggle').innerHTML = '[Show]';
+
        document.getElementById('infoboxtoggle').innerHTML = '[Show]';
        nowShown = false;
+
        nowShown = false;
    }
+
    }
    else
+
    else
    {
+
    {
        document.getElementById('infoboxinternal').style.display = 'block';
+
        document.getElementById('infoboxinternal').style.display = 'block';
        document.getElementById('infoboxtoggle').innerHTML = '[Hide]';
+
        document.getElementById('infoboxtoggle').innerHTML = '[Hide]';
        nowShown = true;
+
        nowShown = true;
    }
+
    }
  
     if(storagePresent)
+
     if(window.storagePresent)
 
     {
 
     {
 
         var storage = globalStorage[window.location.hostname];
 
         var storage = globalStorage[window.location.hostname];
Line 92: Line 89:
 
}
 
}
  
function divToggle(line)
+
function fillEditSummaries()
{
+
{
    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");
 
     var label = document.getElementById("wpSummaryLabel");
  
 
     if(label == null)
 
     if(label == null)
    {
 
 
         return;
 
         return;
    }
 
  
 
     var comboString = "Standard summaries: <select id='stdSummaries' onchange='onStdSummaryChange()'>";
 
     var comboString = "Standard summaries: <select id='stdSummaries' onchange='onStdSummaryChange()'>";
Line 119: Line 100:
 
     label.innerHTML = comboString + label.innerHTML;
 
     label.innerHTML = comboString + label.innerHTML;
  
     var request = getRequest();
+
     requestComboFill('stdSummaries', 'Template:Stdsummaries');
    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()
+
function onStdSummaryChange()
{
+
{
 
     var combo = document.getElementById("stdSummaries");
 
     var combo = document.getElementById("stdSummaries");
 
     var value = combo.options[combo.selectedIndex].value;
 
     var value = combo.options[combo.selectedIndex].value;
Line 133: Line 110:
 
     if(value != "")
 
     if(value != "")
 
         document.getElementById("wpSummary").value = value;
 
         document.getElementById("wpSummary").value = value;
}
+
}
  
function fillDeleteReasons()
+
function fillDeleteReasons()
{
+
{
 
     var label = document.getElementById("wpReason");
 
     var label = document.getElementById("wpReason");
  
 
     if(label == null)
 
     if(label == null)
    {
 
 
         return;
 
         return;
    }
 
  
 
     label = document.getElementById("contentSub");
 
     label = document.getElementById("contentSub");
  
 
     if(label == null)
 
     if(label == null)
    {
 
 
         return;
 
         return;
    }
 
  
 
     var comboString = "<br /><select id='stdReasons' onchange='onStdReasonChange()'>";
 
     var comboString = "<br /><select id='stdReasons' onchange='onStdReasonChange()'>";
Line 155: Line 128:
 
     label.innerHTML += comboString;
 
     label.innerHTML += comboString;
  
     var request = getRequest();
+
     requestComboFill('stdReasons', "Template:Stdreasons");
    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()
+
function onStdReasonChange()
{
+
{
 
     var combo = document.getElementById("stdReasons");
 
     var combo = document.getElementById("stdReasons");
 
     var value = combo.options[combo.selectedIndex].value;
 
     var value = combo.options[combo.selectedIndex].value;
Line 169: Line 138:
 
     if(value != "")
 
     if(value != "")
 
         document.getElementById("wpReason").value = value;
 
         document.getElementById("wpReason").value = value;
}
+
}
  
function fillPreloads()
+
function fillPreloads()
{
+
{
 
     var div = document.getElementById("lf-preload");
 
     var div = document.getElementById("lf-preload");
  
 
     if(div == null)
 
     if(div == null)
    {
 
 
         return;
 
         return;
    }
 
  
 
     div.style.display = 'block';
 
     div.style.display = 'block';
Line 186: Line 153:
 
     comboString += "</select>";
 
     comboString += "</select>";
 
     span.innerHTML = comboString;
 
     span.innerHTML = comboString;
 +
   
 +
    span = document.getElementById('lf-preload-pagename');
 +
    span.innerHTML = '<input type="text" class="textbox" />';
 +
    span = document.getElementById('lf-preload-button');
 +
    span.innerHTML = '<input type="button" class="button" value="Insert" onclick="doCustomPreload()" />';
  
     var request = getRequest();
+
     requestComboFill('stdPreloads', "Template:Stdpreloads");
    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()
+
function doCustomPreload()
{
+
{
 +
    doPreload(document.getElementById('lf-preload-pagename').getElementsByTagName('input')[0].value);
 +
}
 +
 
 +
function onPreloadChange()
 +
{
 
     var combo = document.getElementById("stdPreloads");
 
     var combo = document.getElementById("stdPreloads");
 
     var value = combo.options[combo.selectedIndex].value;
 
     var value = combo.options[combo.selectedIndex].value;
Line 204: Line 177:
 
     value = "Template:" + value + "/preload";
 
     value = "Template:" + value + "/preload";
 
     value = value.replace(" ", "_");
 
     value = value.replace(" ", "_");
     window.location.href += "&preload=" + value;
+
     doPreload(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}}
 
  
 
// ============================================================
 
// ============================================================
Line 299: Line 185:
 
function rewriteTitle()
 
function rewriteTitle()
 
{
 
{
     if(typeof(SKIP_TITLE_REWRITE) != 'undefined' && SKIP_TITLE_REWRITE)
+
     if(typeof(window.SKIP_TITLE_REWRITE) != 'undefined' && window.SKIP_TITLE_REWRITE)
 
         return;
 
         return;
  
 
     var titleDiv = document.getElementById('title-meta');
 
     var titleDiv = document.getElementById('title-meta');
  
     if(titleDiv == null || titleDiv == undefined)
+
     if(titleDiv == null)
 
         return;
 
         return;
  
 
     var cloneNode = titleDiv.cloneNode(true);
 
     var cloneNode = titleDiv.cloneNode(true);
     var firstHeading = getElementsByClass('firstHeading', document.getElementById('content'), 'h1')[0];
+
     var firstHeading = getFirstHeading();
 
     var node = firstHeading.childNodes[0];
 
     var node = firstHeading.childNodes[0];
  
Line 330: Line 216:
  
 
     var cloneNode = titleDiv.cloneNode(true);
 
     var cloneNode = titleDiv.cloneNode(true);
     var firstHeading = getElementsByClass('firstHeading', document.getElementById('content'), 'h1')[0];
+
     var firstHeading = getFirstHeading();
 
     firstHeading.insertBefore(cloneNode, firstHeading.childNodes[0]);
 
     firstHeading.insertBefore(cloneNode, firstHeading.childNodes[0]);
 
     cloneNode.style.display = "block";
 
     cloneNode.style.display = "block";
Line 340: Line 226:
 
     var storage = globalStorage[window.location.hostname];
 
     var storage = globalStorage[window.location.hostname];
  
     var page = pageName.replace(/\W/g,'_');
+
     var page = window.pageName.replace(/\W/g,'_');
 
     var show = storage.getItem('infoboxshow-' + page);
 
     var show = storage.getItem('infoboxshow-' + page);
  
Line 347: Line 233:
 
         infoboxToggle();
 
         infoboxToggle();
 
     }
 
     }
}
+
   
 
+
    var hidables = getElementsByClass('hidable');
addOnloadHook(loadFunc);
+
   
 
+
    for(var i = 0; i < hidables.length; i++)
//[[User:Editcount/editcount.js]]
+
    {
if (document.title.indexOf('User:Editcount/editcount.js') != -1) {
+
        show = storage.getItem('hidableshow-' + i  + '_' + page);
  document.write('<script type="text/javascript" src="'  
+
       
    + 'http://starwars.wikia.com/index.php?title=User:Editcount/editcount.js'  
+
        if(show == 'false')
    + '&action=raw&ctype=text/javascript&dontcountme=s"></script>'); }
+
         {
 
+
            var content = getElementsByClass('hidable-content', hidables[i]);
// ==================================================
+
            var button = getElementsByClass('hidable-button', hidables[i]);
//  Begin temporary per-page unique CSS (Splarka)
+
           
// ==================================================
+
            if(content != null && content.length > 0 &&
// The following script sort of emulates the per-page
+
                button != null && button.length > 0 && content[0].style.display != 'none')
// unique class functionality that is added to future
+
            {
// version of MediaWiki.
+
                button[0].onclick('bypass');
//
+
            }
// This takes <h1 class="firstHeading"> text.Content
+
        }
// and makes it a class in the format:
+
        else if(show == 'true')
//  page-Page_Title_Here
+
        {
// with all spaces and non-alphanumeric characters
+
            var content = getElementsByClass('hidable-content', hidables[i]);
// converted to underscores.
+
            var button = getElementsByClass('hidable-button', hidables[i]);
//
+
           
// So one can style Special:Contributions with a css
+
            if(content != null && content.length > 0 &&
//  command like: body.page-User_Contributions {}
+
                button != null && button.length > 0 && content[0].style.display == 'none')
//
+
            {
// Query string debugging tools:
+
                button[0].onclick('bypass');
//  &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"), "");
 
 
     }
 
     }
  }
 
 
}
 
}
  
Line 464: Line 308:
 
     }
 
     }
 
}
 
}
 +
 +
function addHideButtons()
 +
{
 +
    var hidables = getElementsByClass('hidable');
 +
   
 +
    for(var i = 0; i < hidables.length; i++)
 +
    {
 +
        var box = hidables[i];
 +
        var button = getElementsByClass('hidable-button', box, 'span');
 +
       
 +
        if(button != null && button.length > 0)
 +
        {
 +
            button = button[0];
 +
           
 +
            button.onclick = toggleHidable;
 +
            button.appendChild(document.createTextNode('[Hide]'));
 +
 +
            if(new ClassTester('start-hidden').isMatch(box))
 +
                button.onclick('bypass');
 +
        }
 +
    }
 +
}
 +
 +
function toggleHidable(bypassStorage)
 +
{
 +
    var parent = getParentByClass('hidable', this);
 +
    var content = getElementsByClass('hidable-content', parent);
 +
    var nowShown;
 +
   
 +
    if(content != null && content.length > 0)
 +
    {
 +
        content = content[0];
 +
       
 +
        if(content.style.display == 'none')
 +
        {
 +
            content.style.display = content.oldDisplayStyle;
 +
            this.firstChild.nodeValue = '[Hide]';
 +
            nowShown = true;
 +
        }
 +
        else
 +
        {
 +
            content.oldDisplayStyle = content.style.display;
 +
            content.style.display = 'none';
 +
            this.firstChild.nodeValue = '[Show]';
 +
            nowShown = false;
 +
        }
 +
       
 +
        if(window.storagePresent && (typeof(bypassStorage) == 'undefined' || bypassStorage != 'bypass'))
 +
        {
 +
            var page = window.pageName.replace(/\W/g, '_');
 +
            var items = getElementsByClass('hidable');
 +
            var item = -1;
 +
           
 +
            for(var i = 0; i < items.length; i++)
 +
            {
 +
                if(items[i] == parent)
 +
                {
 +
                    item = i;
 +
                    break;
 +
                }
 +
            }
 +
           
 +
            if(item == -1)
 +
            {
 +
                return;
 +
            }
 +
       
 +
            var storage = globalStorage[window.location.hostname];
 +
            storage.setItem('hidableshow-' + item + '_' + page, nowShown);
 +
        }
 +
    }
 +
}
 +
 +
function substUsernameTOC()
 +
{
 +
    var toc = document.getElementById('toc');
 +
    var userpage = document.getElementById('pt-userpage');
 +
   
 +
    if(!userpage || !toc)
 +
        return;
 +
       
 +
    var username = userpage.firstChild.firstChild.nodeValue;
 +
    var elements = getElementsByClass('toctext', toc, 'span');
 +
 +
    for(var i = 0; i < elements.length; i++)
 +
        elements[i].firstChild.nodeValue = elements[i].firstChild.nodeValue.replace('<insert name here>', username);
 +
}
 +
 +
function replaceSearchIcon()
 +
{
 +
    var innerDiv;
 +
 +
    var searchbox = document.getElementById('searchBody');
 +
 +
    if(searchbox)
 +
    {
 +
        innerDiv = searchbox.getElementsByTagName('div')[0];
 +
        var link = innerDiv.getElementsByTagName('a')[0];
 +
 +
        if(link)
 +
            innerDiv.removeChild(link);
 +
    }
 +
    else
 +
    {
 +
        searchbox = getElementsByClass('r_boxContent', document.getElementById('searchBox'), 'div')[0];
 +
        innerDiv = searchbox.getElementsByTagName('div')[1];
 +
    }
 +
   
 +
    var loader = new ContentLoader();
 +
    loader.div = innerDiv;
 +
    loader.callback = onSearchIconsArrival;
 +
    loader.send('/index.php?title=Template:Searchicons&action=raw');
 +
}
 +
 +
function rand(n)
 +
{
 +
    return Math.round(Math.random() * n);
 +
}
 +
 +
 +
function onSearchIconsArrival()
 +
{
 +
    var lines = this.text.split('\n');
 +
    var line = lines[rand(lines.length - 1)];
 +
    var pos = line.indexOf(' ');
 +
 
 +
    var link = document.createElement('div');
 +
//    link.href = '/index.php?title=Special:Search&adv=1';
 +
    link.id = 'search-icon-wrapper';
 +
    var img = document.createElement('img');
 +
    img.alt = 'Search';
 +
    img.src = (pos == -1) ? line : line.substring(0, pos);
 +
    link.appendChild(img);
 +
   
 +
    this.div.insertBefore(link, this.div.firstChild);
 +
 +
    var div = document.createElement('div');
 +
    div.id = 'search-popup';
 +
    div.style.display = 'none';
 +
    var ul = document.createElement('ul');
 +
   
 +
    var li;
 +
    var a;
 +
 +
    li = document.createElement('li');
 +
    a = document.createElement('a');
 +
    a.href = '/index.php?title=Special:Search&adv=1';
 +
    a.appendChild(document.createTextNode('Advanced search'));
 +
    li.appendChild(a);
 +
    ul.appendChild(li);
 +
   
 +
    li = document.createElement('li');
 +
    a = document.createElement('a');
 +
    a.href = (pos == -1) ? 'javascript:emptySearchDesc()' : '/wiki/' + line.substring(pos + 1);
 +
    a.appendChild(document.createTextNode("What's this? (" + ((pos == -1) ? 'NO DESCRIPTION' : line.substring(pos + 1)) + ')'));
 +
    li.appendChild(a);
 +
    ul.appendChild(li);
 +
 +
    li = document.createElement('li');
 +
    a = document.createElement('a');
 +
    a.href = 'javascript:closeSearchPopup()';
 +
    a.appendChild(document.createTextNode("Close"));
 +
    li.appendChild(a);
 +
    ul.appendChild(li);
 +
 +
    var container = document.getElementById('globalWrapper');
 +
    if(!container)
 +
        container = document.getElementById('container');
 +
 +
    div.appendChild(ul);
 +
    container.appendChild(div);
 +
 +
    link.onclick = openSearchPopup;
 +
}
 +
 +
function openSearchPopup(event)
 +
{
 +
    var div = document.getElementById('search-popup');
 +
    var e = event || window.event;
 +
   
 +
    div.style.display = (div.style.display == 'none') ? 'block' : 'none';
 +
    div.style.left = e.clientX + 'px';
 +
    div.style.top = (e.clientY + document.documentElement.scrollTop) + 'px';
 +
}
 +
 +
function closeSearchPopup()
 +
{
 +
    document.getElementById('search-popup').style.display = 'none';
 +
}
 +
 +
function emptySearchDesc()
 +
{
 +
    alert('No description exists for this search icon. Please contact the administrators to resolve this problem.');
 +
}
 +
 +
// Reskin parser script from [[Uncyclopedia:MediaWiki:Uncyclopedia.js]]
 +
skinjs = {
 +
    "Logout": "Logout.js"
 +
}
 +
 +
var re = RegExp("(.*) - Wookieepedia, the Star Wars Wiki");
 +
var matches = re.exec(document.title);
 +
 +
var skinNamejs;
 +
 +
if (matches) {
 +
    if (skinjs[matches[1]] != undefined) {
 +
        skinNamejs = (skinjs[matches[1]].length > 0) ? skinjs[matches[1]] : matches[1] + '.js';
 +
        document.write('<script type="text/javascript" src="/index.php?title=MediaWiki:Skin/' + skinNamejs + '&action=raw&ctype=text/javascript"></script>');
 +
    }
 +
}
 +
 +
function fixSearch()
 +
{
 +
    var button = document.getElementById('searchSubmit');
 +
 +
    if(button)
 +
        button.name = 'go';
 +
}
 +
 +
//addOnloadHook(loadFunc);
 +
 +
YAHOO.util.Event.onDOMReady(loadFunc);
  
 
// </nowiki></pre>
 
// </nowiki></pre>

Revision as of 02:58, 20 August 2007

/* <pre><nowiki> */

document.write('<script type="text/javascript" src="' 
    + '/index.php?title=MediaWiki:Functions.js&action=raw&ctype=text/javascript"></script>');
    

// onload stuff
var firstRun = true;

function loadFunc()
{
    if(firstRun)
        firstRun = false;
    else
        return;

    initFunctionsJS();

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

    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();

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

    substUsername();
    substUsernameTOC();
    rewriteTitle();
    showEras('title-eraicons');
    showEras('title-shortcut');
    rewriteHover();
    addAlternatingRowColors();
    replaceSearchIcon();
    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();
    }
}

function infoboxToggle()
{
    var page = window.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(window.storagePresent)
    {
        var storage = globalStorage[window.location.hostname];
        storage.setItem('infoboxshow-' + page, nowShown);
    }
}

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;

    requestComboFill('stdSummaries', 'Template:Stdsummaries');
}

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;

    requestComboFill('stdReasons', "Template:Stdreasons");
}

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;
    
    span = document.getElementById('lf-preload-pagename');
    span.innerHTML = '<input type="text" class="textbox" />';
    span = document.getElementById('lf-preload-button');
    span.innerHTML = '<input type="button" class="button" value="Insert" onclick="doCustomPreload()" />';

    requestComboFill('stdPreloads', "Template:Stdpreloads");
}

function doCustomPreload()
{
    doPreload(document.getElementById('lf-preload-pagename').getElementsByTagName('input')[0].value);
}

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

    if(value == "")
        return;

    value = "Template:" + value + "/preload";
    value = value.replace(" ", "_");
    doPreload(value);
}

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

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

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

    if(titleDiv == null)
        return;

    var cloneNode = titleDiv.cloneNode(true);
    var firstHeading = getFirstHeading();
    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 = getFirstHeading();
    firstHeading.insertBefore(cloneNode, firstHeading.childNodes[0]);
    cloneNode.style.display = "block";
}
// END JavaScript title rewrite

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

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

    if(show == 'false')
    {
        infoboxToggle();
    }
    
    var hidables = getElementsByClass('hidable');
    
    for(var i = 0; i < hidables.length; i++)
    {
        show = storage.getItem('hidableshow-' + i  + '_' + page);
        
        if(show == 'false')
        {
            var content = getElementsByClass('hidable-content', hidables[i]);
            var button = getElementsByClass('hidable-button', hidables[i]);
            
            if(content != null && content.length > 0 &&
                button != null && button.length > 0 && content[0].style.display != 'none')
            {
                button[0].onclick('bypass');
            }
        }
        else if(show == 'true')
        {
            var content = getElementsByClass('hidable-content', hidables[i]);
            var button = getElementsByClass('hidable-button', hidables[i]);
            
            if(content != null && content.length > 0 &&
                button != null && button.length > 0 && content[0].style.display == 'none')
            {
                button[0].onclick('bypass');
            }
        }
    }
}

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;
        }
    }
}

function addHideButtons()
{
    var hidables = getElementsByClass('hidable');
    
    for(var i = 0; i < hidables.length; i++)
    {
        var box = hidables[i];
        var button = getElementsByClass('hidable-button', box, 'span');
        
        if(button != null && button.length > 0)
        {
            button = button[0];
            
            button.onclick = toggleHidable;
            button.appendChild(document.createTextNode('[Hide]'));

            if(new ClassTester('start-hidden').isMatch(box))
                button.onclick('bypass');
        }
    }
}

function toggleHidable(bypassStorage)
{
    var parent = getParentByClass('hidable', this);
    var content = getElementsByClass('hidable-content', parent);
    var nowShown;
    
    if(content != null && content.length > 0)
    {
        content = content[0];
        
        if(content.style.display == 'none')
        {
            content.style.display = content.oldDisplayStyle;
            this.firstChild.nodeValue = '[Hide]';
            nowShown = true;
        }
        else
        {
            content.oldDisplayStyle = content.style.display;
            content.style.display = 'none';
            this.firstChild.nodeValue = '[Show]';
            nowShown = false;
        }
        
        if(window.storagePresent && (typeof(bypassStorage) == 'undefined' || bypassStorage != 'bypass'))
        {
            var page = window.pageName.replace(/\W/g, '_');
            var items = getElementsByClass('hidable');
            var item = -1;
            
            for(var i = 0; i < items.length; i++)
            {
                if(items[i] == parent)
                {
                    item = i;
                    break;
                }
            }
            
            if(item == -1)
            {
                return;
            }
        
            var storage = globalStorage[window.location.hostname];
            storage.setItem('hidableshow-' + item + '_' + page, nowShown);
        }
    }
}

function substUsernameTOC()
{
    var toc = document.getElementById('toc');
    var userpage = document.getElementById('pt-userpage');
    
    if(!userpage || !toc)
        return;
        
    var username = userpage.firstChild.firstChild.nodeValue;
    var elements = getElementsByClass('toctext', toc, 'span');

    for(var i = 0; i < elements.length; i++)
        elements[i].firstChild.nodeValue = elements[i].firstChild.nodeValue.replace('<insert name here>', username);
}

function replaceSearchIcon()
{
    var innerDiv;

    var searchbox = document.getElementById('searchBody');

    if(searchbox)
    {
        innerDiv = searchbox.getElementsByTagName('div')[0];
        var link = innerDiv.getElementsByTagName('a')[0];

        if(link)
            innerDiv.removeChild(link);
    }
    else
    {
        searchbox = getElementsByClass('r_boxContent', document.getElementById('searchBox'), 'div')[0];
        innerDiv = searchbox.getElementsByTagName('div')[1];
    }
    
    var loader = new ContentLoader();
    loader.div = innerDiv;
    loader.callback = onSearchIconsArrival;
    loader.send('/index.php?title=Template:Searchicons&action=raw');
}

function rand(n)
{
    return Math.round(Math.random() * n);
}


function onSearchIconsArrival()
{
    var lines = this.text.split('\n');
    var line = lines[rand(lines.length - 1)];
    var pos = line.indexOf(' ');
   
    var link = document.createElement('div');
//    link.href = '/index.php?title=Special:Search&adv=1';
    link.id = 'search-icon-wrapper';
    var img = document.createElement('img');
    img.alt = 'Search';
    img.src = (pos == -1) ? line : line.substring(0, pos);
    link.appendChild(img);
    
    this.div.insertBefore(link, this.div.firstChild);

    var div = document.createElement('div');
    div.id = 'search-popup';
    div.style.display = 'none';
    var ul = document.createElement('ul');
    
    var li;
    var a;

    li = document.createElement('li');
    a = document.createElement('a');
    a.href = '/index.php?title=Special:Search&adv=1';
    a.appendChild(document.createTextNode('Advanced search'));
    li.appendChild(a);
    ul.appendChild(li);
    
    li = document.createElement('li');
    a = document.createElement('a');
    a.href = (pos == -1) ? 'javascript:emptySearchDesc()' : '/wiki/' + line.substring(pos + 1);
    a.appendChild(document.createTextNode("What's this? (" + ((pos == -1) ? 'NO DESCRIPTION' : line.substring(pos + 1)) + ')'));
    li.appendChild(a);
    ul.appendChild(li);

    li = document.createElement('li');
    a = document.createElement('a');
    a.href = 'javascript:closeSearchPopup()';
    a.appendChild(document.createTextNode("Close"));
    li.appendChild(a);
    ul.appendChild(li);

    var container = document.getElementById('globalWrapper');
    if(!container)
        container = document.getElementById('container');

    div.appendChild(ul);
    container.appendChild(div);

    link.onclick = openSearchPopup;
}

function openSearchPopup(event)
{
    var div = document.getElementById('search-popup');
    var e = event || window.event;
    
    div.style.display = (div.style.display == 'none') ? 'block' : 'none';
    div.style.left = e.clientX + 'px';
    div.style.top = (e.clientY + document.documentElement.scrollTop) + 'px';
}

function closeSearchPopup()
{
    document.getElementById('search-popup').style.display = 'none';
}

function emptySearchDesc()
{
    alert('No description exists for this search icon. Please contact the administrators to resolve this problem.');
}

// Reskin parser script from [[Uncyclopedia:MediaWiki:Uncyclopedia.js]]
skinjs = {
    "Logout": "Logout.js"
}

var re = RegExp("(.*) - Wookieepedia, the Star Wars Wiki");
var matches = re.exec(document.title);

var skinNamejs;

if (matches) {
    if (skinjs[matches[1]] != undefined) {
        skinNamejs = (skinjs[matches[1]].length > 0) ? skinjs[matches[1]] : matches[1] + '.js';
        document.write('<script type="text/javascript" src="/index.php?title=MediaWiki:Skin/' + skinNamejs + '&action=raw&ctype=text/javascript"></script>');
    }
}

function fixSearch()
{
    var button = document.getElementById('searchSubmit');

    if(button)
        button.name = 'go';
}

//addOnloadHook(loadFunc);

YAHOO.util.Event.onDOMReady(loadFunc);

// </nowiki></pre>