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