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>
18 $.fn
.makeCollapsible = function() {
20 return this.each(function() {
21 mw
.config
.set( 'mw.log.prefix', 'jquery.makeCollapsible' );
23 // Define reused variables and functions
24 var $that
= $(this).addClass( 'mw-collapsible' ), // case: $( '#myAJAXelement' ).makeCollapsible()
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
33 if ( action
!= 'expand' && action
!= 'collapse' ) {
34 // action must be string with 'expand' or 'collapse'
37 if ( typeof $defaultToggle
!== 'undefined' && !$defaultToggle
.jquery
) {
38 // is optional, but if passed must be an instance of jQuery
42 if ( action
== 'collapse' ) {
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
50 if ( $defaultToggle
.jquery
) {
51 // Exclude tablerow containing togglelink
52 $collapsible
.find( '>tbody>tr' ).not( $defaultToggle
.parent().parent() ).stop(true, true).fadeOut();
54 $collapsible
.find( '>tbody>tr' ).stop( true, true ).fadeOut();
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();
62 $collapsible
.find( '> li' ).stop( true, true ).slideUp();
65 } else { // <div>, <p> etc.
66 var $collapsibleContent
= $collapsible
.find( '> .mw-collapsible-content' );
68 // If a collapsible-content is defined, collapse it
69 if ( $collapsibleContent
.size() ) {
70 $collapsibleContent
.slideUp();
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
75 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
76 $collapsible
.fadeOut();
78 $collapsible
.slideUp();
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();
91 $collapsible
.find( '>tbody>tr' ).stop(true, true).fadeIn();
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();
99 $collapsible
.find( '> li' ).stop( true, true ).slideDown();
102 } else { // <div>, <p> etc.
103 var $collapsibleContent
= $collapsible
.find( '> .mw-collapsible-content' );
105 // If a collapsible-content is defined, collapse it
106 if ( $collapsibleContent
.size() ) {
107 $collapsibleContent
.slideDown();
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
112 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
113 $collapsible
.fadeIn();
115 $collapsible
.slideDown();
121 // Toggles collapsible and togglelink class and updates text label
122 toggleLinkDefault = function( that
, e
) {
124 $collapsible
= $that
.closest( '.mw-collapsible.mw-made-collapsible' ).toggleClass( 'mw-collapsed' );
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
);
134 $that
.text( expandtext
);
137 toggleElement( $collapsible
, 'collapse', $that
);
139 // It's collapsed right now
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
);
146 $that
.text( collapsetext
);
149 toggleElement( $collapsible
, 'expand', $that
);
153 // Toggles collapsible and togglelink class
154 toggleLinkPremade = function( that
, e
) {
156 $collapsible
= $that
.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( this, 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( this, 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( this, 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' );