e8e59d006a0abd09fba822a83fbf8254648932a6
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 // Ensure class "mw-collapsible" is present in case .makeCollapsible()
251 // is called on element(s) that don't have it yet.
252 $collapsible
= $(this).addClass( 'mw-collapsible' );
254 // Return if it has been enabled already.
255 if ( $collapsible
.data( 'mw-made-collapsible' ) ) {
258 $collapsible
.data( 'mw-made-collapsible', true );
261 // Use custom text or default?
262 collapsetext
= options
.collapsetext
|| $collapsible
.attr( 'data-collapsetext' ) || mw
.msg( 'collapsible-collapse' );
263 expandtext
= options
.expandText
|| $collapsible
.attr( 'data-expandtext' ) || mw
.msg( 'collapsible-expand' );
265 // Create toggle link with a space around the brackets ( [text] )
267 $( '<a href="#"></a>' )
268 .text( collapsetext
)
269 .wrap( '<span class="mw-collapsible-toggle"></span>' )
271 .prepend( ' [' )
273 .on( 'click.mw-collapse', function ( e
, opts
) {
274 opts
= $.extend( { toggleText
: { collapseText
: collapsetext
, expandText
: expandtext
} }, options
, opts
);
275 toggleLinkDefault( $(this), e
, opts
);
278 // Check if this element has a custom position for the toggle link
279 // (ie. outside the container or deeper inside the tree)
280 if ( options
.$customTogglers
) {
281 $customTogglers
= $( options
.$customTogglers
);
283 collapsibleId
= $collapsible
.attr( 'id' ) || '';
284 if ( collapsibleId
.indexOf( 'mw-customcollapsible-' ) === 0 ) {
285 mw
.log( lpx
+ 'Found custom collapsible: #' + collapsibleId
);
286 $customTogglers
= $( '.' + collapsibleId
.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
288 // Double check that there is actually a customtoggle link
289 if ( !$customTogglers
.length
) {
290 mw
.log( lpx
+ '#' + collapsibleId
+ ': Missing toggler!' );
295 // Bind the custom togglers
296 if ( $customTogglers
&& $customTogglers
.length
) {
297 $customTogglers
.on( 'click.mw-collapse', function ( e
, opts
) {
298 opts
= $.extend( {}, options
, opts
);
299 toggleLinkCustom( $(this), e
, opts
, $collapsible
);
303 if ( options
.collapsed
|| $collapsible
.hasClass( 'mw-collapsed' ) ) {
304 // Remove here so that the toggler goes in the right direction,
305 // It re-adds the class.
306 $collapsible
.removeClass( 'mw-collapsed' );
307 toggleLinkCustom( $customTogglers
, null, $.extend( { instantHide
: true }, options
), $collapsible
);
310 // If this is not a custom case, do the default:
311 // Wrap the contents and add the toggle link
313 // Elements are treated differently
314 if ( $collapsible
.is( 'table' ) ) {
315 // The toggle-link will be in one the the cells (td or th) of the first row
316 $firstItem
= $collapsible
.find( 'tr:first th, tr:first td' );
317 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
319 // If theres no toggle link, add it to the last cell
320 if ( !$toggle
.length
) {
321 $firstItem
.eq(-1).prepend( $toggleLink
);
323 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, opts
) {
324 opts
= $.extend( {}, options
, opts
);
325 toggleLinkPremade( $toggle
, e
, opts
);
329 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
330 // The toggle-link will be in the first list-item
331 $firstItem
= $collapsible
.find( 'li:first' );
332 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
334 // If theres no toggle link, add it
335 if ( !$toggle
.length
) {
336 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
337 // to be "1". Except if the value-attribute is already used.
338 // If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
339 firstval
= $firstItem
.attr( 'value' );
340 if ( firstval
=== undefined || !firstval
|| firstval
=== '-1' || firstval
=== -1 ) {
341 $firstItem
.attr( 'value', '1' );
343 $collapsible
.prepend( $toggleLink
.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
345 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, opts
) {
346 opts
= $.extend( {}, options
, opts
);
347 toggleLinkPremade( $toggle
, e
, opts
);
351 } else { // <div>, <p> etc.
353 // The toggle-link will be the first child of the element
354 $toggle
= $collapsible
.find( '> .mw-collapsible-toggle' );
356 // If a direct child .content-wrapper does not exists, create it
357 if ( !$collapsible
.find( '> .mw-collapsible-content' ).length
) {
358 $collapsible
.wrapInner( '<div class="mw-collapsible-content"></div>' );
361 // If theres no toggle link, add it
362 if ( !$toggle
.length
) {
363 $collapsible
.prepend( $toggleLink
);
365 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, opts
) {
366 opts
= $.extend( {}, options
, opts
);
367 toggleLinkPremade( $toggle
, e
, opts
);
373 // Initial state (only for those that are not custom,
374 // because the initial state of those has been taken care of already).
376 ( options
.collapsed
|| $collapsible
.hasClass( 'mw-collapsed' ) ) &&
377 ( !$customTogglers
|| !$customTogglers
.length
)
379 $collapsible
.removeClass( 'mw-collapsed' );
380 // The collapsible element could have multiple togglers
381 // To toggle the initial state only click one of them (ie. the first one, eq(0) )
382 // Else it would go like: hide,show,hide,show for each toggle link.
383 // This is just like it would be in reality (only one toggle is clicked at a time).
384 $toggleLink
.eq( 0 ).trigger( 'click', [ { instantHide
: true } ] );
388 }( jQuery
, mediaWiki
) );