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