2010-12-23 19:25:16 +00:00
|
|
|
/**
|
2010-12-23 19:26:30 +00:00
|
|
|
* jQuery makeCollapsible
|
2010-12-23 19:25:16 +00:00
|
|
|
*
|
|
|
|
|
* This will enable collapsible-functionality on all passed elements.
|
|
|
|
|
* Will prevent binding twice to the same element.
|
|
|
|
|
* Initial state is expanded by default, this can be overriden by adding class
|
2010-12-24 01:30:23 +00:00
|
|
|
* "mw-collapsed" to the "mw-collapsible" element.
|
|
|
|
|
* Elements made collapsible have class "mw-made-collapsible".
|
|
|
|
|
* Except for tables and lists, the inner content is wrapped in "mw-collapsible-content".
|
2010-12-23 19:25:16 +00:00
|
|
|
*
|
2010-12-23 19:26:30 +00:00
|
|
|
* @author Krinkle <krinklemail@gmail.com>
|
2010-12-23 19:25:16 +00:00
|
|
|
*
|
2010-12-23 19:26:30 +00:00
|
|
|
* Dual license:
|
|
|
|
|
* @license CC-BY 3.0 <http://creativecommons.org/licenses/by/3.0>
|
|
|
|
|
* @license GPL2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
|
2010-12-23 19:25:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
$.fn.makeCollapsible = function() {
|
|
|
|
|
|
|
|
|
|
return this.each(function() {
|
2010-12-23 23:34:32 +00:00
|
|
|
|
2010-12-24 01:30:23 +00:00
|
|
|
var $that = $(this).addClass( 'mw-collapsible' ), // in case $( '#myAJAXelement' ).makeCollapsible() was called
|
2010-12-23 19:25:16 +00:00
|
|
|
that = this,
|
2010-12-23 19:26:30 +00:00
|
|
|
collapsetext = $(this).attr( 'data-collapsetext' ),
|
2010-12-23 23:34:32 +00:00
|
|
|
expandtext = $(this).attr( 'data-expandtext' ),
|
|
|
|
|
toggleFunction = function( that ) {
|
|
|
|
|
var $that = $(that),
|
2010-12-24 01:30:23 +00:00
|
|
|
$collapsible = $that.closest( '.mw-collapsible.mw-made-collapsible' ).toggleClass( 'mw-collapsed' );
|
2010-12-23 23:34:32 +00:00
|
|
|
|
|
|
|
|
// It's expanded right now
|
2010-12-24 01:30:23 +00:00
|
|
|
if ( $that.hasClass( 'mw-collapsible-toggle-expanded' ) ) {
|
2010-12-23 23:34:32 +00:00
|
|
|
// Change link to "Show"
|
2010-12-24 01:30:23 +00:00
|
|
|
$that.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
|
2010-12-23 23:34:32 +00:00
|
|
|
if ( $that.find( '> a' ).size() ) {
|
|
|
|
|
$that.find( '> a' ).text( expandtext );
|
|
|
|
|
} else {
|
|
|
|
|
$that.text( expandtext );
|
|
|
|
|
}
|
|
|
|
|
// Hide the collapsible element
|
|
|
|
|
if ( $collapsible.is( 'table' ) ) {
|
|
|
|
|
// Hide all direct childing table rows of this table, except the row containing the link
|
|
|
|
|
// Slide doens't work, but fade works fine as of jQuery 1.1.3
|
|
|
|
|
// http://stackoverflow.com/questions/467336/jquery-how-to-use-slidedown-or-collapsed-function-on-a-table-row#920480
|
|
|
|
|
// Stop to prevent animations from stacking up
|
|
|
|
|
$collapsible.find( '> tbody > tr' ).not( $that.parent().parent() ).stop( true, true ).fadeOut();
|
|
|
|
|
|
|
|
|
|
} else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
|
|
|
|
|
$collapsible.find( '> li' ).not( $that.parent() ).stop( true, true ).slideUp();
|
|
|
|
|
|
|
|
|
|
} else { // <div>, <p> etc.
|
2010-12-24 01:30:23 +00:00
|
|
|
$collapsible.find( '> .mw-collapsible-content' ).slideUp();
|
2010-12-23 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// It's collapsed right now
|
|
|
|
|
} else {
|
|
|
|
|
// Change link to "Hide"
|
2010-12-24 01:30:23 +00:00
|
|
|
$that.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
|
2010-12-23 23:34:32 +00:00
|
|
|
if ( $that.find( '> a' ).size() ) {
|
|
|
|
|
$that.find( '> a' ).text( collapsetext );
|
|
|
|
|
} else {
|
|
|
|
|
$that.text( collapsetext );
|
|
|
|
|
}
|
|
|
|
|
// Show the collapsible element
|
|
|
|
|
if ( $collapsible.is( 'table' ) ) {
|
|
|
|
|
$collapsible.find( '> tbody > tr' ).not( $that.parent().parent() ).stop( true, true ).fadeIn();
|
|
|
|
|
|
|
|
|
|
} else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
|
|
|
|
|
$collapsible.find( '> li' ).not( $that.parent() ).stop( true, true ).slideDown();
|
|
|
|
|
|
|
|
|
|
} else { // <div>, <p> etc.
|
2010-12-24 01:30:23 +00:00
|
|
|
$collapsible.find( '> .mw-collapsible-content' ).slideDown();
|
2010-12-23 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
2010-12-23 19:25:16 +00:00
|
|
|
// Use custom text or default ?
|
2010-12-23 19:26:30 +00:00
|
|
|
if( !collapsetext || collapsetext == '' ){
|
2010-12-23 23:34:32 +00:00
|
|
|
collapsetext = mw.msg( 'collapsible-collapse' );
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
2010-12-23 19:26:30 +00:00
|
|
|
if ( !expandtext || expandtext == '' ){
|
2010-12-23 23:34:32 +00:00
|
|
|
expandtext = mw.msg( 'collapsible-expand' );
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
2010-12-23 19:26:30 +00:00
|
|
|
|
2010-12-23 23:34:32 +00:00
|
|
|
// Create toggle link with a space around the brackets ( [text] )
|
2010-12-24 15:24:28 +00:00
|
|
|
var $toggleLink = $( '<a href="#">' ).text( collapsetext ).wrap( '<span class="mw-collapsible-toggle mw-collapsible-toggle-expanded">' ).parent().prepend( ' [' ).append( '] ' ).bind( 'click.mw-collapse', function(e){
|
2010-12-23 23:34:32 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
toggleFunction( this );
|
2010-12-23 19:26:30 +00:00
|
|
|
} );
|
|
|
|
|
|
2010-12-23 19:25:16 +00:00
|
|
|
// Skip if it has been enabled already.
|
2010-12-24 01:30:23 +00:00
|
|
|
if ( $that.hasClass( 'mw-made-collapsible' ) ) {
|
2010-12-23 19:25:16 +00:00
|
|
|
return;
|
|
|
|
|
} else {
|
2010-12-24 01:30:23 +00:00
|
|
|
$that.addClass( 'mw-made-collapsible' );
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
2010-12-23 19:26:30 +00:00
|
|
|
|
2010-12-23 19:25:16 +00:00
|
|
|
// Elements are treated differently
|
2010-12-23 19:26:30 +00:00
|
|
|
if ( $that.is( 'table' ) ) {
|
2010-12-23 19:25:16 +00:00
|
|
|
// The toggle-link will be in the last cell (td or th) of the first row
|
2010-12-23 23:34:32 +00:00
|
|
|
var $lastCell = $( 'tr:first th, tr:first td', that ).eq(-1),
|
2010-12-24 01:30:23 +00:00
|
|
|
$toggle = $lastCell.find( '> .mw-collapsible-toggle' );
|
2010-12-23 23:34:32 +00:00
|
|
|
|
|
|
|
|
if ( !$toggle.size() ) {
|
2010-12-23 19:26:30 +00:00
|
|
|
$lastCell.prepend( $toggleLink );
|
2010-12-23 23:34:32 +00:00
|
|
|
} else {
|
2010-12-24 15:24:28 +00:00
|
|
|
$toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ){
|
2010-12-23 23:34:32 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
toggleFunction( this );
|
|
|
|
|
} );
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
2010-12-23 23:34:32 +00:00
|
|
|
} else if ( $that.is( 'ul' ) || $that.is( 'ol' ) ) {
|
|
|
|
|
// The toggle-link will be in the first list-item
|
|
|
|
|
var $firstItem = $( 'li:first', $that),
|
2010-12-24 01:30:23 +00:00
|
|
|
$toggle = $firstItem.find( '> .mw-collapsible-toggle' );
|
2010-12-23 23:34:32 +00:00
|
|
|
|
|
|
|
|
if ( !$toggle.size() ) {
|
|
|
|
|
// Make sure the numeral order doesn't get messed up, reset to 1 unless value-attribute is already used
|
|
|
|
|
if ( $firstItem.attr( 'value' ) == '' ) {
|
|
|
|
|
$firstItem.attr( 'value', '1' );
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
2010-12-24 01:30:23 +00:00
|
|
|
$that.prepend( $toggleLink.wrap( '<li class="mw-collapsibile-toggle-li">' ).parent() );
|
2010-12-23 23:34:32 +00:00
|
|
|
} else {
|
2010-12-24 15:24:28 +00:00
|
|
|
$toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ){
|
2010-12-23 23:34:32 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
toggleFunction( this );
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-23 19:25:16 +00:00
|
|
|
} else { // <div>, <p> etc.
|
|
|
|
|
// Is there an content-wrapper already made ?
|
|
|
|
|
// If a direct child with the class does not exists, create the wrap.
|
2010-12-24 01:30:23 +00:00
|
|
|
if ( !$that.find( '> .mw-collapsible-content' ).size() ) {
|
|
|
|
|
$that.wrapInner( '<div class="mw-collapsible-content">' );
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
2010-12-23 23:34:32 +00:00
|
|
|
// The toggle-link will be the first child of the element
|
2010-12-24 01:30:23 +00:00
|
|
|
var $toggle = $that.find( '> .mw-collapsible-toggle' );
|
2010-12-23 23:34:32 +00:00
|
|
|
|
|
|
|
|
if ( !$toggle.size() ) {
|
2010-12-23 19:26:30 +00:00
|
|
|
$that.prepend( $toggleLink );
|
2010-12-23 23:34:32 +00:00
|
|
|
} else {
|
2010-12-24 15:24:28 +00:00
|
|
|
$toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ){
|
2010-12-23 23:34:32 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
toggleFunction( this );
|
|
|
|
|
} );
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
2010-12-23 23:34:32 +00:00
|
|
|
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
|
|
|
|
// Initial state
|
2010-12-24 01:30:23 +00:00
|
|
|
if ( $that.hasClass( 'mw-collapsed' ) ) {
|
2010-12-23 19:25:16 +00:00
|
|
|
$toggleLink.click();
|
|
|
|
|
}
|
2010-12-23 19:26:30 +00:00
|
|
|
} );
|
2010-12-24 01:30:23 +00:00
|
|
|
};
|