123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708 |
- (function($){
-
- var templates = {
-
- $switcher : $( '<span>', {'class': 'swraper'} ).hide(),
- $toggler: $( '<span>', {'class': 'stoggler' } ),
- $on: $( '<span>', {'class': 'slabel-on'} ),
- $off: $( '<span>', {'class': 'slabel-off'} ),
- $blob: $( '<span>', {'class': 'sblob'} ),
-
- themes: [ 'light', 'modern', 'dark', 'flat', 'yellowish-green' ],
- };
-
-
- var switcherM = {
-
- start: function( switcherP, options )
- {
-
- this.setTemplateStyle( options, switcherP );
-
-
- switcherP.$this.each( function( i, input )
- {
-
- if( input.hasOwnProperty( 'rcSwitcher' ) )
- {
- console.warn( 'already has instance' );
- return;
- }
- this.makeSwitcher( $( input ), switcherP, options );
- }.bind( this ) );
-
- this.behaviour( switcherP );
- },
-
- makeSwitcher: function( $input, switcherP, options )
- {
-
- var inputName = $input.attr( 'name' ).replace( /(\[\])+$/, '' ),
- inputValue = $input.attr('value') || '',
- type = $input.attr('type'),
- cat = ( type == 'checkbox' ) ? switcherP.checkboxs : switcherP.radios ,
- data = { components: {}, '$input': $input };
- data.$switcher = templates.$switcher.clone().attr( { 'input-name': inputName, 'input-value': inputValue, 'input-type': type } );
- if( switcherP.cssValues.dir == 'rtl' ) data.$switcher.addClass( 'rtl' );
-
- data.components.$toggler = templates.$toggler.clone();
-
- data.components.$on = templates.$on.clone().html( $input.attr('data-ontext') || options.onText );
- data.components.$off = templates.$off.clone().html( $input.attr('data-offtext') || options.offText );
- data.components.$blob = templates.$blob.clone();
-
-
- if( options.reverse )
- data.components.$toggler.append( data.components.$off, data.components.$blob, data.components.$on )
- .appendTo( data.$switcher );
-
- else
- data.components.$toggler.append( data.components.$on, data.components.$blob, data.components.$off )
- .appendTo( data.$switcher );
-
-
-
- $input.before( data.$switcher );
-
- if( $input[ 0 ].checked )
- {
- data.switcherChecked = true;
-
-
-
- this.turnOn( data, switcherP, false, true );
- }
- else
- {
- data.switcherChecked = false;
- this.turnOff( data, switcherP, false, true );
- }
-
- inputDisabled = $input[ 0 ].disabled;
-
- if( inputDisabled )
- {
- data.switcherDisabled = true;
- data.$switcher.addClass( 'disabled' ).attr( 'title', 'switch is disabled!' );
- }
- else
- {
- data.switcherDisabled = false;
- }
-
-
-
-
-
- if( family = cat.group[ inputName ] )
- {
- family.group[ inputValue ] = data;
-
-
- family.length++;
- }
- else
- {
-
- cat.group[ inputName ] = family = { group: {}, length: 1 }
-
- family.group[ inputValue ] = data;
-
- cat.groupLength++;
-
- if( type == 'radio' )
- {
- family.enabled = 0;
- family.disabled = 0;
- family.switchable = false;
- }
- }
-
-
- if( type == 'radio' )
- {
-
- if( inputDisabled )
- family.disabled++;
- else
- family.enabled++;
-
- if( $input[ 0 ].checked )
- family.checkedKey = inputValue;
-
- family.switchable = ( family.enabled >= 2 ) ? true : false;
- }
-
-
- switcherP.$switchers = switcherP.$switchers.add( data.$switcher );
-
- $input[0].rcSwitcher = data;
-
- $input.attr( 'data-has-rcswitcher', 1 );
-
- cat.length++;
-
- data.$switcher.show();
-
-
- $input.on( 'change', function(e)
- {
- this.trackChanges({
- type: type,
- name: inputName,
- value: inputValue,
- }, switcherP );
- }.bind(this));
- },
-
- setTemplateStyle: function( options, switcherP ){
-
- templates.$switcher.css( { 'width': [ options.width, 'px' ].join(''), 'line-height': [ options.height, 'px' ].join('') } )
- .removeClass( templates.themes.join(' ') ).addClass( options.theme );
-
- switcherP.cssValues.switcherWidth = options.width;
- switcherP.cssValues.switcherHeight = options.height;
-
-
- switcherP.cssValues.blobOffset = options.blobOffset;
- switcherP.cssValues.blobWidth = switcherP.cssValues.blobHeight = switcherP.cssValues.switcherHeight - options.blobOffset * 2;
- templates.$blob.css( { 'width': switcherP.cssValues.blobWidth, 'height': switcherP.cssValues.blobHeight } );
-
- if( options.reverse )
- {
- switcherP.transformOff = ['translateX(', switcherP.transformDir , switcherP.cssValues.blobWidth / 2 + switcherP.cssValues.blobOffset, 'px)' ].join('');
-
- switcherP.transformOn = ['translateX(', switcherP.transformDir , switcherP.cssValues.switcherWidth - switcherP.cssValues.blobWidth / 2 - switcherP.cssValues.blobOffset, 'px)' ].join('');
- }
- else
- {
- switcherP.transformOn = ['translateX(', switcherP.transformDir , switcherP.cssValues.blobWidth / 2 + switcherP.cssValues.blobOffset, 'px)' ].join('');
-
- switcherP.transformOff = ['translateX(', switcherP.transformDir , switcherP.cssValues.switcherWidth - switcherP.cssValues.blobWidth / 2 - switcherP.cssValues.blobOffset, 'px)' ].join('');
- }
- switcherP.transformOn = {
- '-webkit-transform': switcherP.transformOn,
- '-moz-transform': switcherP.transformOn,
- '-o-transform': switcherP.transformOn,
- '-ms-transform': switcherP.transformOn,
- 'transform': switcherP.transformOn
- }
- switcherP.transformOff = {
- '-webkit-transform': switcherP.transformOff,
- '-moz-transform': switcherP.transformOff,
- '-o-transform': switcherP.transformOff,
- '-ms-transform': switcherP.transformOff,
- 'transform': switcherP.transformOff
- }
-
-
-
- templates.$toggler.css( 'font-size', '' );
-
- if( options.height < 20 || options.autoFontSize ) templates.$toggler.css( { 'font-size': [ options.height / 2, 'px' ].join('') } );
-
-
-
-
- templates.$switcher.css( 'margin', '' );
- if( options.autoStick )
- {
- var $fInput = switcherP.$this.first(),
- $label = $fInput.prev('label'),
- parentAW;
-
- if( $label )
- {
- parentAW = $fInput.parent().width();
-
- parentAW -= ( options.inputs ) ? $fInput.outerWidth( true ) : 0;
-
- parentAW -= $label.outerWidth( true );
-
- var margin = parentAW - options.width;
-
- switch( options.theme )
- {
- case 'dark':
- margin -= 2;
- break;
- case 'yellowish-green':
- margin -= 4;
- break;
- }
-
-
- if( switcherP.cssValues.dir == 'rtl' )
-
- templates.$switcher.css( 'margin-right', margin );
- else
- templates.$switcher.css( 'margin-left', margin );
- }
- }
- },
-
- toggle: function( info, switcherP, outsideChange ){
-
- var family, data;
- if( info.type == 'checkbox' )
- {
-
- family = switcherP.checkboxs.group[ info.name ];
- data = family.group[ info.value ];
- if( !data.$input[ 0 ].disabled )
- {
-
- if( outsideChange )
- status = data.$input[ 0 ].checked ? 'turnOn' : 'turnOff';
- else
- status = data.$input[ 0 ].checked ? 'turnOff' : 'turnOn';
- this[ status ]( data, switcherP, outsideChange );
- }
- }
- else
- {
-
- family = switcherP.radios.group[ info.name ];
-
- data = family.group[ info.value ] ;
- if( outsideChange && data.$input[0].checked == data.switcherChecked )
- return;
-
-
- if( outsideChange && !data.$input[ 0 ].checked )
- {
-
- console.log( 'can not disable radio button, try to activate another sibiling one to deactivate current.' );
- data.$input.prop( 'checked', true );
- return;
- }
- if( !data.$input[ 0 ].disabled && family.switchable )
- {
-
- if( family.checkedKey != info.name )
- {
-
- this.turnOff( family.group[ family.checkedKey ], switcherP, outsideChange );
- this.turnOn( data, switcherP, outsideChange );
- family.checkedKey = info.value;
- }
- }
- }
- },
-
- trackChanges: function( info, switcherP )
- {
-
- var family, data;
-
- if( info.type == 'checkbox' )
- family = switcherP.checkboxs.group[ info.name ];
- else
- family = switcherP.radios.group[ info.name ];
- data = family.group[ info.value ];
-
- if( data.$input[0].checked != data.switcherChecked )
- {
- if( data.switcherDisabled )
- {
-
- data.$input.prop( 'checked', data.switcherChecked );
-
- if( info.type == 'radio' )
- family.group[ family.checkedKey ].$input.prop( 'checked', true );
- }
- else
- this.toggle( info, switcherP, true );
- }
-
- if( data.$input[0].disabled != data.switcherDisabled )
- {
- if( data.$input[0].disabled )
- {
- if( !data.$switcher.hasClass( 'disabled' ) )
- data.$switcher.addClass( 'disabled' );
- if( info.type == 'radio' )
- {
- family.disabled++;
- family.enabled--;
-
- if( data.$input[0].checked == true )
- family.switchable = false;
-
- }
- data.switcherDisabled = true;
-
- data.$input.trigger( 'disable.rcSwitcher', data );
- }
- else
- {
- data.$switcher.removeClass( 'disabled' );
-
- data.$input.trigger( 'enable.rcSwitcher', data );
- if( info.type == 'radio' )
- {
- --family.disabled;
- ++family.enabled;
- if( family.enabled - family.disabled >= 2 )
- family.switchable = true;
-
- }
- data.switcherDisabled = false;
- }
- }
- },
-
-
- turnOff: function( data, switcherP, outsideChange, styleOnly ){
-
- data.components.$toggler.css( switcherP.transformOff ).removeClass( 'on' ).addClass('off');
- data.switcherChecked = false;
- if( styleOnly ) return;
-
-
- if( !outsideChange )
- data.$input.prop( 'checked', false );
-
-
- setTimeout( function(){
- data.$input.trigger( 'turnoff.rcSwitcher', data );
- data.$input.trigger( 'toggle.rcSwitcher', [ data, 'turnoff' ] );
- }, 500 );
- },
-
- turnOn: function( data, switcherP, outsideChange, styleOnly ){
-
-
- data.components.$toggler.css( switcherP.transformOn ).addClass( 'on' ).removeClass('off');
-
- data.switcherChecked = true;
- if( styleOnly ) return;
-
-
-
- if( !outsideChange )
- data.$input.prop( 'checked', true );
-
- setTimeout( function(){
- data.$input.trigger( 'turnon.rcSwitcher', data );
- data.$input.trigger( 'toggle.rcSwitcher', [ data, 'turnon' ] );
- }, 500 );
- },
-
- behaviour: function( switcherP ){
-
-
- switcherP.$this.on( 'click', function( e )
- {
- e.preventDefault();
- e.stopPropagation();
- }.bind( this ) );
-
- switcherP.$switchers.on( 'click', function( e )
- {
- var obj = {};
- obj.type = e.currentTarget.getAttribute( 'input-type' );
- obj.name = e.currentTarget.getAttribute( 'input-name' );
- obj.value = e.currentTarget.getAttribute( 'input-value' );
-
- this.toggle( obj, switcherP );
- e.preventDefault();
- e.stopPropagation();
- }.bind( this ) );
- },
- };
-
-
-
-
-
- $.fn.rcSwitcher = function( options ){
-
- var switcherP = {
- checkboxs: {
- group:{},
- groupLength: 0,
- length: 0,
- },
- radios: {
- group:{},
- groupLength: 0,
- length: 0,
- },
-
- $switchers: $([]),
- cssValues:{},
- };
-
-
- switcherP.$this = this.filter( 'input[type=checkbox], input[type=radio]' ).not( '[data-has-rcswitcher]' );
-
- if( switcherP.$this.length == 0 )
- return this;
- switcherP.cssValues.dir = window.getComputedStyle( switcherP.$this[0], null ).direction || 'ltr';
-
- switcherP.transformDir = ( switcherP.cssValues.dir == 'rtl' ) ? '' : '-';
-
-
- options = $.extend(
- {
- onText: 'ON',
- offText: 'OFF',
- reverse: false,
- inputs: false,
- width: 56,
- height: 20,
- blobOffset: 1,
- autoFontSize: false,
- theme: 'light',
- autoStick: false,
- },
- options || {} );
-
- if( !options.inputs ) switcherP.$this.css( 'display', 'none' );
-
- if( templates.themes.indexOf( options.theme ) == -1 ) options.theme = 'light';
-
-
-
- switcherM.start( switcherP, options );
-
- return this;
- };
- })(jQuery);
|