Open main menu

Snippets/Toggle animations

How to use Snippets
List of Snippets
Crystal Clear action run.png
Toggle animations
Language(s): JavaScript
Compatible with: MediaWiki 1.17+ (Vector)

DescriptionEdit

Adds a link to the Toolbox portlet for toggling the animations on/off (see also bugzilla:30401).

CodeEdit

/**
 * Action link: Toggle animations
 * Dependencies: jquery.cookie
 *
 * @source mediawiki.org/wiki/Snippets/Toggle_animations
 * @version 4
 *
 */
jQuery(document).ready( function ( $ ) {
	var	cookieKey = mw.config.get( 'wgCookiePrefix' ) + '-enable-animations',
		cookieOptions = {
			expires: 30, // expires in 30 days
			path: '/' // domain-wide, entire wiki
		},
		label = {
			'true': 'Disable animations',
			'false': 'Enable animations'
		};
	if ( $('#ca-toggle-anim').length ) {
		return;
	}
	if ( $.cookie( cookieKey ) === null ) {
		$.cookie( cookieKey, !$.fx.off, cookieOptions );
	} else {
		$.fx.off = $.cookie( cookieKey ) === 'false';
	}
	$( mw.util.addPortletLink(
		'p-tb',
		'#',
		label[ !$.fx.off ],
		'#ca-toggle-anim',
		'Turn interface animations on or off'
	) )
	.click( function ( e ) {
		e.preventDefault();

		$.fx.off = !$.fx.off;
		$.cookie( cookieKey, String( !$.fx.off ), cookieOptions );
		$portlet.find( 'a' ).text( label[ String( !$.fx.off ) ] );
	} );
} );