Fixing issue with multiple premade toggles in a non-custom collapsible element.
[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 ) {
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.jquery ) {
39 // is optional, but if passed must be an instance of jQuery
40 return;
41 }
42
43 if ( action == 'collapse' ) {
44
45 // Collapse the element
46 if ( $collapsible.is( 'table' ) ) {
47 // Hide all table rows of this table
48 // Slide doens't work with tables, but fade does as of jQuery 1.1.3
49 // http://stackoverflow.com/questions/467336#920480
50
51 if ( $defaultToggle.jquery ) {
52 // Exclude tablerow containing togglelink
53 $collapsible.find( '>tbody>tr' ).not( $defaultToggle.parent().parent() ).stop(true, true).fadeOut();
54 } else {
55 $collapsible.find( '>tbody>tr' ).stop( true, true ).fadeOut();
56 }
57
58 } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
59 if ( $defaultToggle.jquery ) {
60 // Exclude list-item containing togglelink
61 $collapsible.find( '> li' ).not( $defaultToggle.parent() ).stop( true, true ).slideUp();
62 } else {
63 $collapsible.find( '> li' ).stop( true, true ).slideUp();
64 }
65
66 } else { // <div>, <p> etc.
67 var $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
68
69 // If a collapsible-content is defined, collapse it
70 if ( $collapsibleContent.size() ) {
71 $collapsibleContent.slideUp();
72
73 // Otherwise assume this is a customcollapse with a remote toggle
74 // .. and there is no collapsible-content because the entire element should be toggled
75 } else {
76 if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
77 $collapsible.fadeOut();
78 } else {
79 $collapsible.slideUp();
80 }
81 }
82 }
83
84 } else {
85
86 // Expand the element
87 if ( $collapsible.is( 'table' ) ) {
88 if ( $defaultToggle.jquery ) {
89 // Exclude tablerow containing togglelink
90 $collapsible.find( '>tbody>tr' ).not( $defaultToggle.parent().parent() ).stop(true, true).fadeIn();
91 } else {
92 $collapsible.find( '>tbody>tr' ).stop(true, true).fadeIn();
93 }
94
95 } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
96 if ( $defaultToggle.jquery ) {
97 // Exclude list-item containing togglelink
98 $collapsible.find( '> li' ).not( $defaultToggle.parent() ).stop( true, true ).slideDown();
99 } else {
100 $collapsible.find( '> li' ).stop( true, true ).slideDown();
101 }
102
103 } else { // <div>, <p> etc.
104 var $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
105
106 // If a collapsible-content is defined, collapse it
107 if ( $collapsibleContent.size() ) {
108 $collapsibleContent.slideDown();
109
110 // Otherwise assume this is a customcollapse with a remote toggle
111 // .. and there is no collapsible-content because the entire element should be toggled
112 } else {
113 if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
114 $collapsible.fadeIn();
115 } else {
116 $collapsible.slideDown();
117 }
118 }
119 }
120 }
121 },
122 // Toggles collapsible and togglelink class and updates text label
123 toggleLinkDefault = function( that, e ) {
124 var $that = $(that),
125 $collapsible = $that.closest( '.mw-collapsible.mw-made-collapsible' ).toggleClass( 'mw-collapsed' );
126 e.preventDefault();
127
128 // It's expanded right now
129 if ( !$that.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
130 // Change link to "Show"
131 $that.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
132 if ( $that.find( '> a' ).size() ) {
133 $that.find( '> a' ).text( expandtext );
134 } else {
135 $that.text( expandtext );
136 }
137 // Collapse element
138 toggleElement( $collapsible, 'collapse', $that );
139
140 // It's collapsed right now
141 } else {
142 // Change link to "Hide"
143 $that.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
144 if ( $that.find( '> a' ).size() ) {
145 $that.find( '> a' ).text( collapsetext );
146 } else {
147 $that.text( collapsetext );
148 }
149 // Expand element
150 toggleElement( $collapsible, 'expand', $that );
151 }
152 return;
153 },
154 // Toggles collapsible and togglelink class
155 toggleLinkPremade = function( $that, e ) {
156 var $collapsible = $that.eq(0).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( $toggle, 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( $toggle, 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( $toggle, 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 // The collapsible element could have multiple togglers
294 // To toggle the initial state only click one of them (ie. the first one, eq(0) )
295 // Else it would go like: hide,show,hide,show for each toggle link.
296 $toggleLink.eq(0).click();
297 }
298 } );
299 };
300 } )( jQuery, mediaWiki );