MediaWiki:Common.js: Difference between revisions

(Created page with "→‎Any JavaScript here will be loaded for all users on every page load.: /** START COLLAPSIBLE TABLES *********************************************************<br> *<br> * De...")
 
No edit summary
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */  
/* Any JavaScript here will be loaded for all users on every page load. */


/** START COLLAPSIBLE TABLES *********************************************************<br> *<br> * Description: Allows tables to be collapsed, showing only the header. See<br> * http://www.mediawiki.org/wiki/Manual:Collapsible_tables.<br> * Maintainers: [[en:User:R. Koot]]<br> */<br> <br>var autoCollapse = 2;<br>var collapseCaption = 'hide';<br>var expandCaption = 'show';<br> <br>function collapseTable( tableIndex ) {<br> var Button = document.getElementById( 'collapseButton' + tableIndex );<br> var Table = document.getElementById( 'collapsibleTable' + tableIndex );<br> <br> if ( !Table || !Button ) {<br> return false;<br> }<br> <br> var Rows = Table.rows;<br> <br> if ( Button.firstChild.data == collapseCaption ) {<br> for ( var i = 1; i &lt; Rows.length; i++ ) {<br> Rows[i].style.display = 'none';<br> }<br> Button.firstChild.data = expandCaption;<br> } else {<br> for ( var i = 1; i &lt; Rows.length; i++ ) {<br> Rows[i].style.display = Rows[0].style.display;<br> }<br> Button.firstChild.data = collapseCaption;<br> }<br>}<br> <br>function createCollapseButtons() {<br> var tableIndex = 0;<br> var NavigationBoxes = new Object();<br> var Tables = document.getElementsByTagName( 'table' );<br> <br> for ( var i = 0; i &lt; Tables.length; i++ ) {<br> if ( hasClass( Tables[i], 'collapsible' ) ) {<br> <br> /* only add button and increment count if there is a header row to work with */<br> var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];<br> if ( !HeaderRow ) {<br> continue;<br> }<br> var Header = HeaderRow.getElementsByTagName( 'th' )[0];<br> if ( !Header ) {<br> continue;<br> }<br> <br> NavigationBoxes[tableIndex] = Tables[i];<br> Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );<br> <br> var Button = document.createElement( 'span' );<br> var ButtonLink = document.createElement( 'a' );<br> var ButtonText = document.createTextNode( collapseCaption );<br> <br> Button.className = 'collapseButton'; // Styles are declared in [[MediaWiki:Common.css]]<br> <br> ButtonLink.style.color = Header.style.color;<br> ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );<br> ButtonLink.setAttribute( 'href', "javascript:collapseTable(" + tableIndex + ");" );<br> ButtonLink.appendChild( ButtonText );<br> <br> Button.appendChild( document.createTextNode( '[' ) );<br> Button.appendChild( ButtonLink );<br> Button.appendChild( document.createTextNode( ']' ) );<br> <br> Header.insertBefore( Button, Header.childNodes[0] );<br> tableIndex++;<br> }<br> }<br> <br> for ( var i = 0; i &lt; tableIndex; i++ ) {<br> if ( hasClass( NavigationBoxes[i], 'collapsed' ) || ( tableIndex &gt;= autoCollapse &amp;&amp; hasClass( NavigationBoxes[i], 'autocollapse' ) ) ) {<br> collapseTable( i );<br> } else if ( hasClass( NavigationBoxes[i], 'innercollapse' ) ) {<br> var element = NavigationBoxes[i];<br> while ( element = element.parentNode ) {<br> if ( hasClass( element, 'outercollapse' ) ) {<br> collapseTable( i );<br> break;<br> }<br> }<br> }<br> }<br>}<br> <br>addOnloadHook( createCollapseButtons );<br> <br>/** Test if an element has a certain class **************************************<br> *<br> * Description: Uses regular expressions and caching for better performance.<br> * Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]<br> */<br> <br>var hasClass = ( function() {<br> var reCache = {};<br> return function( element, className ) {<br> return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );<br> };<br>})();
/** START COLLAPSIBLE TABLES *********************************************************


*
*  Description: Allows tables to be collapsed, showing only the header. See
*                        http://www.mediawiki.org/wiki/Manual:Collapsible_tables.
*  Maintainers: [[En:User:R. Koot]]
*/




/**END COLLAPSIBLE TABLES********************************************/
var autoCollapse = 2; var collapseCaption = 'hide'; var expandCaption = 'show';
 
function collapseTable( 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 === collapseCaption ) {
        for ( var i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = 'none';
        }
        Button.firstChild.data = expandCaption;
    } else {
        for ( var 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 = 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' );
            var ButtonLink = document.createElement( 'a' );
            var ButtonText = document.createTextNode( collapseCaption );
 
            Button.className = 'collapseButton'; // Styles are declared in [[MediaWiki:Common.css]]
 
            ButtonLink.style.color = Header.style.color;
            ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
            ButtonLink.setAttribute( 'href', "javascript:collapseTable(" + tableIndex + ");" );
            ButtonLink.appendChild( ButtonText );
 
            Button.appendChild( document.createTextNode( '[' ) );
            Button.appendChild( ButtonLink );
            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;
                }
            }
        }
    }
 
}
 
$( createCollapseButtons );
 
/** Test if an element has a certain class **************************************
 
*
* Description: Uses regular expressions and caching for better performance.
* Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
*/
 
 
var hasClass = ( function() {
 
    var reCache = {};
    return function( element, className ) {
        return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
    };
 
})();/**END COLLAPSIBLE TABLES********************************************/

Latest revision as of 11:00, 26 April 2021

/* Any JavaScript here will be loaded for all users on every page load. */

/** START COLLAPSIBLE TABLES *********************************************************

 *
 *  Description: Allows tables to be collapsed, showing only the header. See
 *                         http://www.mediawiki.org/wiki/Manual:Collapsible_tables.
 *  Maintainers: [[En:User:R. Koot]]
 */


var autoCollapse = 2; var collapseCaption = 'hide'; var expandCaption = 'show';

function collapseTable( 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 === collapseCaption ) {
        for ( var i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = 'none';
        }
        Button.firstChild.data = expandCaption;
    } else {
        for ( var 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 = 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' );
            var ButtonLink = document.createElement( 'a' );
            var ButtonText = document.createTextNode( collapseCaption );

            Button.className = 'collapseButton'; // Styles are declared in [[MediaWiki:Common.css]]

            ButtonLink.style.color = Header.style.color;
            ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
            ButtonLink.setAttribute( 'href', "javascript:collapseTable(" + tableIndex + ");" );
            ButtonLink.appendChild( ButtonText );

            Button.appendChild( document.createTextNode( '[' ) );
            Button.appendChild( ButtonLink );
            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;
                }
            }
        }
    }

}

$( createCollapseButtons );

/** Test if an element has a certain class **************************************

 *
 * Description: Uses regular expressions and caching for better performance.
 * Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
 */


var hasClass = ( function() {

    var reCache = {};
    return function( element, className ) {
        return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
    };

})();/**END COLLAPSIBLE TABLES********************************************/