* @license CC-BY 3.0 <http://creativecommons.org/licenses/by/3.0>
* @license GPL2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
*/
-( function( $, mw ) {
+( function ( $, mw ) {
-$.fn.makeCollapsible = function() {
+$.fn.makeCollapsible = function () {
- return this.each(function() {
- var _fn = 'jquery.makeCollapsible> ';
+ return this.each(function () {
// Define reused variables and functions
- var $that = $(this).addClass( 'mw-collapsible' ), // case: $( '#myAJAXelement' ).makeCollapsible()
+ var $toggle,
+ lpx = 'jquery.makeCollapsible> ',
+ $that = $(this).addClass( 'mw-collapsible' ), // case: $( '#myAJAXelement' ).makeCollapsible()
that = this,
collapsetext = $(this).attr( 'data-collapsetext' ),
expandtext = $(this).attr( 'data-expandtext' ),
- toggleElement = function( $collapsible, action, $defaultToggle, instantHide ) {
+ toggleElement = function ( $collapsible, action, $defaultToggle, instantHide ) {
+ var $collapsibleContent, $containers;
+
// Validate parameters
if ( !$collapsible.jquery ) { // $collapsible must be an instance of jQuery
return;
}
- if ( action != 'expand' && action != 'collapse' ) {
+ if ( action !== 'expand' && action !== 'collapse' ) {
// action must be string with 'expand' or 'collapse'
return;
}
- if ( typeof $defaultToggle == 'undefined' ) {
+ if ( $defaultToggle === undefined ) {
$defaultToggle = null;
}
- if ( $defaultToggle !== null && !($defaultToggle instanceof jQuery) ) {
+ if ( $defaultToggle !== null && !($defaultToggle instanceof $) ) {
// is optional (may be undefined), but if defined it must be an instance of jQuery.
// If it's not, abort right away.
// After this $defaultToggle is either null or a valid jQuery instance.
return;
}
-
- var $containers = null;
- if ( action == 'collapse' ) {
+ if ( action === 'collapse' ) {
// Collapse the element
if ( $collapsible.is( 'table' ) ) {
// Slide doens't work with tables, but fade does as of jQuery 1.1.3
// http://stackoverflow.com/questions/467336#920480
$containers = $collapsible.find( '>tbody>tr' );
- if ( $defaultToggle ) {
+ if ( $defaultToggle ) {
// Exclude tablerow containing togglelink
$containers.not( $defaultToggle.closest( 'tr' ) ).stop(true, true).fadeOut();
} else {
- if ( instantHide ) {
+ if ( instantHide ) {
$containers.hide();
} else {
$containers.stop( true, true ).fadeOut();
}
}
-
+
} else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
$containers = $collapsible.find( '> li' );
if ( $defaultToggle ) {
$containers.stop( true, true ).slideUp();
}
}
-
+
} else { // <div>, <p> etc.
- var $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
-
+ $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
+
// If a collapsible-content is defined, collapse it
if ( $collapsibleContent.length ) {
if ( instantHide ) {
}
} else {
-
+
// Expand the element
if ( $collapsible.is( 'table' ) ) {
$containers = $collapsible.find( '>tbody>tr' );
} else {
$containers.stop(true, true).fadeIn();
}
-
+
} else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
$containers = $collapsible.find( '> li' );
if ( $defaultToggle ) {
} else {
$containers.stop( true, true ).slideDown();
}
-
+
} else { // <div>, <p> etc.
- var $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
-
+ $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
+
// If a collapsible-content is defined, collapse it
if ( $collapsibleContent.length ) {
$collapsibleContent.slideDown();
}
},
// Toggles collapsible and togglelink class and updates text label
- toggleLinkDefault = function( that, e ) {
- var $that = $(that),
+ toggleLinkDefault = function ( that, e ) {
+ var $that = $(that),
$collapsible = $that.closest( '.mw-collapsible.mw-made-collapsible' ).toggleClass( 'mw-collapsed' );
e.preventDefault();
e.stopPropagation();
-
+
// It's expanded right now
if ( !$that.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
// Change link to "Show"
return;
},
// Toggles collapsible and togglelink class
- toggleLinkPremade = function( $that, e ) {
- var $collapsible = $that.eq(0).closest( '.mw-collapsible.mw-made-collapsible' ).toggleClass( 'mw-collapsed' );
+ toggleLinkPremade = function ( $that, e ) {
+ var $collapsible = $that.eq(0).closest( '.mw-collapsible.mw-made-collapsible' ).toggleClass( 'mw-collapsed' );
+ if ( $(e.target).is( 'a' ) ) {
+ return true;
+ }
e.preventDefault();
e.stopPropagation();
-
+
// It's expanded right now
if ( !$that.hasClass( 'mw-collapsible-toggle-collapsed' ) ) {
// Change toggle to collapsed
return;
},
// Toggles customcollapsible
- toggleLinkCustom = function( $that, e, $collapsible ) {
+ toggleLinkCustom = function ( $that, e, $collapsible ) {
// For the initial state call of customtogglers there is no event passed
if (e) {
e.preventDefault();
- e.stopPropagation();
+ e.stopPropagation();
}
// Get current state and toggle to the opposite
var action = $collapsible.hasClass( 'mw-collapsed' ) ? 'expand' : 'collapse';
$collapsible.toggleClass( 'mw-collapsed' );
toggleElement( $collapsible, action, $that );
-
+
};
// Use custom text or default ?
- if( !collapsetext ) {
+ if ( !collapsetext ) {
collapsetext = mw.msg( 'collapsible-collapse' );
}
if ( !expandtext ) {
.parent()
.prepend( ' [' )
.append( '] ' )
- .bind( 'click.mw-collapse', function(e) {
+ .on( 'click.mw-collapse', function ( e ) {
toggleLinkDefault( this, e );
} );
} else {
$that.addClass( 'mw-made-collapsible' );
}
-
+
// Check if this element has a custom position for the toggle link
// (ie. outside the container or deeper inside the tree)
// Then: Locate the custom toggle link(s) and bind them
var thatId = $that.attr( 'id' ),
$customTogglers = $( '.' + thatId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
- mw.log( _fn + 'Found custom collapsible: #' + thatId );
-
+ mw.log( lpx + 'Found custom collapsible: #' + thatId );
+
// Double check that there is actually a customtoggle link
if ( $customTogglers.length ) {
- $customTogglers.bind( 'click.mw-collapse', function( e ) {
+ $customTogglers.on( 'click.mw-collapse', function ( e ) {
toggleLinkCustom( $(this), e, $that );
} );
} else {
- mw.log( _fn + '#' + thatId + ': Missing toggler!' );
+ mw.log( lpx + '#' + thatId + ': Missing toggler!' );
}
-
+
// Initial state
if ( $that.hasClass( 'mw-collapsed' ) ) {
$that.removeClass( 'mw-collapsed' );
}
// If this is not a custom case, do the default:
- // Wrap the contents add the toggle link
+ // Wrap the contents add the toggle link
} else {
// Elements are treated differently
if ( $that.is( 'table' ) ) {
// The toggle-link will be in one the the cells (td or th) of the first row
- var $firstRowCells = $( 'tr:first th, tr:first td', that ),
- $toggle = $firstRowCells.find( '> .mw-collapsible-toggle' );
-
+ var $firstRowCells = $that.find( 'tr:first th, tr:first td' );
+ $toggle = $firstRowCells.find( '> .mw-collapsible-toggle' );
+
// If theres no toggle link, add it to the last cell
if ( !$toggle.length ) {
$firstRowCells.eq(-1).prepend( $toggleLink );
} else {
- $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ) {
+ $toggleLink = $toggle.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e ) {
toggleLinkPremade( $toggle, e );
} );
}
-
+
} else if ( $that.is( 'ul' ) || $that.is( 'ol' ) ) {
// The toggle-link will be in the first list-item
- var $firstItem = $( 'li:first', $that),
- $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
-
+ var $firstItem = $that.find( 'li:first' );
+ $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
+
// If theres no toggle link, add it
if ( !$toggle.length ) {
// Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
// to be "1". Except if the value-attribute is already used.
// If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
var firstval = $firstItem.attr( 'value' );
- if ( firstval === undefined || !firstval || firstval == '-1' ) {
+ if ( firstval === undefined || !firstval || firstval === '-1' || firstval === -1 ) {
$firstItem.attr( 'value', '1' );
}
$that.prepend( $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
} else {
- $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ) {
+ $toggleLink = $toggle.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e ) {
toggleLinkPremade( $toggle, e );
} );
}
-
+
} else { // <div>, <p> etc.
-
+
// The toggle-link will be the first child of the element
- var $toggle = $that.find( '> .mw-collapsible-toggle' );
+ $toggle = $that.find( '> .mw-collapsible-toggle' );
// If a direct child .content-wrapper does not exists, create it
if ( !$that.find( '> .mw-collapsible-content' ).length ) {
$that.wrapInner( '<div class="mw-collapsible-content"></div>' );
}
-
+
// If theres no toggle link, add it
if ( !$toggle.length ) {
$that.prepend( $toggleLink );
} else {
- $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ) {
+ $toggleLink = $toggle.off( 'click.mw-collapse' ).on( 'click.mw-collapse', function ( e ) {
toggleLinkPremade( $toggle, e );
} );
}
}
} );
};
-} )( jQuery, mediaWiki );
\ No newline at end of file
+
+}( jQuery, mediaWiki ) );