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 * Handler for a click on a collapsible toggler.
23 * @param {jQuery} $collapsible
24 * @param {string} action The action this function will take ('expand' or 'collapse').
25 * @param {jQuery|null} [optional] $defaultToggle
26 * @param {Object|undefined} options
28 function toggleElement( $collapsible
, action
, $defaultToggle
, options
) {
29 var $collapsibleContent
, $containers
, hookCallback
;
30 options
= options
|| {};
32 // Validate parameters
34 // $collapsible must be an instance of jQuery
35 if ( !$collapsible
.jquery
) {
38 if ( action
!== 'expand' && action
!== 'collapse' ) {
39 // action must be string with 'expand' or 'collapse'
42 if ( $defaultToggle
=== undefined ) {
43 $defaultToggle
= null;
45 if ( $defaultToggle
!== null && !$defaultToggle
.jquery
) {
46 // is optional (may be undefined), but if defined it must be an instance of jQuery.
47 // If it's not, abort right away.
48 // After this $defaultToggle is either null or a valid jQuery instance.
52 // Trigger a custom event to allow callers to hook to the collapsing/expanding,
53 // allowing the module to be testable, and making it possible to
54 // e.g. implement persistence via cookies
55 $collapsible
.trigger( action
=== 'expand' ? 'beforeExpand.mw-collapsible' : 'beforeCollapse.mw-collapsible' );
56 hookCallback = function () {
57 $collapsible
.trigger( action
=== 'expand' ? 'afterExpand.mw-collapsible' : 'afterCollapse.mw-collapsible' );
60 // Handle different kinds of elements
62 if ( !options
.plainMode
&& $collapsible
.is( 'table' ) ) {
64 $containers
= $collapsible
.find( '> tbody > tr' );
65 if ( $defaultToggle
) {
66 // Exclude table row containing togglelink
67 $containers
= $containers
.not( $defaultToggle
.closest( 'tr' ) );
70 if ( action
=== 'collapse' ) {
71 // Hide all table rows of this table
72 // Slide doesn't work with tables, but fade does as of jQuery 1.1.3
73 // http://stackoverflow.com/questions/467336#920480
74 if ( options
.instantHide
) {
78 $containers
.stop( true, true ).fadeOut( hookCallback
);
81 $containers
.stop( true, true ).fadeIn( hookCallback
);
84 } else if ( !options
.plainMode
&& ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) ) {
86 $containers
= $collapsible
.find( '> li' );
87 if ( $defaultToggle
) {
88 // Exclude list-item containing togglelink
89 $containers
= $containers
.not( $defaultToggle
.parent() );
92 if ( action
=== 'collapse' ) {
93 if ( options
.instantHide
) {
97 $containers
.stop( true, true ).slideUp( hookCallback
);
100 $containers
.stop( true, true ).slideDown( hookCallback
);
104 // Everything else: <div>, <p> etc.
105 $collapsibleContent
= $collapsible
.find( '> .mw-collapsible-content' );
107 // If a collapsible-content is defined, act on it
108 if ( !options
.plainMode
&& $collapsibleContent
.length
) {
109 if ( action
=== 'collapse' ) {
110 if ( options
.instantHide
) {
111 $collapsibleContent
.hide();
114 $collapsibleContent
.slideUp( hookCallback
);
117 $collapsibleContent
.slideDown( hookCallback
);
120 // Otherwise assume this is a customcollapse with a remote toggle
121 // .. and there is no collapsible-content because the entire element should be toggled
123 if ( action
=== 'collapse' ) {
124 if ( options
.instantHide
) {
128 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
129 $collapsible
.fadeOut( hookCallback
);
131 $collapsible
.slideUp( hookCallback
);
135 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
136 $collapsible
.fadeIn( hookCallback
);
138 $collapsible
.slideDown( hookCallback
);
146 * Handles clicking/keypressing on the collapsible element toggle and other
147 * situations where a collapsible element is toggled (e.g. the initial
148 * toggle for collapsed ones).
150 * @param {jQuery} $toggle the clickable toggle itself
151 * @param {jQuery} $collapsible the collapsible element
152 * @param {jQuery.Event|null} e either the event or null if unavailable
153 * @param {Object|undefined} options
155 function togglingHandler( $toggle
, $collapsible
, e
, options
) {
156 var wasCollapsed
, $textContainer
, collapseText
, expandText
;
158 if ( options
=== undefined ) {
163 if ( e
.type
=== 'click' && options
.linksPassthru
&& $.nodeName( e
.target
, 'a' ) ) {
164 // Don't fire if a link was clicked, if requested (for premade togglers by default)
166 } else if ( e
.type
=== 'keypress' && e
.which
!== 13 ) {
167 // Only handle keypresses on the "Enter" key
175 wasCollapsed
= $collapsible
.hasClass( 'mw-collapsed' );
177 // Toggle the state of the collapsible element (that is, expand or collapse)
178 $collapsible
.toggleClass( 'mw-collapsed', !wasCollapsed
);
180 // Toggle the mw-collapsible-toggle classes, if requested (for default and premade togglers by default)
181 if ( options
.toggleClasses
) {
183 .toggleClass( 'mw-collapsible-toggle-collapsed', !wasCollapsed
)
184 .toggleClass( 'mw-collapsible-toggle-expanded', wasCollapsed
);
187 // Toggle the text ("Show"/"Hide"), if requested (for default togglers by default)
188 if ( options
.toggleText
) {
189 collapseText
= options
.toggleText
.collapseText
;
190 expandText
= options
.toggleText
.expandText
;
192 $textContainer
= $toggle
.find( '> a' );
193 if ( !$textContainer
.length
) {
194 $textContainer
= $toggle
;
196 $textContainer
.text( wasCollapsed
? collapseText
: expandText
);
199 // And finally toggle the element state itself
200 toggleElement( $collapsible
, wasCollapsed
? 'expand' : 'collapse', $toggle
, options
);
204 * Make any element collapsible.
207 * - collapseText: text to be used for the toggler when clicking it would
208 * collapse the element. Default: the 'data-collapsetext' attribute of
209 * the collapsible element or the content of 'collapsible-collapse'
211 * - expandText: text to be used for the toggler when clicking it would
212 * expand the element. Default: the 'data-expandtext' attribute of
213 * the collapsible element or the content of 'collapsible-expand'
215 * - collapsed: boolean, whether to collapse immediately. By default
216 * collapse only if the elements has the 'mw-collapsible' class.
217 * - $customTogglers: jQuerified list of elements to be used as togglers
218 * for this collapsible element. By default, if the collapsible element
219 * has an id attribute like 'mw-customcollapsible-XXX', elements with a
220 * *class* of 'mw-customtoggle-XXX' are made togglers for it.
221 * - plainMode: boolean, whether to use a "plain mode" when making the
222 * element collapsible - that is, hide entire tables and lists (instead
223 * of hiding only all rows but first of tables, and hiding each list
224 * item separately for lists) and don't wrap other elements in
225 * div.mw-collapsible-content. May only be used with custom togglers.
227 $.fn
.makeCollapsible = function ( options
) {
228 if ( options
=== undefined ) {
232 return this.each( function () {
233 var $collapsible
, collapseText
, expandText
, $toggle
, actionHandler
, buildDefaultToggleLink
,
234 premadeToggleHandler
, $toggleLink
, $firstItem
, collapsibleId
, $customTogglers
, firstval
;
236 // Ensure class "mw-collapsible" is present in case .makeCollapsible()
237 // is called on element(s) that don't have it yet.
238 $collapsible
= $( this ).addClass( 'mw-collapsible' );
240 // Return if it has been enabled already.
241 if ( $collapsible
.data( 'mw-made-collapsible' ) ) {
244 $collapsible
.data( 'mw-made-collapsible', true );
247 // Use custom text or default?
248 collapseText
= options
.collapseText
|| $collapsible
.attr( 'data-collapsetext' ) || mw
.msg( 'collapsible-collapse' );
249 expandText
= options
.expandText
|| $collapsible
.attr( 'data-expandtext' ) || mw
.msg( 'collapsible-expand' );
251 // Default click/keypress handler and toggle link to use when none is present
252 actionHandler = function ( e
, opts
) {
255 toggleText
: { collapseText
: collapseText
, expandText
: expandText
}
257 opts
= $.extend( defaultOpts
, options
, opts
);
258 togglingHandler( $( this ), $collapsible
, e
, opts
);
260 // Default toggle link. Only build it when needed to avoid jQuery memory leaks (event data).
261 buildDefaultToggleLink = function () {
262 return $( '<a href="#"></a>' )
263 .text( collapseText
)
264 .wrap( '<span class="mw-collapsible-toggle"></span>' )
266 .prepend( ' [' )
268 .on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler
);
271 // Default handler for clicking on premade toggles
272 premadeToggleHandler = function ( e
, opts
) {
273 var defaultOpts
= { toggleClasses
: true, linksPassthru
: true };
274 opts
= $.extend( defaultOpts
, options
, opts
);
275 togglingHandler( $( this ), $collapsible
, 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!' );
296 if ( $customTogglers
&& $customTogglers
.length
) {
297 actionHandler = function ( e
, opts
) {
298 var defaultOpts
= {};
299 opts
= $.extend( defaultOpts
, options
, opts
);
300 togglingHandler( $( this ), $collapsible
, e
, opts
);
303 $toggleLink
= $customTogglers
;
304 $toggleLink
.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler
);
307 // If this is not a custom case, do the default: wrap the
308 // contents and add the toggle link. Different elements are
309 // treated differently.
310 if ( $collapsible
.is( 'table' ) ) {
311 // The toggle-link will be in one the the cells (td or th) of the first row
312 $firstItem
= $collapsible
.find( 'tr:first th, tr:first td' );
313 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
315 // If theres no toggle link, add it to the last cell
316 if ( !$toggle
.length
) {
317 $toggleLink
= buildDefaultToggleLink().prependTo( $firstItem
.eq( -1 ) );
319 actionHandler
= premadeToggleHandler
;
320 $toggleLink
= $toggle
.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler
);
323 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
324 // The toggle-link will be in the first list-item
325 $firstItem
= $collapsible
.find( 'li:first' );
326 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
328 // If theres no toggle link, add it
329 if ( !$toggle
.length
) {
330 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
331 // to be "1". Except if the value-attribute is already used.
332 // If no value was set WebKit returns "", Mozilla returns '-1', others return 0, null or undefined.
333 firstval
= $firstItem
.attr( 'value' );
334 if ( firstval
=== undefined || !firstval
|| firstval
=== '-1' || firstval
=== -1 ) {
335 $firstItem
.attr( 'value', '1' );
337 $toggleLink
= buildDefaultToggleLink();
338 $toggleLink
.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent().prependTo( $collapsible
);
340 actionHandler
= premadeToggleHandler
;
341 $toggleLink
= $toggle
.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler
);
344 } else { // <div>, <p> etc.
346 // The toggle-link will be the first child of the element
347 $toggle
= $collapsible
.find( '> .mw-collapsible-toggle' );
349 // If a direct child .content-wrapper does not exists, create it
350 if ( !$collapsible
.find( '> .mw-collapsible-content' ).length
) {
351 $collapsible
.wrapInner( '<div class="mw-collapsible-content"></div>' );
354 // If theres no toggle link, add it
355 if ( !$toggle
.length
) {
356 $toggleLink
= buildDefaultToggleLink().prependTo( $collapsible
);
358 actionHandler
= premadeToggleHandler
;
359 $toggleLink
= $toggle
.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler
);
364 // Attributes for accessibility. This isn't necessary when the toggler is already
365 // an <a> or a <button> etc., but it doesn't hurt either, and it's consistent.
366 $toggleLink
.prop( 'tabIndex', 0 ).attr( 'role', 'button' );
369 if ( options
.collapsed
|| $collapsible
.hasClass( 'mw-collapsed' ) ) {
370 // Remove here so that the toggler goes in the right direction (the class is re-added)
371 $collapsible
.removeClass( 'mw-collapsed' );
372 // One toggler can hook to multiple elements, and one element can have
373 // multiple togglers. This is the sanest way to handle that.
374 actionHandler
.call( $toggleLink
.get( 0 ), null, { instantHide
: true } );
378 }( jQuery
, mediaWiki
) );