|
1 | | -angular.module('toggle-switch', ['ng']).directive('toggleSwitch', function ($compile) { |
2 | | - return { |
3 | | - restrict: 'EA', |
4 | | - replace: true, |
5 | | - scope: { |
6 | | - model: '=', |
7 | | - disabled: '@', |
8 | | - onLabel: '@', |
9 | | - offLabel: '@', |
10 | | - knobLabel: '@', |
11 | | - html: '=', |
12 | | - onChange: '&' |
13 | | - }, |
14 | | - template: '<div class="switch" ng-click="toggle()" ng-class="{ \'disabled\': disabled }"><div class="switch-animate" ng-class="{\'switch-off\': !model, \'switch-on\': model}"><span class="switch-left"></span><span class="knob"></span><span class="switch-right"></span></div></div>', |
15 | | - controller: function($scope) { |
16 | | - $scope.toggle = function toggle() { |
17 | | - if(!$scope.disabled) { |
18 | | - $scope.model = !$scope.model; |
19 | | - } |
20 | | - $scope.onChange(); |
21 | | - }; |
22 | | - }, |
23 | | - compile: function(element, attrs) { |
24 | | - if (angular.isUndefined(attrs.onLabel)) { attrs.onLabel = 'On'; } |
25 | | - if (angular.isUndefined(attrs.offLabel)) { attrs.offLabel = 'Off'; } |
26 | | - if (angular.isUndefined(attrs.knobLabel)) { attrs.knobLabel = '\u00a0'; } |
27 | | - if (angular.isUndefined(attrs.disabled)) { attrs.disabled = false; } |
28 | | - if (angular.isUndefined(attrs.html)) { attrs.html = false; } |
29 | | - |
30 | | - return function postLink(scope, iElement, iAttrs, controller) { |
| 1 | +angular.module('toggle-switch', ['ng']).directive('toggleSwitch', function($compile) { |
| 2 | + return { |
| 3 | + restrict: 'EA', |
| 4 | + replace: true, |
| 5 | + scope: { |
| 6 | + model: '=', |
| 7 | + disabled: '@', |
| 8 | + onLabel: '@', |
| 9 | + offLabel: '@', |
| 10 | + knobLabel: '@', |
| 11 | + html: '=', |
| 12 | + onChange: '&' |
| 13 | + }, |
| 14 | + template: '<div class="ats-switch" ng-click="toggle()" ng-class="{ \'disabled\': disabled }"><div class="switch-animate" ng-class="{\'switch-off\': !model, \'switch-on\': model}"><span class="switch-left"></span><span class="knob"></span><span class="switch-right"></span></div></div>', |
| 15 | + controller: function($scope) { |
| 16 | + $scope.toggle = function toggle() { |
| 17 | + if (!$scope.disabled) { |
| 18 | + $scope.model = !$scope.model; |
| 19 | + } |
| 20 | + $scope.onChange(); |
| 21 | + }; |
| 22 | + }, |
| 23 | + compile: function(element, attrs) { |
| 24 | + if (angular.isUndefined(attrs.onLabel)) { |
| 25 | + attrs.onLabel = 'On'; |
| 26 | + } |
| 27 | + if (angular.isUndefined(attrs.offLabel)) { |
| 28 | + attrs.offLabel = 'Off'; |
| 29 | + } |
| 30 | + if (angular.isUndefined(attrs.knobLabel)) { |
| 31 | + attrs.knobLabel = '\u00a0'; |
| 32 | + } |
| 33 | + if (angular.isUndefined(attrs.disabled)) { |
| 34 | + attrs.disabled = false; |
| 35 | + } |
| 36 | + if (angular.isUndefined(attrs.html)) { |
| 37 | + attrs.html = false; |
| 38 | + } |
31 | 39 |
|
32 | | - var bindSpan = function (span, html) { |
33 | | - span = angular.element(span); |
34 | | - var bindAttributeName = (html === true) ? 'ng-bind-html' : 'ng-bind'; |
| 40 | + return function postLink(scope, iElement, iAttrs, controller) { |
35 | 41 |
|
36 | | - // remove old ng-bind attributes |
37 | | - span.removeAttr('ng-bind-html'); |
38 | | - span.removeAttr('ng-bind'); |
| 42 | + var bindSpan = function(span, html) { |
| 43 | + span = angular.element(span); |
| 44 | + var bindAttributeName = (html === true) ? 'ng-bind-html' : 'ng-bind'; |
39 | 45 |
|
40 | | - if(angular.element(span).hasClass("switch-left")) |
41 | | - span.attr(bindAttributeName, 'onLabel'); |
42 | | - if(span.hasClass("knob")) |
43 | | - span.attr(bindAttributeName, 'knobLabel'); |
44 | | - if(span.hasClass("switch-right")) |
45 | | - span.attr(bindAttributeName, 'offLabel'); |
| 46 | + // remove old ng-bind attributes |
| 47 | + span.removeAttr('ng-bind-html'); |
| 48 | + span.removeAttr('ng-bind'); |
46 | 49 |
|
47 | | - $compile(span)(scope, function(cloned, scope) { |
48 | | - span.replaceWith(cloned); |
49 | | - }); |
50 | | - }; |
| 50 | + if (angular.element(span).hasClass("switch-left")) |
| 51 | + span.attr(bindAttributeName, 'onLabel'); |
| 52 | + if (span.hasClass("knob")) |
| 53 | + span.attr(bindAttributeName, 'knobLabel'); |
| 54 | + if (span.hasClass("switch-right")) |
| 55 | + span.attr(bindAttributeName, 'offLabel'); |
51 | 56 |
|
52 | | - // add ng-bind attribute to each span element. |
53 | | - // NOTE: you need angular-sanitize to use ng-bind-html |
54 | | - var bindSwitch = function (iElement, html) { |
55 | | - angular.forEach(iElement[0].children[0].children, function (span, index) { |
56 | | - bindSpan(span, html); |
57 | | - }); |
58 | | - }; |
| 57 | + $compile(span)(scope, function(cloned, scope) { |
| 58 | + span.replaceWith(cloned); |
| 59 | + }); |
| 60 | + }; |
59 | 61 |
|
60 | | - scope.$watch('html', function (newValue) { |
61 | | - bindSwitch(iElement, newValue); |
62 | | - }); |
| 62 | + // add ng-bind attribute to each span element. |
| 63 | + // NOTE: you need angular-sanitize to use ng-bind-html |
| 64 | + var bindSwitch = function(iElement, html) { |
| 65 | + angular.forEach(iElement[0].children[0].children, function(span, index) { |
| 66 | + bindSpan(span, html); |
| 67 | + }); |
| 68 | + }; |
63 | 69 |
|
64 | | - }; |
65 | | - } |
66 | | - }; |
| 70 | + scope.$watch('html', function(newValue) { |
| 71 | + bindSwitch(iElement, newValue); |
| 72 | + }); |
| 73 | + }; |
| 74 | + } |
| 75 | + }; |
67 | 76 | }); |
0 commit comments