User:Ilmari Karonen/test.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/** Dynamic Navigation Bars (experimental) *************************************
 *
 *  Description: See [[Wikipedia:xNavFrame]].
 *  Maintainers: UNMAINTAINED
 */

// set up the words in your language
var xNavigationBarHide = '[' + collapseCaption + ']';
var xNavigationBarShow = '[' + expandCaption + ']';

// shows and hides content and picture (if available) of navigation bars
// Parameters:
//     indexNavigationBar: the index of navigation bar to be toggled
function xtoggleNavigationBar(indexNavigationBar)
{
    var xNavToggle = document.getElementById("xNavToggle" + indexNavigationBar);
    var xNavFrame = document.getElementById("xNavFrame" + indexNavigationBar);

    if (!xNavFrame || !xNavToggle) {
        return false;
    }

    // if shown now
    if (xNavToggle.firstChild.data == xNavigationBarHide) {
        for (var xNavChild = xNavFrame.firstChild; xNavChild != null; xNavChild = xNavChild.nextSibling) {
            if ( hasClass( xNavChild, 'xNavPic' ) ) {
                xNavChild.style.display = 'none';
            }
            if ( hasClass( xNavChild, 'xNavContent') ) {
                xNavChild.style.display = 'none';
            }
        }
    xNavToggle.firstChild.data = xNavigationBarShow;

    // if hidden now
    } else if (xNavToggle.firstChild.data == xNavigationBarShow) {
        for (var xNavChild = xNavFrame.firstChild; xNavChild != null; xNavChild = xNavChild.nextSibling) {
            if (hasClass(xNavChild, 'xNavPic')) {
                xNavChild.style.display = 'block';
            }
            if (hasClass(xNavChild, 'xNavContent')) {
                xNavChild.style.display = 'block';
            }
        }
        xNavToggle.firstChild.data = xNavigationBarHide;
    }
}

// adds show/hide-button to navigation bars
function xcreateNavigationBarToggleButton()
{
    var indexNavigationBar = 0;
    // iterate over all < div >-elements 
    var divs = document.getElementsByTagName("div");
    for (var i = 0; xNavFrame = divs[i]; i++) {
        // if found a navigation bar
        if (hasClass(xNavFrame, "xNavFrame")) {

            indexNavigationBar++;
            var xNavToggle = document.createElement("a");
            xNavToggle.className = 'xNavToggle';
            xNavToggle.setAttribute('id', 'xNavToggle' + indexNavigationBar);
            xNavToggle.setAttribute('href', 'javascript:xtoggleNavigationBar(' + indexNavigationBar + ');');

            var isCollapsed = hasClass( xNavFrame, "collapsed" );
            /*
             * Check if any children are already hidden.  This loop is here for backwards compatibility:
             * the old way of making xNavFrames start out collapsed was to manually add style="display:none"
             * to all the xNavPic/xNavContent elements.  Since this was bad for accessibility (no way to make
             * the content visible without JavaScript support), the new recommended way is to add the class
             * "collapsed" to the xNavFrame itself, just like with collapsible tables.
             */
            for (var xNavChild = xNavFrame.firstChild; xNavChild != null; xNavChild = xNavChild.nextSibling) {
                if ( hasClass( xNavChild, 'xNavPic' ) || hasClass( xNavChild, 'xNavContent' ) ) {
                    if (xNavChild.style.display == 'none') {
                        isCollapsed = true;
                        break;
                    }
                }
            }
            if (isCollapsed) {
                for (var xNavChild = xNavFrame.firstChild; xNavChild != null; xNavChild = xNavChild.nextSibling) {
                    if ( hasClass( xNavChild, 'xNavPic' ) || hasClass( xNavChild, 'xNavContent' ) ) {
                        xNavChild.style.display = 'none';
                    }
                }
            }
            var xNavToggleText = document.createTextNode(isCollapsed ? xNavigationBarShow : xNavigationBarHide);
            xNavToggle.appendChild(xNavToggleText);

            // Find the xNavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
            for(var j=0; j < xNavFrame.childNodes.length; j++) {
                if (hasClass(xNavFrame.childNodes[j], "xNavHead")) {
                    xNavFrame.childNodes[j].appendChild(xNavToggle);
                }
            }
            xNavFrame.setAttribute('id', 'xNavFrame' + indexNavigationBar);
        }
    }
}

addOnloadHook( xcreateNavigationBarToggleButton );