forked from seiyria/angular-bootstrap-slider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslider.js
60 lines (54 loc) · 2.11 KB
/
slider.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
angular.module('ui.bootstrap-slider', [])
.directive('slider', function ($parse, $timeout) {
return {
restrict: 'AE',
replace: true,
template: '<input type="text" />',
link: function ($scope, element, attrs) {
var model = $parse(attrs.ngModel);
var options = {};
if(attrs.sliderId) options.id = attrs.sliderId;
if(attrs.min) options.min = parseFloat(attrs.min);
if(attrs.max) options.max = parseFloat(attrs.max);
if(attrs.step) options.step = parseFloat(attrs.step);
if(attrs.precision) options.precision = parseFloat(attrs.precision);
if(attrs.orientation) options.orientation = attrs.orientation;
if(attrs.value) {
if (angular.isNumber(attrs.value) || angular.isArray(attrs.value)) {
options.value = attrs.value;
} else if (angular.isString(attrs.value)) {
if (attrs.value.indexOf("[") === 0) {
options.value = angular.fromJson(attrs.value);
} else {
options.value = parseFloat(attrs.value);
}
}
}
if(attrs.range) options.range = attrs.range === 'true';
if(attrs.selection) options.selection = attrs.selection;
if(attrs.tooltip) options.tooltip = attrs.tooltip;
if(attrs.tooltipSeparator) options.tooltip_separator = attrs.tooltipSeparator;
if(attrs.tooltipSplit) options.tooltip_split = attrs.tooltipSplit === 'true';
if(attrs.handle) options.handle = attrs.handle;
if(attrs.reversed) options.reversed = attrs.reversed === 'true';
if(attrs.enabled) options.enabled = attrs.enabled === 'true';
if(attrs.naturalArrowKeys) options.natural_arrow_keys = attrs.naturalArrowKeys === 'true';
if (options.range && !options.value) {
options.value = [0,0]; // This is needed, because of value defined at $.fn.slider.defaults - default value 5 prevents creating range slider
}
var slider = $(element[0]).slider(options);
slider.on('slide', function(ev) {
model.assign($scope, ev.value);
$timeout(function() {
$scope.$apply();
});
});
$scope.$watch(attrs.ngModel, function(value) {
if(value) {
slider.slider('setValue', value, false);
}
});
}
}
})
;