Treat premade toggle links as a custom instead of default case (ie. dont change the...
[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 $collapsible.find( '> .mw-collapsible-content' ).slideUp();
67 }
68
69 } else {
70
71 // Expand the element
72 if ( $collapsible.is( 'table' ) ) {
73 if ( $defaultToggle.jquery ) {
74 // Exclude tablerow containing togglelink
75 $collapsible.find( '>tbody>tr' ).not( $defaultToggle.parent().parent() ).stop(true, true).fadeIn();
76 } else {
77 $collapsible.find( '>tbody>tr' ).stop(true, true).fadeIn();
78 }
79
80 } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
81 if ( $defaultToggle.jquery ) {
82 // Exclude list-item containing togglelink
83 $collapsible.find( '> li' ).not( $defaultToggle.parent() ).stop( true, true ).slideDown();
84 } else {
85 $collapsible.find( '> li' ).stop( true, true ).slideDown();
86 }
87
88 } else { // <div>, <p> etc.
89 $collapsible.find( '> .mw-collapsible-content' ).slideDown();
90 }
91 }
92 },
93 // Toggles collapsible and togglelink class and updates text label
94 toggleLinkDefault = function( that, e ) {
95 var $that = $(that),
96 $collapsible = $that.closest( '.mw-collapsible.mw-made-collapsible' ).toggleClass( 'mw-collapsed' );
97 e.preventDefault();
98
99 // It's expanded right now
100 if ( !$that.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
101 // Change link to "Show"
102 $that.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
103 if ( $that.find( '> a' ).size() ) {
104 $that.find( '> a' ).text( expandtext );
105 } else {
106 $that.text( expandtext );
107 }
108 // Collapse element
109 toggleElement( $collapsible, 'collapse', $that );
110
111 // It's collapsed right now
112 } else {
113 // Change link to "Hide"
114 $that.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
115 if ( $that.find( '> a' ).size() ) {
116 $that.find( '> a' ).text( collapsetext );
117 } else {
118 $that.text( collapsetext );
119 }
120 // Expand element
121 toggleElement( $collapsible, 'expand', $that );
122 }
123 return;
124 },
125 // Toggles collapsible and togglelink class
126 toggleLinkPremade = function( that, e ) {
127 var $that = $(that),
128 $collapsible = $that.closest( '.mw-collapsible.mw-made-collapsible' ).toggleClass( 'mw-collapsed' );
129 e.preventDefault();
130
131 // It's expanded right now
132 if ( !$that.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
133 // Change toggle to collapsed
134 $that.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
135 // Collapse element
136 toggleElement( $collapsible, 'collapse', $that );
137
138 // It's collapsed right now
139 } else {
140 // Change toggle to expanded
141 $that.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
142 // Expand element
143 toggleElement( $collapsible, 'expand', $that );
144 }
145 return;
146 },
147 // Toggles customcollapsible
148 toggleLinkCustom = function( that, e ) {
149 var $that = $(that),
150 classes = that.className.split(' ');
151 e.preventDefault();
152 // Check each class to see if it belongs to a customcollapse
153 for ( i = 0; i < classes.length; i++ ) {
154 if ( classes[i].indexOf( 'mw-customtoggle-' ) === 0 ) {
155 var id = '#' + classes[i].replace( 'mw-customtoggle-', 'mw-customcollapsible-' ),
156 $collapsible = $( id ),
157 action = $collapsible.hasClass( 'mw-collapsed' ) ? 'expand' : 'collapse';
158
159 $collapsible.toggleClass( 'mw-collapsed' );
160 toggleElement( $collapsible, action, $that );
161 }
162 }
163 };
164
165 // Use custom text or default ?
166 if( !collapsetext || collapsetext === '' ){
167 collapsetext = mw.msg( 'collapsible-collapse', 'Collapse' );
168 }
169 if ( !expandtext || expandtext === '' ){
170 expandtext = mw.msg( 'collapsible-expand', 'Expand' );
171 }
172
173 // Create toggle link with a space around the brackets (&nbsp;[text]&nbsp;)
174 var $toggleLink = $( '<a href="#">' ).text( collapsetext ).wrap( '<span class="mw-collapsible-toggle">' ).parent().prepend( '&nbsp;[' ).append( ']&nbsp;' ).bind( 'click.mw-collapse', function(e){
175 toggleLinkDefault( this, e );
176 } );
177
178 // Return if it has been enabled already.
179 if ( $that.hasClass( 'mw-made-collapsible' ) ) {
180 return;
181 } else {
182 $that.addClass( 'mw-made-collapsible' );
183 }
184
185 // Check if this element has a custom position for the toggle link
186 // (ie. outside the container or deeper inside the tree)
187 // Then: Locate the custom toggle link(s) and bind them
188 if ( $that.attr( 'id' ).indexOf( 'mw-customcollapsible-' ) === 0 ) {
189
190 var thatId = $that.attr( 'id' ),
191 $customTogglers = $( '.' + thatId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
192 mw.log( 'Found custom collapsible: #' + thatId );
193
194 // Double check that there is actually a customtoggle link
195 if ( $customTogglers.size() ) {
196 $customTogglers.bind( 'click.mw-collapse', function( e ) {
197 toggleLinkCustom( this, e );
198 } );
199 } else {
200 mw.log( '#' + thatId + ': Missing toggler!' );
201 }
202
203 // To change initial state at the bottom of the script
204 // Set this variable to one of the togglers
205 var $toggleLink = $customTogglers.eq(0);
206
207 // If this is not a custom case, do the default:
208 // Wrap the contents add the toggle link
209 } else {
210
211 // Elements are treated differently
212 if ( $that.is( 'table' ) ) {
213 // The toggle-link will be in one the the cells (td or th) of the first row
214 var $firstRowCells = $( 'tr:first th, tr:first td', that ),
215 $toggle = $firstRowCells.find( '> .mw-collapsible-toggle' );
216
217 // If theres no toggle link, add it to the last cell
218 if ( !$toggle.size() ) {
219 $firstRowCells.eq(-1).prepend( $toggleLink );
220 } else {
221 $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ){
222 toggleLinkPremade( this, e );
223 } );
224 }
225
226 } else if ( $that.is( 'ul' ) || $that.is( 'ol' ) ) {
227 // The toggle-link will be in the first list-item
228 var $firstItem = $( 'li:first', $that),
229 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
230
231 // If theres no toggle link, add it
232 if ( !$toggle.size() ) {
233 // Make sure the numeral order doesn't get messed up, reset to 1 unless value-attribute is already used
234 // WebKit return '' if no value, Mozilla returns '-1' is no value
235 if ( $firstItem.attr( 'value' ) == '' || $firstItem.attr( 'value' ) == '-1' ) { // Will fail with ===
236 $firstItem.attr( 'value', '1' );
237 }
238 $that.prepend( $toggleLink.wrap( '<li class="mw-collapsible-toggle-li">' ).parent() );
239 } else {
240 $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ){
241 toggleLinkPremade( this, e );
242 } );
243 }
244
245 } else { // <div>, <p> etc.
246 // If a direct child .content-wrapper does not exists, create it
247 if ( !$that.find( '> .mw-collapsible-content' ).size() ) {
248 $that.wrapInner( '<div class="mw-collapsible-content">' );
249 }
250
251 // The toggle-link will be the first child of the element
252 var $toggle = $that.find( '> .mw-collapsible-toggle' );
253
254 // If theres no toggle link, add it
255 if ( !$toggle.size() ) {
256 $that.prepend( $toggleLink );
257 } else {
258 $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ){
259 toggleLinkPremade( this, e );
260 } );
261 }
262 }
263 }
264
265 // Initial state
266 if ( $that.hasClass( 'mw-collapsed' ) ) {
267 $that.removeClass( 'mw-collapsed' );
268 $toggleLink.click();
269 }
270 } );
271 };