2 * jQuery makeCollapsible
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".
11 * @author Krinkle <krinklemail@gmail.com>
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>
19 $.fn
.makeCollapsible = function() {
21 return this.each(function() {
22 mw
.config
.set( 'mw.log.prefix', 'jquery.makeCollapsible' );
24 // Define reused variables and functions
25 var $that
= $(this).addClass( 'mw-collapsible' ), // case: $( '#myAJAXelement' ).makeCollapsible()
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
34 if ( action
!= 'expand' && action
!= 'collapse' ) {
35 // action must be string with 'expand' or 'collapse'
38 if ( typeof $defaultToggle
!== 'undefined' && !$defaultToggle
.jquery
) {
39 // is optional, but if passed must be an instance of jQuery
43 if ( action
== 'collapse' ) {
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
51 if ( $defaultToggle
.jquery
) {
52 // Exclude tablerow containing togglelink
53 $collapsible
.find( '>tbody>tr' ).not( $defaultToggle
.parent().parent() ).stop(true, true).fadeOut();
55 $collapsible
.find( '>tbody>tr' ).stop( true, true ).fadeOut();
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();
63 $collapsible
.find( '> li' ).stop( true, true ).slideUp();
66 } else { // <div>, <p> etc.
67 var $collapsibleContent
= $collapsible
.find( '> .mw-collapsible-content' );
69 // If a collapsible-content is defined, collapse it
70 if ( $collapsibleContent
.size() ) {
71 $collapsibleContent
.slideUp();
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
76 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
77 $collapsible
.fadeOut();
79 $collapsible
.slideUp();
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();
92 $collapsible
.find( '>tbody>tr' ).stop(true, true).fadeIn();
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();
100 $collapsible
.find( '> li' ).stop( true, true ).slideDown();
103 } else { // <div>, <p> etc.
104 var $collapsibleContent
= $collapsible
.find( '> .mw-collapsible-content' );
106 // If a collapsible-content is defined, collapse it
107 if ( $collapsibleContent
.size() ) {
108 $collapsibleContent
.slideDown();
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
113 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
114 $collapsible
.fadeIn();
116 $collapsible
.slideDown();
122 // Toggles collapsible and togglelink class and updates text label
123 toggleLinkDefault = function( that
, e
) {
125 $collapsible
= $that
.closest( '.mw-collapsible.mw-made-collapsible' ).toggleClass( 'mw-collapsed' );
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
);
135 $that
.text( expandtext
);
138 toggleElement( $collapsible
, 'collapse', $that
);
140 // It's collapsed right now
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
);
147 $that
.text( collapsetext
);
150 toggleElement( $collapsible
, 'expand', $that
);
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' );
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' );
164 toggleElement( $collapsible
, 'collapse', $that
);
166 // It's collapsed right now
168 // Change toggle to expanded
169 $that
.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
171 toggleElement( $collapsible
, 'expand', $that
);
175 // Toggles customcollapsible
176 toggleLinkCustom = function( $that
, e
, $collapsible
) {
177 // For the initial state call of customtogglers there is no event passed
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
)
188 // Use custom text or default ?
189 if( !collapsetext
|| collapsetext
=== '' ){
190 collapsetext
= mw
.msg( 'collapsible-collapse', 'Collapse' );
192 if ( !expandtext
|| expandtext
=== '' ){
193 expandtext
= mw
.msg( 'collapsible-expand', 'Expand' );
196 // Create toggle link with a space around the brackets ( [text] )
197 var $toggleLink
= $( '<a href="#">' ).text( collapsetext
).wrap( '<span class="mw-collapsible-toggle">' ).parent().prepend( ' [' ).append( '] ' ).bind( 'click.mw-collapse', function(e
){
198 toggleLinkDefault( this, e
);
201 // Return if it has been enabled already.
202 if ( $that
.hasClass( 'mw-made-collapsible' ) ) {
205 $that
.addClass( 'mw-made-collapsible' );
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 ) {
213 var thatId
= $that
.attr( 'id' ),
214 $customTogglers
= $( '.' + thatId
.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
215 mw
.log( 'Found custom collapsible: #' + thatId
);
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
);
223 mw
.log( '#' + thatId
+ ': Missing toggler!' );
227 if ( $that
.hasClass( 'mw-collapsed' ) ) {
228 $that
.removeClass( 'mw-collapsed' );
229 toggleLinkCustom( $customTogglers
, null, $that
)
232 // If this is not a custom case, do the default:
233 // Wrap the contents add the toggle link
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' );
242 // If theres no toggle link, add it to the last cell
243 if ( !$toggle
.size() ) {
244 $firstRowCells
.eq(-1).prepend( $toggleLink
);
246 $toggleLink
= $toggle
.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e
){
247 toggleLinkPremade( $toggle
, e
);
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' );
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' );
263 $that
.prepend( $toggleLink
.wrap( '<li class="mw-collapsible-toggle-li">' ).parent() );
265 $toggleLink
= $toggle
.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e
){
266 toggleLinkPremade( $toggle
, e
);
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">' );
276 // The toggle-link will be the first child of the element
277 var $toggle
= $that
.find( '> .mw-collapsible-toggle' );
279 // If theres no toggle link, add it
280 if ( !$toggle
.size() ) {
281 $that
.prepend( $toggleLink
);
283 $toggleLink
= $toggle
.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e
){
284 toggleLinkPremade( $toggle
, e
);
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();
300 } )( jQuery
, mediaWiki
);