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