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