Merge "makeCollapsible: Reduce duplicate code for adding eventhandlers"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Mon, 4 Jun 2018 21:40:38 +0000 (21:40 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 4 Jun 2018 21:40:38 +0000 (21:40 +0000)
resources/src/jquery/jquery.makeCollapsible.js

index eb42311..082603a 100644 (file)
                options = options || {};
 
                this.each( function () {
-                       var $collapsible, collapseText, expandText, $caption, $toggle, actionHandler, buildDefaultToggleLink,
-                               $toggleLink, $firstItem, collapsibleId, $customTogglers, firstval;
+                       var $collapsible, collapseText, expandText, $caption, $toggle, actionHandler,
+                               buildDefaultToggleLink, $firstItem, collapsibleId, $customTogglers, firstval;
 
                        // Ensure class "mw-collapsible" is present in case .makeCollapsible()
                        // is called on element(s) that don't have it yet.
                                        .attr( {
                                                role: 'button',
                                                tabindex: 0
-                                       } )
-                                       .on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler );
+                                       } );
                        };
 
                        // Check if this element has a custom position for the toggle link
                                        togglingHandler( $( this ), $collapsible, e, opts );
                                };
 
-                               $toggleLink = $customTogglers
-                                       .on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
-                                       .prop( 'tabIndex', 0 );
+                               $toggle = $customTogglers;
 
                        } else {
                                // If this is not a custom case, do the default: wrap the
 
                                                // If there is no toggle link, add it to the end of the caption
                                                if ( !$toggle.length ) {
-                                                       $toggleLink = buildDefaultToggleLink().appendTo( $caption );
-                                               } else {
-                                                       $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
-                                                               .prop( 'tabIndex', 0 );
+                                                       $toggle = buildDefaultToggleLink().appendTo( $caption );
                                                }
                                        } else {
                                                // The toggle-link will be in one of the cells (td or th) of the first row
 
                                                // If theres no toggle link, add it to the last cell
                                                if ( !$toggle.length ) {
-                                                       $toggleLink = buildDefaultToggleLink().prependTo( $firstItem.eq( -1 ) );
-                                               } else {
-                                                       $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
-                                                               .prop( 'tabIndex', 0 );
+                                                       $toggle = buildDefaultToggleLink().prependTo( $firstItem.eq( -1 ) );
                                                }
                                        }
 
                                        $collapsible.find( '> .mw-collapsible-toggle' ).length === 0
                                ) {
                                        // special case of one collapsible in <li> tag
-                                       $toggleLink = buildDefaultToggleLink();
-                                       $collapsible.before( $toggleLink );
+                                       $toggle = buildDefaultToggleLink();
+                                       $collapsible.before( $toggle );
                                } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
                                        // The toggle-link will be in the first list-item
                                        $firstItem = $collapsible.find( 'li:first' );
                                                if ( firstval === undefined || !firstval || firstval === '-1' || firstval === -1 ) {
                                                        $firstItem.prop( 'value', '1' );
                                                }
-                                               $toggleLink = buildDefaultToggleLink();
-                                               $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent().prependTo( $collapsible );
-                                       } else {
-                                               $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
-                                                       .prop( 'tabIndex', 0 );
+                                               $toggle = buildDefaultToggleLink();
+                                               $toggle.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent().prependTo( $collapsible );
                                        }
 
                                } else { // <div>, <p> etc.
 
                                        // If theres no toggle link, add it
                                        if ( !$toggle.length ) {
-                                               $toggleLink = buildDefaultToggleLink().prependTo( $collapsible );
-                                       } else {
-                                               $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
-                                                       .prop( 'tabIndex', 0 );
+                                               $toggle = buildDefaultToggleLink().prependTo( $collapsible );
                                        }
                                }
                        }
 
+                       // Attach event handlers to togglelink
+                       $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
+                               .prop( 'tabIndex', 0 );
+
                        $( this ).data( 'mw-collapsible', {
                                collapse: function () {
-                                       actionHandler.call( $toggleLink.get( 0 ), null, { wasCollapsed: false } );
+                                       actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: false } );
                                },
                                expand: function () {
-                                       actionHandler.call( $toggleLink.get( 0 ), null, { wasCollapsed: true } );
+                                       actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: true } );
                                },
                                toggle: function () {
-                                       actionHandler.call( $toggleLink.get( 0 ), null, null );
+                                       actionHandler.call( $toggle.get( 0 ), null, null );
                                }
                        } );
 
                        if ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) {
                                // One toggler can hook to multiple elements, and one element can have
                                // multiple togglers. This is the sanest way to handle that.
-                               actionHandler.call( $toggleLink.get( 0 ), null, { wasCollapsed: false } );
+                               actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: false } );
                        }
 
                } );