MediaWiki:Gadget-switcher.js
Nota: Després de desar, heu de netejar la memòria cau del navegador per veure els canvis. En la majoria de navegadors amb Windows o Linux, premeu Ctrl+F5 o bé premeu Shift i cliqueu el botó "Actualitza" (Ctrl i "Actualitza" amb Internet Explorer). Vegeu més informació i instruccions per a cada navegador a Viquipèdia:Neteja de la memòria cau.
'use strict';
$( function () {
$.each( document.querySelectorAll( '.switcher-container' ), function ( i ) {
var activeElement, $radio,
switchers = [], container = this, radioName = 'switcher-' + i;
$.each( this.children, function () {
var 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">Mostra-ho tot</label>' ).prepend(
$( '<input type="radio">' ).attr( 'name', radioName ).click( function () {
$( switchers ).show();
activeElement = switchers;
} )
).appendTo( container );
}
if ( switchers.length === 1 ) {
$radio.remove();
}
} );
} );