Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ Disable switch
<input type="checkbox" class="js-switch" ui-switch ng-disabled="isDisabled" />
```

##Changing model values##
By default, values passed to your ng-model will be `true` and `false`.
If you want to change this behavior you can add `ui-switch-true-value` and `ui-switch-false-value` attributes to your input element.
```html
<input type="checkbox" class="js-switch" ng-model="myModel" ui-switch ui-switch-true-value="1" ui-switch-false-value="0" />
```

Bower install
```
Expand Down
124 changes: 61 additions & 63 deletions src/ng-switchery.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,84 @@
'use strict';
angular.module('Postblazer')
.directive('uiSwitch', ['$window', '$timeout', '$log', '$parse', function ($window, $timeout, $log, $parse) {

/**
* Module to use Switchery as a directive for angular.
* @TODO implement Switchery as a service, https://github.com/abpetkov/switchery/pull/11
*/
angular.module('NgSwitchery', [])
.directive('uiSwitch', ['$window', '$timeout','$log', '$parse', function($window, $timeout, $log, $parse) {

/**
* Initializes the HTML element as a Switchery switch.
*
* $timeout is in place as a workaround to work within angular-ui tabs.
*
* @param scope
* @param elem
* @param attrs
* @param ngModel
*/
function linkSwitchery(scope, elem, attrs, ngModel) {
if(!ngModel) return false;
var options = {};
try {
options = $parse(attrs.uiSwitch)(scope);
}
catch (e) {}
/**
* Initializes the HTML element as a Switchery switch.
*
* $timeout is in place as a workaround to work within angular-ui tabs.
*
* @param scope
* @param elem
* @param attrs
* @param ngModel
*/
function linkSwitchery(scope, elem, attrs, ngModel) {
if (!ngModel) return false;
var options = {};
try {
options = $parse(attrs.uiSwitch)(scope);
}
catch (e) {
}
var instanceTrueValue = true; // default value if no ui-switch-true-value provided
var instanceFalseValue = false; // default value if no ui-switch-false-value provided
var switcher;

var switcher;
//set model true/false value if ui-switch-true/false-value provided
if (attrs.uiSwitchTrueValue) {
instanceTrueValue = isNaN(parseInt(attrs.uiSwitchTrueValue)) ? attrs.uiSwitchTrueValue : parseInt(attrs.uiSwitchTrueValue);
}
if (attrs.uiSwitchFalseValue) {
instanceFalseValue = isNaN(parseInt(attrs.uiSwitchFalseValue)) ? attrs.uiSwitchFalseValue : parseInt(attrs.uiSwitchFalseValue);
}

attrs.$observe('disabled', function(value) {
if (!switcher) {
attrs.$observe('disabled', function (value) {
if (!switcher) {
return;
}
}

if (value) {
if (value) {
switcher.disable();
}
else {
}
else {
switcher.enable();
}
});
}
});

initializeSwitch();

// Watch changes
scope.$watch(function () {
return ngModel.$modelValue;
}, function(newValue,oldValue) {
initializeSwitch()
});

function initializeSwitch() {
$timeout(function() {
function initializeSwitch() {
$timeout(function () {
// Remove any old switcher
if (switcher) {
angular.element(switcher.switcher).remove();
angular.element(switcher.switcher).remove();
}
// (re)create switcher to reflect latest state of the checkbox element
switcher = new $window.Switchery(elem[0], options);
var element = switcher.element;
element.checked = scope.initValue;
element.checked = scope.ngModel == instanceTrueValue;
if (attrs.disabled) {
switcher.disable();
switcher.disable();
}

switcher.setPosition(false);
element.addEventListener('change',function(evt) {
scope.$apply(function() {
ngModel.$setViewValue(element.checked);
element.addEventListener('change', function (evt) {
scope.$apply(function () {
scope.ngModel = element.checked ? instanceTrueValue : instanceFalseValue; // overwrites ngModel ngModel
})
});
scope.$watch('initValue', function(newValue, oldValue) {
scope.$watch('ngModel', function (newngModel, oldngModel) {
switcher.setPosition(false);
});
}, 0);
}
initializeSwitch();
}

return {
require: 'ngModel',
restrict: 'AE',
scope : {
initValue : '=ngModel'
},
link: linkSwitchery
}, 0);
}
}]);
}

return {
require: 'ngModel',
restrict: 'AE',
scope: {
ngModel: '=ngModel'
},
link: linkSwitchery
}
}]);