From 391c28ccbae80bca4de7d1b79d57d4ecfaa02413 Mon Sep 17 00:00:00 2001 From: MatmaRex Date: Sat, 23 Mar 2013 15:27:46 +0100 Subject: [PATCH] jquery.makeCollapsible: cleanup * Ensure correct handling of togglers which hook to multiple elements and elements which have multiple togglers * Move the `options === undefined` check outside the loop per CR on I8dd4f0d0 * Get rid of the old private toggling functions for various kinds of toggles (obsoleted, but left alone, in Id3f457a8) * Don't use separate logic for initial toggling of custom togglers * Consistently use collapseText/expandText (uppercase T) instead of collapsetext/expandtext (lowercase t) where possible * Adjust comments and whitespace Change-Id: Id8ecd8f577fb4cd3f640a7830a85436a301b3e2c --- resources/jquery/jquery.makeCollapsible.js | 158 ++++++++------------- 1 file changed, 60 insertions(+), 98 deletions(-) diff --git a/resources/jquery/jquery.makeCollapsible.js b/resources/jquery/jquery.makeCollapsible.js index e1a07b6b9e..2d46bdef56 100644 --- a/resources/jquery/jquery.makeCollapsible.js +++ b/resources/jquery/jquery.makeCollapsible.js @@ -18,6 +18,8 @@ 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 @@ -195,44 +197,6 @@ 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. * @@ -258,17 +222,17 @@ * 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' ) ) { @@ -278,21 +242,33 @@ } // 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 = $( '' ) - .text( collapsetext ) + .text( collapseText ) .wrap( '' ) .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) @@ -311,25 +287,21 @@ } } - // 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' ); @@ -337,12 +309,10 @@ // 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' ) ) { @@ -354,17 +324,16 @@ 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( '
  • ' ).parent() ); + $toggleLink = $defaultToggleLink; + $toggleLink.wrap( '
  • ' ).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 { //
    ,

    etc. @@ -379,28 +348,21 @@ // 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 } ); } } ); }; -- 2.20.1