User:Alexis Jazz/Gadget-switcher.js

/*globals $:false,mw:false,OO:false*/ $( function { 'use strict';	if ( $( '.switcher-container' )[0] ) {		mw.loader.using(['oojs-ui-core','mediawiki.util']).then(function{ mw.util.addCSS('.switcher-container fieldset{margin-top:0.5em}'); $.each( document.querySelectorAll( '.switcher-container' ), function ( i, container ) {				var selected, $radio, $fieldlayouts;				$fieldlayouts = {};				var switchers = [];				var radioName = 'switcher-' + i;				var fieldset = new OO.ui.FieldsetLayout( { } );				$(container).append(fieldset.$element);				$.each( container.children, function ( j, switcher ) { var label = switcher.querySelector( '.switcher-label' ); if ( !label || !label.childNodes.length ) { return; }					switchers.push( switcher ); $radio = new OO.ui.RadioInputWidget( {						name: radioName					} ); if ( !selected ) { // Mark the first one as selected selected = switcher; $radio.setSelected(true); } else if ( label.getAttribute( 'data-switcher-default' ) !== null ) { // Custom default $radio.setSelected(true); } else { // Hide non-default $( switcher ).hide; }					$fieldlayouts[i] = new OO.ui.FieldLayout( $radio, { label: label.innerText, align: 'inline' } ); fieldset.addItems( [					   $fieldlayouts[i]					] ); $radio.$element.on('click',function {						$( selected ).hide;						$( switcher ).show;						selected = switcher;					}); $( label ).remove; } );				if ( switchers.length > 1 ) {					$radio = new OO.ui.RadioInputWidget( { name: radioName } );					$fieldlayouts[i] = new OO.ui.FieldLayout( $radio, { label: 'Show all', align: 'inline' } );					fieldset.addItems( [ $fieldlayouts[i] ] );					$radio.$element.on('click',function { $( switchers ).show; selected = switchers; });				}				if ( switchers.length === 1 ) {					$fieldlayouts[i].toggle(false);				}			}); } );	} } );