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
;
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 // Handle different kinds of elements
52 if ( !options
.plainMode
&& $collapsible
.is( 'table' ) ) {
54 $containers
= $collapsible
.find( '> tbody > tr' );
55 if ( $defaultToggle
) {
56 // Exclude table row containing togglelink
57 $containers
= $containers
.not( $defaultToggle
.closest( 'tr' ) );
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
) {
67 $containers
.stop( true, true ).fadeOut();
70 $containers
.stop( true, true ).fadeIn();
73 } else if ( !options
.plainMode
&& ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) ) {
75 $containers
= $collapsible
.find( '> li' );
76 if ( $defaultToggle
) {
77 // Exclude list-item containing togglelink
78 $containers
= $containers
.not( $defaultToggle
.parent() );
81 if ( action
=== 'collapse' ) {
82 if ( options
.instantHide
) {
85 $containers
.stop( true, true ).slideUp();
88 $containers
.stop( true, true ).slideDown();
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();
101 $collapsibleContent
.slideUp();
104 $collapsibleContent
.slideDown();
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
110 if ( action
=== 'collapse' ) {
111 if ( options
.instantHide
) {
114 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
115 $collapsible
.fadeOut();
117 $collapsible
.slideUp();
121 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
122 $collapsible
.fadeIn();
124 $collapsible
.slideDown();
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).
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
141 function togglingHandler( $toggle
, $collapsible
, event
, options
) {
142 var wasCollapsed
, $textContainer
, collapseText
, expandText
;
145 // Don't fire if a link was clicked, if requested (for premade togglers by default)
146 if ( options
.linksPassthru
&& $.nodeName( event
.target
, 'a' ) ) {
149 event
.preventDefault();
150 event
.stopPropagation();
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
) {
162 .toggleClass( 'mw-collapsible-toggle-collapsed', !wasCollapsed
)
163 .toggleClass( 'mw-collapsible-toggle-expanded', wasCollapsed
);
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
;
175 $textContainer
.text( wasCollapsed
? collapseText
: expandText
);
178 // And finally toggle the element state itself
179 toggleElement( $collapsible
, wasCollapsed
? 'expand' : 'collapse', $toggle
, options
);
183 * Toggles collapsible and togglelink class and updates text label.
185 * @param {jQuery} $that
186 * @param {jQuery.Event} e
187 * @param {Object|undefined} options
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
);
196 * Toggles collapsible and togglelink class.
198 * @param {jQuery} $that
199 * @param {jQuery.Event} e
200 * @param {Object|undefined} options
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
);
209 * Toggles customcollapsible.
211 * @param {jQuery} $that
212 * @param {jQuery.Event} e
213 * @param {Object|undefined} options
214 * @param {jQuery} $collapsible
216 function toggleLinkCustom( $that
, e
, options
, $collapsible
) {
217 options
= $.extend( { linksPassthru
: true }, options
);
218 togglingHandler( $that
, $collapsible
, e
, options
);
222 * Make any element collapsible.
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'
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'
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.
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 ) {
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' ) ) {
262 $collapsible
.data( 'mw-made-collapsible', true );
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 ( [text] )
271 $( '<a href="#"></a>' )
272 .text( collapsetext
)
273 .wrap( '<span class="mw-collapsible-toggle"></span>' )
275 .prepend( ' [' )
277 .on( 'click.mw-collapse', function ( e
, opts
) {
278 opts
= $.extend( { toggleText
: { collapseText
: collapsetext
, expandText
: expandtext
} }, options
, opts
);
279 toggleLinkDefault( $(this), e
, opts
);
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
);
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!' );
299 // Bind the custom togglers
300 if ( $customTogglers
&& $customTogglers
.length
) {
301 $customTogglers
.on( 'click.mw-collapse', function ( e
, opts
) {
302 opts
= $.extend( {}, options
, opts
);
303 toggleLinkCustom( $(this), e
, opts
, $collapsible
);
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
);
314 // If this is not a custom case, do the default:
315 // Wrap the contents and add the toggle link
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
);
327 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, opts
) {
328 opts
= $.extend( {}, options
, opts
);
329 toggleLinkPremade( $toggle
, e
, opts
);
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' );
347 $collapsible
.prepend( $toggleLink
.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
349 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, opts
) {
350 opts
= $.extend( {}, options
, opts
);
351 toggleLinkPremade( $toggle
, e
, opts
);
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>' );
365 // If theres no toggle link, add it
366 if ( !$toggle
.length
) {
367 $collapsible
.prepend( $toggleLink
);
369 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, opts
) {
370 opts
= $.extend( {}, options
, opts
);
371 toggleLinkPremade( $toggle
, e
, opts
);
377 // Initial state (only for those that are not custom,
378 // because the initial state of those has been taken care of already).
380 ( options
.collapsed
|| $collapsible
.hasClass( 'mw-collapsed' ) ) &&
381 ( !$customTogglers
|| !$customTogglers
.length
)
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 } ] );
392 }( jQuery
, mediaWiki
) );