Merge "Add to/tweak mediawiki and jqueryMsg JS tests, fix comment"
[lhc/web/wiklou.git] / resources / jquery / jquery.makeCollapsible.js
1 /**
2 * jQuery makeCollapsible
3 *
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).
10 *
11 * @author Krinkle, 2011-2012
12 *
13 * Dual license:
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>
16 */
17 ( function ( $, mw ) {
18 var lpx = 'jquery.makeCollapsible> ';
19
20 /**
21 * @param {jQuery} $collapsible
22 * @param {string} action The action this function will take ('expand' or 'collapse').
23 * @param {jQuery|null} [optional] $defaultToggle
24 * @param {Object|undefined} options
25 */
26 function toggleElement( $collapsible, action, $defaultToggle, options ) {
27 var $collapsibleContent, $containers, hookCallback;
28 options = options || {};
29
30 // Validate parameters
31
32 // $collapsible must be an instance of jQuery
33 if ( !$collapsible.jquery ) {
34 return;
35 }
36 if ( action !== 'expand' && action !== 'collapse' ) {
37 // action must be string with 'expand' or 'collapse'
38 return;
39 }
40 if ( $defaultToggle === undefined ) {
41 $defaultToggle = null;
42 }
43 if ( $defaultToggle !== null && !$defaultToggle.jquery ) {
44 // is optional (may be undefined), but if defined it must be an instance of jQuery.
45 // If it's not, abort right away.
46 // After this $defaultToggle is either null or a valid jQuery instance.
47 return;
48 }
49
50 // Trigger a custom event to allow callers to hook to the collapsing/expanding,
51 // allowing the module to be testable, and making it possible to
52 // e.g. implement persistence via cookies
53 $collapsible.trigger( action === 'expand' ? 'beforeExpand.mw-collapsible' : 'beforeCollapse.mw-collapsible' );
54 hookCallback = function () {
55 $collapsible.trigger( action === 'expand' ? 'afterExpand.mw-collapsible' : 'afterCollapse.mw-collapsible' );
56 };
57
58 // Handle different kinds of elements
59
60 if ( !options.plainMode && $collapsible.is( 'table' ) ) {
61 // Tables
62 $containers = $collapsible.find( '> tbody > tr' );
63 if ( $defaultToggle ) {
64 // Exclude table row containing togglelink
65 $containers = $containers.not( $defaultToggle.closest( 'tr' ) );
66 }
67
68 if ( action === 'collapse' ) {
69 // Hide all table rows of this table
70 // Slide doesn't work with tables, but fade does as of jQuery 1.1.3
71 // http://stackoverflow.com/questions/467336#920480
72 if ( options.instantHide ) {
73 $containers.hide();
74 hookCallback();
75 } else {
76 $containers.stop( true, true ).fadeOut( hookCallback );
77 }
78 } else {
79 $containers.stop( true, true ).fadeIn( hookCallback );
80 }
81
82 } else if ( !options.plainMode && ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) ) {
83 // Lists
84 $containers = $collapsible.find( '> li' );
85 if ( $defaultToggle ) {
86 // Exclude list-item containing togglelink
87 $containers = $containers.not( $defaultToggle.parent() );
88 }
89
90 if ( action === 'collapse' ) {
91 if ( options.instantHide ) {
92 $containers.hide();
93 hookCallback();
94 } else {
95 $containers.stop( true, true ).slideUp( hookCallback );
96 }
97 } else {
98 $containers.stop( true, true ).slideDown( hookCallback );
99 }
100
101 } else {
102 // Everything else: <div>, <p> etc.
103 $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
104
105 // If a collapsible-content is defined, act on it
106 if ( !options.plainMode && $collapsibleContent.length ) {
107 if ( action === 'collapse' ) {
108 if ( options.instantHide ) {
109 $collapsibleContent.hide();
110 hookCallback();
111 } else {
112 $collapsibleContent.slideUp( hookCallback );
113 }
114 } else {
115 $collapsibleContent.slideDown( hookCallback );
116 }
117
118 // Otherwise assume this is a customcollapse with a remote toggle
119 // .. and there is no collapsible-content because the entire element should be toggled
120 } else {
121 if ( action === 'collapse' ) {
122 if ( options.instantHide ) {
123 $collapsible.hide();
124 hookCallback();
125 } else {
126 if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
127 $collapsible.fadeOut( hookCallback );
128 } else {
129 $collapsible.slideUp( hookCallback );
130 }
131 }
132 } else {
133 if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
134 $collapsible.fadeIn( hookCallback );
135 } else {
136 $collapsible.slideDown( hookCallback );
137 }
138 }
139 }
140 }
141 }
142
143 /**
144 * Handles clicking on the collapsible element toggle and other
145 * situations where a collapsible element is toggled (e.g. the initial
146 * toggle for collapsed ones).
147 *
148 * @param {jQuery} $toggle the clickable toggle itself
149 * @param {jQuery} $collapsible the collapsible element
150 * @param {jQuery.Event|null} e either the event or null if unavailable
151 * @param {Object|undefined} options
152 */
153 function togglingHandler( $toggle, $collapsible, event, options ) {
154 var wasCollapsed, $textContainer, collapseText, expandText;
155
156 if ( event ) {
157 // Don't fire if a link was clicked, if requested (for premade togglers by default)
158 if ( options.linksPassthru && $.nodeName( event.target, 'a' ) ) {
159 return true;
160 } else {
161 event.preventDefault();
162 event.stopPropagation();
163 }
164 }
165
166 wasCollapsed = $collapsible.hasClass( 'mw-collapsed' );
167
168 // Toggle the state of the collapsible element (that is, expand or collapse)
169 $collapsible.toggleClass( 'mw-collapsed', !wasCollapsed );
170
171 // Toggle the mw-collapsible-toggle classes, if requested (for default and premade togglers by default)
172 if ( options.toggleClasses ) {
173 $toggle
174 .toggleClass( 'mw-collapsible-toggle-collapsed', !wasCollapsed )
175 .toggleClass( 'mw-collapsible-toggle-expanded', wasCollapsed );
176 }
177
178 // Toggle the text ("Show"/"Hide"), if requested (for default togglers by default)
179 if ( options.toggleText ) {
180 collapseText = options.toggleText.collapseText;
181 expandText = options.toggleText.expandText;
182
183 $textContainer = $toggle.find( '> a' );
184 if ( !$textContainer.length ) {
185 $textContainer = $toggle;
186 }
187 $textContainer.text( wasCollapsed ? collapseText : expandText );
188 }
189
190 // And finally toggle the element state itself
191 toggleElement( $collapsible, wasCollapsed ? 'expand' : 'collapse', $toggle, options );
192 }
193
194 /**
195 * Toggles collapsible and togglelink class and updates text label.
196 *
197 * @param {jQuery} $that
198 * @param {jQuery.Event} e
199 * @param {Object|undefined} options
200 */
201 function toggleLinkDefault( $that, e, options ) {
202 var $collapsible = $that.closest( '.mw-collapsible' );
203 options = $.extend( { toggleClasses: true }, options );
204 togglingHandler( $that, $collapsible, e, options );
205 }
206
207 /**
208 * Toggles collapsible and togglelink class.
209 *
210 * @param {jQuery} $that
211 * @param {jQuery.Event} e
212 * @param {Object|undefined} options
213 */
214 function toggleLinkPremade( $that, e, options ) {
215 var $collapsible = $that.eq( 0 ).closest( '.mw-collapsible' );
216 options = $.extend( { toggleClasses: true }, options );
217 togglingHandler( $that, $collapsible, e, options );
218 }
219
220 /**
221 * Toggles customcollapsible.
222 *
223 * @param {jQuery} $that
224 * @param {jQuery.Event} e
225 * @param {Object|undefined} options
226 * @param {jQuery} $collapsible
227 */
228 function toggleLinkCustom( $that, e, options, $collapsible ) {
229 options = $.extend( { linksPassthru: true }, options );
230 togglingHandler( $that, $collapsible, e, options );
231 }
232
233 /**
234 * Make any element collapsible.
235 *
236 * Supported options:
237 * - collapseText: text to be used for the toggler when clicking it would
238 * collapse the element. Default: the 'data-collapsetext' attribute of
239 * the collapsible element or the content of 'collapsible-collapse'
240 * message.
241 * - expandText: text to be used for the toggler when clicking it would
242 * expand the element. Default: the 'data-expandtext' attribute of
243 * the collapsible element or the content of 'collapsible-expand'
244 * message.
245 * - collapsed: boolean, whether to collapse immediately. By default
246 * collapse only if the elements has the 'mw-collapsible' class.
247 * - $customTogglers: jQuerified list of elements to be used as togglers
248 * for this collapsible element. By default, if the collapsible element
249 * has an id attribute like 'mw-customcollapsible-XXX', elements with a
250 * *class* of 'mw-customtoggle-XXX' are made togglers for it.
251 * - plainMode: boolean, whether to use a "plain mode" when making the
252 * element collapsible - that is, hide entire tables and lists (instead
253 * of hiding only all rows but first of tables, and hiding each list
254 * item separately for lists) and don't wrap other elements in
255 * div.mw-collapsible-content. May only be used with custom togglers.
256 */
257 $.fn.makeCollapsible = function ( options ) {
258 return this.each(function () {
259 var $collapsible, collapsetext, expandtext, $toggle, $toggleLink, $firstItem, collapsibleId,
260 $customTogglers, firstval;
261
262 if ( options === undefined ) {
263 options = {};
264 }
265
266 // Ensure class "mw-collapsible" is present in case .makeCollapsible()
267 // is called on element(s) that don't have it yet.
268 $collapsible = $(this).addClass( 'mw-collapsible' );
269
270 // Return if it has been enabled already.
271 if ( $collapsible.data( 'mw-made-collapsible' ) ) {
272 return;
273 } else {
274 $collapsible.data( 'mw-made-collapsible', true );
275 }
276
277 // Use custom text or default?
278 collapsetext = options.collapseText || $collapsible.attr( 'data-collapsetext' ) || mw.msg( 'collapsible-collapse' );
279 expandtext = options.expandText || $collapsible.attr( 'data-expandtext' ) || mw.msg( 'collapsible-expand' );
280
281 // Create toggle link with a space around the brackets (&nbsp;[text]&nbsp;)
282 $toggleLink =
283 $( '<a href="#"></a>' )
284 .text( collapsetext )
285 .wrap( '<span class="mw-collapsible-toggle"></span>' )
286 .parent()
287 .prepend( '&nbsp;[' )
288 .append( ']&nbsp;' )
289 .on( 'click.mw-collapsible', function ( e, opts ) {
290 opts = $.extend( { toggleText: { collapseText: collapsetext, expandText: expandtext } }, options, opts );
291 toggleLinkDefault( $(this), e, opts );
292 } );
293
294 // Check if this element has a custom position for the toggle link
295 // (ie. outside the container or deeper inside the tree)
296 if ( options.$customTogglers ) {
297 $customTogglers = $( options.$customTogglers );
298 } else {
299 collapsibleId = $collapsible.attr( 'id' ) || '';
300 if ( collapsibleId.indexOf( 'mw-customcollapsible-' ) === 0 ) {
301 mw.log( lpx + 'Found custom collapsible: #' + collapsibleId );
302 $customTogglers = $( '.' + collapsibleId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
303
304 // Double check that there is actually a customtoggle link
305 if ( !$customTogglers.length ) {
306 mw.log( lpx + '#' + collapsibleId + ': Missing toggler!' );
307 }
308 }
309 }
310
311 // Bind the custom togglers
312 if ( $customTogglers && $customTogglers.length ) {
313 $customTogglers.on( 'click.mw-collapsible', function ( e, opts ) {
314 opts = $.extend( {}, options, opts );
315 toggleLinkCustom( $(this), e, opts, $collapsible );
316 } );
317
318 // Initial state
319 if ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) {
320 // Remove here so that the toggler goes in the right direction,
321 // It re-adds the class.
322 $collapsible.removeClass( 'mw-collapsed' );
323 toggleLinkCustom( $customTogglers, null, $.extend( { instantHide: true }, options ), $collapsible );
324 }
325
326 // If this is not a custom case, do the default:
327 // Wrap the contents and add the toggle link
328 } else {
329 // Elements are treated differently
330 if ( $collapsible.is( 'table' ) ) {
331 // The toggle-link will be in one the the cells (td or th) of the first row
332 $firstItem = $collapsible.find( 'tr:first th, tr:first td' );
333 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
334
335 // If theres no toggle link, add it to the last cell
336 if ( !$toggle.length ) {
337 $firstItem.eq(-1).prepend( $toggleLink );
338 } else {
339 $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
340 opts = $.extend( {}, options, opts );
341 toggleLinkPremade( $toggle, e, opts );
342 } );
343 }
344
345 } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
346 // The toggle-link will be in the first list-item
347 $firstItem = $collapsible.find( 'li:first' );
348 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
349
350 // If theres no toggle link, add it
351 if ( !$toggle.length ) {
352 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
353 // to be "1". Except if the value-attribute is already used.
354 // If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
355 firstval = $firstItem.attr( 'value' );
356 if ( firstval === undefined || !firstval || firstval === '-1' || firstval === -1 ) {
357 $firstItem.attr( 'value', '1' );
358 }
359 $collapsible.prepend( $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
360 } else {
361 $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
362 opts = $.extend( {}, options, opts );
363 toggleLinkPremade( $toggle, e, opts );
364 } );
365 }
366
367 } else { // <div>, <p> etc.
368
369 // The toggle-link will be the first child of the element
370 $toggle = $collapsible.find( '> .mw-collapsible-toggle' );
371
372 // If a direct child .content-wrapper does not exists, create it
373 if ( !$collapsible.find( '> .mw-collapsible-content' ).length ) {
374 $collapsible.wrapInner( '<div class="mw-collapsible-content"></div>' );
375 }
376
377 // If theres no toggle link, add it
378 if ( !$toggle.length ) {
379 $collapsible.prepend( $toggleLink );
380 } else {
381 $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
382 opts = $.extend( {}, options, opts );
383 toggleLinkPremade( $toggle, e, opts );
384 } );
385 }
386 }
387 }
388
389 // Initial state (only for those that are not custom,
390 // because the initial state of those has been taken care of already).
391 if (
392 ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) &&
393 ( !$customTogglers || !$customTogglers.length )
394 ) {
395 $collapsible.removeClass( 'mw-collapsed' );
396 // The collapsible element could have multiple togglers
397 // To toggle the initial state only click one of them (ie. the first one, eq(0) )
398 // Else it would go like: hide,show,hide,show for each toggle link.
399 // This is just like it would be in reality (only one toggle is clicked at a time).
400 $toggleLink.eq( 0 ).trigger( 'click', [ { instantHide: true } ] );
401 }
402 } );
403 };
404 }( jQuery, mediaWiki ) );