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 40: Line 40:
         var visible = switchableGetVisible(i);
         var visible = switchableGetVisible(i);
         var sections = switchable[i].sections;
         var sections = switchable[i].sections;
         var anchors = switchable[i].anchorContainer.anchors;
         var p = switchable[i].anchorContainer;
        var strong;
         for (var j = 0; j < sections.length; j++) {
         for (var j = 0; j < sections.length; j++) {
             var a = anchors[j];
             var a = p.anchors[j];
             if (j == visible) {
             if (j == visible) {
                 // change 'show' link
                 // change 'show' link
                 a.removeAttribute('href');
                 strong = document.createElement('strong');
                 var t = document.createTextNode(sections[j].sectionName)
                 strong.appendChild(document.createTextNode(sections[j].sectionName));
                 a.replaceChild(t, a.childNodes[0]);
                 strong.j = j;
                p.replaceChild(strong, a);
                 // show section if hidden
                 // show section if hidden
                 if (sections.hidden[j] !== undefined) {
                 if (sections.hidden[j] !== undefined) {
Line 55: Line 57:
             } else {
             } else {
                 // change 'show' link
                 // change 'show' link
                 a.setAttribute('href', '#');
                 if (p.strong !== undefined && p.strong.j == j)
                var t = document.createTextNode('show ' + sections[j].sectionName);
                    p.replaceChild(a, p.strong);
                a.replaceChild(t, a.childNodes[0]);
                 // hide section if visible
                 // hide section if visible
                 if (sections.hidden[j] === undefined)
                 if (sections.hidden[j] === undefined)
Line 63: Line 64:
             }
             }
         }
         }
        if (strong !== undefined) p.strong = strong;
     }
     }
}
}

Revision as of 18:37, October 26, 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>');

function getElementsByClassName (cls, node) {
    if (node === undefined) node = document;
    var result = [];
    var pool = node.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;
}

// some functions
switchableGetVisible = function (i) {
    var visible = switchable[i].getAttribute('visiblesection');
    if (visible) visible = parseInt(visible);
    if (visible === null || isNaN(visible)) {
        visible = 0;
        switchable[i].setAttribute('visiblesection', visible.toString());
    }
    return visible;
}

switchableUpdateVisible = function (i) {
    if (isNaN(parseInt(i))) {
        // update all switchables if no valid number given
        for (var i = 0; i < switchable.length; i++)
            switchableUpdateVisible(i);
    } else {
        var visible = switchableGetVisible(i);
        var sections = switchable[i].sections;
        var p = switchable[i].anchorContainer;
        var strong;
        for (var j = 0; j < sections.length; j++) {
            var a = p.anchors[j];
            if (j == visible) {
                // change 'show' link
                strong = document.createElement('strong');
                strong.appendChild(document.createTextNode(sections[j].sectionName));
                strong.j = j;
                p.replaceChild(strong, a);
                // show section if hidden
                if (sections.hidden[j] !== undefined) {
                    switchable[i].appendChild(sections[j]);
                    sections.hidden[j] = undefined;
                }
            } else {
                // change 'show' link
                if (p.strong !== undefined && p.strong.j == j)
                    p.replaceChild(a, p.strong);
                // hide section if visible
                if (sections.hidden[j] === undefined)
                    sections.hidden[j] = switchable[i].removeChild(sections[j]);
            }
        }
        if (strong !== undefined) p.strong = strong;
    }
}

switchableSetVisible = function (i, j) {
    switchable[i].setAttribute('visiblesection', j);
    switchableUpdateVisible(i);
}

// initial show/hide
function initialiseSwitchable () {
    // get the elements we care about
    switchable = getElementsByClassName('switchable');
    for (var i = 0; i < switchable.length; i++) {
        var sections = getElementsByClassName('switch', switchable[i]);
        sections.hidden = Array(sections.length);
        switchable[i].sections = sections;
        // create show/hide anchors
        var p = document.createElement('p');
        switchable[i].appendChild(p);
        switchable[i].anchorContainer = p;
        p.anchors = [];
        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 a = document.createElement('a');
            a.appendChild(document.createTextNode('show ' + sectionName));
            a.setAttribute('href', '#');
            a.setAttribute('onclick',
                'switchableSetVisible(' + i + ', ' + j + ');');
            p.appendChild(a);
            p.anchors.push(a);
            if (j < sections.length - 1)
                p.appendChild(document.createTextNode(' '));
        }
    }
    this.switchable = switchable;
    switchableUpdateVisible();
}
if (window.addEventListener !== undefined)
    window.addEventListener('load', initialiseSwitchable, false);
else if (window.attachEvent !== undefined)
    window.attachEvent('onload', initialiseSwitchable);
else window.onload = initialiseSwitchable;