Jump to content

MediaWiki:Gadget-switcher.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Krinkle (talk | contribs) at 02:30, 18 September 2019 (Maintenance: Remove redundant closure). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
'use strict';
$( function () {
	$( document.querySelectorAll( '.switcher-container' ) ).each( function ( i ) {
		var activeElement, $showAllRadio;
		var elements = [], container = this, radioName = 'switcher-' + i;
		$( this ).children().each( function () {
			var self = this, $showRadio;
			var $labelContainer = $( self ).find( '.switcher-label' );
			var $labelText = $labelContainer.contents();
			if ( !$labelText.length ) {
				return;
			}
			elements.push( self );
			$showRadio = $( '<input type="radio">' ).attr( 'name', radioName ).click( function () {
				$( activeElement ).hide();
				$( self ).show();
				activeElement = self;
			} );
			$( '<label style="display:block"></label>' ).append( $showRadio, $labelText ).appendTo( container );
			if ( !activeElement ) {
				activeElement = self;
				$showRadio.prop( 'checked', true );
			} else if ( $labelContainer.is( '[data-switcher-default]' ) ) {
				$showRadio.click();
			} else {
				$( self ).hide();
			}
			$labelContainer.remove();
		} );
		if ( elements.length > 1 ) {
			$showAllRadio = $( '<input type="radio">' ).attr( 'name', radioName ).click( function () {
				$( elements ).show();
				activeElement = elements;
			} );
			$( '<label style="display:block">Show all</label>' ).prepend( $showAllRadio ).appendTo( container );
		} else if ( elements.length === 1 ) {
			$showRadio.remove();
		}
	} );
} );