Jump to content

User:Frietjes/collapse infoboxes.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.
// Taken from [[MediaWiki:Common.js]]
// Modified to collapse based on infobox class, rather than collapsible class
jQuery(document).ready(function($) {
mw.loader.using(['mediawiki.util']).done( function() {

var collapseCaption = 'hide';
var expandCaption = 'show';
 
window.collapseTable = function ( tableIndex ) {
    var Button = document.getElementById( 'mycollapseButton' + tableIndex );
    var Table = document.getElementById( 'mycollapsibleTable' + tableIndex );
 
    if ( !Table || !Button ) {
        return false;
    }
 
    var Rows = Table.rows;
    var i;
 
    if ( Button.firstChild.data === collapseCaption ) {
        for ( i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = 'none';
        }
        Button.firstChild.data = expandCaption;
    } else {
        for ( i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = Rows[0].style.display;
        }
        Button.firstChild.data = collapseCaption;
    }
};
 
function createCollapseButtons() {
    var tableIndex = 0;
    var NavigationBoxes = {};
    var Tables = document.getElementsByTagName( 'table' );
    var i;
 
    function handleButtonLink( index, e ) {
        window.collapseTable( index );
        e.preventDefault();
    }
 
    for ( i = 0; i < Tables.length; i++ ) {
        if ( $( Tables[i] ).hasClass( 'infobox' ) ) {
 
            /* only add button and increment count if there is a header row to work with */
            var Header = Tables[i].getElementsByTagName( 'caption' )[0];
            if ( !Header ) {
              var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];
              if ( !HeaderRow ) continue;
              var Header = HeaderRow.getElementsByTagName( 'th' )[0];
              if ( !Header ) Header = HeaderRow.getElementsByTagName( 'td' )[0];
              if ( !Header ) continue;
            }
            if ( !Header ) continue;

            NavigationBoxes[ tableIndex ] = Tables[i];
            Tables[i].setAttribute( 'id', 'mycollapsibleTable' + tableIndex );
 
            var Button     = document.createElement( 'span' );
            var ButtonLink = document.createElement( 'a' );
            var ButtonText = document.createTextNode( collapseCaption );
 
            Button.className = 'collapseButton';  /* Styles are declared in Common.css */
            Button.setAttribute('style', 'font-size:smaller; font-weight:normal;');
 
            ButtonLink.style.color = Header.style.color;
            ButtonLink.setAttribute( 'id', 'mycollapseButton' + tableIndex );
            ButtonLink.setAttribute( 'href', '#' );
            $( ButtonLink ).on( 'click', $.proxy( handleButtonLink, ButtonLink, tableIndex ) );
            ButtonLink.appendChild( ButtonText );
 
            Button.appendChild( document.createTextNode( '[' ) );
            Button.appendChild( ButtonLink );
            Button.appendChild( document.createTextNode( ']' ) );
 
            Header.insertBefore( Button, Header.firstChild );
            tableIndex++;
        }
    }
 
    for ( i = 0;  i < tableIndex; i++ ) {
        window.collapseTable( i );
    }
}
 
$( createCollapseButtons );

});
});