forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathionicRadio.js
129 lines (111 loc) · 3.36 KB
/
ionicRadio.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
(function(ionic) {
'use strict';
angular.module('ionic.ui.radio', [])
/**
* @ngdoc directive
* @name ionRadio
* @module ionic
* @restrict E
* @description
* No different than the HTML radio input, except it's styled differently.
*
* Behaves like any [AngularJS radio](http://docs.angularjs.org/api/ng/input/input[radio]).
*
* @usage
* ```html
* <ion-radio ng-model="choice" value="A">Choose A</ion-radio>
* <ion-radio ng-model="choice" value="B">Choose B</ion-radio>
* <ion-radio ng-model="choice" value="C">Choose C</ion-radio>
* ```
*/
.directive('ionRadio', function() {
return {
restrict: 'E',
replace: true,
require: '?ngModel',
scope: {
ngModel: '=?',
ngValue: '=?',
ngChange: '&',
icon: '@'
},
transclude: true,
template: '<label class="item item-radio">' +
'<input type="radio" name="radio-group"' +
' ng-model="ngModel" ng-value="ngValue" ng-change="ngChange()">' +
'<div class="item-content disable-pointer-events" ng-transclude></div>' +
'<i class="radio-icon disable-pointer-events icon ion-checkmark"></i>' +
'</label>',
compile: function(element, attr) {
if(attr.name) element.children().eq(0).attr('name', attr.name);
if(attr.icon) element.children().eq(2).removeClass('ion-checkmark').addClass(attr.icon);
}
};
})
// The radio button is a radio powered element with only
// one possible selection in a set of options.
.directive('ionRadioButtons', function() {
return {
restrict: 'E',
replace: true,
require: '?ngModel',
scope: {
value: '@'
},
transclude: true,
template: '<div class="button-bar button-bar-inline" ng-transclude></div>',
controller: ['$scope', '$element', function($scope, $element) {
this.select = function(element) {
var c, children = $element.children();
for(var i = 0; i < children.length; i++) {
c = children[i];
if(c != element[0]) {
c.classList.remove('active');
}
}
};
}],
link: function($scope, $element, $attr, ngModel) {
var radio;
if(ngModel) {
//$element.bind('tap', tapHandler);
ngModel.$render = function() {
var children = $element.children();
for(var i = 0; i < children.length; i++) {
children[i].classList.remove('active');
}
$scope.$parent.$broadcast('radioButton.select', ngModel.$viewValue);
};
}
}
};
})
.directive('ionButtonRadio', function() {
return {
restrict: 'CA',
require: ['?^ngModel', '?^ionRadioButtons'],
link: function($scope, $element, $attr, ctrls) {
var ngModel = ctrls[0];
var radioButtons = ctrls[1];
if(!ngModel || !radioButtons) { return; }
var setIt = function() {
$element.addClass('active');
ngModel.$setViewValue($scope.$eval($attr.ngValue));
radioButtons.select($element);
};
var clickHandler = function(e) {
setIt();
};
$scope.$on('radioButton.select', function(e, val) {
if(val == $scope.$eval($attr.ngValue)) {
$element.addClass('active');
}
});
ionic.on('tap', clickHandler, $element[0]);
$scope.$on('$destroy', function() {
ionic.off('tap', clickHandler);
});
}
};
});
})(window.ionic);