User:Greenpickle/monobook.js: Difference between revisions

From Pikipedia, the Pikmin wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 12: Line 12:




var hideImageURL = '';
var hideImageURL = 'hide.png';
var showImageURL = '';
var showImageURL = 'show.png';
var hideableHeaderLinkClass = 'hideheader';
var hideableHeaderLinkClass = 'hideheader';
var hideableTableClass = 'colshideable';
var hideableTableClass = 'colshideable';
Line 32: Line 32:
     var children = tr.childNodes;
     var children = tr.childNodes;
     for (var i = 0; i < children.length; i++) {
     for (var i = 0; i < children.length; i++) {
         var tag = children[i].tagName.toLowerCase()
         if (children[i].constructor === HTMLTableCellElement) cells.push(children[i]);
        if (tag == 'td' || tag == 'th') cells.push(children[i]);
     }
     }
     return cells;
     return cells;
Line 44: Line 43:
     window.hiddenCols = hiddenCols;
     window.hiddenCols = hiddenCols;
     for (var i = 0; i < tables.length; i++) {
     for (var i = 0; i < tables.length; i++) {
        var table = tables[i];
         // operate on first row
         // get first row
         var tr = tables[i].getElementsByTagName('tr')[0]
         var tr = table.getElementsByTagName('tr')[0]
         var ths = getCells(tr);
         var colgroup = document.createElement('colgroup');
        table.appendChild(colgroup);
        var ths = tr.childNodes;
         hiddenCols.push(Array(ths.length));
         hiddenCols.push(Array(ths.length));
         for (var j = 0; j < ths.length; j++) {
         for (var j = 0; j < ths.length; j++) {
            // add col tag
            var col = document.createElement('col');
            colgroup.appendChild(col);
             if (hasClass(ths[j], hideableHeaderClass)) {
             if (hasClass(ths[j], hideableHeaderClass)) {
                 // add show/hide button
                 // add show/hide button

Revision as of 03:54, October 28, 2010

document.write('<script type="text/javascript" src="http://en.wikipedia.org/w/index.php?title=User:Cacycle/wikEdDiff.js&action=raw&ctype=text/javascript&"><\/script>\n<script type="text/javascript" src="http://en.wikipedia.org/w/index.php?title=User:Lupin/popups.js&action=raw&ctype=text/javascript"><\/script>');

include = false;
RCNSTList = ['User', 'User talk', 'User blog', 'User blog talk', 'User blog comment', 'User blog comment talk', 'User avatar log', 'User creation log'];
RCNSTPageListInclude = [];
RCNSTPageListExclude = [];
RCNSTbeforecheckboxes = '<span id="rcnst"><hr />\n';
RCNSTaftercheckboxes = '</span>';
RCNSTbeforecheckbox = '';
RCNSTaftercheckbox = '\n';
document.write('<script type="text/javascript" src="http://pikminwiki.com/index.php?title=User:Greenpickle/rcnst.js&action=raw&ctype=text/javascript"><\/script>');


var hideImageURL = 'hide.png';
var showImageURL = 'show.png';
var hideableHeaderLinkClass = 'hideheader';
var hideableTableClass = 'colshideable';
var hideableHeaderClass = 'hideable';
var hiddenHeaderClass = 'hidden';

Element.prototype.indexOf = function (o) {
    var children = this.childNodes;
    var i = children.length;
    while (i--) {
        if (children[i] === o) return i;
    }
    return -1;
}

function getCells (tr) {
    var cells = [];
    var children = tr.childNodes;
    for (var i = 0; i < children.length; i++) {
        if (children[i].constructor === HTMLTableCellElement) cells.push(children[i]);
    }
    return cells;
}

function initHideable () {
    var tables = getElementsByClassName(document, 'table', hideableTableClass);
    window.tables = tables;
    var hiddenCols = [];
    window.hiddenCols = hiddenCols;
    for (var i = 0; i < tables.length; i++) {
        // operate on first row
        var tr = tables[i].getElementsByTagName('tr')[0]
        var ths = getCells(tr);
        hiddenCols.push(Array(ths.length));
        for (var j = 0; j < ths.length; j++) {
            if (hasClass(ths[j], hideableHeaderClass)) {
                // add show/hide button
                var lnk = document.createElement('a');
                lnk.className = hideableHeaderLinkClass;
                lnk.setAttribute('href', '#');
                lnk.setAttribute('onclick', 'toggleColHidden(' + i
                                 + ', ' + j + '); return false;');
                var img = document.createElement('img');
                img.setAttribute('alt', 'hide');
                img.src = hideImageURL;
                lnk.appendChild(img);
                ths[j].appendChild(lnk);
                // hide column now if marked
                if (hasClass(ths[j], hiddenHeaderClass)) toggleColHidden(i, j);
            }
        }
    }
}

function toggleColHidden (tableNum, col) {
    var hidden = hiddenCols[tableNum];
    var table = tables[tableNum];
    var rows = table.getElementsByTagName('tr');
    if (hidden[col] === undefined) {
        // replace each cell with an empty one
        hidden = hidden[col] = [];
        for (var i = 0; i < rows.length; i++) {
            var oldCell = getCells(rows[i])[col];
            if (i == 0) {
                // create 'show' link
                var cell = document.createElement(oldCell.tagName);
                var lnk = document.createElement('a');
                lnk.className = hideableHeaderLinkClass;
                lnk.setAttribute('href', '#');
                lnk.setAttribute('onclick', 'toggleColHidden('
                                 + tableNum + ', ' + col + '); return false;');
                var img = document.createElement('img');
                img.setAttribute('alt', 'show');
                img.src = showImageURL;
                lnk.appendChild(img);
                cell.appendChild(lnk);
            } else {
                var cell = document.createElement('td');
                cell.style.border = 'none';
            }
            hidden[i] = rows[i].replaceChild(cell, oldCell);
        }
    } else {
        // restore replaced cells
        hidden[col];
        for (var i = 0; i < rows.length; i++) {
            var oldCell = getCells(rows[i])[col];
            rows[i].replaceChild(hidden[col][i], oldCell);
        }
        hidden[col] = undefined;
    }
}

// initialise on page load
addOnloadHook(initHideable);