2 * jQuery makeCollapsible
5 * - CC BY 3.0 <http://creativecommons.org/licenses/by/3.0>
6 * - GPL2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
8 * @class jQuery.plugin.makeCollapsible
10 ( function ( $, mw
) {
13 * Handler for a click on a collapsible toggler.
16 * @param {jQuery} $collapsible
17 * @param {string} action The action this function will take ('expand' or 'collapse').
18 * @param {jQuery|null} [$defaultToggle]
19 * @param {Object|undefined} [options]
21 function toggleElement( $collapsible
, action
, $defaultToggle
, options
) {
22 var $collapsibleContent
, $containers
, hookCallback
;
23 options
= options
|| {};
25 // Validate parameters
27 // $collapsible must be an instance of jQuery
28 if ( !$collapsible
.jquery
) {
31 if ( action
!== 'expand' && action
!== 'collapse' ) {
32 // action must be string with 'expand' or 'collapse'
35 if ( $defaultToggle
=== undefined ) {
36 $defaultToggle
= null;
38 if ( $defaultToggle
!== null && !$defaultToggle
.jquery
) {
39 // is optional (may be undefined), but if defined it must be an instance of jQuery.
40 // If it's not, abort right away.
41 // After this $defaultToggle is either null or a valid jQuery instance.
45 // Trigger a custom event to allow callers to hook to the collapsing/expanding,
46 // allowing the module to be testable, and making it possible to
47 // e.g. implement persistence via cookies
48 $collapsible
.trigger( action
=== 'expand' ? 'beforeExpand.mw-collapsible' : 'beforeCollapse.mw-collapsible' );
49 hookCallback = function () {
50 $collapsible
.trigger( action
=== 'expand' ? 'afterExpand.mw-collapsible' : 'afterCollapse.mw-collapsible' );
53 // Handle different kinds of elements
55 if ( !options
.plainMode
&& $collapsible
.is( 'table' ) ) {
57 // If there is a caption, hide all rows; otherwise, only hide body rows
58 if ( $collapsible
.find( '> caption' ).length
) {
59 $containers
= $collapsible
.find( '> * > tr' );
61 $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().promise().done( hookCallback
);
79 $containers
.stop( true, true ).fadeIn().promise().done( 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().promise().done( hookCallback
);
98 $containers
.stop( true, true ).slideDown().promise().done( 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().promise().done( hookCallback
);
115 $collapsibleContent
.slideDown().promise().done( 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().promise().done( hookCallback
);
129 $collapsible
.slideUp().promise().done( hookCallback
);
133 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
134 $collapsible
.fadeIn().promise().done( hookCallback
);
136 $collapsible
.slideDown().promise().done( hookCallback
);
144 * Handle clicking/keypressing on the collapsible element toggle and other
145 * situations where a collapsible element is toggled (e.g. the initial
146 * toggle for collapsed ones).
149 * @param {jQuery} $toggle the clickable toggle itself
150 * @param {jQuery} $collapsible the collapsible element
151 * @param {jQuery.Event|null} e either the event or null if unavailable
152 * @param {Object|undefined} options
154 function togglingHandler( $toggle
, $collapsible
, e
, options
) {
155 var wasCollapsed
, $textContainer
, collapseText
, expandText
;
157 if ( options
=== undefined ) {
162 if ( e
.type
=== 'click' && options
.linksPassthru
&& $.nodeName( e
.target
, 'a' ) ) {
163 // Don't fire if a link was clicked, if requested (for premade togglers by default)
165 } else if ( e
.type
=== 'keypress' && e
.which
!== 13 && e
.which
!== 32 ) {
166 // Only handle keypresses on the "Enter" or "Space" keys
174 // This allows the element to be hidden on initial toggle without fiddling with the class
175 if ( options
.wasCollapsed
!== undefined ) {
176 wasCollapsed
= options
.wasCollapsed
;
178 wasCollapsed
= $collapsible
.hasClass( 'mw-collapsed' );
181 // Toggle the state of the collapsible element (that is, expand or collapse)
182 $collapsible
.toggleClass( 'mw-collapsed', !wasCollapsed
);
184 // Toggle the mw-collapsible-toggle classes, if requested (for default and premade togglers by default)
185 if ( options
.toggleClasses
) {
187 .toggleClass( 'mw-collapsible-toggle-collapsed', !wasCollapsed
)
188 .toggleClass( 'mw-collapsible-toggle-expanded', wasCollapsed
);
191 // Toggle the text ("Show"/"Hide"), if requested (for default togglers by default)
192 if ( options
.toggleText
) {
193 collapseText
= options
.toggleText
.collapseText
;
194 expandText
= options
.toggleText
.expandText
;
196 $textContainer
= $toggle
.find( '> a' );
197 if ( !$textContainer
.length
) {
198 $textContainer
= $toggle
;
200 $textContainer
.text( wasCollapsed
? collapseText
: expandText
);
203 // And finally toggle the element state itself
204 toggleElement( $collapsible
, wasCollapsed
? 'expand' : 'collapse', $toggle
, options
);
208 * Enable collapsible-functionality on all elements in the collection.
210 * - Will prevent binding twice to the same element.
211 * - Initial state is expanded by default, this can be overriden by adding class
212 * "mw-collapsed" to the "mw-collapsible" element.
213 * - Elements made collapsible have jQuery data "mw-made-collapsible" set to true.
214 * - The inner content is wrapped in a "div.mw-collapsible-content" (except for tables and lists).
216 * @param {Object} [options]
217 * @param {string} [options.collapseText] Text used for the toggler, when clicking it would
218 * collapse the element. Default: the 'data-collapsetext' attribute of the
219 * collapsible element or the content of 'collapsible-collapse' message.
220 * @param {string} [options.expandText] Text used for the toggler, when clicking it would
221 * expand the element. Default: the 'data-expandtext' attribute of the
222 * collapsible element or the content of 'collapsible-expand' message.
223 * @param {boolean} [options.collapsed] Whether to collapse immediately. By default
224 * collapse only if the elements has the 'mw-collapsible' class.
225 * @param {jQuery} [options.$customTogglers] Elements to be used as togglers
226 * for this collapsible element. By default, if the collapsible element
227 * has an id attribute like 'mw-customcollapsible-XXX', elements with a
228 * *class* of 'mw-customtoggle-XXX' are made togglers for it.
229 * @param {boolean} [options.plainMode=false] Whether to use a "plain mode" when making the
230 * element collapsible - that is, hide entire tables and lists (instead
231 * of hiding only all rows but first of tables, and hiding each list
232 * item separately for lists) and don't wrap other elements in
233 * div.mw-collapsible-content. May only be used with custom togglers.
237 $.fn
.makeCollapsible = function ( options
) {
238 if ( options
=== undefined ) {
242 return this.each( function () {
243 var $collapsible
, collapseText
, expandText
, $caption
, $toggle
, actionHandler
, buildDefaultToggleLink
,
244 premadeToggleHandler
, $toggleLink
, $firstItem
, collapsibleId
, $customTogglers
, firstval
;
246 // Ensure class "mw-collapsible" is present in case .makeCollapsible()
247 // is called on element(s) that don't have it yet.
248 $collapsible
= $( this ).addClass( 'mw-collapsible' );
250 // Return if it has been enabled already.
251 if ( $collapsible
.data( 'mw-made-collapsible' ) ) {
254 $collapsible
.data( 'mw-made-collapsible', true );
257 // Use custom text or default?
258 collapseText
= options
.collapseText
|| $collapsible
.attr( 'data-collapsetext' ) || mw
.msg( 'collapsible-collapse' );
259 expandText
= options
.expandText
|| $collapsible
.attr( 'data-expandtext' ) || mw
.msg( 'collapsible-expand' );
261 // Default click/keypress handler and toggle link to use when none is present
262 actionHandler = function ( e
, opts
) {
265 toggleText
: { collapseText
: collapseText
, expandText
: expandText
}
267 opts
= $.extend( defaultOpts
, options
, opts
);
268 togglingHandler( $( this ), $collapsible
, e
, opts
);
270 // Default toggle link. Only build it when needed to avoid jQuery memory leaks (event data).
271 buildDefaultToggleLink = function () {
272 return $( '<a href="#"></a>' )
273 .text( collapseText
)
274 .wrap( '<span class="mw-collapsible-toggle"></span>' )
276 .prepend( '<span class="mw-collapsible-bracket">[</span>' )
277 .append( '<span class="mw-collapsible-bracket">]</span>' )
278 .on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler
);
281 // Default handler for clicking on premade toggles
282 premadeToggleHandler = function ( e
, opts
) {
283 var defaultOpts
= { toggleClasses
: true, linksPassthru
: true };
284 opts
= $.extend( defaultOpts
, options
, opts
);
285 togglingHandler( $( this ), $collapsible
, e
, opts
);
288 // Check if this element has a custom position for the toggle link
289 // (ie. outside the container or deeper inside the tree)
290 if ( options
.$customTogglers
) {
291 $customTogglers
= $( options
.$customTogglers
);
293 collapsibleId
= $collapsible
.attr( 'id' ) || '';
294 if ( collapsibleId
.indexOf( 'mw-customcollapsible-' ) === 0 ) {
295 $customTogglers
= $( '.' + collapsibleId
.replace( 'mw-customcollapsible', 'mw-customtoggle' ) )
296 .addClass( 'mw-customtoggle' );
300 // Add event handlers to custom togglers or create our own ones
301 if ( $customTogglers
&& $customTogglers
.length
) {
302 actionHandler = function ( e
, opts
) {
303 var defaultOpts
= {};
304 opts
= $.extend( defaultOpts
, options
, opts
);
305 togglingHandler( $( this ), $collapsible
, e
, opts
);
308 $toggleLink
= $customTogglers
309 .on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler
)
310 .prop( 'tabIndex', 0 );
313 // If this is not a custom case, do the default: wrap the
314 // contents and add the toggle link. Different elements are
315 // treated differently.
316 if ( $collapsible
.is( 'table' ) ) {
318 // If the table has a caption, collapse to the caption
319 // as opposed to the first row
320 $caption
= $collapsible
.find( '> caption' );
321 if ( $caption
.length
) {
322 $toggle
= $caption
.find( '> .mw-collapsible-toggle' );
324 // If there is no toggle link, add it to the end of the caption
325 if ( !$toggle
.length
) {
326 $toggleLink
= buildDefaultToggleLink().appendTo( $caption
);
328 actionHandler
= premadeToggleHandler
;
329 $toggleLink
= $toggle
.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler
)
330 .prop( 'tabIndex', 0 );
333 // The toggle-link will be in one the the cells (td or th) of the first row
334 $firstItem
= $collapsible
.find( 'tr:first th, tr:first td' );
335 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
337 // If theres no toggle link, add it to the last cell
338 if ( !$toggle
.length
) {
339 $toggleLink
= buildDefaultToggleLink().prependTo( $firstItem
.eq( -1 ) );
341 actionHandler
= premadeToggleHandler
;
342 $toggleLink
= $toggle
.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler
)
343 .prop( 'tabIndex', 0 );
347 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
348 // The toggle-link will be in the first list-item
349 $firstItem
= $collapsible
.find( 'li:first' );
350 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
352 // If theres no toggle link, add it
353 if ( !$toggle
.length
) {
354 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
355 // to be "1". Except if the value-attribute is already used.
356 // If no value was set WebKit returns "", Mozilla returns '-1', others return 0, null or undefined.
357 firstval
= $firstItem
.prop( 'value' );
358 if ( firstval
=== undefined || !firstval
|| firstval
=== '-1' || firstval
=== -1 ) {
359 $firstItem
.prop( 'value', '1' );
361 $toggleLink
= buildDefaultToggleLink();
362 $toggleLink
.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent().prependTo( $collapsible
);
364 actionHandler
= premadeToggleHandler
;
365 $toggleLink
= $toggle
.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler
)
366 .prop( 'tabIndex', 0 );
369 } else { // <div>, <p> etc.
371 // The toggle-link will be the first child of the element
372 $toggle
= $collapsible
.find( '> .mw-collapsible-toggle' );
374 // If a direct child .content-wrapper does not exists, create it
375 if ( !$collapsible
.find( '> .mw-collapsible-content' ).length
) {
376 $collapsible
.wrapInner( '<div class="mw-collapsible-content"></div>' );
379 // If theres no toggle link, add it
380 if ( !$toggle
.length
) {
381 $toggleLink
= buildDefaultToggleLink().prependTo( $collapsible
);
383 actionHandler
= premadeToggleHandler
;
384 $toggleLink
= $toggle
.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler
)
385 .prop( 'tabIndex', 0 );
391 if ( options
.collapsed
|| $collapsible
.hasClass( 'mw-collapsed' ) ) {
392 // One toggler can hook to multiple elements, and one element can have
393 // multiple togglers. This is the sanest way to handle that.
394 actionHandler
.call( $toggleLink
.get( 0 ), null, { instantHide
: true, wasCollapsed
: false } );
401 * @mixins jQuery.plugin.makeCollapsible
404 }( jQuery
, mediaWiki
) );