Matmarex has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/53685


Change subject: jQuery.makeCollapsible: clean up the handler toggling logic
......................................................................

jQuery.makeCollapsible: clean up the handler toggling logic

Merged three functions which did almost the same thing into one,
togglingHandler(), which takes a few options instead.

Left the old functions as wrappers for now, I'll clean them up in
another commit.

Change-Id: Id3f457a84064382f2d962e9ac4d926827de93126
---
M resources/jquery/jquery.makeCollapsible.js
1 file changed, 56 insertions(+), 60 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/85/53685/1

diff --git a/resources/jquery/jquery.makeCollapsible.js 
b/resources/jquery/jquery.makeCollapsible.js
index b369584..95d4eec 100644
--- a/resources/jquery/jquery.makeCollapsible.js
+++ b/resources/jquery/jquery.makeCollapsible.js
@@ -36,6 +36,8 @@
                        firstval;
 
                /**
+                * Toggle visibility of 
+                *
                 * @param {jQuery} $collapsible
                 * @param {string} action The action this function will take 
('expand' or 'collapse').
                 * @param {jQuery|null} [optional] $defaultToggle
@@ -147,6 +149,52 @@
                }
 
                /**
+                * Handlers clicking on the collapsible element toggle and other
+                * situations where a collapsible element is toggled (e.g. the 
initial
+                * toggle for collapsed ones).
+                *
+                * @param {jQuery} $toggle the clickable toggle itself
+                * @param {jQuery} $collapsible the collapsible element
+                * @param {jQuery.Event|null} e either the event or null if 
unavailable
+                * @param {Object|undefined} options
+                */
+               function togglingHandler( $toggle, $collapsible, event, options 
) {
+                       if ( options.linksPassthru ) {
+                               // Don't fire if a link was clicked, if 
requested  (for premade togglers by default),
+                               if ( $.nodeName( event.target, 'a' ) ) {
+                                       return true;
+                               }
+                       } else if ( event ) {
+                               event.preventDefault();
+                               event.stopPropagation();
+                       }
+
+                       var wasCollapsed = $collapsible.hasClass( 
'mw-collapsed' );
+
+                       // Toggle the state of the collapsible element (that 
is, expand or collapse)
+                       $collapsible.toggleClass( 'mw-collapsed', !wasCollapsed 
);
+
+                       // Toggle the mw-collapsible-toggle classes, if 
requested (for default and premade togglers by default)
+                       if ( options.toggleClasses ) {
+                               $toggle
+                                       .toggleClass( 
'mw-collapsible-toggle-collapsed', !wasCollapsed )
+                                       .toggleClass( 
'mw-collapsible-toggle-expanded', wasCollapsed );
+                       }
+
+                       // Toggle the text ("Show"/"Hide"), if requested (for 
default togglers by default)
+                       if ( options.toggleText ) {
+                               var $textContainer = $toggle.find( '> a' );
+                               if ( !$textContainer.length ) {
+                                       $textContainer = $toggle;
+                               }
+                               $textContainer.text( wasCollapsed ? 
collapsetext : expandtext );
+                       }
+
+                       // And finally toggle the element state itself
+                       toggleElement( $collapsible, wasCollapsed ? 'expand' : 
'collapse', $toggle, options );
+               }
+
+               /**
                 * Toggles collapsible and togglelink class and updates text 
label.
                 *
                 * @param {jQuery} $that
@@ -154,35 +202,9 @@
                 * @param {Object|undefined} options
                 */
                function toggleLinkDefault( $that, e, options ) {
-                       var $collapsible = $that.closest( '.mw-collapsible' 
).toggleClass( 'mw-collapsed' );
-                       e.preventDefault();
-                       e.stopPropagation();
-
-                       // It's expanded right now
-                       if ( !$that.hasClass( 'mw-collapsible-toggle-collapsed' 
) ) {
-                               // Change link to "Show"
-                               $that.removeClass( 
'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' 
);
-                               if ( $that.find( '> a' ).length ) {
-                                       $that.find( '> a' ).text( expandtext );
-                               } else {
-                                       $that.text( expandtext );
-                               }
-                               // Collapse element
-                               toggleElement( $collapsible, 'collapse', $that, 
options );
-
-                       // It's collapsed right now
-                       } else {
-                               // Change link to "Hide"
-                               $that.removeClass( 
'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' 
);
-                               if ( $that.find( '> a' ).length ) {
-                                       $that.find( '> a' ).text( collapsetext 
);
-                               } else {
-                                       $that.text( collapsetext );
-                               }
-                               // Expand element
-                               toggleElement( $collapsible, 'expand', $that, 
options );
-                       }
-                       return;
+                       var $collapsible = $that.closest( '.mw-collapsible' );
+                       options = $.extend( {}, options, { toggleText: true, 
toggleClasses: true } );
+                       togglingHandler( $that, $collapsible, e, options );
                }
 
                /**
@@ -193,28 +215,9 @@
                 * @param {Object|undefined} options
                 */
                function toggleLinkPremade( $that, e, options ) {
-                       var $collapsible = $that.eq( 0 ).closest( 
'.mw-collapsible' ).toggleClass( 'mw-collapsed' );
-                       if ( $.nodeName( e.target, 'a' ) ) {
-                               return true;
-                       }
-                       e.preventDefault();
-                       e.stopPropagation();
-
-                       // It's expanded right now
-                       if ( !$that.hasClass( 'mw-collapsible-toggle-collapsed' 
) ) {
-                               // Change toggle to collapsed
-                               $that.removeClass( 
'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' 
);
-                               // Collapse element
-                               toggleElement( $collapsible, 'collapse', $that, 
options );
-
-                       // It's collapsed right now
-                       } else {
-                               // Change toggle to expanded
-                               $that.removeClass( 
'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' 
);
-                               // Expand element
-                               toggleElement( $collapsible, 'expand', $that, 
options );
-                       }
-                       return;
+                       var $collapsible = $that.eq( 0 ).closest( 
'.mw-collapsible' );
+                       options = $.extend( {}, options, { toggleClasses: true 
} );
+                       togglingHandler( $that, $collapsible, e, options );
                }
 
                /**
@@ -226,15 +229,8 @@
                 * @param {jQuery} $collapsible
                 */
                function toggleLinkCustom( $that, e, options, $collapsible ) {
-                       // For the initial state call of customtogglers there 
is no event passed
-                       if ( e ) {
-                               e.preventDefault();
-                               e.stopPropagation();
-                       }
-                       // Get current state and toggle to the opposite
-                       var action = $collapsible.hasClass( 'mw-collapsed' ) ? 
'expand' : 'collapse';
-                       $collapsible.toggleClass( 'mw-collapsed' );
-                       toggleElement( $collapsible, action, $that, options );
+                       options = $.extend( {}, options, { linksPassthru: true 
} );
+                       togglingHandler( $that, $collapsible, e, options );
                }
 
                // Return if it has been enabled already.

-- 
To view, visit https://gerrit.wikimedia.org/r/53685
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id3f457a84064382f2d962e9ac4d926827de93126
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Matmarex <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to