MediaWiki:Gadget-switcher.js

From Seeds of the Word, the encyclopedia of the influence of the Gospel on culture

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
'use strict';
$( function () {
	$.each( document.querySelectorAll( '.switcher-container' ), function ( i ) {
		var activeElement,
			switchers = [], container = this, radioName = 'switcher-' + i;
		$.each( this.children, function () {
			var $radio, switcher = this,
				$labelContainer = $( switcher.querySelector('.switcher-label') ),
				$labelText = $labelContainer.contents();
			if ( !$labelText.length ) {
				return;
			}
			switchers.push( switcher );
			$radio = $( '<input type="radio">' ).attr( 'name', radioName ).click( function () {
				$( activeElement ).hide();
				$( switcher ).show();
				activeElement = switcher;
			} );
			if ( !activeElement ) {
				// Mark the first one as selected
				activeElement = switcher;
				$radio.prop( 'checked', true );
			} else if ( $labelContainer.attr( 'data-switcher-default' ) !== undefined ) {
				// Custom default
				$radio.click();
			} else {
				// Hide non-default
				$( switcher ).hide();
			}
			$( '<label style="display:block"></label>' ).append( $radio, $labelText ).appendTo( container );
			$labelContainer.remove();
		} );
		if ( switchers.length > 1 ) {
			$( '<label style="display:block">Show all</label>' ).prepend(
				$( '<input type="radio">' ).attr( 'name', radioName ).click( function () {
					$( switchers ).show();
					activeElement = switchers;
				} )
			).appendTo( container );
		}
		if ( switchers.length === 1 ) {
			$radio.remove();
		}
	} );
} );