MediaWiki:Common.js

From Ultronomicon
Jump to: navigation, 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: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences
/* Any JavaScript here will be loaded for all users on every page load. */

function alternate_init() {
    var tables = getElementsByClassName(document, "table", "alternateable");
    for(var ti = 0; ti < tables.length; ti++) {
        ts_alternate(tables[ti]);
    }
}
addOnloadHook(alternate_init);
 
function ts_alternate(table) {
    // Take object table and get all it's tbodies.
    var tableBodies = table.getElementsByTagName("tbody");
    // Loop through these tbodies
    for (var i = 0; i < tableBodies.length; i++) {
        // Take the tbody, and get all it's rows
        var tableRows = tableBodies[i].getElementsByTagName("tr");
        // Loop through these rows
        // Start at 1 because we want to leave the heading row untouched
        var curRow = 0
        var sections = new Array();
        var s = 0;
        sections[s] = new Array();
        for (var j = 0; j < tableRows.length; j++) {
            if(tableRows[j].parentNode != tableBodies[i])
                continue;
            if(tableRows[j].tagName != 'TR')
                continue;
            var ignore = false;
            var oldClasses = tableRows[j].className.split(" ");
 
            var newClassName = "";
            for (var k = 0; k < oldClasses.length; k++) {
                if (oldClasses[k] == "alternate-ignore")
                    ignore = true;
                if (oldClasses[k] == "alternate-reset") {
                    curRow = 0;
                    ignore = true;
                }                    
                if (oldClasses[k] != "" && oldClasses[k] != "even" && oldClasses[k] != "odd")
                    newClassName += oldClasses[k] + " ";
            }
            if(curRow==0 && j>0) {
                s++;
                sections[s] = new Array();
            }
            if(ignore) {
                continue;
            }
            sections[s].push(tableRows[j]);
            curRow++;
        }
        for(var s = 0; s < sections.length; s++) {
            var l = sections[s].length;
            if (l<=1)
                continue;
            for(var r = 0; r < l; r++) {
                var row = sections[s][r];
                var oldClasses = row.className.split(" ");
 
                var newClassName = "";
                for (var k = 0; k < oldClasses.length; k++) {         
                    if (oldClasses[k] != "" && oldClasses[k] != "even" && oldClasses[k] != "odd")
                        newClassName += oldClasses[k] + " ";
                }
                row.className = newClassName + ((l-r) % 2 == 0 ? "odd" : "even");
            }
        }
    }
}