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
|
|
|
|
|
* "kr-collapsed" to the "kr-collapsible" element.
|
|
|
|
|
* Elements made collapsible have class "kr-made-collapsible".
|
|
|
|
|
* Except for tables and lists, the inner content is wrapped in "kr-collapsible-content".
|
|
|
|
|
*
|
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
|
|
|
* @TODO: Remove old "kr-" prefix
|
|
|
|
|
*
|
|
|
|
|
* 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 19:26:30 +00:00
|
|
|
var $that = $(this).addClass( 'kr-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' ),
|
|
|
|
|
expandtext = $(this).attr( 'data-expandtext' );
|
2010-12-23 19:25:16 +00:00
|
|
|
// Use custom text or default ?
|
2010-12-23 19:26:30 +00:00
|
|
|
if( !collapsetext || collapsetext == '' ){
|
|
|
|
|
collapsetext = mw.msg( 'hide' );
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
2010-12-23 19:26:30 +00:00
|
|
|
if ( !expandtext || expandtext == '' ){
|
|
|
|
|
expandtext = mw.msg( 'show' );
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
|
|
|
|
// Create toggle link with a space around the brackets ( )
|
2010-12-23 19:26:30 +00:00
|
|
|
$toggleLink = $( '<span class="kr-collapsible-toggle kr-collapsible-toggle-hide"> [<a href="#">' + collapsetext + '</a>] </span>' ).click( function(){
|
2010-12-23 19:25:16 +00:00
|
|
|
var $that = $(this),
|
2010-12-23 19:26:30 +00:00
|
|
|
$collapsible = $that.closest( '.kr-collapsible.kr-made-collapsible' ).toggleClass( 'kr-collapsed' );
|
|
|
|
|
if ( $that.hasClass( 'kr-collapsible-toggle-hide' ) ) {
|
2010-12-23 19:25:16 +00:00
|
|
|
// Change link to "Show"
|
|
|
|
|
$that
|
2010-12-23 19:26:30 +00:00
|
|
|
.removeClass( 'kr-collapsible-toggle-hide' ).addClass( 'kr-collapsible-toggle-show' )
|
|
|
|
|
.find( '> a' ).html( expandtext );
|
2010-12-23 19:25:16 +00:00
|
|
|
// Hide the collapsible element
|
2010-12-23 19:26:30 +00:00
|
|
|
if ( $collapsible.is( 'table' ) ) {
|
2010-12-23 19:25:16 +00:00
|
|
|
// 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-show-function-on-a-table-row#920480
|
|
|
|
|
// Stop to prevent animaties from stacking up
|
2010-12-23 19:26:30 +00:00
|
|
|
$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();
|
|
|
|
|
|
2010-12-23 19:25:16 +00:00
|
|
|
} else { // <div>, <p> etc.
|
2010-12-23 19:26:30 +00:00
|
|
|
$collapsible.find( '> .kr-collapsible-content' ).slideUp();
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
2010-12-23 19:26:30 +00:00
|
|
|
|
2010-12-23 19:25:16 +00:00
|
|
|
} else {
|
|
|
|
|
// Change link to "Hide"
|
|
|
|
|
$that
|
2010-12-23 19:26:30 +00:00
|
|
|
.removeClass( 'kr-collapsible-toggle-show' ).addClass( 'kr-collapsible-toggle-hide' )
|
|
|
|
|
.find( '> a' ).html( collapsetext );
|
2010-12-23 19:25:16 +00:00
|
|
|
// Show the collapsible element
|
2010-12-23 19:26:30 +00:00
|
|
|
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();
|
|
|
|
|
|
2010-12-23 19:25:16 +00:00
|
|
|
} else { // <div>, <p> etc.
|
2010-12-23 19:26:30 +00:00
|
|
|
$collapsible.find( '> .kr-collapsible-content' ).slideDown();
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
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-23 19:26:30 +00:00
|
|
|
if ($that.hasClass( 'kr-made-collapsible' )) {
|
2010-12-23 19:25:16 +00:00
|
|
|
return;
|
|
|
|
|
} else {
|
2010-12-23 19:26:30 +00:00
|
|
|
$that.addClass( 'kr-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 19:26:30 +00:00
|
|
|
var $lastCell = $( 'tr:first th, tr:first td', that ).eq(-1);
|
|
|
|
|
if ( !$lastCell.find( '> .kr-collapsible-toggle' ).size() ) {
|
|
|
|
|
$lastCell.prepend( $toggleLink );
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
2010-12-23 19:26:30 +00:00
|
|
|
} else if ($that.is( 'ul' ) || $that.is( 'ol' )) {
|
|
|
|
|
if ( !$( 'li:first', $that).find( '> .kr-collapsible-toggle' ).size() ) {
|
2010-12-23 19:25:16 +00:00
|
|
|
// Make sure the numeral count doesn't get messed up, reset to 1 unless value-attribute is already used
|
2010-12-23 19:26:30 +00:00
|
|
|
if ( $( 'li:first', $that ).attr( 'value' ) == '' ) {
|
|
|
|
|
$( 'li:first', $that ).attr( 'value', '1' );
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
2010-12-23 19:26:30 +00:00
|
|
|
$that.prepend( $toggleLink.wrap( '<li class="kr-collapsibile-toggle-li">' ).parent() );
|
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-23 19:26:30 +00:00
|
|
|
if ( !$that.find( '> .kr-collapsible-content' ).size() ) {
|
|
|
|
|
$that.wrapInner( '<div class="kr-collapsible-content">' );
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add toggle-link if not present
|
2010-12-23 19:26:30 +00:00
|
|
|
if ( !$that.find( '> .kr-collapsible-toggle' ).size() ) {
|
|
|
|
|
$that.prepend( $toggleLink );
|
2010-12-23 19:25:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Initial state
|
2010-12-23 19:26:30 +00:00
|
|
|
if ( $that.hasClass( 'kr-collapsed' ) ) {
|
2010-12-23 19:25:16 +00:00
|
|
|
$toggleLink.click();
|
|
|
|
|
}
|
2010-12-23 19:26:30 +00:00
|
|
|
} );
|
2010-12-23 19:25:16 +00:00
|
|
|
};
|
2010-12-23 19:26:30 +00:00
|
|
|
$( function(){
|
|
|
|
|
$( '.kr-collapsible' ).makeCollapsible();
|
|
|
|
|
} );
|