forked from frapontillo/angular-bootstrap-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-bootstrap-switch.js
250 lines (247 loc) · 9.65 KB
/
angular-bootstrap-switch.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* angular-bootstrap-switch
* @version v0.4.0 - 2015-04-13
* @author Francesco Pontillo ([email protected])
* @link https://github.com/frapontillo/angular-bootstrap-switch
* @license Apache License 2.0(http://www.apache.org/licenses/LICENSE-2.0.html)
**/
(function() {
'use strict';
// Source: common/module.js
angular.module('frapontillo.bootstrap-switch', []);
// Source: dist/.temp/directives/bsSwitch.js
angular.module('frapontillo.bootstrap-switch').directive('bsSwitch', [
'$parse',
'$timeout',
function ($parse, $timeout) {
return {
restrict: 'A',
require: 'ngModel',
link: function link(scope, element, attrs, controller) {
var isInit = false;
/**
* Return the true value for this specific checkbox.
* @returns {Object} representing the true view value; if undefined, returns true.
*/
var getTrueValue = function () {
if (attrs.type === 'radio') {
return attrs.value || $parse(attrs.ngValue)(scope) || true;
}
var trueValue = $parse(attrs.ngTrueValue)(scope);
if (!angular.isString(trueValue)) {
trueValue = true;
}
return trueValue;
};
/**
* Get a boolean value from a boolean-like string, evaluating it on the current scope.
* @param value The input object
* @returns {boolean} A boolean value
*/
var getBooleanFromString = function (value) {
return scope.$eval(value) === true;
};
/**
* Get a boolean value from a boolean-like string, defaulting to true if undefined.
* @param value The input object
* @returns {boolean} A boolean value
*/
var getBooleanFromStringDefTrue = function (value) {
return value === true || value === 'true' || !value;
};
/**
* Returns the value if it is truthy, or undefined.
*
* @param value The value to check.
* @returns the original value if it is truthy, {@link undefined} otherwise.
*/
var getValueOrUndefined = function (value) {
return value ? value : undefined;
};
/**
* Get the value of the angular-bound attribute, given its name.
* The returned value may or may not equal the attribute value, as it may be transformed by a function.
*
* @param attrName The angular-bound attribute name to get the value for
* @returns {*} The attribute value
*/
var getSwitchAttrValue = function (attrName) {
var map = {
'switchRadioOff': getBooleanFromStringDefTrue,
'switchActive': function (value) {
return !getBooleanFromStringDefTrue(value);
},
'switchAnimate': getBooleanFromStringDefTrue,
'switchLabel': function (value) {
return value ? value : ' ';
},
'switchIcon': function (value) {
if (value) {
return '<span class=\'' + value + '\'></span>';
}
},
'switchWrapper': function (value) {
return value || 'wrapper';
},
'switchInverse': getBooleanFromString,
'switchReadonly': getBooleanFromString
};
var transFn = map[attrName] || getValueOrUndefined;
return transFn(attrs[attrName]);
};
/**
* Set a bootstrapSwitch parameter according to the angular-bound attribute.
* The parameter will be changed only if the switch has already been initialized
* (to avoid creating it before the model is ready).
*
* @param element The switch to apply the parameter modification to
* @param attr The name of the switch parameter
* @param modelAttr The name of the angular-bound parameter
*/
var setSwitchParamMaybe = function (element, attr, modelAttr) {
if (!isInit) {
return;
}
var newValue = getSwitchAttrValue(modelAttr);
element.bootstrapSwitch(attr, newValue);
};
var setActive = function () {
setSwitchParamMaybe(element, 'disabled', 'switchActive');
};
/**
* If the directive has not been initialized yet, do so.
*/
var initMaybe = function () {
// if it's the first initialization
if (!isInit) {
var viewValue = controller.$modelValue === getTrueValue();
isInit = !isInit;
// Bootstrap the switch plugin
element.bootstrapSwitch({
radioAllOff: getSwitchAttrValue('switchRadioOff'),
disabled: getSwitchAttrValue('switchActive'),
state: viewValue,
onText: getSwitchAttrValue('switchOnText'),
offText: getSwitchAttrValue('switchOffText'),
onColor: getSwitchAttrValue('switchOnColor'),
offColor: getSwitchAttrValue('switchOffColor'),
animate: getSwitchAttrValue('switchAnimate'),
size: getSwitchAttrValue('switchSize'),
labelText: attrs.switchLabel ? getSwitchAttrValue('switchLabel') : getSwitchAttrValue('switchIcon'),
wrapperClass: getSwitchAttrValue('switchWrapper'),
handleWidth: getSwitchAttrValue('switchHandleWidth'),
labelWidth: getSwitchAttrValue('switchLabelWidth'),
inverse: getSwitchAttrValue('switchInverse'),
readonly: getSwitchAttrValue('switchReadonly')
});
if (attrs.type === 'radio') {
controller.$setViewValue(controller.$modelValue);
} else {
controller.$setViewValue(viewValue);
}
}
};
/**
* Listen to model changes.
*/
var listenToModel = function () {
attrs.$observe('switchActive', function (newValue) {
var active = getBooleanFromStringDefTrue(newValue);
// if we are disabling the switch, delay the deactivation so that the toggle can be switched
if (!active) {
$timeout(function () {
setActive(active);
});
} else {
// if we are enabling the switch, set active right away
setActive(active);
}
});
function modelValue() {
return controller.$modelValue;
}
// When the model changes
scope.$watch(modelValue, function (newValue) {
initMaybe();
if (newValue !== undefined) {
element.bootstrapSwitch('state', newValue === getTrueValue(), false);
}
}, true);
// angular attribute to switch property bindings
var bindings = {
'switchRadioOff': 'radioAllOff',
'switchOnText': 'onText',
'switchOffText': 'offText',
'switchOnColor': 'onColor',
'switchOffColor': 'offColor',
'switchAnimate': 'animate',
'switchSize': 'size',
'switchLabel': 'labelText',
'switchIcon': 'labelText',
'switchWrapper': 'wrapperClass',
'switchHandleWidth': 'handleWidth',
'switchLabelWidth': 'labelWidth',
'switchInverse': 'inverse',
'switchReadonly': 'readonly'
};
var observeProp = function (prop, bindings) {
return function () {
attrs.$observe(prop, function () {
setSwitchParamMaybe(element, bindings[prop], prop);
});
};
};
// for every angular-bound attribute, observe it and trigger the appropriate switch function
for (var prop in bindings) {
attrs.$observe(prop, observeProp(prop, bindings));
}
};
/**
* Listen to view changes.
*/
var listenToView = function () {
if (attrs.type === 'radio') {
// when the switch is clicked
element.on('change.bootstrapSwitch', function (e) {
// discard not real change events
if (controller.$modelValue === controller.$viewValue && e.target.checked !== $(e.target).bootstrapSwitch('state')) {
// $setViewValue --> $viewValue --> $parsers --> $modelValue
// if the switch is indeed selected
if (e.target.checked) {
// set its value into the view
controller.$setViewValue(getTrueValue());
} else if (getTrueValue() === controller.$viewValue) {
// otherwise if it's been deselected, delete the view value
controller.$setViewValue(undefined);
}
}
});
} else {
// When the checkbox switch is clicked, set its value into the ngModel
element.on('switchChange.bootstrapSwitch', function (e) {
// $setViewValue --> $viewValue --> $parsers --> $modelValue
controller.$setViewValue(e.target.checked);
});
}
};
// Listen and respond to view changes
listenToView();
// Listen and respond to model changes
listenToModel();
// On destroy, collect ya garbage
scope.$on('$destroy', function () {
element.bootstrapSwitch('destroy');
});
}
};
}
]).directive('bsSwitch', function () {
return {
restrict: 'E',
require: 'ngModel',
template: '<input bs-switch>',
replace: true
};
});
// Source: bsSwitch.suffix
})();