'use strict';angular.module('bootstrapSwitch',[]).directive('bsSwitch',function($parse,$timeout){return{restrict:'A',require:'ngModel',link:function link(scope,element,attrs,controller){var isInit=false;var getTrueValue=function(){if(attrs.type==='radio'){return attrs.value||$parse(attrs.ngValue)(scope)||true;} var trueValue=($parse(attrs.ngTrueValue)(scope));if(angular.isUndefined(trueValue)){trueValue=true;} return trueValue;};var getBooleanFromString=function(value){return scope.$eval(value)===true;};var getBooleanFromStringDefTrue=function(value){return(value===true||value==='true'||!value);};var getValueOrUndefined=function(value){return(value?value:undefined);};var getExprFromString=function(value){if(angular.isUndefined(value)){return angular.noop;} return function(){scope.$evalAsync(value);};};var getSwitchAttrValue=function(attrName){var map={'switchRadioOff':getBooleanFromStringDefTrue,'switchActive':function(value){return!getBooleanFromStringDefTrue(value);},'switchAnimate':getBooleanFromStringDefTrue,'switchLabel':function(value){return value?value:' ';},'switchIcon':function(value){if(value){return'';}},'switchWrapper':function(value){return value||'wrapper';},'switchInverse':getBooleanFromString,'switchReadonly':getBooleanFromString,'switchChange':getExprFromString};var transFn=map[attrName]||getValueOrUndefined;return transFn(attrs[attrName]);};var setSwitchParamMaybe=function(element,attr,modelAttr){if(!isInit){return;} var newValue=getSwitchAttrValue(modelAttr);element.bootstrapSwitch(attr,newValue);};var setActive=function(){setSwitchParamMaybe(element,'disabled','switchActive');};var initMaybe=function(){if(!isInit){var viewValue=(controller.$modelValue===getTrueValue());isInit=!isInit;element.bootstrapSwitch({radioAllOff:getSwitchAttrValue('switchRadioOff'),disabled:getSwitchAttrValue('switchActive'),state:viewValue,onText:getSwitchAttrValue('switchOnText'),offText:getSwitchAttrValue('switchOffText'),onColor:getSwitchAttrValue('switchOnColor'),offColor:getSwitchAttrValue('switchOffColor'),animate:getSwitchAttrValue('switchAnimate'),size:getSwitchAttrValue('switchSize'),labelText:attrs.switchLabel?getSwitchAttrValue('switchLabel'):getSwitchAttrValue('switchIcon'),wrapperClass:getSwitchAttrValue('switchWrapper'),handleWidth:getSwitchAttrValue('switchHandleWidth'),labelWidth:getSwitchAttrValue('switchLabelWidth'),inverse:getSwitchAttrValue('switchInverse'),readonly:getSwitchAttrValue('switchReadonly')});if(attrs.type==='radio'){controller.$setViewValue(controller.$modelValue);}else{controller.$setViewValue(viewValue);}}};var switchChange=getSwitchAttrValue('switchChange');var listenToModel=function(){attrs.$observe('switchActive',function(newValue){var active=getBooleanFromStringDefTrue(newValue);if(!active){$timeout(setActive);}else{setActive();}});controller.$render=function(){initMaybe();var newValue=controller.$modelValue;if(newValue!==undefined&&newValue!==null){element.bootstrapSwitch('state',newValue===getTrueValue(),true);}else{element.bootstrapSwitch('indeterminate',true,true);controller.$setViewValue(undefined);} switchChange();};var bindings={'switchRadioOff':'radioAllOff','switchOnText':'onText','switchOffText':'offText','switchOnColor':'onColor','switchOffColor':'offColor','switchAnimate':'animate','switchSize':'size','switchLabel':'labelText','switchIcon':'labelText','switchWrapper':'wrapperClass','switchHandleWidth':'handleWidth','switchLabelWidth':'labelWidth','switchInverse':'inverse','switchReadonly':'readonly'};var observeProp=function(prop,bindings){return function(){attrs.$observe(prop,function(){setSwitchParamMaybe(element,bindings[prop],prop);});};};for(var prop in bindings){attrs.$observe(prop,observeProp(prop,bindings));}};var listenToView=function(){if(attrs.type==='radio'){element.on('change.bootstrapSwitch',function(e){if((controller.$modelValue===controller.$viewValue)&&(e.target.checked!==$(e.target).bootstrapSwitch('state'))){if(e.target.checked){controller.$setViewValue(getTrueValue());}else if(getTrueValue()===controller.$viewValue){controller.$setViewValue(undefined);} switchChange();}});}else{element.on('switchChange.bootstrapSwitch',function(e){controller.$setViewValue(e.target.checked);switchChange();});}};listenToView();listenToModel();scope.$on('$destroy',function(){element.bootstrapSwitch('destroy');});}};}).directive('bsSwitch',function(){return{restrict:'E',require:'ngModel',template:'',replace:true};});