jquery.makeCollapsible: use 'mw-collapsible' event namespace
[mediawiki.git] / resources / jquery / jquery.makeCollapsible.js
blob3600c5d6d60330794ef62c489bede1823bd7ece6
1 /**
2  * jQuery makeCollapsible
3  *
4  * This will enable collapsible-functionality on all passed elements.
5  * - Will prevent binding twice to the same element.
6  * - Initial state is expanded by default, this can be overriden by adding class
7  *   "mw-collapsed" to the "mw-collapsible" element.
8  * - Elements made collapsible have jQuery data "mw-made-collapsible" set to true.
9  * - The inner content is wrapped in a "div.mw-collapsible-content" (except for tables and lists).
10  *
11  * @author Krinkle, 2011-2012
12  *
13  * Dual license:
14  * @license CC-BY 3.0 <http://creativecommons.org/licenses/by/3.0>
15  * @license GPL2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
16  */
17 ( function ( $, mw ) {
18         var lpx = 'jquery.makeCollapsible> ';
20         /**
21          * @param {jQuery} $collapsible
22          * @param {string} action The action this function will take ('expand' or 'collapse').
23          * @param {jQuery|null} [optional] $defaultToggle
24          * @param {Object|undefined} options
25          */
26         function toggleElement( $collapsible, action, $defaultToggle, options ) {
27                 var $collapsibleContent, $containers;
28                 options = options || {};
30                 // Validate parameters
32                 // $collapsible must be an instance of jQuery
33                 if ( !$collapsible.jquery ) {
34                         return;
35                 }
36                 if ( action !== 'expand' && action !== 'collapse' ) {
37                         // action must be string with 'expand' or 'collapse'
38                         return;
39                 }
40                 if ( $defaultToggle === undefined ) {
41                         $defaultToggle = null;
42                 }
43                 if ( $defaultToggle !== null && !$defaultToggle.jquery ) {
44                         // is optional (may be undefined), but if defined it must be an instance of jQuery.
45                         // If it's not, abort right away.
46                         // After this $defaultToggle is either null or a valid jQuery instance.
47                         return;
48                 }
50                 // Handle different kinds of elements
52                 if ( !options.plainMode && $collapsible.is( 'table' ) ) {
53                         // Tables
54                         $containers = $collapsible.find( '> tbody > tr' );
55                         if ( $defaultToggle ) {
56                                 // Exclude table row containing togglelink
57                                 $containers = $containers.not( $defaultToggle.closest( 'tr' ) );
58                         }
60                         if ( action === 'collapse' ) {
61                                 // Hide all table rows of this table
62                                 // Slide doesn't work with tables, but fade does as of jQuery 1.1.3
63                                 // http://stackoverflow.com/questions/467336#920480
64                                 if ( options.instantHide ) {
65                                         $containers.hide();
66                                 } else {
67                                         $containers.stop( true, true ).fadeOut();
68                                 }
69                         } else {
70                                 $containers.stop( true, true ).fadeIn();
71                         }
73                 } else if ( !options.plainMode && ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) ) {
74                         // Lists
75                         $containers = $collapsible.find( '> li' );
76                         if ( $defaultToggle ) {
77                                 // Exclude list-item containing togglelink
78                                 $containers = $containers.not( $defaultToggle.parent() );
79                         }
81                         if ( action === 'collapse' ) {
82                                 if ( options.instantHide ) {
83                                         $containers.hide();
84                                 } else {
85                                         $containers.stop( true, true ).slideUp();
86                                 }
87                         } else {
88                                 $containers.stop( true, true ).slideDown();
89                         }
91                 } else {
92                         // Everything else: <div>, <p> etc.
93                         $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
95                         // If a collapsible-content is defined, act on it
96                         if ( !options.plainMode && $collapsibleContent.length ) {
97                                 if ( action === 'collapse' ) {
98                                         if ( options.instantHide ) {
99                                                 $collapsibleContent.hide();
100                                         } else {
101                                                 $collapsibleContent.slideUp();
102                                         }
103                                 } else {
104                                         $collapsibleContent.slideDown();
105                                 }
107                         // Otherwise assume this is a customcollapse with a remote toggle
108                         // .. and there is no collapsible-content because the entire element should be toggled
109                         } else {
110                                 if ( action === 'collapse' ) {
111                                         if ( options.instantHide ) {
112                                                 $collapsible.hide();
113                                         } else {
114                                                 if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
115                                                         $collapsible.fadeOut();
116                                                 } else {
117                                                         $collapsible.slideUp();
118                                                 }
119                                         }
120                                 } else {
121                                         if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
122                                                 $collapsible.fadeIn();
123                                         } else {
124                                                 $collapsible.slideDown();
125                                         }
126                                 }
127                         }
128                 }
129         }
131         /**
132          * Handles clicking on the collapsible element toggle and other
133          * situations where a collapsible element is toggled (e.g. the initial
134          * toggle for collapsed ones).
135          *
136          * @param {jQuery} $toggle the clickable toggle itself
137          * @param {jQuery} $collapsible the collapsible element
138          * @param {jQuery.Event|null} e either the event or null if unavailable
139          * @param {Object|undefined} options
140          */
141         function togglingHandler( $toggle, $collapsible, event, options ) {
142                 var wasCollapsed, $textContainer, collapseText, expandText;
144                 if ( event ) {
145                         // Don't fire if a link was clicked, if requested  (for premade togglers by default)
146                         if ( options.linksPassthru && $.nodeName( event.target, 'a' ) ) {
147                                 return true;
148                         } else {
149                                 event.preventDefault();
150                                 event.stopPropagation();
151                         }
152                 }
154                 wasCollapsed = $collapsible.hasClass( 'mw-collapsed' );
156                 // Toggle the state of the collapsible element (that is, expand or collapse)
157                 $collapsible.toggleClass( 'mw-collapsed', !wasCollapsed );
159                 // Toggle the mw-collapsible-toggle classes, if requested (for default and premade togglers by default)
160                 if ( options.toggleClasses ) {
161                         $toggle
162                                 .toggleClass( 'mw-collapsible-toggle-collapsed', !wasCollapsed )
163                                 .toggleClass( 'mw-collapsible-toggle-expanded', wasCollapsed );
164                 }
166                 // Toggle the text ("Show"/"Hide"), if requested (for default togglers by default)
167                 if ( options.toggleText ) {
168                         collapseText = options.toggleText.collapseText;
169                         expandText = options.toggleText.expandText;
171                         $textContainer = $toggle.find( '> a' );
172                         if ( !$textContainer.length ) {
173                                 $textContainer = $toggle;
174                         }
175                         $textContainer.text( wasCollapsed ? collapseText : expandText );
176                 }
178                 // And finally toggle the element state itself
179                 toggleElement( $collapsible, wasCollapsed ? 'expand' : 'collapse', $toggle, options );
180         }
182         /**
183          * Toggles collapsible and togglelink class and updates text label.
184          *
185          * @param {jQuery} $that
186          * @param {jQuery.Event} e
187          * @param {Object|undefined} options
188          */
189         function toggleLinkDefault( $that, e, options ) {
190                 var $collapsible = $that.closest( '.mw-collapsible' );
191                 options = $.extend( { toggleClasses: true }, options );
192                 togglingHandler( $that, $collapsible, e, options );
193         }
195         /**
196          * Toggles collapsible and togglelink class.
197          *
198          * @param {jQuery} $that
199          * @param {jQuery.Event} e
200          * @param {Object|undefined} options
201          */
202         function toggleLinkPremade( $that, e, options ) {
203                 var $collapsible = $that.eq( 0 ).closest( '.mw-collapsible' );
204                 options = $.extend( { toggleClasses: true }, options );
205                 togglingHandler( $that, $collapsible, e, options );
206         }
208         /**
209          * Toggles customcollapsible.
210          *
211          * @param {jQuery} $that
212          * @param {jQuery.Event} e
213          * @param {Object|undefined} options
214          * @param {jQuery} $collapsible
215          */
216         function toggleLinkCustom( $that, e, options, $collapsible ) {
217                 options = $.extend( { linksPassthru: true }, options );
218                 togglingHandler( $that, $collapsible, e, options );
219         }
221         /**
222          * Make any element collapsible.
223          *
224          * Supported options:
225          * - collapseText: text to be used for the toggler when clicking it would
226          *   collapse the element. Default: the 'data-collapsetext' attribute of
227          *   the collapsible element or the content of 'collapsible-collapse'
228          *   message.
229          * - expandText: text to be used for the toggler when clicking it would
230          *   expand the element. Default: the 'data-expandtext' attribute of
231          *   the collapsible element or the content of 'collapsible-expand'
232          *   message.
233          * - collapsed: boolean, whether to collapse immediately. By default
234          *   collapse only if the elements has the 'mw-collapsible' class.
235          * - $customTogglers: jQuerified list of elements to be used as togglers
236          *   for this collapsible element. By default, if the collapsible element
237          *   has an id attribute like 'mw-customcollapsible-XXX', elements with a
238          *   *class* of 'mw-customtoggle-XXX' are made togglers for it.
239          * - plainMode: boolean, whether to use a "plain mode" when making the
240          *   element collapsible - that is, hide entire tables and lists (instead
241          *   of hiding only all rows but first of tables, and hiding each list
242          *   item separately for lists) and don't wrap other elements in
243          *   div.mw-collapsible-content. May only be used with custom togglers.
244          */
245         $.fn.makeCollapsible = function ( options ) {
246                 return this.each(function () {
247                         var $collapsible, collapsetext, expandtext, $toggle, $toggleLink, $firstItem, collapsibleId,
248                                 $customTogglers, firstval;
250                         if ( options === undefined ) {
251                                 options = {};
252                         }
254                         // Ensure class "mw-collapsible" is present in case .makeCollapsible()
255                         // is called on element(s) that don't have it yet.
256                         $collapsible = $(this).addClass( 'mw-collapsible' );
258                         // Return if it has been enabled already.
259                         if ( $collapsible.data( 'mw-made-collapsible' ) ) {
260                                 return;
261                         } else {
262                                 $collapsible.data( 'mw-made-collapsible', true );
263                         }
265                         // Use custom text or default?
266                         collapsetext = options.collapseText || $collapsible.attr( 'data-collapsetext' ) || mw.msg( 'collapsible-collapse' );
267                         expandtext = options.expandText || $collapsible.attr( 'data-expandtext' ) || mw.msg( 'collapsible-expand' );
269                         // Create toggle link with a space around the brackets (&nbsp;[text]&nbsp;)
270                         $toggleLink =
271                                 $( '<a href="#"></a>' )
272                                         .text( collapsetext )
273                                         .wrap( '<span class="mw-collapsible-toggle"></span>' )
274                                                 .parent()
275                                                 .prepend( '&nbsp;[' )
276                                                 .append( ']&nbsp;' )
277                                                 .on( 'click.mw-collapsible', function ( e, opts ) {
278                                                         opts = $.extend( { toggleText: { collapseText: collapsetext, expandText: expandtext } }, options, opts );
279                                                         toggleLinkDefault( $(this), e, opts );
280                                                 } );
282                         // Check if this element has a custom position for the toggle link
283                         // (ie. outside the container or deeper inside the tree)
284                         if ( options.$customTogglers ) {
285                                 $customTogglers = $( options.$customTogglers );
286                         } else {
287                                 collapsibleId = $collapsible.attr( 'id' ) || '';
288                                 if ( collapsibleId.indexOf( 'mw-customcollapsible-' ) === 0 ) {
289                                         mw.log( lpx + 'Found custom collapsible: #' + collapsibleId );
290                                         $customTogglers = $( '.' + collapsibleId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
292                                         // Double check that there is actually a customtoggle link
293                                         if ( !$customTogglers.length ) {
294                                                 mw.log( lpx + '#' + collapsibleId + ': Missing toggler!' );
295                                         }
296                                 }
297                         }
299                         // Bind the custom togglers
300                         if ( $customTogglers && $customTogglers.length ) {
301                                 $customTogglers.on( 'click.mw-collapsible', function ( e, opts ) {
302                                         opts = $.extend( {}, options, opts );
303                                         toggleLinkCustom( $(this), e, opts, $collapsible );
304                                 } );
306                                 // Initial state
307                                 if ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) {
308                                         // Remove here so that the toggler goes in the right direction,
309                                         // It re-adds the class.
310                                         $collapsible.removeClass( 'mw-collapsed' );
311                                         toggleLinkCustom( $customTogglers, null, $.extend( { instantHide: true }, options ), $collapsible );
312                                 }
314                         // If this is not a custom case, do the default:
315                         // Wrap the contents and add the toggle link
316                         } else {
317                                 // Elements are treated differently
318                                 if ( $collapsible.is( 'table' ) ) {
319                                         // The toggle-link will be in one the the cells (td or th) of the first row
320                                         $firstItem = $collapsible.find( 'tr:first th, tr:first td' );
321                                         $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
323                                         // If theres no toggle link, add it to the last cell
324                                         if ( !$toggle.length ) {
325                                                 $firstItem.eq(-1).prepend( $toggleLink );
326                                         } else {
327                                                 $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
328                                                         opts = $.extend( {}, options, opts );
329                                                         toggleLinkPremade( $toggle, e, opts );
330                                                 } );
331                                         }
333                                 } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
334                                         // The toggle-link will be in the first list-item
335                                         $firstItem = $collapsible.find( 'li:first' );
336                                         $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
338                                         // If theres no toggle link, add it
339                                         if ( !$toggle.length ) {
340                                                 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
341                                                 // to be "1". Except if the value-attribute is already used.
342                                                 // If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
343                                                 firstval = $firstItem.attr( 'value' );
344                                                 if ( firstval === undefined || !firstval || firstval === '-1' || firstval === -1 ) {
345                                                         $firstItem.attr( 'value', '1' );
346                                                 }
347                                                 $collapsible.prepend( $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
348                                         } else {
349                                                 $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
350                                                         opts = $.extend( {}, options, opts );
351                                                         toggleLinkPremade( $toggle, e, opts );
352                                                 } );
353                                         }
355                                 } else { // <div>, <p> etc.
357                                         // The toggle-link will be the first child of the element
358                                         $toggle = $collapsible.find( '> .mw-collapsible-toggle' );
360                                         // If a direct child .content-wrapper does not exists, create it
361                                         if ( !$collapsible.find( '> .mw-collapsible-content' ).length ) {
362                                                 $collapsible.wrapInner( '<div class="mw-collapsible-content"></div>' );
363                                         }
365                                         // If theres no toggle link, add it
366                                         if ( !$toggle.length ) {
367                                                 $collapsible.prepend( $toggleLink );
368                                         } else {
369                                                 $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
370                                                         opts = $.extend( {}, options, opts );
371                                                         toggleLinkPremade( $toggle, e, opts );
372                                                 } );
373                                         }
374                                 }
375                         }
377                         // Initial state (only for those that are not custom,
378                         // because the initial state of those has been taken care of already).
379                         if (
380                                 ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) &&
381                                 ( !$customTogglers || !$customTogglers.length )
382                         ) {
383                                 $collapsible.removeClass( 'mw-collapsed' );
384                                 // The collapsible element could have multiple togglers
385                                 // To toggle the initial state only click one of them (ie. the first one, eq(0) )
386                                 // Else it would go like: hide,show,hide,show for each toggle link.
387                                 // This is just like it would be in reality (only one toggle is clicked at a time).
388                                 $toggleLink.eq( 0 ).trigger( 'click', [ { instantHide: true } ] );
389                         }
390                 } );
391         };
392 }( jQuery, mediaWiki ) );