Jump to content

User:Cacycle/navbox.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Cacycle (talk | contribs) at 02:18, 24 August 2009. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// remove original createCollapseButtons functions from onload hook for demo
var createCollapseButtonsOrig = createCollapseButtons;
for (var i = 0; i < onloadFuncts.length; i ++) {
  if (onloadFuncts[i] == createCollapseButtonsOrig) {
    onloadFuncts.splice(i, 1);
  }
}
addOnloadHook(
    function() {
      for (var i = 0; i < onloadFuncts.length; i ++) {
        if (onloadFuncts[i] == createCollapseButtonsOrig) {
           onloadFuncts.splice(i, 1);
        }
      }
    }
);


/** Collapsible tables *********************************************************
 *
 *  Description: Allows tables to be collapsed, showing only the header. See
 *               [[Wikipedia:NavFrame]].
 *  Maintainers: [[User:R. Koot]]
 *  Changed for better usability by [[User:Cacycle]]
 */


var autoCollapseTest = 2;
var expandCaptionTest = '▼\xa0show';
var collapseCaptionTest = '▲\xa0hide';
var expandCaptionTitle = 'Click to show hidden content';
var collapseCaptionTitle = 'Click to hide content';


collapseTableTest = function( tableIndex )
{
    var Button = document.getElementById( "collapseButton" + tableIndex );
    var Table = document.getElementById( "collapsibleTable" + tableIndex );

    if ( !Table || !Button ) {
        return false;
    }

    var Rows = Table.rows;

    if ( Button.firstChild.data == collapseCaptionTest ) {
        for ( var i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = "none";
        }
        Button.firstChild.data = expandCaptionTest;
        Button.title = expandCaptionTitle;
    } else {
        for ( var i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = Rows[0].style.display;
        }
        Button.firstChild.data = collapseCaptionTest;
        Button.title = collapseCaptionTitle;
    }
    window.getSelection().removeAllRanges();
}

function createCollapseButtonsTest()
{
    var tableIndex = 0;
    var NavigationBoxes = new Object();
    var Tables = document.getElementsByTagName( "table" );

    for ( var i = 0; i < Tables.length; i++ ) {
        if ( hasClass( Tables[i], "collapsible" ) ) {

            /* only add button and increment count if there is a header row to work with */
            var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];
            if (!HeaderRow) continue;
            var Header = HeaderRow.getElementsByTagName( "th" )[0];
            if (!Header) continue;

            NavigationBoxes[ tableIndex ] = Tables[i];
            Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );

            var Button = document.createElement('span');
            Button.id = 'collapseButton' + tableIndex;
            Button.className = 'collapseButtonTest';  // Styles are declared in Common.css
            Button.attributes['onclick'].value = 'collapseTableTest(' + tableIndex + ')';
            Button.title = collapseCaptionTitle;
            Button.innerHTML = collapseCaptionTest;
            Header.appendChild( Button );
            tableIndex++;
        }
    }

    for ( var i = 0;  i < tableIndex; i++ ) {
        if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapseTest && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
            collapseTableTest( i );
        }
        else if ( hasClass( NavigationBoxes[i], "innercollapse" ) ) {
            var element = NavigationBoxes[i];
            while (element = element.parentNode) {
                if ( hasClass( element, "outercollapse" ) ) {
                    collapseTableTest ( i );
                    break;
                }
            }
        }
    }
}

addOnloadHook(createCollapseButtonsTest);