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 // Handle different kinds of elements
70 if ( $collapsible
.is( 'table' ) ) {
72 $containers
= $collapsible
.find( '> tbody > tr' );
73 if ( $defaultToggle
) {
74 // Exclude table row containing togglelink
75 $containers
= $containers
.not( $defaultToggle
.closest( 'tr' ) );
78 if ( action
=== 'collapse' ) {
79 // Hide all table rows of this table
80 // Slide doesn't work with tables, but fade does as of jQuery 1.1.3
81 // http://stackoverflow.com/questions/467336#920480
82 if ( options
.instantHide
) {
85 $containers
.stop( true, true ).fadeOut();
88 $containers
.stop( true, true ).fadeIn();
91 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
93 $containers
= $collapsible
.find( '> li' );
94 if ( $defaultToggle
) {
95 // Exclude list-item containing togglelink
96 $containers
= $containers
.not( $defaultToggle
.parent() );
99 if ( action
=== 'collapse' ) {
100 if ( options
.instantHide
) {
103 $containers
.stop( true, true ).slideUp();
106 $containers
.stop( true, true ).slideDown();
110 // Everything else: <div>, <p> etc.
111 $collapsibleContent
= $collapsible
.find( '> .mw-collapsible-content' );
113 // If a collapsible-content is defined, act on it
114 if ( $collapsibleContent
.length
) {
115 if ( action
=== 'collapse' ) {
116 if ( options
.instantHide
) {
117 $collapsibleContent
.hide();
119 $collapsibleContent
.slideUp();
122 $collapsibleContent
.slideDown();
125 // Otherwise assume this is a customcollapse with a remote toggle
126 // .. and there is no collapsible-content because the entire element should be toggled
128 if ( action
=== 'collapse' ) {
129 if ( options
.instantHide
) {
132 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
133 $collapsible
.fadeOut();
135 $collapsible
.slideUp();
139 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
140 $collapsible
.fadeIn();
142 $collapsible
.slideDown();
150 * Toggles collapsible and togglelink class and updates text label.
152 * @param {jQuery} $that
153 * @param {jQuery.Event} e
154 * @param {Object|undefined} options
156 function toggleLinkDefault( $that
, e
, options
) {
157 var $collapsible
= $that
.closest( '.mw-collapsible' ).toggleClass( 'mw-collapsed' );
161 // It's expanded right now
162 if ( !$that
.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
163 // Change link to "Show"
164 $that
.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
165 if ( $that
.find( '> a' ).length
) {
166 $that
.find( '> a' ).text( expandtext
);
168 $that
.text( expandtext
);
171 toggleElement( $collapsible
, 'collapse', $that
, options
);
173 // It's collapsed right now
175 // Change link to "Hide"
176 $that
.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
177 if ( $that
.find( '> a' ).length
) {
178 $that
.find( '> a' ).text( collapsetext
);
180 $that
.text( collapsetext
);
183 toggleElement( $collapsible
, 'expand', $that
, options
);
189 * Toggles collapsible and togglelink class.
191 * @param {jQuery} $that
192 * @param {jQuery.Event} e
193 * @param {Object|undefined} options
195 function toggleLinkPremade( $that
, e
, options
) {
196 var $collapsible
= $that
.eq( 0 ).closest( '.mw-collapsible' ).toggleClass( 'mw-collapsed' );
197 if ( $.nodeName( e
.target
, 'a' ) ) {
203 // It's expanded right now
204 if ( !$that
.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
205 // Change toggle to collapsed
206 $that
.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
208 toggleElement( $collapsible
, 'collapse', $that
, options
);
210 // It's collapsed right now
212 // Change toggle to expanded
213 $that
.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
215 toggleElement( $collapsible
, 'expand', $that
, 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 // For the initial state call of customtogglers there is no event passed
234 // Get current state and toggle to the opposite
235 var action
= $collapsible
.hasClass( 'mw-collapsed' ) ? 'expand' : 'collapse';
236 $collapsible
.toggleClass( 'mw-collapsed' );
237 toggleElement( $collapsible
, action
, $that
, options
);
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 if ( !collapsetext
) {
249 collapsetext
= mw
.msg( 'collapsible-collapse' );
252 expandtext
= mw
.msg( 'collapsible-expand' );
255 // Create toggle link with a space around the brackets ( [text] )
257 $( '<a href="#"></a>' )
258 .text( collapsetext
)
259 .wrap( '<span class="mw-collapsible-toggle"></span>' )
261 .prepend( ' [' )
263 .on( 'click.mw-collapse', function ( e
, options
) {
264 toggleLinkDefault( $(this), e
, options
);
267 // Check if this element has a custom position for the toggle link
268 // (ie. outside the container or deeper inside the tree)
269 // Then: Locate the custom toggle link(s) and bind them
270 if ( ( $collapsible
.attr( 'id' ) || '' ).indexOf( 'mw-customcollapsible-' ) === 0 ) {
272 collapsibleId
= $collapsible
.attr( 'id' );
273 $customTogglers
= $( '.' + collapsibleId
.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
274 mw
.log( lpx
+ 'Found custom collapsible: #' + collapsibleId
);
276 // Double check that there is actually a customtoggle link
277 if ( $customTogglers
.length
) {
278 $customTogglers
.on( 'click.mw-collapse', function ( e
, options
) {
279 toggleLinkCustom( $(this), e
, options
, $collapsible
);
282 mw
.log( lpx
+ '#' + collapsibleId
+ ': Missing toggler!' );
286 if ( $collapsible
.hasClass( 'mw-collapsed' ) ) {
287 // Remove here so that the toggler goes in the right direction,
288 // It re-adds the class.
289 $collapsible
.removeClass( 'mw-collapsed' );
290 toggleLinkCustom( $customTogglers
, null, { instantHide
: true }, $collapsible
);
293 // If this is not a custom case, do the default:
294 // Wrap the contents add the toggle link
297 // Elements are treated differently
298 if ( $collapsible
.is( 'table' ) ) {
299 // The toggle-link will be in one the the cells (td or th) of the first row
300 $firstItem
= $collapsible
.find( 'tr:first th, tr:first td' );
301 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
303 // If theres no toggle link, add it to the last cell
304 if ( !$toggle
.length
) {
305 $firstItem
.eq(-1).prepend( $toggleLink
);
307 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, options
) {
308 toggleLinkPremade( $toggle
, e
, options
);
312 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
313 // The toggle-link will be in the first list-item
314 $firstItem
= $collapsible
.find( 'li:first' );
315 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
317 // If theres no toggle link, add it
318 if ( !$toggle
.length
) {
319 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
320 // to be "1". Except if the value-attribute is already used.
321 // If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
322 firstval
= $firstItem
.attr( 'value' );
323 if ( firstval
=== undefined || !firstval
|| firstval
=== '-1' || firstval
=== -1 ) {
324 $firstItem
.attr( 'value', '1' );
326 $collapsible
.prepend( $toggleLink
.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
328 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, options
) {
329 toggleLinkPremade( $toggle
, e
, options
);
333 } else { // <div>, <p> etc.
335 // The toggle-link will be the first child of the element
336 $toggle
= $collapsible
.find( '> .mw-collapsible-toggle' );
338 // If a direct child .content-wrapper does not exists, create it
339 if ( !$collapsible
.find( '> .mw-collapsible-content' ).length
) {
340 $collapsible
.wrapInner( '<div class="mw-collapsible-content"></div>' );
343 // If theres no toggle link, add it
344 if ( !$toggle
.length
) {
345 $collapsible
.prepend( $toggleLink
);
347 $toggleLink
= $toggle
.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e
, options
) {
348 toggleLinkPremade( $toggle
, e
, options
);
354 // Initial state (only for those that are not custom,
355 // because the initial state of those has been taken care of already).
356 if ( $collapsible
.hasClass( 'mw-collapsed' ) && ( $collapsible
.attr( 'id' ) || '').indexOf( 'mw-customcollapsible-' ) !== 0 ) {
357 $collapsible
.removeClass( 'mw-collapsed' );
358 // The collapsible element could have multiple togglers
359 // To toggle the initial state only click one of them (ie. the first one, eq(0) )
360 // Else it would go like: hide,show,hide,show for each toggle link.
361 // This is just like it would be in reality (only one toggle is clicked at a time).
362 $toggleLink
.eq( 0 ).trigger( 'click', [ { instantHide
: true } ] );
367 }( jQuery
, mediaWiki
) );