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
, event
, options
) {
154 var wasCollapsed
, $textContainer
, collapseText
, expandText
;
157 // Don't fire if a link was clicked, if requested (for premade togglers by default)
158 if ( options
.linksPassthru
&& $.nodeName( event
.target
, 'a' ) ) {
161 event
.preventDefault();
162 event
.stopPropagation();
166 wasCollapsed
= $collapsible
.hasClass( 'mw-collapsed' );
168 // Toggle the state of the collapsible element (that is, expand or collapse)
169 $collapsible
.toggleClass( 'mw-collapsed', !wasCollapsed
);
171 // Toggle the mw-collapsible-toggle classes, if requested (for default and premade togglers by default)
172 if ( options
.toggleClasses
) {
174 .toggleClass( 'mw-collapsible-toggle-collapsed', !wasCollapsed
)
175 .toggleClass( 'mw-collapsible-toggle-expanded', wasCollapsed
);
178 // Toggle the text ("Show"/"Hide"), if requested (for default togglers by default)
179 if ( options
.toggleText
) {
180 collapseText
= options
.toggleText
.collapseText
;
181 expandText
= options
.toggleText
.expandText
;
183 $textContainer
= $toggle
.find( '> a' );
184 if ( !$textContainer
.length
) {
185 $textContainer
= $toggle
;
187 $textContainer
.text( wasCollapsed
? collapseText
: expandText
);
190 // And finally toggle the element state itself
191 toggleElement( $collapsible
, wasCollapsed
? 'expand' : 'collapse', $toggle
, options
);
195 * Toggles collapsible and togglelink class and updates text label.
197 * @param {jQuery} $that
198 * @param {jQuery.Event} e
199 * @param {Object|undefined} options
201 function toggleLinkDefault( $that
, e
, options
) {
202 var $collapsible
= $that
.closest( '.mw-collapsible' );
203 options
= $.extend( { toggleClasses
: true }, options
);
204 togglingHandler( $that
, $collapsible
, e
, options
);
208 * Toggles collapsible and togglelink class.
210 * @param {jQuery} $that
211 * @param {jQuery.Event} e
212 * @param {Object|undefined} options
214 function toggleLinkPremade( $that
, e
, options
) {
215 var $collapsible
= $that
.eq( 0 ).closest( '.mw-collapsible' );
216 options
= $.extend( { toggleClasses
: true }, options
);
217 togglingHandler( $that
, $collapsible
, e
, options
);
221 * Toggles customcollapsible.
223 * @param {jQuery} $that
224 * @param {jQuery.Event} e
225 * @param {Object|undefined} options
226 * @param {jQuery} $collapsible
228 function toggleLinkCustom( $that
, e
, options
, $collapsible
) {
229 options
= $.extend( { linksPassthru
: true }, options
);
230 togglingHandler( $that
, $collapsible
, e
, options
);
234 * Make any element collapsible.
237 * - collapseText: text to be used for the toggler when clicking it would
238 * collapse the element. Default: the 'data-collapsetext' attribute of
239 * the collapsible element or the content of 'collapsible-collapse'
241 * - expandText: text to be used for the toggler when clicking it would
242 * expand the element. Default: the 'data-expandtext' attribute of
243 * the collapsible element or the content of 'collapsible-expand'
245 * - collapsed: boolean, whether to collapse immediately. By default
246 * collapse only if the elements has the 'mw-collapsible' class.
247 * - $customTogglers: jQuerified list of elements to be used as togglers
248 * for this collapsible element. By default, if the collapsible element
249 * has an id attribute like 'mw-customcollapsible-XXX', elements with a
250 * *class* of 'mw-customtoggle-XXX' are made togglers for it.
251 * - plainMode: boolean, whether to use a "plain mode" when making the
252 * element collapsible - that is, hide entire tables and lists (instead
253 * of hiding only all rows but first of tables, and hiding each list
254 * item separately for lists) and don't wrap other elements in
255 * div.mw-collapsible-content. May only be used with custom togglers.
257 $.fn
.makeCollapsible = function ( options
) {
258 return this.each(function () {
259 var $collapsible
, collapsetext
, expandtext
, $toggle
, $toggleLink
, $firstItem
, collapsibleId
,
260 $customTogglers
, firstval
;
262 if ( options
=== undefined ) {
266 // Ensure class "mw-collapsible" is present in case .makeCollapsible()
267 // is called on element(s) that don't have it yet.
268 $collapsible
= $(this).addClass( 'mw-collapsible' );
270 // Return if it has been enabled already.
271 if ( $collapsible
.data( 'mw-made-collapsible' ) ) {
274 $collapsible
.data( 'mw-made-collapsible', true );
277 // Use custom text or default?
278 collapsetext
= options
.collapseText
|| $collapsible
.attr( 'data-collapsetext' ) || mw
.msg( 'collapsible-collapse' );
279 expandtext
= options
.expandText
|| $collapsible
.attr( 'data-expandtext' ) || mw
.msg( 'collapsible-expand' );
281 // Create toggle link with a space around the brackets ( [text] )
283 $( '<a href="#"></a>' )
284 .text( collapsetext
)
285 .wrap( '<span class="mw-collapsible-toggle"></span>' )
287 .prepend( ' [' )
289 .on( 'click.mw-collapsible', function ( e
, opts
) {
290 opts
= $.extend( { toggleText
: { collapseText
: collapsetext
, expandText
: expandtext
} }, options
, opts
);
291 toggleLinkDefault( $(this), e
, opts
);
294 // Check if this element has a custom position for the toggle link
295 // (ie. outside the container or deeper inside the tree)
296 if ( options
.$customTogglers
) {
297 $customTogglers
= $( options
.$customTogglers
);
299 collapsibleId
= $collapsible
.attr( 'id' ) || '';
300 if ( collapsibleId
.indexOf( 'mw-customcollapsible-' ) === 0 ) {
301 mw
.log( lpx
+ 'Found custom collapsible: #' + collapsibleId
);
302 $customTogglers
= $( '.' + collapsibleId
.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
304 // Double check that there is actually a customtoggle link
305 if ( !$customTogglers
.length
) {
306 mw
.log( lpx
+ '#' + collapsibleId
+ ': Missing toggler!' );
311 // Bind the custom togglers
312 if ( $customTogglers
&& $customTogglers
.length
) {
313 $customTogglers
.on( 'click.mw-collapsible', function ( e
, opts
) {
314 opts
= $.extend( {}, options
, opts
);
315 toggleLinkCustom( $(this), e
, opts
, $collapsible
);
319 if ( options
.collapsed
|| $collapsible
.hasClass( 'mw-collapsed' ) ) {
320 // Remove here so that the toggler goes in the right direction,
321 // It re-adds the class.
322 $collapsible
.removeClass( 'mw-collapsed' );
323 toggleLinkCustom( $customTogglers
, null, $.extend( { instantHide
: true }, options
), $collapsible
);
326 // If this is not a custom case, do the default:
327 // Wrap the contents and add the toggle link
329 // Elements are treated differently
330 if ( $collapsible
.is( 'table' ) ) {
331 // The toggle-link will be in one the the cells (td or th) of the first row
332 $firstItem
= $collapsible
.find( 'tr:first th, tr:first td' );
333 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
335 // If theres no toggle link, add it to the last cell
336 if ( !$toggle
.length
) {
337 $firstItem
.eq(-1).prepend( $toggleLink
);
339 $toggleLink
= $toggle
.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e
, opts
) {
340 opts
= $.extend( {}, options
, opts
);
341 toggleLinkPremade( $toggle
, e
, opts
);
345 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
346 // The toggle-link will be in the first list-item
347 $firstItem
= $collapsible
.find( 'li:first' );
348 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
350 // If theres no toggle link, add it
351 if ( !$toggle
.length
) {
352 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
353 // to be "1". Except if the value-attribute is already used.
354 // If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
355 firstval
= $firstItem
.attr( 'value' );
356 if ( firstval
=== undefined || !firstval
|| firstval
=== '-1' || firstval
=== -1 ) {
357 $firstItem
.attr( 'value', '1' );
359 $collapsible
.prepend( $toggleLink
.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
361 $toggleLink
= $toggle
.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e
, opts
) {
362 opts
= $.extend( {}, options
, opts
);
363 toggleLinkPremade( $toggle
, e
, opts
);
367 } else { // <div>, <p> etc.
369 // The toggle-link will be the first child of the element
370 $toggle
= $collapsible
.find( '> .mw-collapsible-toggle' );
372 // If a direct child .content-wrapper does not exists, create it
373 if ( !$collapsible
.find( '> .mw-collapsible-content' ).length
) {
374 $collapsible
.wrapInner( '<div class="mw-collapsible-content"></div>' );
377 // If theres no toggle link, add it
378 if ( !$toggle
.length
) {
379 $collapsible
.prepend( $toggleLink
);
381 $toggleLink
= $toggle
.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e
, opts
) {
382 opts
= $.extend( {}, options
, opts
);
383 toggleLinkPremade( $toggle
, e
, opts
);
389 // Initial state (only for those that are not custom,
390 // because the initial state of those has been taken care of already).
392 ( options
.collapsed
|| $collapsible
.hasClass( 'mw-collapsed' ) ) &&
393 ( !$customTogglers
|| !$customTogglers
.length
)
395 $collapsible
.removeClass( 'mw-collapsed' );
396 // The collapsible element could have multiple togglers
397 // To toggle the initial state only click one of them (ie. the first one, eq(0) )
398 // Else it would go like: hide,show,hide,show for each toggle link.
399 // This is just like it would be in reality (only one toggle is clicked at a time).
400 $toggleLink
.eq( 0 ).trigger( 'click', [ { instantHide
: true } ] );
404 }( jQuery
, mediaWiki
) );