User:Greenpickle/monobook.js: Difference between revisions

From Pikipedia, the Pikmin wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 11: Line 11:
document.write('<script type="text/javascript" src="http://pikminwiki.com/index.php?title=User:Greenpickle/rcnst.js&action=raw&ctype=text/javascript"><\/script>');
document.write('<script type="text/javascript" src="http://pikminwiki.com/index.php?title=User:Greenpickle/rcnst.js&action=raw&ctype=text/javascript"><\/script>');


var switchableAnchorPrefix = '';
var switchableClass = 'switchable';
var switchableSectionClass = 'switch';
function initialiseSwitchable () {
    // get the elements we care about
    switchable = document.myGetElementsByClassName(switchableClass);
    // some functions
    switchable.createTab = function (label, isAnchor, i, j) {
        var tab = document.createElement('li');
        var child;
        if (isAnchor) {
            child = document.createElement('a');
            child.setAttribute('href',
                    'javascript:switchable.setVisible(' + i + ', ' + j + ');');
        } else child = document.createElement('strong');
        child.appendChild(document.createTextNode(label));
        tab.appendChild(child);
        return tab;
    }
    switchable.getVisible = function (i) {
        var visible = this[i].getAttribute('visiblesection');
        if (visible) visible = parseInt(visible);
        if (visible === null || isNaN(visible)) {
            visible = 0;
            this[i].setAttribute('visiblesection', visible.toString());
        }
        return Math.max(0, Math.min(visible, this[i].sections.length - 1));
    }
    switchable.updateVisible = function (i) {
        if (isNaN(parseInt(i))) {
            // update all switchables if no valid number given
            for (var i = 0; i < this.length; i++)
                this.updateVisible(i);
        } else {
            var visible = this.getVisible(i);
            var sections = this[i].sections;
            var tc = this[i].tabContainer;
            var currentTab;
            for (var j = 0; j < sections.length; j++) {
                if (j == visible) {
                    // change 'show' link
                    currentTab = this.createTab(sections[j].sectionName,
                                                    false);
                    currentTab.j = j;
                    tc.replaceChild(currentTab, tc.tabs[j]);
                    // show section if hidden
                    if (sections.hidden[j] !== undefined) {
                        this[i].appendChild(sections[j]);
                        sections.hidden[j] = undefined;
                    }
                } else {
                    // change 'show' link
                    if (tc.currentTab !== undefined && tc.currentTab.j == j)
                        tc.replaceChild(tc.tabs[j], tc.currentTab);
                    // hide section if visible
                    if (sections.hidden[j] === undefined)
                        sections.hidden[j] = this[i].removeChild(sections[j]);
                }
            }
            if (currentTab !== undefined) tc.currentTab = currentTab;
        }
    }
    switchable.setVisible = function (i, j) {
        this[i].setAttribute('visiblesection', j);
        this.updateVisible(i);
    }
    // initialise
    for (var i = 0; i < switchable.length; i++) {
        var sections = switchable[i].myGetElementsByClassName(
                                                    switchableSectionClass);
        sections.hidden = Array(sections.length);
        switchable[i].sections = sections;
        // create show/hide anchors
        var tabContainer = document.createElement('ul');
        switchable[i].appendChild(tabContainer);
        switchable[i].tabContainer = tabContainer;
        tabContainer.tabs = [];
        for (var j = 0; j < sections.length; j++) {
            // re-append section to place it after links
            switchable[i].appendChild(sections[j]);
            // use section's name if it has one
            var sectionName = sections[j].getAttribute('sectionname');
            if (!sectionName) sectionName = j.toString();
            sections[j].sectionName = sectionName;
            // create anchor
            var tab = switchable.createTab(
                            switchableAnchorPrefix + sectionName, true, i, j);
            tabContainer.appendChild(tab);
            tabContainer.tabs.push(tab);
        }
    }
    this.switchable = switchable;
    // initial show/hide
    switchable.updateVisible();
}
Node.prototype.myGetElementsByClassName = function (cls) {
    var result = [];
    var pool = this.getElementsByTagName("*");
    var re = new RegExp('\\b' + cls + '\\b');
    for (var i = 0; i < pool.length; i++)
        if (re.test(pool[i].className)) result.push(pool[i]);
    return result;
}
// initialise on page load
addOnloadHook(initialiseSwitchable);


