Merge "(bug 46751) Make Buryat (Russia) (буряад) (bxr) fallback to Russian"
[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, e, options ) {
154 var wasCollapsed, $textContainer, collapseText, expandText;
155
156 if ( options === undefined ) {
157 options = {};
158 }
159
160 if ( e ) {
161 // Don't fire if a link was clicked, if requested (for premade togglers by default)
162 if ( options.linksPassthru && $.nodeName( e.target, 'a' ) ) {
163 return;
164 } else {
165 e.preventDefault();
166 e.stopPropagation();
167 }
168 }
169
170 wasCollapsed = $collapsible.hasClass( 'mw-collapsed' );
171
172 // Toggle the state of the collapsible element (that is, expand or collapse)
173 $collapsible.toggleClass( 'mw-collapsed', !wasCollapsed );
174
175 // Toggle the mw-collapsible-toggle classes, if requested (for default and premade togglers by default)
176 if ( options.toggleClasses ) {
177 $toggle
178 .toggleClass( 'mw-collapsible-toggle-collapsed', !wasCollapsed )
179 .toggleClass( 'mw-collapsible-toggle-expanded', wasCollapsed );
180 }
181
182 // Toggle the text ("Show"/"Hide"), if requested (for default togglers by default)
183 if ( options.toggleText ) {
184 collapseText = options.toggleText.collapseText;
185 expandText = options.toggleText.expandText;
186
187 $textContainer = $toggle.find( '> a' );
188 if ( !$textContainer.length ) {
189 $textContainer = $toggle;
190 }
191 $textContainer.text( wasCollapsed ? collapseText : expandText );
192 }
193
194 // And finally toggle the element state itself
195 toggleElement( $collapsible, wasCollapsed ? 'expand' : 'collapse', $toggle, options );
196 }
197
198 /**
199 * Toggles collapsible and togglelink class and updates text label.
200 *
201 * @param {jQuery} $that
202 * @param {jQuery.Event} e
203 * @param {Object|undefined} options
204 */
205 function toggleLinkDefault( $that, e, options ) {
206 var $collapsible = $that.closest( '.mw-collapsible' );
207 options = $.extend( { toggleClasses: true }, options );
208 togglingHandler( $that, $collapsible, e, options );
209 }
210
211 /**
212 * Toggles collapsible and togglelink class.
213 *
214 * @param {jQuery} $that
215 * @param {jQuery.Event} e
216 * @param {Object|undefined} options
217 */
218 function toggleLinkPremade( $that, e, options ) {
219 var $collapsible = $that.eq( 0 ).closest( '.mw-collapsible' );
220 options = $.extend( { toggleClasses: true, linksPassthru: true }, options );
221 togglingHandler( $that, $collapsible, e, options );
222 }
223
224 /**
225 * Toggles customcollapsible.
226 *
227 * @param {jQuery} $that
228 * @param {jQuery.Event} e
229 * @param {Object|undefined} options
230 * @param {jQuery} $collapsible
231 */
232 function toggleLinkCustom( $that, e, options, $collapsible ) {
233 togglingHandler( $that, $collapsible, e, options );
234 }
235
236 /**
237 * Make any element collapsible.
238 *
239 * Supported options:
240 * - collapseText: text to be used for the toggler when clicking it would
241 * collapse the element. Default: the 'data-collapsetext' attribute of
242 * the collapsible element or the content of 'collapsible-collapse'
243 * message.
244 * - expandText: text to be used for the toggler when clicking it would
245 * expand the element. Default: the 'data-expandtext' attribute of
246 * the collapsible element or the content of 'collapsible-expand'
247 * message.
248 * - collapsed: boolean, whether to collapse immediately. By default
249 * collapse only if the elements has the 'mw-collapsible' class.
250 * - $customTogglers: jQuerified list of elements to be used as togglers
251 * for this collapsible element. By default, if the collapsible element
252 * has an id attribute like 'mw-customcollapsible-XXX', elements with a
253 * *class* of 'mw-customtoggle-XXX' are made togglers for it.
254 * - plainMode: boolean, whether to use a "plain mode" when making the
255 * element collapsible - that is, hide entire tables and lists (instead
256 * of hiding only all rows but first of tables, and hiding each list
257 * item separately for lists) and don't wrap other elements in
258 * div.mw-collapsible-content. May only be used with custom togglers.
259 */
260 $.fn.makeCollapsible = function ( options ) {
261 return this.each(function () {
262 var $collapsible, collapsetext, expandtext, $toggle, $toggleLink, $firstItem, collapsibleId,
263 $customTogglers, firstval;
264
265 if ( options === undefined ) {
266 options = {};
267 }
268
269 // Ensure class "mw-collapsible" is present in case .makeCollapsible()
270 // is called on element(s) that don't have it yet.
271 $collapsible = $(this).addClass( 'mw-collapsible' );
272
273 // Return if it has been enabled already.
274 if ( $collapsible.data( 'mw-made-collapsible' ) ) {
275 return;
276 } else {
277 $collapsible.data( 'mw-made-collapsible', true );
278 }
279
280 // Use custom text or default?
281 collapsetext = options.collapseText || $collapsible.attr( 'data-collapsetext' ) || mw.msg( 'collapsible-collapse' );
282 expandtext = options.expandText || $collapsible.attr( 'data-expandtext' ) || mw.msg( 'collapsible-expand' );
283
284 // Create toggle link with a space around the brackets (&nbsp;[text]&nbsp;)
285 $toggleLink =
286 $( '<a href="#"></a>' )
287 .text( collapsetext )
288 .wrap( '<span class="mw-collapsible-toggle"></span>' )
289 .parent()
290 .prepend( '&nbsp;[' )
291 .append( ']&nbsp;' )
292 .on( 'click.mw-collapsible', function ( e, opts ) {
293 opts = $.extend( { toggleText: { collapseText: collapsetext, expandText: expandtext } }, options, opts );
294 toggleLinkDefault( $(this), e, opts );
295 } );
296
297 // Check if this element has a custom position for the toggle link
298 // (ie. outside the container or deeper inside the tree)
299 if ( options.$customTogglers ) {
300 $customTogglers = $( options.$customTogglers );
301 } else {
302 collapsibleId = $collapsible.attr( 'id' ) || '';
303 if ( collapsibleId.indexOf( 'mw-customcollapsible-' ) === 0 ) {
304 mw.log( lpx + 'Found custom collapsible: #' + collapsibleId );
305 $customTogglers = $( '.' + collapsibleId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
306
307 // Double check that there is actually a customtoggle link
308 if ( !$customTogglers.length ) {
309 mw.log( lpx + '#' + collapsibleId + ': Missing toggler!' );
310 }
311 }
312 }
313
314 // Bind the custom togglers
315 if ( $customTogglers && $customTogglers.length ) {
316 $customTogglers.on( 'click.mw-collapsible', function ( e, opts ) {
317 opts = $.extend( {}, options, opts );
318 toggleLinkCustom( $(this), e, opts, $collapsible );
319 } );
320
321 // Initial state
322 if ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) {
323 // Remove here so that the toggler goes in the right direction,
324 // It re-adds the class.
325 $collapsible.removeClass( 'mw-collapsed' );
326 toggleLinkCustom( $customTogglers, null, $.extend( { instantHide: true }, options ), $collapsible );
327 }
328
329 // If this is not a custom case, do the default:
330 // Wrap the contents and add the toggle link
331 } else {
332 // Elements are treated differently
333 if ( $collapsible.is( 'table' ) ) {
334 // The toggle-link will be in one the the cells (td or th) of the first row
335 $firstItem = $collapsible.find( 'tr:first th, tr:first td' );
336 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
337
338 // If theres no toggle link, add it to the last cell
339 if ( !$toggle.length ) {
340 $firstItem.eq(-1).prepend( $toggleLink );
341 } else {
342 $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
343 opts = $.extend( {}, options, opts );
344 toggleLinkPremade( $toggle, e, opts );
345 } );
346 }
347
348 } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
349 // The toggle-link will be in the first list-item
350 $firstItem = $collapsible.find( 'li:first' );
351 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
352
353 // If theres no toggle link, add it
354 if ( !$toggle.length ) {
355 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
356 // to be "1". Except if the value-attribute is already used.
357 // If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
358 firstval = $firstItem.attr( 'value' );
359 if ( firstval === undefined || !firstval || firstval === '-1' || firstval === -1 ) {
360 $firstItem.attr( 'value', '1' );
361 }
362 $collapsible.prepend( $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
363 } else {
364 $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
365 opts = $.extend( {}, options, opts );
366 toggleLinkPremade( $toggle, e, opts );
367 } );
368 }
369
370 } else { // <div>, <p> etc.
371
372 // The toggle-link will be the first child of the element
373 $toggle = $collapsible.find( '> .mw-collapsible-toggle' );
374
375 // If a direct child .content-wrapper does not exists, create it
376 if ( !$collapsible.find( '> .mw-collapsible-content' ).length ) {
377 $collapsible.wrapInner( '<div class="mw-collapsible-content"></div>' );
378 }
379
380 // If theres no toggle link, add it
381 if ( !$toggle.length ) {
382 $collapsible.prepend( $toggleLink );
383 } else {
384 $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
385 opts = $.extend( {}, options, opts );
386 toggleLinkPremade( $toggle, e, opts );
387 } );
388 }
389 }
390 }
391
392 // Initial state (only for those that are not custom,
393 // because the initial state of those has been taken care of already).
394 if (
395 ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) &&
396 ( !$customTogglers || !$customTogglers.length )
397 ) {
398 $collapsible.removeClass( 'mw-collapsed' );
399 // The collapsible element could have multiple togglers
400 // To toggle the initial state only click one of them (ie. the first one, eq(0) )
401 // Else it would go like: hide,show,hide,show for each toggle link.
402 // This is just like it would be in reality (only one toggle is clicked at a time).
403 $toggleLink.eq( 0 ).trigger( 'click', [ { instantHide: true } ] );
404 }
405 } );
406 };
407 }( jQuery, mediaWiki ) );