jquery.makeCollapsible: Simplify code
[lhc/web/wiklou.git] / resources / src / jquery / jquery.makeCollapsible.js
index f7c4217..bdb5ce8 100644 (file)
         */
        function togglingHandler( $toggle, $collapsible, e, options ) {
                var wasCollapsed, $textContainer, collapseText, expandText;
-
-               if ( options === undefined ) {
-                       options = {};
-               }
+               options = options || {};
 
                if ( e ) {
-                       if ( e.type === 'click' && options.linksPassthru && $.nodeName( e.target, 'a' ) ) {
-                               // Don't fire if a link was clicked, if requested  (for premade togglers by default)
+                       if (
+                               e.type === 'click' &&
+                               options.linksPassthru &&
+                               $.nodeName( e.target, 'a' ) &&
+                               $( e.target ).attr( 'href' ) &&
+                               $( e.target ).attr( 'href' ) !== '#'
+                       ) {
+                               // Don't fire if a link with href !== '#' was clicked, if requested  (for premade togglers by default)
                                return;
                        } else if ( e.type === 'keypress' && e.which !== 13 && e.which !== 32 ) {
                                // Only handle keypresses on the "Enter" or "Space" keys
         * @chainable
         */
        $.fn.makeCollapsible = function ( options ) {
-               if ( options === undefined ) {
-                       options = {};
-               }
+               options = options || {};
 
-               return this.each( function () {
+               this.each( function () {
                        var $collapsible, collapseText, expandText, $caption, $toggle, actionHandler, buildDefaultToggleLink,
                                premadeToggleHandler, $toggleLink, $firstItem, collapsibleId, $customTogglers, firstval;
 
                                }
                        }
 
+                       $( this ).data( 'mw-collapsible', {
+                               collapse: function () {
+                                       actionHandler.call( $toggleLink.get( 0 ), null, { instantHide: true, wasCollapsed: false } );
+                               },
+                               expand: function () {
+                                       actionHandler.call( $toggleLink.get( 0 ), null, { instantHide: true, wasCollapsed: true } );
+                               },
+                               toggle: function () {
+                                       actionHandler.call( $toggleLink.get( 0 ), null, null );
+                               }
+                       } );
+
                        // Initial state
                        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, { instantHide: true, wasCollapsed: false } );
                        }
+
                } );
+
+               /**
+                * Fired after collapsible content has been initialized
+                *
+                * This gives an option to modify the collapsible behavior.
+                *
+                * @event wikipage_collapsibleContent
+                * @member mw.hook
+                * @param {jQuery} $content All the elements that have been made collapsible
+                */
+               mw.hook( 'wikipage.collapsibleContent' ).fire( this );
+
+               return this;
        };
 
        /**