var hideImageURL = 'http://img87.imageshack.us/img87/5183/hide.png';
var hideImageURL = 'http://img87.imageshack.us/img87/5183/hide.png';

Revision as of 14:27, 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 switchableAnchorPrefix = '';
var switchableClass = 'switchable';
var switchableSectionClass = 'switch';

function initialiseSwitchable () {
    // get the elements we care about
    switchable = document.myGetElementsByClassName(switchableClass);

    // some functions
    switchable.createTab = function (label, isAnchor, i, j) {
        var tab = document.createElement('li');
        var child;
        if (isAnchor) {
            child = document.createElement('a');
            child.setAttribute('href',
                    'javascript:switchable.setVisible(' + i + ', ' + j + ');');
        } else child = document.createElement('strong');
        child.appendChild(document.createTextNode(label));
        tab.appendChild(child);
        return tab;
    }

    switchable.getVisible = function (i) {
        var visible = this[i].getAttribute('visiblesection');
        if (visible) visible = parseInt(visible);
        if (visible === null || isNaN(visible)) {
            visible = 0;
            this[i].setAttribute('visiblesection', visible.toString());
        }
        return Math.max(0, Math.min(visible, this[i].sections.length - 1));
    }

    switchable.updateVisible = function (i) {
        if (isNaN(parseInt(i))) {
            // update all switchables if no valid number given
            for (var i = 0; i < this.length; i++)
                this.updateVisible(i);
        } else {
            var visible = this.getVisible(i);
            var sections = this[i].sections;
            var tc = this[i].tabContainer;
            var currentTab;
            for (var j = 0; j < sections.length; j++) {
                if (j == visible) {
                    // change 'show' link
                    currentTab = this.createTab(sections[j].sectionName,
                                                    false);
                    currentTab.j = j;
                    tc.replaceChild(currentTab, tc.tabs[j]);
                    // show section if hidden
                    if (sections.hidden[j] !== undefined) {
                        this[i].appendChild(sections[j]);
                        sections.hidden[j] = undefined;
                    }
                } else {
                    // change 'show' link
                    if (tc.currentTab !== undefined && tc.currentTab.j == j)
                        tc.replaceChild(tc.tabs[j], tc.currentTab);
                    // hide section if visible
                    if (sections.hidden[j] === undefined)
                        sections.hidden[j] = this[i].removeChild(sections[j]);
                }
            }
            if (currentTab !== undefined) tc.currentTab = currentTab;
        }
    }

    switchable.setVisible = function (i, j) {
        this[i].setAttribute('visiblesection', j);
        this.updateVisible(i);
    }

    // initialise
    for (var i = 0; i < switchable.length; i++) {
        var sections = switchable[i].myGetElementsByClassName(
                                                    switchableSectionClass);
        sections.hidden = Array(sections.length);
        switchable[i].sections = sections;
        // create show/hide anchors
        var tabContainer = document.createElement('ul');
        switchable[i].appendChild(tabContainer);
        switchable[i].tabContainer = tabContainer;
        tabContainer.tabs = [];
        for (var j = 0; j < sections.length; j++) {
            // re-append section to place it after links
            switchable[i].appendChild(sections[j]);
            // use section's name if it has one
            var sectionName = sections[j].getAttribute('sectionname');
            if (!sectionName) sectionName = j.toString();
            sections[j].sectionName = sectionName;
            // create anchor
            var tab = switchable.createTab(
                            switchableAnchorPrefix + sectionName, true, i, j);
            tabContainer.appendChild(tab);
            tabContainer.tabs.push(tab);
        }
    }
    this.switchable = switchable;
    // initial show/hide
    switchable.updateVisible();
}

Node.prototype.myGetElementsByClassName = function (cls) {
    var result = [];
    var pool = this.getElementsByTagName("*");
    var re = new RegExp('\\b' + cls + '\\b');
    for (var i = 0; i < pool.length; i++)
        if (re.test(pool[i].className)) result.push(pool[i]);
    return result;
}

// initialise on page load
addOnloadHook(initialiseSwitchable);

var hideImageURL = 'http://img87.imageshack.us/img87/5183/hide.png';
var showImageURL = 'http://img87.imageshack.us/img87/2237/showg.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);