Fix usage of the jQuery global in a few spots.
[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 $) ) {
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 if ( $(e.target).is('a') ) {
181 return true;
182 }
183 e.preventDefault();
184 e.stopPropagation();
185
186 // It's expanded right now
187 if ( !$that.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
188 // Change toggle to collapsed
189 $that.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
190 // Collapse element
191 toggleElement( $collapsible, 'collapse', $that );
192
193 // It's collapsed right now
194 } else {
195 // Change toggle to expanded
196 $that.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
197 // Expand element
198 toggleElement( $collapsible, 'expand', $that );
199 }
200 return;
201 },
202 // Toggles customcollapsible
203 toggleLinkCustom = function( $that, e, $collapsible ) {
204 // For the initial state call of customtogglers there is no event passed
205 if (e) {
206 e.preventDefault();
207 e.stopPropagation();
208 }
209 // Get current state and toggle to the opposite
210 var action = $collapsible.hasClass( 'mw-collapsed' ) ? 'expand' : 'collapse';
211 $collapsible.toggleClass( 'mw-collapsed' );
212 toggleElement( $collapsible, action, $that );
213
214 };
215
216 // Use custom text or default ?
217 if( !collapsetext ) {
218 collapsetext = mw.msg( 'collapsible-collapse' );
219 }
220 if ( !expandtext ) {
221 expandtext = mw.msg( 'collapsible-expand' );
222 }
223
224 // Create toggle link with a space around the brackets (&nbsp;[text]&nbsp;)
225 var $toggleLink =
226 $( '<a href="#"></a>' )
227 .text( collapsetext )
228 .wrap( '<span class="mw-collapsible-toggle"></span>' )
229 .parent()
230 .prepend( '&nbsp;[' )
231 .append( ']&nbsp;' )
232 .bind( 'click.mw-collapse', function(e) {
233 toggleLinkDefault( this, e );
234 } );
235
236 // Return if it has been enabled already.
237 if ( $that.hasClass( 'mw-made-collapsible' ) ) {
238 return;
239 } else {
240 $that.addClass( 'mw-made-collapsible' );
241 }
242
243 // Check if this element has a custom position for the toggle link
244 // (ie. outside the container or deeper inside the tree)
245 // Then: Locate the custom toggle link(s) and bind them
246 if ( ( $that.attr( 'id' ) || '' ).indexOf( 'mw-customcollapsible-' ) === 0 ) {
247
248 var thatId = $that.attr( 'id' ),
249 $customTogglers = $( '.' + thatId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
250 mw.log( _fn + 'Found custom collapsible: #' + thatId );
251
252 // Double check that there is actually a customtoggle link
253 if ( $customTogglers.length ) {
254 $customTogglers.bind( 'click.mw-collapse', function( e ) {
255 toggleLinkCustom( $(this), e, $that );
256 } );
257 } else {
258 mw.log( _fn + '#' + thatId + ': Missing toggler!' );
259 }
260
261 // Initial state
262 if ( $that.hasClass( 'mw-collapsed' ) ) {
263 $that.removeClass( 'mw-collapsed' );
264 toggleLinkCustom( $customTogglers, null, $that );
265 }
266
267 // If this is not a custom case, do the default:
268 // Wrap the contents add the toggle link
269 } else {
270
271 // Elements are treated differently
272 if ( $that.is( 'table' ) ) {
273 // The toggle-link will be in one the the cells (td or th) of the first row
274 var $firstRowCells = $( 'tr:first th, tr:first td', that ),
275 $toggle = $firstRowCells.find( '> .mw-collapsible-toggle' );
276
277 // If theres no toggle link, add it to the last cell
278 if ( !$toggle.length ) {
279 $firstRowCells.eq(-1).prepend( $toggleLink );
280 } else {
281 $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ) {
282 toggleLinkPremade( $toggle, e );
283 } );
284 }
285
286 } else if ( $that.is( 'ul' ) || $that.is( 'ol' ) ) {
287 // The toggle-link will be in the first list-item
288 var $firstItem = $( 'li:first', $that),
289 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
290
291 // If theres no toggle link, add it
292 if ( !$toggle.length ) {
293 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
294 // to be "1". Except if the value-attribute is already used.
295 // If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
296 var firstval = $firstItem.attr( 'value' );
297 if ( firstval === undefined || !firstval || firstval == '-1' ) {
298 $firstItem.attr( 'value', '1' );
299 }
300 $that.prepend( $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
301 } else {
302 $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ) {
303 toggleLinkPremade( $toggle, e );
304 } );
305 }
306
307 } else { // <div>, <p> etc.
308
309 // The toggle-link will be the first child of the element
310 var $toggle = $that.find( '> .mw-collapsible-toggle' );
311
312 // If a direct child .content-wrapper does not exists, create it
313 if ( !$that.find( '> .mw-collapsible-content' ).length ) {
314 $that.wrapInner( '<div class="mw-collapsible-content"></div>' );
315 }
316
317 // If theres no toggle link, add it
318 if ( !$toggle.length ) {
319 $that.prepend( $toggleLink );
320 } else {
321 $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ) {
322 toggleLinkPremade( $toggle, e );
323 } );
324 }
325 }
326 }
327
328 // Initial state (only for those that are not custom)
329 if ( $that.hasClass( 'mw-collapsed' ) && ( $that.attr( 'id' ) || '').indexOf( 'mw-customcollapsible-' ) !== 0 ) {
330 $that.removeClass( 'mw-collapsed' );
331 // The collapsible element could have multiple togglers
332 // To toggle the initial state only click one of them (ie. the first one, eq(0) )
333 // Else it would go like: hide,show,hide,show for each toggle link.
334 toggleElement( $that, 'collapse', $toggleLink.eq(0), /* instantHide = */ true );
335 $toggleLink.eq(0).click();
336 }
337 } );
338 };
339 } )( jQuery, mediaWiki );