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
) {
19 $.fn
.makeCollapsible = function () {
21 return this.each(function () {
23 // Define reused variables and functions
24 var lpx
= 'jquery.makeCollapsible> ',
26 // Ensure class "mw-collapsible" is present in case .makeCollapsible()
27 // is called on element(s) that don't have it yet.
28 $collapsible
= $(collapsible
).addClass( 'mw-collapsible' ),
29 collapsetext
= $collapsible
.attr( 'data-collapsetext' ),
30 expandtext
= $collapsible
.attr( 'data-expandtext' ),
38 * @param {jQuery} $collapsible
39 * @param {string} action The action this function will take ('expand' or 'collapse').
40 * @param {jQuery|null} [optional] $defaultToggle
41 * @param {Object|undefined} options
43 toggleElement = function ( $collapsible
, action
, $defaultToggle
, options
) {
44 var $collapsibleContent
, $containers
;
45 options
= options
|| {};
47 // Validate parameters
49 // $collapsible must be an instance of jQuery
50 if ( !$collapsible
.jquery
) {
53 if ( action
!== 'expand' && action
!== 'collapse' ) {
54 // action must be string with 'expand' or 'collapse'
57 if ( $defaultToggle
=== undefined ) {
58 $defaultToggle
= null;
60 if ( $defaultToggle
!== null && !$defaultToggle
.jquery
) {
61 // is optional (may be undefined), but if defined it must be an instance of jQuery.
62 // If it's not, abort right away.
63 // After this $defaultToggle is either null or a valid jQuery instance.
67 if ( action
=== 'collapse' ) {
69 // Collapse the element
70 if ( $collapsible
.is( 'table' ) ) {
71 // Hide all table rows of this table
72 // Slide doens't work with tables, but fade does as of jQuery 1.1.3
73 // http://stackoverflow.com/questions/467336#920480
74 $containers
= $collapsible
.find( '> tbody > tr' );
75 if ( $defaultToggle
) {
76 // Exclude tablerow containing togglelink
77 $containers
.not( $defaultToggle
.closest( 'tr' ) ).stop(true, true).fadeOut();
79 if ( options
.instantHide
) {
82 $containers
.stop( true, true ).fadeOut();
86 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
87 $containers
= $collapsible
.find( '> li' );
88 if ( $defaultToggle
) {
89 // Exclude list-item containing togglelink
90 $containers
.not( $defaultToggle
.parent() ).stop( true, true ).slideUp();
92 if ( options
.instantHide
) {
95 $containers
.stop( true, true ).slideUp();
101 $collapsibleContent
= $collapsible
.find( '> .mw-collapsible-content' );
103 // If a collapsible-content is defined, collapse it
104 if ( $collapsibleContent
.length
) {
105 if ( options
.instantHide
) {
106 $collapsibleContent
.hide();
108 $collapsibleContent
.slideUp();
111 // Otherwise assume this is a customcollapse with a remote toggle
112 // .. and there is no collapsible-content because the entire element should be toggled
114 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
115 $collapsible
.fadeOut();
117 $collapsible
.slideUp();
124 // Expand the element
125 if ( $collapsible
.is( 'table' ) ) {
126 $containers
= $collapsible
.find( '>tbody>tr' );
127 if ( $defaultToggle
) {
128 // Exclude tablerow containing togglelink
129 $containers
.not( $defaultToggle
.parent().parent() ).stop(true, true).fadeIn();
131 $containers
.stop( true, true ).fadeIn();
134 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
135 $containers
= $collapsible
.find( '> li' );
136 if ( $defaultToggle
) {
137 // Exclude list-item containing togglelink
138 $containers
.not( $defaultToggle
.parent() ).stop( true, true ).slideDown();
140 $containers
.stop( true, true ).slideDown();
145 $collapsibleContent
= $collapsible
.find( '> .mw-collapsible-content' );
147 // If a collapsible-content is defined, collapse it
148 if ( $collapsibleContent
.length
) {
149 $collapsibleContent
.slideDown();
151 // Otherwise assume this is a customcollapse with a remote toggle
152 // .. and there is no collapsible-content because the entire element should be toggled
154 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
155 $collapsible
.fadeIn();
157 $collapsible
.slideDown();
164 * Toggles collapsible and togglelink class and updates text label.
166 * @param {jQuery} $that
167 * @param {jQuery.Event} e
168 * @param {Object|undefined} options
170 toggleLinkDefault = function ( $that
, e
, options
) {
171 var $collapsible
= $that
.closest( '.mw-collapsible' ).toggleClass( 'mw-collapsed' );
175 // It's expanded right now
176 if ( !$that
.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
177 // Change link to "Show"
178 $that
.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
179 if ( $that
.find( '> a' ).length
) {
180 $that
.find( '> a' ).text( expandtext
);
182 $that
.text( expandtext
);
185 toggleElement( $collapsible
, 'collapse', $that
, options
);
187 // It's collapsed right now
189 // Change link to "Hide"
190 $that
.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
191 if ( $that
.find( '> a' ).length
) {
192 $that
.find( '> a' ).text( collapsetext
);
194 $that
.text( collapsetext
);
197 toggleElement( $collapsible
, 'expand', $that
, options
);
202 * Toggles collapsible and togglelink class.
204 * @param {jQuery} $that
205 * @param {jQuery.Event} e
206 * @param {Object|undefined} options
208 toggleLinkPremade = function ( $that
, e
, options
) {
209 var $collapsible
= $that
.eq( 0 ).closest( '.mw-collapsible' ).toggleClass( 'mw-collapsed' );
210 if ( $.nodeName( e
.target
, 'a' ) ) {
216 // It's expanded right now
217 if ( !$that
.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
218 // Change toggle to collapsed
219 $that
.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
221 toggleElement( $collapsible
, 'collapse', $that
, options
);
223 // It's collapsed right now
225 // Change toggle to expanded
226 $that
.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
228 toggleElement( $collapsible
, 'expand', $that
, options
);
233 * Toggles customcollapsible.
235 * @param {jQuery} $that
236 * @param {jQuery.Event} e
237 * @param {Object|undefined} options
238 * @param {jQuery} $collapsible
240 toggleLinkCustom = function ( $that
, e
, options
, $collapsible
) {
241 // For the initial state call of customtogglers there is no event passed
246 // Get current state and toggle to the opposite
247 var action
= $collapsible
.hasClass( 'mw-collapsed' ) ? 'expand' : 'collapse';
248 $collapsible
.toggleClass( 'mw-collapsed' );
249 toggleElement( $collapsible
, action
, $that
, options
);
253 // Return if it has been enabled already.
254 if ( $collapsible
.data( 'mw-made-collapsible' ) ) {
257 $collapsible
.data( 'mw-made-collapsible', true );
260 // Use custom text or default ?
261 if ( !collapsetext
) {
262 collapsetext
= mw
.msg( 'collapsible-collapse' );
265 expandtext
= mw
.msg( 'collapsible-expand' );
268 // Create toggle link with a space around the brackets ( [text] )
270 $( '<a href="#"></a>' )
271 .text( collapsetext
)
272 .wrap( '<span class="mw-collapsible-toggle"></span>' )
274 .prepend( ' [' )
276 .on( 'click.mw-collapse', function ( e
, options
) {
277 toggleLinkDefault( $(this), e
, options
);
280 // Check if this element has a custom position for the toggle link
281 // (ie. outside the container or deeper inside the tree)
282 // Then: Locate the custom toggle link(s) and bind them
283 if ( ( $collapsible
.attr( 'id' ) || '' ).indexOf( 'mw-customcollapsible-' ) === 0 ) {
285 collapsibleId
= $collapsible
.attr( 'id' );
286 $customTogglers
= $( '.' + collapsibleId
.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
287 mw
.log( lpx
+ 'Found custom collapsible: #' + collapsibleId
);
289 // Double check that there is actually a customtoggle link
290 if ( $customTogglers
.length
) {
291 $customTogglers
.on( 'click.mw-collapse', function ( e
, options
) {
292 toggleLinkCustom( $(this), e
, options
, $collapsible
);
295 mw
.log( lpx
+ '#' + collapsibleId
+ ': Missing toggler!' );
299 if ( $collapsible
.hasClass( 'mw-collapsed' ) ) {
300 // Remove here so that the toggler goes in the right direction,
301 // It re-adds the class.
302 $collapsible
.removeClass( 'mw-collapsed' );
303 toggleLinkCustom( $customTogglers
, null, { instantHide
: true }, $collapsible
);
306 // If this is not a custom case, do the default:
307 // Wrap the contents add the toggle link
310 // Elements are treated differently
311 if ( $collapsible
.is( 'table' ) ) {
312 // The toggle-link will be in one the the cells (td or th) of the first row
313 $firstItem
= $collapsible
.find( 'tr:first th, tr:first td' );
314 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
316 // If theres no toggle link, add it to the last cell
317 if ( !$toggle
.length
) {
318 $firstItem
.eq(-1).prepend( $toggleLink
);
320 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, options
) {
321 toggleLinkPremade( $toggle
, e
, options
);
325 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
326 // The toggle-link will be in the first list-item
327 $firstItem
= $collapsible
.find( 'li:first' );
328 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
330 // If theres no toggle link, add it
331 if ( !$toggle
.length
) {
332 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
333 // to be "1". Except if the value-attribute is already used.
334 // If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
335 firstval
= $firstItem
.attr( 'value' );
336 if ( firstval
=== undefined || !firstval
|| firstval
=== '-1' || firstval
=== -1 ) {
337 $firstItem
.attr( 'value', '1' );
339 $collapsible
.prepend( $toggleLink
.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
341 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, options
) {
342 toggleLinkPremade( $toggle
, e
, options
);
346 } else { // <div>, <p> etc.
348 // The toggle-link will be the first child of the element
349 $toggle
= $collapsible
.find( '> .mw-collapsible-toggle' );
351 // If a direct child .content-wrapper does not exists, create it
352 if ( !$collapsible
.find( '> .mw-collapsible-content' ).length
) {
353 $collapsible
.wrapInner( '<div class="mw-collapsible-content"></div>' );
356 // If theres no toggle link, add it
357 if ( !$toggle
.length
) {
358 $collapsible
.prepend( $toggleLink
);
360 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, options
) {
361 toggleLinkPremade( $toggle
, e
, options
);
367 // Initial state (only for those that are not custom,
368 // because the initial state of those has been taken care of already).
369 if ( $collapsible
.hasClass( 'mw-collapsed' ) && ( $collapsible
.attr( 'id' ) || '').indexOf( 'mw-customcollapsible-' ) !== 0 ) {
370 $collapsible
.removeClass( 'mw-collapsed' );
371 // The collapsible element could have multiple togglers
372 // To toggle the initial state only click one of them (ie. the first one, eq(0) )
373 // Else it would go like: hide,show,hide,show for each toggle link.
374 // This is just like it would be in reality (only one toggle is clicked at a time).
375 $toggleLink
.eq( 0 ).trigger( 'click', [ { instantHide
: true } ] );
380 }( jQuery
, mediaWiki
) );