forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathionicToggle.js
85 lines (74 loc) · 2.42 KB
/
ionicToggle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(function(ionic) {
'use strict';
angular.module('ionic.ui.toggle', [])
/**
* @ngdoc directive
* @name ionToggle
* @module ionic
* @restrict E
*
* @description
* An animated switch which binds a given model to a boolean.
*
* Allows dragging of the switch's nub.
*
* Behaves like any [AngularJS checkbox](http://docs.angularjs.org/api/ng/input/input[checkbox]) otherwise.
*
*/
.directive('ionToggle', ['$ionicGesture', '$timeout', function($ionicGesture, $timeout) {
return {
restrict: 'E',
replace: true,
require: '?ngModel',
scope: {
ngModel: '=?',
ngValue: '=?',
ngChecked: '=?',
ngChange: '&',
ngDisabled: '=?'
},
transclude: true,
template: '<div class="item item-toggle disable-pointer-events">' +
'<div ng-transclude></div>' +
'<label class="toggle enable-pointer-events">' +
'<input type="checkbox" ng-model="ngModel" ng-value="ngValue" ng-change="ngChange()" ng-disabled="ngDisabled">' +
'<div class="track disable-pointer-events">' +
'<div class="handle"></div>' +
'</div>' +
'</label>' +
'</div>',
compile: function(element, attr) {
var input = element.find('input');
if(attr.name) input.attr('name', attr.name);
if(attr.ngChecked) input.attr('ng-checked', 'ngChecked');
if(attr.ngTrueValue) input.attr('ng-true-value', attr.ngTrueValue);
if(attr.ngFalseValue) input.attr('ng-false-value', attr.ngFalseValue);
return function($scope, $element, $attr) {
var el, checkbox, track, handle;
el = $element[0].getElementsByTagName('label')[0];
checkbox = el.children[0];
track = el.children[1];
handle = track.children[0];
var ngModelController = angular.element(checkbox).controller('ngModel');
$scope.toggle = new ionic.views.Toggle({
el: el,
track: track,
checkbox: checkbox,
handle: handle,
onChange: function() {
if(checkbox.checked) {
ngModelController.$setViewValue(true);
} else {
ngModelController.$setViewValue(false);
}
$scope.$apply();
}
});
$scope.$on('$destroy', function() {
$scope.toggle.destroy();
});
};
}
};
}]);
})(window.ionic);