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