var lpx = 'jquery.makeCollapsible> ';
/**
+ * Handler for a click on a collapsible toggler.
+ *
* @param {jQuery} $collapsible
* @param {string} action The action this function will take ('expand' or 'collapse').
* @param {jQuery|null} [optional] $defaultToggle
toggleElement( $collapsible, wasCollapsed ? 'expand' : 'collapse', $toggle, options );
}
- /**
- * Toggles collapsible and togglelink class and updates text label.
- *
- * @param {jQuery} $that
- * @param {jQuery.Event} e
- * @param {Object|undefined} options
- */
- function toggleLinkDefault( $that, e, options ) {
- var $collapsible = $that.closest( '.mw-collapsible' );
- options = $.extend( { toggleClasses: true }, options );
- togglingHandler( $that, $collapsible, e, options );
- }
-
- /**
- * Toggles collapsible and togglelink class.
- *
- * @param {jQuery} $that
- * @param {jQuery.Event} e
- * @param {Object|undefined} options
- */
- function toggleLinkPremade( $that, e, options ) {
- var $collapsible = $that.eq( 0 ).closest( '.mw-collapsible' );
- options = $.extend( { toggleClasses: true, linksPassthru: true }, options );
- togglingHandler( $that, $collapsible, e, options );
- }
-
- /**
- * Toggles customcollapsible.
- *
- * @param {jQuery} $that
- * @param {jQuery.Event} e
- * @param {Object|undefined} options
- * @param {jQuery} $collapsible
- */
- function toggleLinkCustom( $that, e, options, $collapsible ) {
- togglingHandler( $that, $collapsible, e, options );
- }
-
/**
* Make any element collapsible.
*
* div.mw-collapsible-content. May only be used with custom togglers.
*/
$.fn.makeCollapsible = function ( options ) {
- return this.each(function () {
- var $collapsible, collapsetext, expandtext, $toggle, $toggleLink, $firstItem, collapsibleId,
- $customTogglers, firstval;
+ if ( options === undefined ) {
+ options = {};
+ }
- if ( options === undefined ) {
- options = {};
- }
+ return this.each( function () {
+ var $collapsible, collapseText, expandText, $toggle, clickHandler, $defaultToggleLink,
+ premadeToggleHandler, $toggleLink, $firstItem, collapsibleId, $customTogglers, firstval;
// Ensure class "mw-collapsible" is present in case .makeCollapsible()
// is called on element(s) that don't have it yet.
- $collapsible = $(this).addClass( 'mw-collapsible' );
+ $collapsible = $( this ).addClass( 'mw-collapsible' );
// Return if it has been enabled already.
if ( $collapsible.data( 'mw-made-collapsible' ) ) {
}
// Use custom text or default?
- collapsetext = options.collapseText || $collapsible.attr( 'data-collapsetext' ) || mw.msg( 'collapsible-collapse' );
- expandtext = options.expandText || $collapsible.attr( 'data-expandtext' ) || mw.msg( 'collapsible-expand' );
-
- // Create toggle link with a space around the brackets ( [text] )
- $toggleLink =
+ collapseText = options.collapseText || $collapsible.attr( 'data-collapsetext' ) || mw.msg( 'collapsible-collapse' );
+ expandText = options.expandText || $collapsible.attr( 'data-expandtext' ) || mw.msg( 'collapsible-expand' );
+
+ // Default click handler and toggle link to use when none is present
+ clickHandler = function ( e, opts ) {
+ var defaultOpts = {
+ toggleClasses: true,
+ toggleText: { collapseText: collapseText, expandText: expandText }
+ };
+ opts = $.extend( defaultOpts, options, opts );
+ togglingHandler( $( this ), $collapsible, e, opts );
+ };
+ $defaultToggleLink =
$( '<a href="#"></a>' )
- .text( collapsetext )
+ .text( collapseText )
.wrap( '<span class="mw-collapsible-toggle"></span>' )
.parent()
.prepend( ' [' )
.append( '] ' )
- .on( 'click.mw-collapsible', function ( e, opts ) {
- opts = $.extend( { toggleText: { collapseText: collapsetext, expandText: expandtext } }, options, opts );
- toggleLinkDefault( $(this), e, opts );
- } );
+ .on( 'click.mw-collapsible', clickHandler );
+
+ // Default handler for clicking on premade toggles
+ premadeToggleHandler = function ( e, opts ) {
+ var defaultOpts = { toggleClasses: true, linksPassthru: true };
+ opts = $.extend( defaultOpts, options, opts );
+ togglingHandler( $( this ), $collapsible, e, opts );
+ };
// Check if this element has a custom position for the toggle link
// (ie. outside the container or deeper inside the tree)
}
}
- // Bind the custom togglers
+ // Bind the togglers
if ( $customTogglers && $customTogglers.length ) {
- $customTogglers.on( 'click.mw-collapsible', function ( e, opts ) {
- opts = $.extend( {}, options, opts );
- toggleLinkCustom( $(this), e, opts, $collapsible );
- } );
-
- // Initial state
- if ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) {
- // Remove here so that the toggler goes in the right direction,
- // It re-adds the class.
- $collapsible.removeClass( 'mw-collapsed' );
- toggleLinkCustom( $customTogglers, null, $.extend( { instantHide: true }, options ), $collapsible );
- }
+ clickHandler = function ( e, opts ) {
+ var defaultOpts = {};
+ opts = $.extend( defaultOpts, options, opts );
+ togglingHandler( $( this ), $collapsible, e, opts );
+ };
+
+ $toggleLink = $customTogglers;
+ $toggleLink.on( 'click.mw-collapsible', clickHandler );
- // If this is not a custom case, do the default:
- // Wrap the contents and add the toggle link
} else {
- // Elements are treated differently
+ // If this is not a custom case, do the default: wrap the
+ // contents and add the toggle link. Different elements are
+ // treated differently.
if ( $collapsible.is( 'table' ) ) {
// The toggle-link will be in one the the cells (td or th) of the first row
$firstItem = $collapsible.find( 'tr:first th, tr:first td' );
// If theres no toggle link, add it to the last cell
if ( !$toggle.length ) {
- $firstItem.eq(-1).prepend( $toggleLink );
+ $toggleLink = $defaultToggleLink.prependTo( $firstItem.eq( -1 ) );
} else {
- $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
- opts = $.extend( {}, options, opts );
- toggleLinkPremade( $toggle, e, opts );
- } );
+ clickHandler = premadeToggleHandler;
+ $toggleLink = $toggle.on( 'click.mw-collapsible', clickHandler );
}
} else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
if ( !$toggle.length ) {
// Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
// to be "1". Except if the value-attribute is already used.
- // If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
+ // If no value was set WebKit returns "", Mozilla returns '-1', others return 0, null or undefined.
firstval = $firstItem.attr( 'value' );
if ( firstval === undefined || !firstval || firstval === '-1' || firstval === -1 ) {
$firstItem.attr( 'value', '1' );
}
- $collapsible.prepend( $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
+ $toggleLink = $defaultToggleLink;
+ $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent().prependTo( $collapsible );
} else {
- $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
- opts = $.extend( {}, options, opts );
- toggleLinkPremade( $toggle, e, opts );
- } );
+ clickHandler = premadeToggleHandler;
+ $toggleLink = $toggle.on( 'click.mw-collapsible', clickHandler );
}
} else { // <div>, <p> etc.
// If theres no toggle link, add it
if ( !$toggle.length ) {
- $collapsible.prepend( $toggleLink );
+ $toggleLink = $defaultToggleLink.prependTo( $collapsible );
} else {
- $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
- opts = $.extend( {}, options, opts );
- toggleLinkPremade( $toggle, e, opts );
- } );
+ clickHandler = premadeToggleHandler;
+ $toggleLink = $toggle.on( 'click.mw-collapsible', clickHandler );
}
}
}
- // Initial state (only for those that are not custom,
- // because the initial state of those has been taken care of already).
- if (
- ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) &&
- ( !$customTogglers || !$customTogglers.length )
- ) {
+ // Initial state
+ if ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) {
+ // Remove here so that the toggler goes in the right direction (the class is re-added)
$collapsible.removeClass( 'mw-collapsed' );
- // The collapsible element could have multiple togglers
- // To toggle the initial state only click one of them (ie. the first one, eq(0) )
- // Else it would go like: hide,show,hide,show for each toggle link.
- // This is just like it would be in reality (only one toggle is clicked at a time).
- $toggleLink.eq( 0 ).trigger( 'click', [ { instantHide: true } ] );
+ // One toggler can hook to multiple elements, and one element can have
+ // multiple togglers. This is the sanest way to handle that.
+ clickHandler.call( $toggleLink.get( 0 ), null, { instantHide: true } );
}
} );
};