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