2 * jQuery makeCollapsible
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).
11 * @author Krinkle, 2011-2012
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>
17 ( function ( $, mw
) {
18 var lpx
= 'jquery.makeCollapsible> ';
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
26 function toggleElement( $collapsible
, action
, $defaultToggle
, options
) {
27 var $collapsibleContent
, $containers
, hookCallback
;
28 options
= options
|| {};
30 // Validate parameters
32 // $collapsible must be an instance of jQuery
33 if ( !$collapsible
.jquery
) {
36 if ( action
!== 'expand' && action
!== 'collapse' ) {
37 // action must be string with 'expand' or 'collapse'
40 if ( $defaultToggle
=== undefined ) {
41 $defaultToggle
= null;
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.
50 // Trigger a custom event to allow callers to hook to the collapsing/expanding,
51 // allowing the module to be testable, and making it possible to
52 // e.g. implement persistence via cookies
53 $collapsible
.trigger( action
=== 'expand' ? 'beforeExpand.mw-collapsible' : 'beforeCollapse.mw-collapsible' );
54 hookCallback = function () {
55 $collapsible
.trigger( action
=== 'expand' ? 'afterExpand.mw-collapsible' : 'afterCollapse.mw-collapsible' );
58 // Handle different kinds of elements
60 if ( !options
.plainMode
&& $collapsible
.is( 'table' ) ) {
62 $containers
= $collapsible
.find( '> tbody > tr' );
63 if ( $defaultToggle
) {
64 // Exclude table row containing togglelink
65 $containers
= $containers
.not( $defaultToggle
.closest( 'tr' ) );
68 if ( action
=== 'collapse' ) {
69 // Hide all table rows of this table
70 // Slide doesn't work with tables, but fade does as of jQuery 1.1.3
71 // http://stackoverflow.com/questions/467336#920480
72 if ( options
.instantHide
) {
76 $containers
.stop( true, true ).fadeOut( hookCallback
);
79 $containers
.stop( true, true ).fadeIn( hookCallback
);
82 } else if ( !options
.plainMode
&& ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) ) {
84 $containers
= $collapsible
.find( '> li' );
85 if ( $defaultToggle
) {
86 // Exclude list-item containing togglelink
87 $containers
= $containers
.not( $defaultToggle
.parent() );
90 if ( action
=== 'collapse' ) {
91 if ( options
.instantHide
) {
95 $containers
.stop( true, true ).slideUp( hookCallback
);
98 $containers
.stop( true, true ).slideDown( hookCallback
);
102 // Everything else: <div>, <p> etc.
103 $collapsibleContent
= $collapsible
.find( '> .mw-collapsible-content' );
105 // If a collapsible-content is defined, act on it
106 if ( !options
.plainMode
&& $collapsibleContent
.length
) {
107 if ( action
=== 'collapse' ) {
108 if ( options
.instantHide
) {
109 $collapsibleContent
.hide();
112 $collapsibleContent
.slideUp( hookCallback
);
115 $collapsibleContent
.slideDown( hookCallback
);
118 // Otherwise assume this is a customcollapse with a remote toggle
119 // .. and there is no collapsible-content because the entire element should be toggled
121 if ( action
=== 'collapse' ) {
122 if ( options
.instantHide
) {
126 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
127 $collapsible
.fadeOut( hookCallback
);
129 $collapsible
.slideUp( hookCallback
);
133 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
134 $collapsible
.fadeIn( hookCallback
);
136 $collapsible
.slideDown( hookCallback
);
144 * Handles clicking on the collapsible element toggle and other
145 * situations where a collapsible element is toggled (e.g. the initial
146 * toggle for collapsed ones).
148 * @param {jQuery} $toggle the clickable toggle itself
149 * @param {jQuery} $collapsible the collapsible element
150 * @param {jQuery.Event|null} e either the event or null if unavailable
151 * @param {Object|undefined} options
153 function togglingHandler( $toggle
, $collapsible
, e
, options
) {
154 var wasCollapsed
, $textContainer
, collapseText
, expandText
;
156 if ( options
=== undefined ) {
161 // Don't fire if a link was clicked, if requested (for premade togglers by default)
162 if ( options
.linksPassthru
&& $.nodeName( e
.target
, 'a' ) ) {
170 wasCollapsed
= $collapsible
.hasClass( 'mw-collapsed' );
172 // Toggle the state of the collapsible element (that is, expand or collapse)
173 $collapsible
.toggleClass( 'mw-collapsed', !wasCollapsed
);
175 // Toggle the mw-collapsible-toggle classes, if requested (for default and premade togglers by default)
176 if ( options
.toggleClasses
) {
178 .toggleClass( 'mw-collapsible-toggle-collapsed', !wasCollapsed
)
179 .toggleClass( 'mw-collapsible-toggle-expanded', wasCollapsed
);
182 // Toggle the text ("Show"/"Hide"), if requested (for default togglers by default)
183 if ( options
.toggleText
) {
184 collapseText
= options
.toggleText
.collapseText
;
185 expandText
= options
.toggleText
.expandText
;
187 $textContainer
= $toggle
.find( '> a' );
188 if ( !$textContainer
.length
) {
189 $textContainer
= $toggle
;
191 $textContainer
.text( wasCollapsed
? collapseText
: expandText
);
194 // And finally toggle the element state itself
195 toggleElement( $collapsible
, wasCollapsed
? 'expand' : 'collapse', $toggle
, options
);
199 * Toggles collapsible and togglelink class and updates text label.
201 * @param {jQuery} $that
202 * @param {jQuery.Event} e
203 * @param {Object|undefined} options
205 function toggleLinkDefault( $that
, e
, options
) {
206 var $collapsible
= $that
.closest( '.mw-collapsible' );
207 options
= $.extend( { toggleClasses
: true }, options
);
208 togglingHandler( $that
, $collapsible
, e
, options
);
212 * Toggles collapsible and togglelink class.
214 * @param {jQuery} $that
215 * @param {jQuery.Event} e
216 * @param {Object|undefined} options
218 function toggleLinkPremade( $that
, e
, options
) {
219 var $collapsible
= $that
.eq( 0 ).closest( '.mw-collapsible' );
220 options
= $.extend( { toggleClasses
: true, linksPassthru
: true }, options
);
221 togglingHandler( $that
, $collapsible
, e
, options
);
225 * Toggles customcollapsible.
227 * @param {jQuery} $that
228 * @param {jQuery.Event} e
229 * @param {Object|undefined} options
230 * @param {jQuery} $collapsible
232 function toggleLinkCustom( $that
, e
, options
, $collapsible
) {
233 togglingHandler( $that
, $collapsible
, e
, options
);
237 * Make any element collapsible.
240 * - collapseText: text to be used for the toggler when clicking it would
241 * collapse the element. Default: the 'data-collapsetext' attribute of
242 * the collapsible element or the content of 'collapsible-collapse'
244 * - expandText: text to be used for the toggler when clicking it would
245 * expand the element. Default: the 'data-expandtext' attribute of
246 * the collapsible element or the content of 'collapsible-expand'
248 * - collapsed: boolean, whether to collapse immediately. By default
249 * collapse only if the elements has the 'mw-collapsible' class.
250 * - $customTogglers: jQuerified list of elements to be used as togglers
251 * for this collapsible element. By default, if the collapsible element
252 * has an id attribute like 'mw-customcollapsible-XXX', elements with a
253 * *class* of 'mw-customtoggle-XXX' are made togglers for it.
254 * - plainMode: boolean, whether to use a "plain mode" when making the
255 * element collapsible - that is, hide entire tables and lists (instead
256 * of hiding only all rows but first of tables, and hiding each list
257 * item separately for lists) and don't wrap other elements in
258 * div.mw-collapsible-content. May only be used with custom togglers.
260 $.fn
.makeCollapsible = function ( options
) {
261 return this.each(function () {
262 var $collapsible
, collapsetext
, expandtext
, $toggle
, $toggleLink
, $firstItem
, collapsibleId
,
263 $customTogglers
, firstval
;
265 if ( options
=== undefined ) {
269 // Ensure class "mw-collapsible" is present in case .makeCollapsible()
270 // is called on element(s) that don't have it yet.
271 $collapsible
= $(this).addClass( 'mw-collapsible' );
273 // Return if it has been enabled already.
274 if ( $collapsible
.data( 'mw-made-collapsible' ) ) {
277 $collapsible
.data( 'mw-made-collapsible', true );
280 // Use custom text or default?
281 collapsetext
= options
.collapseText
|| $collapsible
.attr( 'data-collapsetext' ) || mw
.msg( 'collapsible-collapse' );
282 expandtext
= options
.expandText
|| $collapsible
.attr( 'data-expandtext' ) || mw
.msg( 'collapsible-expand' );
284 // Create toggle link with a space around the brackets ( [text] )
286 $( '<a href="#"></a>' )
287 .text( collapsetext
)
288 .wrap( '<span class="mw-collapsible-toggle"></span>' )
290 .prepend( ' [' )
292 .on( 'click.mw-collapsible', function ( e
, opts
) {
293 opts
= $.extend( { toggleText
: { collapseText
: collapsetext
, expandText
: expandtext
} }, options
, opts
);
294 toggleLinkDefault( $(this), e
, opts
);
297 // Check if this element has a custom position for the toggle link
298 // (ie. outside the container or deeper inside the tree)
299 if ( options
.$customTogglers
) {
300 $customTogglers
= $( options
.$customTogglers
);
302 collapsibleId
= $collapsible
.attr( 'id' ) || '';
303 if ( collapsibleId
.indexOf( 'mw-customcollapsible-' ) === 0 ) {
304 mw
.log( lpx
+ 'Found custom collapsible: #' + collapsibleId
);
305 $customTogglers
= $( '.' + collapsibleId
.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
307 // Double check that there is actually a customtoggle link
308 if ( !$customTogglers
.length
) {
309 mw
.log( lpx
+ '#' + collapsibleId
+ ': Missing toggler!' );
314 // Bind the custom togglers
315 if ( $customTogglers
&& $customTogglers
.length
) {
316 $customTogglers
.on( 'click.mw-collapsible', function ( e
, opts
) {
317 opts
= $.extend( {}, options
, opts
);
318 toggleLinkCustom( $(this), e
, opts
, $collapsible
);
322 if ( options
.collapsed
|| $collapsible
.hasClass( 'mw-collapsed' ) ) {
323 // Remove here so that the toggler goes in the right direction,
324 // It re-adds the class.
325 $collapsible
.removeClass( 'mw-collapsed' );
326 toggleLinkCustom( $customTogglers
, null, $.extend( { instantHide
: true }, options
), $collapsible
);
329 // If this is not a custom case, do the default:
330 // Wrap the contents and add the toggle link
332 // Elements are treated differently
333 if ( $collapsible
.is( 'table' ) ) {
334 // The toggle-link will be in one the the cells (td or th) of the first row
335 $firstItem
= $collapsible
.find( 'tr:first th, tr:first td' );
336 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
338 // If theres no toggle link, add it to the last cell
339 if ( !$toggle
.length
) {
340 $firstItem
.eq(-1).prepend( $toggleLink
);
342 $toggleLink
= $toggle
.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e
, opts
) {
343 opts
= $.extend( {}, options
, opts
);
344 toggleLinkPremade( $toggle
, e
, opts
);
348 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
349 // The toggle-link will be in the first list-item
350 $firstItem
= $collapsible
.find( 'li:first' );
351 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
353 // If theres no toggle link, add it
354 if ( !$toggle
.length
) {
355 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
356 // to be "1". Except if the value-attribute is already used.
357 // If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
358 firstval
= $firstItem
.attr( 'value' );
359 if ( firstval
=== undefined || !firstval
|| firstval
=== '-1' || firstval
=== -1 ) {
360 $firstItem
.attr( 'value', '1' );
362 $collapsible
.prepend( $toggleLink
.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
364 $toggleLink
= $toggle
.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e
, opts
) {
365 opts
= $.extend( {}, options
, opts
);
366 toggleLinkPremade( $toggle
, e
, opts
);
370 } else { // <div>, <p> etc.
372 // The toggle-link will be the first child of the element
373 $toggle
= $collapsible
.find( '> .mw-collapsible-toggle' );
375 // If a direct child .content-wrapper does not exists, create it
376 if ( !$collapsible
.find( '> .mw-collapsible-content' ).length
) {
377 $collapsible
.wrapInner( '<div class="mw-collapsible-content"></div>' );
380 // If theres no toggle link, add it
381 if ( !$toggle
.length
) {
382 $collapsible
.prepend( $toggleLink
);
384 $toggleLink
= $toggle
.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e
, opts
) {
385 opts
= $.extend( {}, options
, opts
);
386 toggleLinkPremade( $toggle
, e
, opts
);
392 // Initial state (only for those that are not custom,
393 // because the initial state of those has been taken care of already).
395 ( options
.collapsed
|| $collapsible
.hasClass( 'mw-collapsed' ) ) &&
396 ( !$customTogglers
|| !$customTogglers
.length
)
398 $collapsible
.removeClass( 'mw-collapsed' );
399 // The collapsible element could have multiple togglers
400 // To toggle the initial state only click one of them (ie. the first one, eq(0) )
401 // Else it would go like: hide,show,hide,show for each toggle link.
402 // This is just like it would be in reality (only one toggle is clicked at a time).
403 $toggleLink
.eq( 0 ).trigger( 'click', [ { instantHide
: true } ] );
407 }( jQuery
, mediaWiki
) );