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' ),
39 * @param {jQuery} $collapsible
40 * @param {string} action The action this function will take ('expand' or 'collapse').
41 * @param {jQuery|null} [optional] $defaultToggle
42 * @param {Object|undefined} options
44 function toggleElement( $collapsible
, action
, $defaultToggle
, options
) {
45 var $collapsibleContent
, $containers
;
46 options
= options
|| {};
48 // Validate parameters
50 // $collapsible must be an instance of jQuery
51 if ( !$collapsible
.jquery
) {
54 if ( action
!== 'expand' && action
!== 'collapse' ) {
55 // action must be string with 'expand' or 'collapse'
58 if ( $defaultToggle
=== undefined ) {
59 $defaultToggle
= null;
61 if ( $defaultToggle
!== null && !$defaultToggle
.jquery
) {
62 // is optional (may be undefined), but if defined it must be an instance of jQuery.
63 // If it's not, abort right away.
64 // After this $defaultToggle is either null or a valid jQuery instance.
68 if ( action
=== 'collapse' ) {
70 // Collapse the element
71 if ( $collapsible
.is( 'table' ) ) {
72 // Hide all table rows of this table
73 // Slide doens't work with tables, but fade does as of jQuery 1.1.3
74 // http://stackoverflow.com/questions/467336#920480
75 $containers
= $collapsible
.find( '> tbody > tr' );
76 if ( $defaultToggle
) {
77 // Exclude tablerow containing togglelink
78 $containers
= $containers
.not( $defaultToggle
.closest( 'tr' ) );
81 if ( options
.instantHide
) {
84 $containers
.stop( true, true ).fadeOut();
87 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
88 $containers
= $collapsible
.find( '> li' );
89 if ( $defaultToggle
) {
90 // Exclude list-item containing togglelink
91 $containers
= $containers
.not( $defaultToggle
.parent() );
94 if ( options
.instantHide
) {
97 $containers
.stop( true, true ).slideUp();
102 $collapsibleContent
= $collapsible
.find( '> .mw-collapsible-content' );
104 // If a collapsible-content is defined, collapse it
105 if ( $collapsibleContent
.length
) {
106 if ( options
.instantHide
) {
107 $collapsibleContent
.hide();
109 $collapsibleContent
.slideUp();
112 // Otherwise assume this is a customcollapse with a remote toggle
113 // .. and there is no collapsible-content because the entire element should be toggled
115 if ( options
.instantHide
) {
118 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
119 $collapsible
.fadeOut();
121 $collapsible
.slideUp();
129 // Expand the element
130 if ( $collapsible
.is( 'table' ) ) {
131 $containers
= $collapsible
.find( '>tbody>tr' );
132 if ( $defaultToggle
) {
133 // Exclude tablerow containing togglelink
134 $containers
.not( $defaultToggle
.parent().parent() ).stop(true, true).fadeIn();
136 $containers
.stop( true, true ).fadeIn();
139 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
140 $containers
= $collapsible
.find( '> li' );
141 if ( $defaultToggle
) {
142 // Exclude list-item containing togglelink
143 $containers
.not( $defaultToggle
.parent() ).stop( true, true ).slideDown();
145 $containers
.stop( true, true ).slideDown();
150 $collapsibleContent
= $collapsible
.find( '> .mw-collapsible-content' );
152 // If a collapsible-content is defined, collapse it
153 if ( $collapsibleContent
.length
) {
154 $collapsibleContent
.slideDown();
156 // Otherwise assume this is a customcollapse with a remote toggle
157 // .. and there is no collapsible-content because the entire element should be toggled
159 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
160 $collapsible
.fadeIn();
162 $collapsible
.slideDown();
170 * Toggles collapsible and togglelink class and updates text label.
172 * @param {jQuery} $that
173 * @param {jQuery.Event} e
174 * @param {Object|undefined} options
176 function toggleLinkDefault( $that
, e
, options
) {
177 var $collapsible
= $that
.closest( '.mw-collapsible' ).toggleClass( 'mw-collapsed' );
181 // It's expanded right now
182 if ( !$that
.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
183 // Change link to "Show"
184 $that
.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
185 if ( $that
.find( '> a' ).length
) {
186 $that
.find( '> a' ).text( expandtext
);
188 $that
.text( expandtext
);
191 toggleElement( $collapsible
, 'collapse', $that
, options
);
193 // It's collapsed right now
195 // Change link to "Hide"
196 $that
.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
197 if ( $that
.find( '> a' ).length
) {
198 $that
.find( '> a' ).text( collapsetext
);
200 $that
.text( collapsetext
);
203 toggleElement( $collapsible
, 'expand', $that
, options
);
209 * Toggles collapsible and togglelink class.
211 * @param {jQuery} $that
212 * @param {jQuery.Event} e
213 * @param {Object|undefined} options
215 function toggleLinkPremade( $that
, e
, options
) {
216 var $collapsible
= $that
.eq( 0 ).closest( '.mw-collapsible' ).toggleClass( 'mw-collapsed' );
217 if ( $.nodeName( e
.target
, 'a' ) ) {
223 // It's expanded right now
224 if ( !$that
.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
225 // Change toggle to collapsed
226 $that
.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
228 toggleElement( $collapsible
, 'collapse', $that
, options
);
230 // It's collapsed right now
232 // Change toggle to expanded
233 $that
.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
235 toggleElement( $collapsible
, 'expand', $that
, options
);
241 * Toggles customcollapsible.
243 * @param {jQuery} $that
244 * @param {jQuery.Event} e
245 * @param {Object|undefined} options
246 * @param {jQuery} $collapsible
248 function toggleLinkCustom( $that
, e
, options
, $collapsible
) {
249 // For the initial state call of customtogglers there is no event passed
254 // Get current state and toggle to the opposite
255 var action
= $collapsible
.hasClass( 'mw-collapsed' ) ? 'expand' : 'collapse';
256 $collapsible
.toggleClass( 'mw-collapsed' );
257 toggleElement( $collapsible
, action
, $that
, options
);
260 // Return if it has been enabled already.
261 if ( $collapsible
.data( 'mw-made-collapsible' ) ) {
264 $collapsible
.data( 'mw-made-collapsible', true );
267 // Use custom text or default ?
268 if ( !collapsetext
) {
269 collapsetext
= mw
.msg( 'collapsible-collapse' );
272 expandtext
= mw
.msg( 'collapsible-expand' );
275 // Create toggle link with a space around the brackets ( [text] )
277 $( '<a href="#"></a>' )
278 .text( collapsetext
)
279 .wrap( '<span class="mw-collapsible-toggle"></span>' )
281 .prepend( ' [' )
283 .on( 'click.mw-collapse', function ( e
, options
) {
284 toggleLinkDefault( $(this), e
, options
);
287 // Check if this element has a custom position for the toggle link
288 // (ie. outside the container or deeper inside the tree)
289 // Then: Locate the custom toggle link(s) and bind them
290 if ( ( $collapsible
.attr( 'id' ) || '' ).indexOf( 'mw-customcollapsible-' ) === 0 ) {
292 collapsibleId
= $collapsible
.attr( 'id' );
293 $customTogglers
= $( '.' + collapsibleId
.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
294 mw
.log( lpx
+ 'Found custom collapsible: #' + collapsibleId
);
296 // Double check that there is actually a customtoggle link
297 if ( $customTogglers
.length
) {
298 $customTogglers
.on( 'click.mw-collapse', function ( e
, options
) {
299 toggleLinkCustom( $(this), e
, options
, $collapsible
);
302 mw
.log( lpx
+ '#' + collapsibleId
+ ': Missing toggler!' );
306 if ( $collapsible
.hasClass( 'mw-collapsed' ) ) {
307 // Remove here so that the toggler goes in the right direction,
308 // It re-adds the class.
309 $collapsible
.removeClass( 'mw-collapsed' );
310 toggleLinkCustom( $customTogglers
, null, { instantHide
: true }, $collapsible
);
313 // If this is not a custom case, do the default:
314 // Wrap the contents add the toggle link
317 // Elements are treated differently
318 if ( $collapsible
.is( 'table' ) ) {
319 // The toggle-link will be in one the the cells (td or th) of the first row
320 $firstItem
= $collapsible
.find( 'tr:first th, tr:first td' );
321 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
323 // If theres no toggle link, add it to the last cell
324 if ( !$toggle
.length
) {
325 $firstItem
.eq(-1).prepend( $toggleLink
);
327 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, options
) {
328 toggleLinkPremade( $toggle
, e
, options
);
332 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
333 // The toggle-link will be in the first list-item
334 $firstItem
= $collapsible
.find( 'li:first' );
335 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
337 // If theres no toggle link, add it
338 if ( !$toggle
.length
) {
339 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
340 // to be "1". Except if the value-attribute is already used.
341 // If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
342 firstval
= $firstItem
.attr( 'value' );
343 if ( firstval
=== undefined || !firstval
|| firstval
=== '-1' || firstval
=== -1 ) {
344 $firstItem
.attr( 'value', '1' );
346 $collapsible
.prepend( $toggleLink
.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
348 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, options
) {
349 toggleLinkPremade( $toggle
, e
, options
);
353 } else { // <div>, <p> etc.
355 // The toggle-link will be the first child of the element
356 $toggle
= $collapsible
.find( '> .mw-collapsible-toggle' );
358 // If a direct child .content-wrapper does not exists, create it
359 if ( !$collapsible
.find( '> .mw-collapsible-content' ).length
) {
360 $collapsible
.wrapInner( '<div class="mw-collapsible-content"></div>' );
363 // If theres no toggle link, add it
364 if ( !$toggle
.length
) {
365 $collapsible
.prepend( $toggleLink
);
367 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, options
) {
368 toggleLinkPremade( $toggle
, e
, options
);
374 // Initial state (only for those that are not custom,
375 // because the initial state of those has been taken care of already).
376 if ( $collapsible
.hasClass( 'mw-collapsed' ) && ( $collapsible
.attr( 'id' ) || '').indexOf( 'mw-customcollapsible-' ) !== 0 ) {
377 $collapsible
.removeClass( 'mw-collapsed' );
378 // The collapsible element could have multiple togglers
379 // To toggle the initial state only click one of them (ie. the first one, eq(0) )
380 // Else it would go like: hide,show,hide,show for each toggle link.
381 // This is just like it would be in reality (only one toggle is clicked at a time).
382 $toggleLink
.eq( 0 ).trigger( 'click', [ { instantHide
: true } ] );
387 }( jQuery
, mediaWiki
) );