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