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