Jump to content

User:Dodoïste/vector.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Dodoïste (talk | contribs) at 15:14, 7 November 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.
/** 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 autoCollapse = 2;  // present on the page before the autocollapsible
                       // table initially appear collapsed.
 
var collapseCaption = "hide";
var expandCaption   = "show";
var collapseTitle   = "Hide the contents of this table."
var expandTitle     = "Show the contents of this table."
var collapseImage   = "/media/wikipedia/commons/thumb/5/5e/Symbol_oppose_vote_oversat.svg/16px-Symbol_oppose_vote_oversat.svg.png";
var expandImage     = "/media/wikipedia/commons/thumb/9/94/Symbol_support_vote.svg/16px-Symbol_support_vote.svg.png";
 
function collapseTable( tableIndex ) {
 
    var Button = document.getElementById( "collapseButton" + tableIndex );
    var Table = document.getElementById( "collapsibleTable" + tableIndex );
 
    if ( !Table || !Button ) {
        return false;
    }
 
    var Rows = Table.rows;
    var ButtonText = Button.firstChild;
 
    if ( ( collapseImage ? ButtonText.alt : ButtonText.data ) == collapseCaption ) {
        for ( var i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = "none";
        }
        if ( collapseImage ) {
            ButtonText.alt   = expandCaption;
            ButtonText.title = expandTitle;
            ButtonText.src   = expandImage;
        } else {
            ButtonText.data  = expandCaption;
            ButtonText.title = expandTitle;
        }
    } else {
        for ( var i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = Rows[0].style.display;
        }
        if ( collapseImage ) {
            ButtonText.alt   = collapseCaption;
            ButtonText.title = collapseTitle;
            ButtonText.src   = collapseImage;
        } else {
            ButtonText.data  = collapseCaption;
            ButtonText.title = collapseTitle;
        }
    }
}
 
function createCollapseButtons() {
 
    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" ) ) {
 
            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.className = "collapseButton";  //Styles are declared in Common.css
 
            var ButtonLink = document.createElement( "a" );
            ButtonLink.id = "collapseButton" + tableIndex;
            ButtonLink.href = "javascript:collapseTable(" + tableIndex + ");";
            ButtonLink.style.color = Header.style.color; // Fix up the colour
 
            var ButtonText = null;
            if ( collapseImage ) {
                ButtonText = document.createElement( "img" );
                ButtonText.alt   = collapseCaption;
                ButtonText.title = collapseTitle;
                ButtonText.src   = collapseImage;
            } else {
                ButtonText = document.createTextNode( collapseCaption );
            }
 
            ButtonLink.appendChild( ButtonText );
 
            if ( !collapseImage ) Button.appendChild( document.createTextNode( "[" ) );
            Button.appendChild( ButtonLink );
            if ( !collapseImage ) Button.appendChild( document.createTextNode( "]" ) );
 
            Header.insertBefore( Button, Header.childNodes[0] );
 
            tableIndex++;
        }
    }
 
    for ( var i = 0;  i < tableIndex; i++ ) {
        if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
            collapseTable( i );
        } 
        else if ( hasClass( NavigationBoxes[i], "innercollapse" ) ) {
            var element = NavigationBoxes[i];
            while (element = element.parentNode) {
                if ( hasClass( element, "outercollapse" ) ) {
                    collapseTable ( i );
                    break;
                }
            }
        }
    }
}
 
addOnloadHook( createCollapseButtons );