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 class "mw-made-collapsible".
9 * Except for tables and lists, the inner content is wrapped in "mw-collapsible-content".
11 * @author Krinkle <krinklemail@gmail.com>
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>
19 $.fn.makeCollapsible = function() {
21 return this.each(function() {
22 var _fn = 'jquery.makeCollapsible> ';
24 // Define reused variables and functions
25 var $that = $(this).addClass( 'mw-collapsible' ), // case: $( '#myAJAXelement' ).makeCollapsible()
27 collapsetext = $(this).attr( 'data-collapsetext' ),
28 expandtext = $(this).attr( 'data-expandtext' ),
29 toggleElement = function( $collapsible, action, $defaultToggle, instantHide ) {
30 // Validate parameters
31 if ( !$collapsible.jquery ) { // $collapsible must be an instance of jQuery
34 if ( action != 'expand' && action != 'collapse' ) {
35 // action must be string with 'expand' or 'collapse'
38 if ( typeof $defaultToggle == 'undefined' ) {
39 $defaultToggle = null;
41 if ( $defaultToggle !== null && !($defaultToggle instanceof $) ) {
42 // is optional (may be undefined), but if defined it must be an instance of jQuery.
43 // If it's not, abort right away.
44 // After this $defaultToggle is either null or a valid jQuery instance.
48 var $containers = null;
50 if ( action == 'collapse' ) {
52 // Collapse the element
53 if ( $collapsible.is( 'table' ) ) {
54 // Hide all table rows of this table
55 // Slide doens't work with tables, but fade does as of jQuery 1.1.3
56 // http://stackoverflow.com/questions/467336#920480
57 $containers = $collapsible.find( '>tbody>tr' );
58 if ( $defaultToggle ) {
59 // Exclude tablerow containing togglelink
60 $containers.not( $defaultToggle.closest( 'tr' ) ).stop(true, true).fadeOut();
65 $containers.stop( true, true ).fadeOut();
69 } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
70 $containers = $collapsible.find( '> li' );
71 if ( $defaultToggle ) {
72 // Exclude list-item containing togglelink
73 $containers.not( $defaultToggle.parent() ).stop( true, true ).slideUp();
78 $containers.stop( true, true ).slideUp();
82 } else { // <div>, <p> etc.
83 var $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
85 // If a collapsible-content is defined, collapse it
86 if ( $collapsibleContent.length ) {
88 $collapsibleContent.hide();
90 $collapsibleContent.slideUp();
93 // Otherwise assume this is a customcollapse with a remote toggle
94 // .. and there is no collapsible-content because the entire element should be toggled
96 if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
97 $collapsible.fadeOut();
99 $collapsible.slideUp();
106 // Expand the element
107 if ( $collapsible.is( 'table' ) ) {
108 $containers = $collapsible.find( '>tbody>tr' );
109 if ( $defaultToggle ) {
110 // Exclude tablerow containing togglelink
111 $containers.not( $defaultToggle.parent().parent() ).stop(true, true).fadeIn();
113 $containers.stop(true, true).fadeIn();
116 } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
117 $containers = $collapsible.find( '> li' );
118 if ( $defaultToggle ) {
119 // Exclude list-item containing togglelink
120 $containers.not( $defaultToggle.parent() ).stop( true, true ).slideDown();
122 $containers.stop( true, true ).slideDown();
125 } else { // <div>, <p> etc.
126 var $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
128 // If a collapsible-content is defined, collapse it
129 if ( $collapsibleContent.length ) {
130 $collapsibleContent.slideDown();
132 // Otherwise assume this is a customcollapse with a remote toggle
133 // .. and there is no collapsible-content because the entire element should be toggled
135 if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
136 $collapsible.fadeIn();
138 $collapsible.slideDown();
144 // Toggles collapsible and togglelink class and updates text label
145 toggleLinkDefault = function( that, e ) {
147 $collapsible = $that.closest( '.mw-collapsible.mw-made-collapsible' ).toggleClass( 'mw-collapsed' );
151 // It's expanded right now
152 if ( !$that.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
153 // Change link to "Show"
154 $that.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
155 if ( $that.find( '> a' ).length ) {
156 $that.find( '> a' ).text( expandtext );
158 $that.text( expandtext );
161 toggleElement( $collapsible, 'collapse', $that );
163 // It's collapsed right now
165 // Change link to "Hide"
166 $that.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
167 if ( $that.find( '> a' ).length ) {
168 $that.find( '> a' ).text( collapsetext );
170 $that.text( collapsetext );
173 toggleElement( $collapsible, 'expand', $that );
177 // Toggles collapsible and togglelink class
178 toggleLinkPremade = function( $that, e ) {
179 var $collapsible = $that.eq(0).closest( '.mw-collapsible.mw-made-collapsible' ).toggleClass( 'mw-collapsed' );
180 if ( $(e.target).is('a') ) {
186 // It's expanded right now
187 if ( !$that.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
188 // Change toggle to collapsed
189 $that.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
191 toggleElement( $collapsible, 'collapse', $that );
193 // It's collapsed right now
195 // Change toggle to expanded
196 $that.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
198 toggleElement( $collapsible, 'expand', $that );
202 // Toggles customcollapsible
203 toggleLinkCustom = function( $that, e, $collapsible ) {
204 // For the initial state call of customtogglers there is no event passed
209 // Get current state and toggle to the opposite
210 var action = $collapsible.hasClass( 'mw-collapsed' ) ? 'expand' : 'collapse';
211 $collapsible.toggleClass( 'mw-collapsed' );
212 toggleElement( $collapsible, action, $that );
216 // Use custom text or default ?
217 if( !collapsetext ) {
218 collapsetext = mw.msg( 'collapsible-collapse' );
221 expandtext = mw.msg( 'collapsible-expand' );
224 // Create toggle link with a space around the brackets ( [text] )
226 $( '<a href="#"></a>' )
227 .text( collapsetext )
228 .wrap( '<span class="mw-collapsible-toggle"></span>' )
230 .prepend( ' [' )
232 .bind( 'click.mw-collapse', function(e) {
233 toggleLinkDefault( this, e );
236 // Return if it has been enabled already.
237 if ( $that.hasClass( 'mw-made-collapsible' ) ) {
240 $that.addClass( 'mw-made-collapsible' );
243 // Check if this element has a custom position for the toggle link
244 // (ie. outside the container or deeper inside the tree)
245 // Then: Locate the custom toggle link(s) and bind them
246 if ( ( $that.attr( 'id' ) || '' ).indexOf( 'mw-customcollapsible-' ) === 0 ) {
248 var thatId = $that.attr( 'id' ),
249 $customTogglers = $( '.' + thatId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
250 mw.log( _fn + 'Found custom collapsible: #' + thatId );
252 // Double check that there is actually a customtoggle link
253 if ( $customTogglers.length ) {
254 $customTogglers.bind( 'click.mw-collapse', function( e ) {
255 toggleLinkCustom( $(this), e, $that );
258 mw.log( _fn + '#' + thatId + ': Missing toggler!' );
262 if ( $that.hasClass( 'mw-collapsed' ) ) {
263 $that.removeClass( 'mw-collapsed' );
264 toggleLinkCustom( $customTogglers, null, $that );
267 // If this is not a custom case, do the default:
268 // Wrap the contents add the toggle link
271 // Elements are treated differently
272 if ( $that.is( 'table' ) ) {
273 // The toggle-link will be in one the the cells (td or th) of the first row
274 var $firstRowCells = $( 'tr:first th, tr:first td', that ),
275 $toggle = $firstRowCells.find( '> .mw-collapsible-toggle' );
277 // If theres no toggle link, add it to the last cell
278 if ( !$toggle.length ) {
279 $firstRowCells.eq(-1).prepend( $toggleLink );
281 $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ) {
282 toggleLinkPremade( $toggle, e );
286 } else if ( $that.is( 'ul' ) || $that.is( 'ol' ) ) {
287 // The toggle-link will be in the first list-item
288 var $firstItem = $( 'li:first', $that),
289 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
291 // If theres no toggle link, add it
292 if ( !$toggle.length ) {
293 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
294 // to be "1". Except if the value-attribute is already used.
295 // If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
296 var firstval = $firstItem.attr( 'value' );
297 if ( firstval === undefined || !firstval || firstval == '-1' ) {
298 $firstItem.attr( 'value', '1' );
300 $that.prepend( $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
302 $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ) {
303 toggleLinkPremade( $toggle, e );
307 } else { // <div>, <p> etc.
309 // The toggle-link will be the first child of the element
310 var $toggle = $that.find( '> .mw-collapsible-toggle' );
312 // If a direct child .content-wrapper does not exists, create it
313 if ( !$that.find( '> .mw-collapsible-content' ).length ) {
314 $that.wrapInner( '<div class="mw-collapsible-content"></div>' );
317 // If theres no toggle link, add it
318 if ( !$toggle.length ) {
319 $that.prepend( $toggleLink );
321 $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ) {
322 toggleLinkPremade( $toggle, e );
328 // Initial state (only for those that are not custom)
329 if ( $that.hasClass( 'mw-collapsed' ) && ( $that.attr( 'id' ) || '').indexOf( 'mw-customcollapsible-' ) !== 0 ) {
330 $that.removeClass( 'mw-collapsed' );
331 // The collapsible element could have multiple togglers
332 // To toggle the initial state only click one of them (ie. the first one, eq(0) )
333 // Else it would go like: hide,show,hide,show for each toggle link.
334 toggleElement( $that, 'collapse', $toggleLink.eq(0), /* instantHide = */ true );
335 $toggleLink.eq(0).click();
339 } )( jQuery, mediaWiki );