Merge "Make autoblocks update with the parent block"
[lhc/web/wiklou.git] / resources / jquery / jquery.makeCollapsible.js
index 0cd6417..60c0afc 100644 (file)
@@ -15,8 +15,6 @@
  * @license GPL2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
  */
 ( function ( $, mw ) {
-       var lpx = 'jquery.makeCollapsible> ';
-
        /**
         * Handler for a click on a collapsible toggler.
         *
 
                if ( !options.plainMode && $collapsible.is( 'table' ) ) {
                        // Tables
-                       $containers = $collapsible.find( '> tbody > tr' );
+                       // If there is a caption, hide all rows; otherwise, only hide body rows
+                       if ( $collapsible.find( '> caption' ).length ) {
+                               $containers = $collapsible.find( '> * > tr' );
+                       } else {
+                               $containers = $collapsible.find( '> tbody > tr' );
+                       }
                        if ( $defaultToggle ) {
                                // Exclude table row containing togglelink
                                $containers = $containers.not( $defaultToggle.closest( 'tr' ) );
                }
 
                return this.each( function () {
-                       var $collapsible, collapseText, expandText, $toggle, actionHandler, buildDefaultToggleLink,
+                       var $collapsible, collapseText, expandText, $caption, $toggle, actionHandler, buildDefaultToggleLink,
                                premadeToggleHandler, $toggleLink, $firstItem, collapsibleId, $customTogglers, firstval;
 
                        // Ensure class "mw-collapsible" is present in case .makeCollapsible()
                        } else {
                                collapsibleId = $collapsible.attr( 'id' ) || '';
                                if ( collapsibleId.indexOf( 'mw-customcollapsible-' ) === 0 ) {
-                                       mw.log( lpx + 'Found custom collapsible: #' + collapsibleId );
                                        $customTogglers = $( '.' + collapsibleId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
-
-                                       // Double check that there is actually a customtoggle link
-                                       if ( !$customTogglers.length ) {
-                                               mw.log( lpx + '#' + collapsibleId + ': Missing toggler!' );
-                                       }
                                }
                        }
 
-                       // Bind the togglers
+                       // Add event handlers to custom togglers or create our own ones
                        if ( $customTogglers && $customTogglers.length ) {
                                actionHandler = function ( e, opts ) {
                                        var defaultOpts = {};
                                // 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' );
-                                       $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
 
-                                       // If theres no toggle link, add it to the last cell
-                                       if ( !$toggle.length ) {
-                                               $toggleLink = buildDefaultToggleLink().prependTo( $firstItem.eq( -1 ) );
+                                       // If the table has a caption, collapse to the caption
+                                       // as opposed to the first row
+                                       $caption = $collapsible.find( '> caption' );
+                                       if ( $caption.length ) {
+                                               $toggle = $caption.find( '> .mw-collapsible-toggle' );
+
+                                               // If there is no toggle link, add it to the end of the caption
+                                               if ( !$toggle.length ) {
+                                                       $toggleLink = buildDefaultToggleLink().appendTo( $caption );
+                                               } else {
+                                                       actionHandler = premadeToggleHandler;
+                                                       $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler );
+                                               }
                                        } else {
-                                               actionHandler = premadeToggleHandler;
-                                               $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler );
+                                               // 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' );
+                                               $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
+
+                                               // If theres no toggle link, add it to the last cell
+                                               if ( !$toggle.length ) {
+                                                       $toggleLink = buildDefaultToggleLink().prependTo( $firstItem.eq( -1 ) );
+                                               } else {
+                                                       actionHandler = premadeToggleHandler;
+                                                       $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler );
+                                               }
                                        }
 
                                } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {