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
, instantHide
) {
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 ( $defaultToggle
&& !$defaultToggle
.jquery
) {
39 // is optional, but if passed must be an instance of jQuery
42 var $containers
= null;
44 if ( action
== 'collapse' ) {
46 // Collapse the element
47 if ( $collapsible
.is( 'table' ) ) {
48 // Hide all table rows of this table
49 // Slide doens't work with tables, but fade does as of jQuery 1.1.3
50 // http://stackoverflow.com/questions/467336#920480
51 $containers
= $collapsible
.find( '>tbody>tr' );
52 if ( $defaultToggle
&& $defaultToggle
.jquery
) {
53 // Exclude tablerow containing togglelink
54 $containers
.not( $defaultToggle
.parent().parent() ).stop(true, true).fadeOut();
59 $containers
.stop( true, true ).fadeOut();
63 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
64 $containers
= $collapsible
.find( '> li' );
65 if ( $defaultToggle
&& $defaultToggle
.jquery
) {
66 // Exclude list-item containing togglelink
67 $containers
.not( $defaultToggle
.parent() ).stop( true, true ).slideUp();
72 $containers
.stop( true, true ).slideUp();
76 } else { // <div>, <p> etc.
77 var $collapsibleContent
= $collapsible
.find( '> .mw-collapsible-content' );
79 // If a collapsible-content is defined, collapse it
80 if ( $collapsibleContent
.size() ) {
82 $collapsibleContent
.hide();
84 $collapsibleContent
.slideUp();
87 // Otherwise assume this is a customcollapse with a remote toggle
88 // .. and there is no collapsible-content because the entire element should be toggled
90 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
91 $collapsible
.fadeOut();
93 $collapsible
.slideUp();
100 // Expand the element
101 if ( $collapsible
.is( 'table' ) ) {
102 $containers
= $collapsible
.find( '>tbody>tr' );
103 if ( $defaultToggle
&& $defaultToggle
.jquery
) {
104 // Exclude tablerow containing togglelink
105 $containers
.not( $defaultToggle
.parent().parent() ).stop(true, true).fadeIn();
107 $containers
.stop(true, true).fadeIn();
110 } else if ( $collapsible
.is( 'ul' ) || $collapsible
.is( 'ol' ) ) {
111 $containers
= $collapsible
.find( '> li' );
112 if ( $defaultToggle
&& $defaultToggle
.jquery
) {
113 // Exclude list-item containing togglelink
114 $containers
.not( $defaultToggle
.parent() ).stop( true, true ).slideDown();
116 $containers
.stop( true, true ).slideDown();
119 } else { // <div>, <p> etc.
120 var $collapsibleContent
= $collapsible
.find( '> .mw-collapsible-content' );
122 // If a collapsible-content is defined, collapse it
123 if ( $collapsibleContent
.size() ) {
124 $collapsibleContent
.slideDown();
126 // Otherwise assume this is a customcollapse with a remote toggle
127 // .. and there is no collapsible-content because the entire element should be toggled
129 if ( $collapsible
.is( 'tr' ) || $collapsible
.is( 'td' ) || $collapsible
.is( 'th' ) ) {
130 $collapsible
.fadeIn();
132 $collapsible
.slideDown();
138 // Toggles collapsible and togglelink class and updates text label
139 toggleLinkDefault = function( that
, e
) {
141 $collapsible
= $that
.closest( '.mw-collapsible.mw-made-collapsible' ).toggleClass( 'mw-collapsed' );
144 // It's expanded right now
145 if ( !$that
.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
146 // Change link to "Show"
147 $that
.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
148 if ( $that
.find( '> a' ).size() ) {
149 $that
.find( '> a' ).text( expandtext
);
151 $that
.text( expandtext
);
154 toggleElement( $collapsible
, 'collapse', $that
);
156 // It's collapsed right now
158 // Change link to "Hide"
159 $that
.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
160 if ( $that
.find( '> a' ).size() ) {
161 $that
.find( '> a' ).text( collapsetext
);
163 $that
.text( collapsetext
);
166 toggleElement( $collapsible
, 'expand', $that
);
170 // Toggles collapsible and togglelink class
171 toggleLinkPremade = function( $that
, e
) {
172 var $collapsible
= $that
.eq(0).closest( '.mw-collapsible.mw-made-collapsible' ).toggleClass( 'mw-collapsed' );
175 // It's expanded right now
176 if ( !$that
.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
177 // Change toggle to collapsed
178 $that
.removeClass( 'mw-collapsible-toggle-expanded' ).addClass( 'mw-collapsible-toggle-collapsed' );
180 toggleElement( $collapsible
, 'collapse', $that
);
182 // It's collapsed right now
184 // Change toggle to expanded
185 $that
.removeClass( 'mw-collapsible-toggle-collapsed' ).addClass( 'mw-collapsible-toggle-expanded' );
187 toggleElement( $collapsible
, 'expand', $that
);
191 // Toggles customcollapsible
192 toggleLinkCustom = function( $that
, e
, $collapsible
) {
193 // For the initial state call of customtogglers there is no event passed
197 // Get current state and toggle to the opposite
198 var action
= $collapsible
.hasClass( 'mw-collapsed' ) ? 'expand' : 'collapse';
199 $collapsible
.toggleClass( 'mw-collapsed' );
200 toggleElement( $collapsible
, action
, $that
)
204 // Use custom text or default ?
205 if( !collapsetext
|| collapsetext
=== '' ){
206 collapsetext
= mw
.msg( 'collapsible-collapse', 'Collapse' );
208 if ( !expandtext
|| expandtext
=== '' ){
209 expandtext
= mw
.msg( 'collapsible-expand', 'Expand' );
212 // Create toggle link with a space around the brackets ( [text] )
213 var $toggleLink
= $( '<a href="#"></a>' ).text( collapsetext
).wrap( '<span class="mw-collapsible-toggle"></span>' ).parent().prepend( ' [' ).append( '] ' ).bind( 'click.mw-collapse', function(e
){
214 toggleLinkDefault( this, e
);
217 // Return if it has been enabled already.
218 if ( $that
.hasClass( 'mw-made-collapsible' ) ) {
221 $that
.addClass( 'mw-made-collapsible' );
224 // Check if this element has a custom position for the toggle link
225 // (ie. outside the container or deeper inside the tree)
226 // Then: Locate the custom toggle link(s) and bind them
227 if ( $that
.attr( 'id' ).indexOf( 'mw-customcollapsible-' ) === 0 ) {
229 var thatId
= $that
.attr( 'id' ),
230 $customTogglers
= $( '.' + thatId
.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
231 mw
.log( 'Found custom collapsible: #' + thatId
);
233 // Double check that there is actually a customtoggle link
234 if ( $customTogglers
.size() ) {
235 $customTogglers
.bind( 'click.mw-collapse', function( e
) {
236 toggleLinkCustom( $(this), e
, $that
);
239 mw
.log( '#' + thatId
+ ': Missing toggler!' );
243 if ( $that
.hasClass( 'mw-collapsed' ) ) {
244 $that
.removeClass( 'mw-collapsed' );
245 toggleLinkCustom( $customTogglers
, null, $that
)
248 // If this is not a custom case, do the default:
249 // Wrap the contents add the toggle link
252 // Elements are treated differently
253 if ( $that
.is( 'table' ) ) {
254 // The toggle-link will be in one the the cells (td or th) of the first row
255 var $firstRowCells
= $( 'tr:first th, tr:first td', that
),
256 $toggle
= $firstRowCells
.find( '> .mw-collapsible-toggle' );
258 // If theres no toggle link, add it to the last cell
259 if ( !$toggle
.size() ) {
260 $firstRowCells
.eq(-1).prepend( $toggleLink
);
262 $toggleLink
= $toggle
.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e
){
263 toggleLinkPremade( $toggle
, e
);
267 } else if ( $that
.is( 'ul' ) || $that
.is( 'ol' ) ) {
268 // The toggle-link will be in the first list-item
269 var $firstItem
= $( 'li:first', $that
),
270 $toggle
= $firstItem
.find( '> .mw-collapsible-toggle' );
272 // If theres no toggle link, add it
273 if ( !$toggle
.size() ) {
274 // Make sure the numeral order doesn't get messed up, reset to 1 unless value-attribute is already used
275 // WebKit return '' if no value, Mozilla returns '-1' is no value
276 if ( $firstItem
.attr( 'value' ) == '' || $firstItem
.attr( 'value' ) == '-1' ) { // Will fail with ===
277 $firstItem
.attr( 'value', '1' );
279 $that
.prepend( $toggleLink
.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
281 $toggleLink
= $toggle
.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e
){
282 toggleLinkPremade( $toggle
, e
);
286 } else { // <div>, <p> etc.
287 // If a direct child .content-wrapper does not exists, create it
288 if ( !$that
.find( '> .mw-collapsible-content' ).size() ) {
289 $that
.wrapInner( '<div class="mw-collapsible-content"></div>' );
292 // The toggle-link will be the first child of the element
293 var $toggle
= $that
.find( '> .mw-collapsible-toggle' );
295 // If theres no toggle link, add it
296 if ( !$toggle
.size() ) {
297 $that
.prepend( $toggleLink
);
299 $toggleLink
= $toggle
.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e
){
300 toggleLinkPremade( $toggle
, e
);
306 // Initial state (only for those that are not custom)
307 if ( $that
.hasClass( 'mw-collapsed' ) && $that
.attr( 'id' ).indexOf( 'mw-customcollapsible-' ) !== 0 ) {
308 $that
.removeClass( 'mw-collapsed' );
309 // The collapsible element could have multiple togglers
310 // To toggle the initial state only click one of them (ie. the first one, eq(0) )
311 // Else it would go like: hide,show,hide,show for each toggle link.
312 toggleElement( $that
, 'collapse', null, /* instantHide = */ true );
313 $toggleLink
.eq(0).click();
317 } )( jQuery
, mediaWiki
);