Skip to content

Commit

Permalink
Added ngDisabled property
Browse files Browse the repository at this point in the history
  • Loading branch information
indrimuska committed Oct 31, 2015
1 parent 3c243e7 commit 8e51807
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-switcher",
"version": "0.2.1",
"version": "0.2.2",
"authors": [
"Indri Muska <indrimuska@gmail.com>"
],
Expand Down
4 changes: 3 additions & 1 deletion dist/angular-switcher.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@

.switcher.active .switcher-line:after { left: 100%; -webkit-transform: translate(-100%, -50%); transform: translate(-100%, -50%); }
.switcher.active .switcher-label.true { color: #4ecb32; }
.switcher.active .switcher-label.false { color: inherit; }
.switcher.active .switcher-label.false { color: inherit; }

.switcher.disabled { opacity: .7; }
15 changes: 10 additions & 5 deletions dist/angular-switcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@
this.require = 'ngModel';
this.scope = {
model: '=ngModel',
disabled: '=?ngDisabled',
trueValue: '=?',
trueLabel: '@?',
falseValue: '=?',
falseLabel: '@?',
change: '&?ngChange'
};
this.template =
'<div class="switcher" ng-class="{active:shadowModel}">' +
'<span class="switcher-label false" ng-bind-html="trustedHtml(falseLabel)" ng-click="shadowModel=false"></span>' +
'<div class="switcher" ng-class="{active:shadowModel,disabled:disabled}">' +
'<span class="switcher-label false" ng-bind-html="trustedHtml(falseLabel)" ng-click="set(false)"></span>' +
'<label class="switcher-line">' +
'<input type="checkbox" ng-model="shadowModel" ng-change="onChange()">' +
'<input type="checkbox" ng-model="shadowModel" ng-disabled="disabled" ng-change="onChange()">' +
'</label>' +
'<span class="switcher-label true" ng-bind-html="trustedHtml(trueLabel)" ng-click="shadowModel=true"></span>' +
'<span class="switcher-label true" ng-bind-html="trustedHtml(trueLabel)" ng-click="set(true)"></span>' +
'</div>';
$sce = sce;
};
Switcher.prototype.$inject = ['$sce'];
Switcher.prototype.link = function ($scope, $element, $attrs, $controller) {
var defaultProperties = { trueValue: true, falseValue: false },
var defaultProperties = { trueValue: true, falseValue: false, disabled: false },
defaultAttributes = { trueLabel: 'On', falseLabel: 'Off' };

angular.forEach(defaultProperties, function (value, key) {
Expand All @@ -40,6 +41,10 @@
$scope.trustedHtml = function (value) {
return $sce.trustAsHtml(value);
};
$scope.set = function (value) {
if ($scope.disabled) return;
$scope.shadowModel = value;
};
$scope.onChange = function () {
var oldValue = $scope.model,
newValue = $scope.shadowModel ? $scope.trueValue : $scope.falseValue;
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-switcher.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/angular-switcher.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-switcher",
"version": "0.2.1",
"version": "0.2.2",
"description": "Angular Switcher is an AngularJS directive that models toggle switches.",
"main": "Gruntfile.js",
"repository": {
Expand Down
4 changes: 3 additions & 1 deletion src/angular-switcher.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@

.switcher.active .switcher-line:after { left: 100%; -webkit-transform: translate(-100%, -50%); transform: translate(-100%, -50%); }
.switcher.active .switcher-label.true { color: #4ecb32; }
.switcher.active .switcher-label.false { color: inherit; }
.switcher.active .switcher-label.false { color: inherit; }

.switcher.disabled { opacity: .7; }
15 changes: 10 additions & 5 deletions src/angular-switcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@
this.require = 'ngModel';
this.scope = {
model: '=ngModel',
disabled: '=?ngDisabled',
trueValue: '=?',
trueLabel: '@?',
falseValue: '=?',
falseLabel: '@?',
change: '&?ngChange'
};
this.template =
'<div class="switcher" ng-class="{active:shadowModel}">' +
'<span class="switcher-label false" ng-bind-html="trustedHtml(falseLabel)" ng-click="shadowModel=false"></span>' +
'<div class="switcher" ng-class="{active:shadowModel,disabled:disabled}">' +
'<span class="switcher-label false" ng-bind-html="trustedHtml(falseLabel)" ng-click="set(false)"></span>' +
'<label class="switcher-line">' +
'<input type="checkbox" ng-model="shadowModel" ng-change="onChange()">' +
'<input type="checkbox" ng-model="shadowModel" ng-disabled="disabled" ng-change="onChange()">' +
'</label>' +
'<span class="switcher-label true" ng-bind-html="trustedHtml(trueLabel)" ng-click="shadowModel=true"></span>' +
'<span class="switcher-label true" ng-bind-html="trustedHtml(trueLabel)" ng-click="set(true)"></span>' +
'</div>';
$sce = sce;
};
Switcher.prototype.$inject = ['$sce'];
Switcher.prototype.link = function ($scope, $element, $attrs, $controller) {
var defaultProperties = { trueValue: true, falseValue: false },
var defaultProperties = { trueValue: true, falseValue: false, disabled: false },
defaultAttributes = { trueLabel: 'On', falseLabel: 'Off' };

angular.forEach(defaultProperties, function (value, key) {
Expand All @@ -40,6 +41,10 @@
$scope.trustedHtml = function (value) {
return $sce.trustAsHtml(value);
};
$scope.set = function (value) {
if ($scope.disabled) return;
$scope.shadowModel = value;
};
$scope.onChange = function () {
var oldValue = $scope.model,
newValue = $scope.shadowModel ? $scope.trueValue : $scope.falseValue;
Expand Down

0 comments on commit 8e51807

Please sign in to comment.