'use strict';
$( function () {
$.each( document.querySelectorAll( '.switcher-container' ), function ( i ) {
var activeElement, $radio,
switchers = , container = this, radioName = 'switcher-' + i, compact = $(container).hasClass('compact');
$.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 ) {
// Elegir el primer elemento por defecto
activeElement = switcher;
$radio.prop( 'checked', true );
} else if ( $labelContainer.attr( 'data-switcher-default' ) !== undefined ) {
// Selección por defecto predeterminada
$radio.click();
} else {
// Ocultar otras opciones
$( switcher ).hide();
}
var $label = $( '<label style="display:block"></label>' );
//Estilo compacto
if ( compact ) {
$label.removeAttr('style');
}
$label.append( $radio, $labelText ).appendTo( container );
$labelContainer.remove();
} );
if ( switchers.length > 1 && !compact ) {
$( '<label style="display:block">Ver todo</label>' ).prepend(
$( '<input type="radio">' ).attr( 'name', radioName ).click( function () {
$( switchers ).show();
activeElement = switchers;
} )
).appendTo( container );
}
if ( switchers.length === 1 ) {
$radio.remove();
}
} );
} );