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