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 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 // Don't fire if a link was clicked, if requested (for premade togglers by default)
164 if ( options.linksPassthru && $.nodeName( e.target, 'a' ) ) {
172 wasCollapsed = $collapsible.hasClass( 'mw-collapsed' );
174 // Toggle the state of the collapsible element (that is, expand or collapse)
175 $collapsible.toggleClass( 'mw-collapsed', !wasCollapsed );
177 // Toggle the mw-collapsible-toggle classes, if requested (for default and premade togglers by default)
178 if ( options.toggleClasses ) {
180 .toggleClass( 'mw-collapsible-toggle-collapsed', !wasCollapsed )
181 .toggleClass( 'mw-collapsible-toggle-expanded', wasCollapsed );
184 // Toggle the text ("Show"/"Hide"), if requested (for default togglers by default)
185 if ( options.toggleText ) {
186 collapseText = options.toggleText.collapseText;
187 expandText = options.toggleText.expandText;
189 $textContainer = $toggle.find( '> a' );
190 if ( !$textContainer.length ) {
191 $textContainer = $toggle;
193 $textContainer.text( wasCollapsed ? collapseText : expandText );
196 // And finally toggle the element state itself
197 toggleElement( $collapsible, wasCollapsed ? 'expand' : 'collapse', $toggle, options );
201 * Make any element collapsible.
204 * - collapseText: text to be used for the toggler when clicking it would
205 * collapse the element. Default: the 'data-collapsetext' attribute of
206 * the collapsible element or the content of 'collapsible-collapse'
208 * - expandText: text to be used for the toggler when clicking it would
209 * expand the element. Default: the 'data-expandtext' attribute of
210 * the collapsible element or the content of 'collapsible-expand'
212 * - collapsed: boolean, whether to collapse immediately. By default
213 * collapse only if the elements has the 'mw-collapsible' class.
214 * - $customTogglers: jQuerified list of elements to be used as togglers
215 * for this collapsible element. By default, if the collapsible element
216 * has an id attribute like 'mw-customcollapsible-XXX', elements with a
217 * *class* of 'mw-customtoggle-XXX' are made togglers for it.
218 * - plainMode: boolean, whether to use a "plain mode" when making the
219 * element collapsible - that is, hide entire tables and lists (instead
220 * of hiding only all rows but first of tables, and hiding each list
221 * item separately for lists) and don't wrap other elements in
222 * div.mw-collapsible-content. May only be used with custom togglers.
224 $.fn.makeCollapsible = function ( options ) {
225 if ( options === undefined ) {
229 return this.each( function () {
230 var $collapsible, collapseText, expandText, $toggle, clickHandler, $defaultToggleLink,
231 premadeToggleHandler, $toggleLink, $firstItem, collapsibleId, $customTogglers, firstval;
233 // Ensure class "mw-collapsible" is present in case .makeCollapsible()
234 // is called on element(s) that don't have it yet.
235 $collapsible = $( this ).addClass( 'mw-collapsible' );
237 // Return if it has been enabled already.
238 if ( $collapsible.data( 'mw-made-collapsible' ) ) {
241 $collapsible.data( 'mw-made-collapsible', true );
244 // Use custom text or default?
245 collapseText = options.collapseText || $collapsible.attr( 'data-collapsetext' ) || mw.msg( 'collapsible-collapse' );
246 expandText = options.expandText || $collapsible.attr( 'data-expandtext' ) || mw.msg( 'collapsible-expand' );
248 // Default click handler and toggle link to use when none is present
249 clickHandler = function ( e, opts ) {
252 toggleText: { collapseText: collapseText, expandText: expandText }
254 opts = $.extend( defaultOpts, options, opts );
255 togglingHandler( $( this ), $collapsible, e, opts );
258 $( '<a href="#"></a>' )
259 .text( collapseText )
260 .wrap( '<span class="mw-collapsible-toggle"></span>' )
262 .prepend( ' [' )
264 .on( 'click.mw-collapsible', clickHandler );
266 // Default handler for clicking on premade toggles
267 premadeToggleHandler = function ( e, opts ) {
268 var defaultOpts = { toggleClasses: true, linksPassthru: true };
269 opts = $.extend( defaultOpts, options, opts );
270 togglingHandler( $( this ), $collapsible, e, opts );
273 // Check if this element has a custom position for the toggle link
274 // (ie. outside the container or deeper inside the tree)
275 if ( options.$customTogglers ) {
276 $customTogglers = $( options.$customTogglers );
278 collapsibleId = $collapsible.attr( 'id' ) || '';
279 if ( collapsibleId.indexOf( 'mw-customcollapsible-' ) === 0 ) {
280 mw.log( lpx + 'Found custom collapsible: #' + collapsibleId );
281 $customTogglers = $( '.' + collapsibleId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
283 // Double check that there is actually a customtoggle link
284 if ( !$customTogglers.length ) {
285 mw.log( lpx + '#' + collapsibleId + ': Missing toggler!' );
291 if ( $customTogglers && $customTogglers.length ) {
292 clickHandler = function ( e, opts ) {
293 var defaultOpts = {};
294 opts = $.extend( defaultOpts, options, opts );
295 togglingHandler( $( this ), $collapsible, e, opts );
298 $toggleLink = $customTogglers;
299 $toggleLink.on( 'click.mw-collapsible', clickHandler );
302 // If this is not a custom case, do the default: wrap the
303 // contents and add the toggle link. Different elements are
304 // treated differently.
305 if ( $collapsible.is( 'table' ) ) {
306 // The toggle-link will be in one the the cells (td or th) of the first row
307 $firstItem = $collapsible.find( 'tr:first th, tr:first td' );
308 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
310 // If theres no toggle link, add it to the last cell
311 if ( !$toggle.length ) {
312 $toggleLink = $defaultToggleLink.prependTo( $firstItem.eq( -1 ) );
314 clickHandler = premadeToggleHandler;
315 $toggleLink = $toggle.on( 'click.mw-collapsible', clickHandler );
318 } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
319 // The toggle-link will be in the first list-item
320 $firstItem = $collapsible.find( 'li:first' );
321 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
323 // If theres no toggle link, add it
324 if ( !$toggle.length ) {
325 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
326 // to be "1". Except if the value-attribute is already used.
327 // If no value was set WebKit returns "", Mozilla returns '-1', others return 0, null or undefined.
328 firstval = $firstItem.attr( 'value' );
329 if ( firstval === undefined || !firstval || firstval === '-1' || firstval === -1 ) {
330 $firstItem.attr( 'value', '1' );
332 $toggleLink = $defaultToggleLink;
333 $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent().prependTo( $collapsible );
335 clickHandler = premadeToggleHandler;
336 $toggleLink = $toggle.on( 'click.mw-collapsible', clickHandler );
339 } else { // <div>, <p> etc.
341 // The toggle-link will be the first child of the element
342 $toggle = $collapsible.find( '> .mw-collapsible-toggle' );
344 // If a direct child .content-wrapper does not exists, create it
345 if ( !$collapsible.find( '> .mw-collapsible-content' ).length ) {
346 $collapsible.wrapInner( '<div class="mw-collapsible-content"></div>' );
349 // If theres no toggle link, add it
350 if ( !$toggle.length ) {
351 $toggleLink = $defaultToggleLink.prependTo( $collapsible );
353 clickHandler = premadeToggleHandler;
354 $toggleLink = $toggle.on( 'click.mw-collapsible', clickHandler );
360 if ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) {
361 // Remove here so that the toggler goes in the right direction (the class is re-added)
362 $collapsible.removeClass( 'mw-collapsed' );
363 // One toggler can hook to multiple elements, and one element can have
364 // multiple togglers. This is the sanest way to handle that.
365 clickHandler.call( $toggleLink.get( 0 ), null, { instantHide: true } );
369 }( jQuery, mediaWiki ) );