/**
* jQuery makeCollapsible
+ * Note: To avoid performance issues such as reflows, several styles are
+ * shipped in mediawiki.makeCollapsible.styles to reserve space for the toggle control. Please
+ * familiarise yourself with that CSS before making any changes to this code.
*
* Dual licensed:
* - CC BY 3.0 <http://creativecommons.org/licenses/by/3.0>
* @class jQuery.plugin.makeCollapsible
*/
( function ( $, mw ) {
-
/**
* Handler for a click on a collapsible toggler.
*
};
// Handle different kinds of elements
-
if ( !options.plainMode && $collapsible.is( 'table' ) ) {
// Tables
// If there is a caption, hide all rows; otherwise, only hide body rows
$containers = $containers.not( $defaultToggle.closest( 'tr' ) );
}
- if ( action === 'collapse' ) {
- // Hide all table rows of this table
- // Slide doesn't work with tables, but fade does as of jQuery 1.1.3
- // http://stackoverflow.com/questions/467336#920480
- if ( options.instantHide ) {
- $containers.hide();
- hookCallback();
- } else {
- $containers.stop( true, true ).fadeOut().promise().done( hookCallback );
- }
- } else {
- $containers.stop( true, true ).fadeIn().promise().done( hookCallback );
- }
-
} else if ( !options.plainMode && ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) ) {
// Lists
$containers = $collapsible.find( '> li' );
// Exclude list-item containing togglelink
$containers = $containers.not( $defaultToggle.parent() );
}
-
- if ( action === 'collapse' ) {
- if ( options.instantHide ) {
- $containers.hide();
- hookCallback();
- } else {
- $containers.stop( true, true ).slideUp().promise().done( hookCallback );
- }
- } else {
- $containers.stop( true, true ).slideDown().promise().done( hookCallback );
- }
-
} else {
// Everything else: <div>, <p> etc.
$collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
// If a collapsible-content is defined, act on it
if ( !options.plainMode && $collapsibleContent.length ) {
- if ( action === 'collapse' ) {
- if ( options.instantHide ) {
- $collapsibleContent.hide();
- hookCallback();
- } else {
- $collapsibleContent.slideUp().promise().done( hookCallback );
- }
- } else {
- $collapsibleContent.slideDown().promise().done( hookCallback );
- }
+ $containers = $collapsibleContent;
// Otherwise assume this is a customcollapse with a remote toggle
// .. and there is no collapsible-content because the entire element should be toggled
} else {
- if ( action === 'collapse' ) {
- if ( options.instantHide ) {
- $collapsible.hide();
- hookCallback();
- } else {
- if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
- $collapsible.fadeOut().promise().done( hookCallback );
- } else {
- $collapsible.slideUp().promise().done( hookCallback );
- }
- }
- } else {
- if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
- $collapsible.fadeIn().promise().done( hookCallback );
- } else {
- $collapsible.slideDown().promise().done( hookCallback );
- }
- }
+ $containers = $collapsible;
}
}
+
+ $containers.toggle( action === 'expand' );
+ hookCallback();
}
/**
collapseText = options.toggleText.collapseText;
expandText = options.toggleText.expandText;
- $textContainer = $collapsible.find( '.mw-collapsible-text' );
+ $textContainer = $toggle.find( '.mw-collapsible-text' );
if ( $textContainer.length ) {
$textContainer.text( wasCollapsed ? collapseText : expandText );
}
* expand the element. Default: the 'data-expandtext' attribute of the
* collapsible element or the content of 'collapsible-expand' message.
* @param {boolean} [options.collapsed] Whether to collapse immediately. By default
- * collapse only if the elements has the 'mw-collapsible' class.
+ * collapse only if the element has the 'mw-collapsed' class.
* @param {jQuery} [options.$customTogglers] Elements to be used as togglers
* for this collapsible element. By default, if the collapsible element
* has an id attribute like 'mw-customcollapsible-XXX', elements with a
options = options || {};
this.each( function () {
- var $collapsible, collapseText, expandText, $caption, $toggle, actionHandler, buildDefaultToggleLink,
- $toggleLink, $firstItem, collapsibleId, $customTogglers, firstval;
+ var $collapsible, collapseText, expandText, $caption, $toggle, actionHandler,
+ buildDefaultToggleLink, $firstItem, collapsibleId, $customTogglers, firstval;
// Ensure class "mw-collapsible" is present in case .makeCollapsible()
// is called on element(s) that don't have it yet.
if ( $collapsible.data( 'mw-made-collapsible' ) ) {
return;
} else {
- $collapsible.data( 'mw-made-collapsible', true );
+ // Let CSS know that it no longer needs to worry about flash of unstyled content.
+ // This will allow mediawiki.makeCollapsible.styles to disable temporary pseudo elements, that
+ // are needed to avoid a flash of unstyled content.
+ $collapsible.addClass( 'mw-made-collapsible' )
+ .data( 'mw-made-collapsible', true );
}
// Use custom text or default?
buildDefaultToggleLink = function () {
return $( '<a class="mw-collapsible-text"></a>' )
.text( collapseText )
- .wrap( '<span class="mw-collapsible-toggle"></span>' ).parent()
- .attr( {
- role: 'button',
- tabindex: 0
- } )
- .prepend( '<span>[</span>' )
- .append( '<span>]</span>' )
- .on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler );
+ .wrap( '<span class="mw-collapsible-toggle mw-collapsible-toggle-default"></span>' )
+ .parent()
+ .attr( {
+ role: 'button',
+ tabindex: 0
+ } );
};
// Check if this element has a custom position for the toggle link
togglingHandler( $( this ), $collapsible, e, opts );
};
- $toggleLink = $customTogglers
- .on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
- .prop( 'tabIndex', 0 );
+ $toggle = $customTogglers;
} else {
// If this is not a custom case, do the default: wrap the
// If there is no toggle link, add it to the end of the caption
if ( !$toggle.length ) {
- $toggleLink = buildDefaultToggleLink().appendTo( $caption );
- } else {
- $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
- .prop( 'tabIndex', 0 );
+ $toggle = buildDefaultToggleLink().appendTo( $caption );
}
} else {
// The toggle-link will be in one of the cells (td or th) of the first row
// If theres no toggle link, add it to the last cell
if ( !$toggle.length ) {
- $toggleLink = buildDefaultToggleLink().prependTo( $firstItem.eq( -1 ) );
- } else {
- $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
- .prop( 'tabIndex', 0 );
+ $toggle = buildDefaultToggleLink().prependTo( $firstItem.eq( -1 ) );
}
}
$collapsible.find( '> .mw-collapsible-toggle' ).length === 0
) {
// special case of one collapsible in <li> tag
- $toggleLink = buildDefaultToggleLink();
- $collapsible.before( $toggleLink );
+ $toggle = buildDefaultToggleLink();
+ $collapsible.before( $toggle );
} else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
// The toggle-link will be in the first list-item
$firstItem = $collapsible.find( 'li:first' );
if ( firstval === undefined || !firstval || firstval === '-1' || firstval === -1 ) {
$firstItem.prop( 'value', '1' );
}
- $toggleLink = buildDefaultToggleLink();
- $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent().prependTo( $collapsible );
- } else {
- $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
- .prop( 'tabIndex', 0 );
+ $toggle = buildDefaultToggleLink();
+ $toggle.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent().prependTo( $collapsible );
}
} else { // <div>, <p> etc.
// If theres no toggle link, add it
if ( !$toggle.length ) {
- $toggleLink = buildDefaultToggleLink().prependTo( $collapsible );
- } else {
- $toggleLink = $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
- .prop( 'tabIndex', 0 );
+ $toggle = buildDefaultToggleLink().prependTo( $collapsible );
}
}
}
+ // Attach event handlers to togglelink
+ $toggle.on( 'click.mw-collapsible keypress.mw-collapsible', actionHandler )
+ .prop( 'tabIndex', 0 );
+
$( this ).data( 'mw-collapsible', {
collapse: function () {
- actionHandler.call( $toggleLink.get( 0 ), null, { instantHide: true, wasCollapsed: false } );
+ actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: false } );
},
expand: function () {
- actionHandler.call( $toggleLink.get( 0 ), null, { instantHide: true, wasCollapsed: true } );
+ actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: true } );
},
toggle: function () {
- actionHandler.call( $toggleLink.get( 0 ), null, null );
+ actionHandler.call( $toggle.get( 0 ), null, null );
}
} );
if ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) {
// One toggler can hook to multiple elements, and one element can have
// multiple togglers. This is the sanest way to handle that.
- actionHandler.call( $toggleLink.get( 0 ), null, { instantHide: true, wasCollapsed: false } );
+ actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: false } );
}
} );