Merge "Move some more classes to comply with class per file"
[lhc/web/wiklou.git] / resources / src / jquery / jquery.makeCollapsible.js
1 /**
2 * jQuery makeCollapsible
3 *
4 * Dual licensed:
5 * - CC BY 3.0 <http://creativecommons.org/licenses/by/3.0>
6 * - GPL2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
7 *
8 * @class jQuery.plugin.makeCollapsible
9 */
10 ( function ( $, mw ) {
11
12 /**
13 * Handler for a click on a collapsible toggler.
14 *
15 * @private
16 * @param {jQuery} $collapsible
17 * @param {string} action The action this function will take ('expand' or 'collapse').
18 * @param {jQuery|null} [$defaultToggle]
19 * @param {Object|undefined} [options]
20 */
21 function toggleElement( $collapsible, action, $defaultToggle, options ) {
22 var $collapsibleContent, $containers, hookCallback;
23 options = options || {};
24
25 // Validate parameters
26
27 // $collapsible must be an instance of jQuery
28 if ( !$collapsible.jquery ) {
29 return;
30 }
31 if ( action !== 'expand' && action !== 'collapse' ) {
32 // action must be string with 'expand' or 'collapse'
33 return;
34 }
35 if ( $defaultToggle === undefined ) {
36 $defaultToggle = null;
37 }
38
39 // Trigger a custom event to allow callers to hook to the collapsing/expanding,
40 // allowing the module to be testable, and making it possible to
41 // e.g. implement persistence via cookies
42 $collapsible.trigger( action === 'expand' ? 'beforeExpand.mw-collapsible' : 'beforeCollapse.mw-collapsible' );
43 hookCallback = function () {
44 $collapsible.trigger( action === 'expand' ? 'afterExpand.mw-collapsible' : 'afterCollapse.mw-collapsible' );
45 };
46
47 // Handle different kinds of elements
48
49 if ( !options.plainMode && $collapsible.is( 'table' ) ) {
50 // Tables
51 // If there is a caption, hide all rows; otherwise, only hide body rows
52 if ( $collapsible.find( '> caption' ).length ) {
53 $containers = $collapsible.find( '> * > tr' );
54 } else {
55 $containers = $collapsible.find( '> tbody > tr' );
56 }
57 if ( $defaultToggle ) {
58 // Exclude table row containing togglelink
59 $containers = $containers.not( $defaultToggle.closest( 'tr' ) );
60 }
61
62 if ( action === 'collapse' ) {
63 // Hide all table rows of this table
64 // Slide doesn't work with tables, but fade does as of jQuery 1.1.3
65 // http://stackoverflow.com/questions/467336#920480
66 if ( options.instantHide ) {
67 $containers.hide();
68 hookCallback();
69 } else {
70 $containers.stop( true, true ).fadeOut().promise().done( hookCallback );
71 }
72 } else {
73 $containers.stop( true, true ).fadeIn().promise().done( hookCallback );
74 }
75
76 } else if ( !options.plainMode && ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) ) {
77 // Lists
78 $containers = $collapsible.find( '> li' );
79 if ( $defaultToggle ) {
80 // Exclude list-item containing togglelink
81 $containers = $containers.not( $defaultToggle.parent() );
82 }
83
84 if ( action === 'collapse' ) {
85 if ( options.instantHide ) {
86 $containers.hide();
87 hookCallback();
88 } else {
89 $containers.stop( true, true ).slideUp().promise().done( hookCallback );
90 }
91 } else {
92 $containers.stop( true, true ).slideDown().promise().done( hookCallback );
93 }
94
95 } else {
96 // Everything else: <div>, <p> etc.
97 $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
98
99 // If a collapsible-content is defined, act on it
100 if ( !options.plainMode && $collapsibleContent.length ) {
101 if ( action === 'collapse' ) {
102 if ( options.instantHide ) {
103 $collapsibleContent.hide();
104 hookCallback();
105 } else {
106 $collapsibleContent.slideUp().promise().done( hookCallback );
107 }
108 } else {
109 $collapsibleContent.slideDown().promise().done( hookCallback );
110 }
111
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
114 } else {
115 if ( action === 'collapse' ) {
116 if ( options.instantHide ) {
117 $collapsible.hide();
118 hookCallback();
119 } else {
120 if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
121 $collapsible.fadeOut().promise().done( hookCallback );
122 } else {
123 $collapsible.slideUp().promise().done( hookCallback );
124 }
125 }
126 } else {
127 if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
128 $collapsible.fadeIn().promise().done( hookCallback );
129 } else {
130 $collapsible.slideDown().promise().done( hookCallback );
131 }
132 }
133 }
134 }
135 }
136
137 /**
138 * Handle clicking/keypressing on the collapsible element toggle and other
139 * situations where a collapsible element is toggled (e.g. the initial
140 * toggle for collapsed ones).
141 *
142 * @private
143 * @param {jQuery} $toggle the clickable toggle itself
144 * @param {jQuery} $collapsible the collapsible element
145 * @param {jQuery.Event|null} e either the event or null if unavailable
146 * @param {Object|undefined} options
147 */
148 function togglingHandler( $toggle, $collapsible, e, options ) {
149 var wasCollapsed, $textContainer, collapseText, expandText;
150 options = options || {};
151
152 if ( e ) {
153 if (
154 e.type === 'click' &&
155 e.target.nodeName.toLowerCase() === 'a'
156 ) {
157 // Don't fire if a link was clicked (for premade togglers)
158 return;
159 } else if ( e.type === 'keypress' && e.which !== 13 && e.which !== 32 ) {
160 // Only handle keypresses on the "Enter" or "Space" keys
161 return;
162 } else {
163 e.preventDefault();
164 e.stopPropagation();
165 }
166 }
167
168 // This allows the element to be hidden on initial toggle without fiddling with the class
169 if ( options.wasCollapsed !== undefined ) {
170 wasCollapsed = options.wasCollapsed;
171 } else {
172 wasCollapsed = $collapsible.hasClass( 'mw-collapsed' );
173 }
174
175 // Toggle the state of the collapsible element (that is, expand or collapse)
176 $collapsible.toggleClass( 'mw-collapsed', !wasCollapsed );
177
178 // Toggle the mw-collapsible-toggle classes, if requested (for default and premade togglers by default)
179 if ( options.toggleClasses ) {
180 $toggle
181 .toggleClass( 'mw-collapsible-toggle-collapsed', !wasCollapsed )
182 .toggleClass( 'mw-collapsible-toggle-expanded', wasCollapsed );
183 }
184
185 // Toggle the text ("Show"/"Hide") within elements tagged with mw-collapsible-text
186 if ( options.toggleText ) {
187 collapseText = options.toggleText.collapseText;
188 expandText = options.toggleText.expandText;
189
190 $textContainer = $collapsible.find( '.mw-collapsible-text' );
191 if ( $textContainer.length ) {
192 $textContainer.text( wasCollapsed ? collapseText : expandText );
193 }
194 }
195
196 // And finally toggle the element state itself
197 toggleElement( $collapsible, wasCollapsed ? 'expand' : 'collapse', $toggle, options );
198 }
199
200 /**
201 * Enable collapsible-functionality on all elements in the collection.
202 *
203 * - Will prevent binding twice to the same element.
204 * - Initial state is expanded by default, this can be overridden by adding class
205 * "mw-collapsed" to the "mw-collapsible" element.
206 * - Elements made collapsible have jQuery data "mw-made-collapsible" set to true.
207 * - The inner content is wrapped in a "div.mw-collapsible-content" (except for tables and lists).
208 *
209 * @param {Object} [options]
210 * @param {string} [options.collapseText] Text used for the toggler, when clicking it would
211 * collapse the element. Default: the 'data-collapsetext' attribute of the
212 * collapsible element or the content of 'collapsible-collapse' message.
213 * @param {string} [options.expandText] Text used for the toggler, when clicking it would
214 * expand the element. Default: the 'data-expandtext' attribute of the
215 * collapsible element or the content of 'collapsible-expand' message.
216 * @param {boolean} [options.collapsed] Whether to collapse immediately. By default
217 * collapse only if the elements has the 'mw-collapsible' class.
218 * @param {jQuery} [options.$customTogglers] Elements to be used as togglers
219 * for this collapsible element. By default, if the collapsible element
220 * has an id attribute like 'mw-customcollapsible-XXX', elements with a
221 * *class* of 'mw-customtoggle-XXX' are made togglers for it.
222 * @param {boolean} [options.plainMode=false] Whether to use a "plain mode" when making the
223 * element collapsible - that is, hide entire tables and lists (instead
224 * of hiding only all rows but first of tables, and hiding each list
225 * item separately for lists) and don't wrap other elements in
226 * div.mw-collapsible-content. May only be used with custom togglers.
227 * @return {jQuery}
228 * @chainable
229 */
230 $.fn.makeCollapsible = function ( options ) {
231 options = options || {};
232
233 this.each( function () {
234 var $collapsible, collapseText, expandText, $caption, $toggle, actionHandler, buildDefaultToggleLink,
235 $toggleLink, $firstItem, collapsibleId, $customTogglers, firstval;
236
237 // Ensure class "mw-collapsible" is present in case .makeCollapsible()
238 // is called on element(s) that don't have it yet.
239 $collapsible = $( this ).addClass( 'mw-collapsible' );
240
241 // Return if it has been enabled already.
242 if ( $collapsible.data( 'mw-made-collapsible' ) ) {
243 return;
244 } else {
245 $collapsible.data( 'mw-made-collapsible', true );
246 }
247
248 // Use custom text or default?
249 collapseText = options.collapseText || $collapsible.attr( 'data-collapsetext' ) || mw.msg( 'collapsible-collapse' );
250 expandText = options.expandText || $collapsible.attr( 'data-expandtext' ) || mw.msg( 'collapsible-expand' );
251
252 // Default click/keypress handler and toggle link to use when none is present
253 actionHandler = function ( e, opts ) {
254 var defaultOpts = {
255 toggleClasses: true,
256 toggleText: { collapseText: collapseText, expandText: expandText }
257 };
258 opts = $.extend( defaultOpts, options, opts );
259 togglingHandler( $( this ), $collapsible, e, opts );
260 };
261
262 // Default toggle link. Only build it when needed to avoid jQuery memory leaks (event data).
263 buildDefaultToggleLink = function () {
264 return $( '<span class="mw-collapsible-text"></span>' )
265 .text( collapseText )
266 .wrap( '<span class="mw-collapsible-toggle"></span>' ).parent()
267 .attr( {
268 role: 'button',
269 tabindex: 0
270 } )
271 .prepend( '<span>[</span>' )
272 .append( '<span>]</span>' )
273 .on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler );
274 };
275
276 // Check if this element has a custom position for the toggle link
277 // (ie. outside the container or deeper inside the tree)
278 if ( options.$customTogglers ) {
279 $customTogglers = $( options.$customTogglers );
280 } else {
281 collapsibleId = $collapsible.attr( 'id' ) || '';
282 if ( collapsibleId.indexOf( 'mw-customcollapsible-' ) === 0 ) {
283 $customTogglers = $( '.' + collapsibleId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) )
284 .addClass( 'mw-customtoggle' );
285 }
286 }
287
288 // Add event handlers to custom togglers or create our own ones
289 if ( $customTogglers && $customTogglers.length ) {
290 actionHandler = function ( e, opts ) {
291 var defaultOpts = {};
292 opts = $.extend( defaultOpts, options, opts );
293 togglingHandler( $( this ), $collapsible, e, opts );
294 };
295
296 $toggleLink = $customTogglers
297 .on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
298 .prop( 'tabIndex', 0 );
299
300 } else {
301 // If this is not a custom case, do the default: wrap the
302 // contents and add the toggle link. Different elements are
303 // treated differently.
304
305 if ( $collapsible.is( 'table' ) ) {
306
307 // If the table has a caption, collapse to the caption
308 // as opposed to the first row
309 $caption = $collapsible.find( '> caption' );
310 if ( $caption.length ) {
311 $toggle = $caption.find( '> .mw-collapsible-toggle' );
312
313 // If there is no toggle link, add it to the end of the caption
314 if ( !$toggle.length ) {
315 $toggleLink = buildDefaultToggleLink().appendTo( $caption );
316 } else {
317 $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
318 .prop( 'tabIndex', 0 );
319 }
320 } else {
321 // The toggle-link will be in one of the cells (td or th) of the first row
322 $firstItem = $collapsible.find( 'tr:first th, tr:first td' );
323 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
324
325 // If theres no toggle link, add it to the last cell
326 if ( !$toggle.length ) {
327 $toggleLink = buildDefaultToggleLink().prependTo( $firstItem.eq( -1 ) );
328 } else {
329 $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
330 .prop( 'tabIndex', 0 );
331 }
332 }
333
334 } else if ( $collapsible.parent().is( 'li' ) &&
335 $collapsible.parent().children( '.mw-collapsible' ).length === 1 &&
336 $collapsible.find( '> .mw-collapsible-toggle' ).length === 0
337 ) {
338 // special case of one collapsible in <li> tag
339 $toggleLink = buildDefaultToggleLink();
340 $collapsible.before( $toggleLink );
341 } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
342 // The toggle-link will be in the first list-item
343 $firstItem = $collapsible.find( 'li:first' );
344 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
345
346 // If theres no toggle link, add it
347 if ( !$toggle.length ) {
348 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
349 // to be "1". Except if the value-attribute is already used.
350 // If no value was set WebKit returns "", Mozilla returns '-1', others return 0, null or undefined.
351 firstval = $firstItem.prop( 'value' );
352 if ( firstval === undefined || !firstval || firstval === '-1' || firstval === -1 ) {
353 $firstItem.prop( 'value', '1' );
354 }
355 $toggleLink = buildDefaultToggleLink();
356 $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent().prependTo( $collapsible );
357 } else {
358 $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
359 .prop( 'tabIndex', 0 );
360 }
361
362 } else { // <div>, <p> etc.
363
364 // The toggle-link will be the first child of the element
365 $toggle = $collapsible.find( '> .mw-collapsible-toggle' );
366
367 // If a direct child .content-wrapper does not exists, create it
368 if ( !$collapsible.find( '> .mw-collapsible-content' ).length ) {
369 $collapsible.wrapInner( '<div class="mw-collapsible-content"></div>' );
370 }
371
372 // If theres no toggle link, add it
373 if ( !$toggle.length ) {
374 $toggleLink = buildDefaultToggleLink().prependTo( $collapsible );
375 } else {
376 $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
377 .prop( 'tabIndex', 0 );
378 }
379 }
380 }
381
382 $( this ).data( 'mw-collapsible', {
383 collapse: function () {
384 actionHandler.call( $toggleLink.get( 0 ), null, { instantHide: true, wasCollapsed: false } );
385 },
386 expand: function () {
387 actionHandler.call( $toggleLink.get( 0 ), null, { instantHide: true, wasCollapsed: true } );
388 },
389 toggle: function () {
390 actionHandler.call( $toggleLink.get( 0 ), null, null );
391 }
392 } );
393
394 // Initial state
395 if ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) {
396 // One toggler can hook to multiple elements, and one element can have
397 // multiple togglers. This is the sanest way to handle that.
398 actionHandler.call( $toggleLink.get( 0 ), null, { instantHide: true, wasCollapsed: false } );
399 }
400
401 } );
402
403 /**
404 * Fired after collapsible content has been initialized
405 *
406 * This gives an option to modify the collapsible behavior.
407 *
408 * @event wikipage_collapsibleContent
409 * @member mw.hook
410 * @param {jQuery} $content All the elements that have been made collapsible
411 */
412 mw.hook( 'wikipage.collapsibleContent' ).fire( this );
413
414 return this;
415 };
416
417 /**
418 * @class jQuery
419 * @mixins jQuery.plugin.makeCollapsible
420 */
421
422 }( jQuery, mediaWiki ) );