-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsm-radio.js
152 lines (126 loc) · 3.73 KB
/
sm-radio.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
(function(app)
{
app
.factory('SemanticRadioLink', ['SemanticUI', SemanticRadioLink])
.directive('smRadioBind', ['SemanticUI', SemanticRadioBind])
.directive('smRadio', ['SemanticRadioLink', SemanticRadio])
;
var BEHAVIORS = {
smRadioCheck: 'check',
smRadioEnable: 'enable',
smRadioDisable: 'disable'
};
angular.forEach( BEHAVIORS, function(method, directive)
{
app.directive( directive, ['SemanticUI', function(SemanticUI)
{
return SemanticUI.createBehavior( directive, 'checkbox', method );
}]);
});
function SemanticRadioBind(SemanticUI)
{
return SemanticUI.createBind( 'smRadioBind', 'checkbox' );
}
function SemanticRadio(SemanticRadioLink)
{
return {
restrict: 'E',
replace: true,
transclude: true,
scope: {
/* Required */
model: '=',
label: '@',
name: '@',
value: '=',
/* Optional */
settings: '=',
enabled: '=',
onInit: '=',
/* Events */
onChange: '=',
onChecked: '=',
onUnchecked: '=',
onEnable: '=',
onDisable: '='
},
template: [
'<div class="ui radio checkbox">',
' <input name="{{ name }}" type="radio">',
' <label>{{ label }}</label>',
'</div>'
].join('\n'),
link: SemanticRadioLink
};
}
function SemanticRadioLink(SemanticUI)
{
return function(scope, element, attributes)
{
element.ready(function()
{
var settings = scope.settings || {};
SemanticUI.linkSettings( scope, element, attributes, 'checkbox', true );
SemanticUI.triggerChange( scope, 'model', element, true );
if ( attributes.enabled )
{
var enabledWatcher = SemanticUI.watcher( scope, 'enabled',
function(updated) {
if ( angular.isDefined( updated ) ) {
element.checkbox( updated ? 'set enabled' : 'set disabled' );
}
}
);
SemanticUI.onEvent( settings, 'onEnable',
function(value) {
enabledWatcher.set( true );
}
);
SemanticUI.onEvent( settings, 'onDisable',
function(value) {
enabledWatcher.set( false );
}
);
}
var modelWatcher = SemanticUI.watcher( scope, 'model',
function(updated) {
if ( updated === scope.value ) {
element.checkbox( 'set checked' );
}
}
);
SemanticUI.onEvent( settings, 'onChecked',
function() {
modelWatcher.set( scope.value );
}
);
SemanticUI.linkEvents( scope, settings, $.fn.checkbox.settings, {
onChange: 'onChange',
onChecked: 'onChecked',
onUnchecked: 'onUnchecked',
onEnable: 'onEnable',
onDisable: 'onDisable'
});
// Initialize the element with the given settings.
element.checkbox( settings );
// Set initial state of the radio
if ( scope.model === scope.value )
{
element.checkbox( 'set checked' );
}
// If the radio is a slider, remove the radio class
if ( element.hasClass( 'slider' ) )
{
element.removeClass( 'radio' );
}
if ( angular.isDefined( scope.enabled ) && !scope.enabled )
{
element.checkbox( 'set disabled' );
}
if ( angular.isFunction( scope.onInit ) ) {
scope.onInit( element );
}
});
};
}
})( angular.module('semantic-ui-radio', ['semantic-ui-core']) );