forked from seiyria/angular-bootstrap-slider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider.js
86 lines (78 loc) · 2.86 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
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
angular.module('ui.bootstrap-slider', [])
.directive('slider', ['$parse', '$timeout', function ($parse, $timeout) {
return {
restrict: 'AE',
replace: true,
template: '<input type="text" />',
link: function ($scope, element, attrs) {
var model = $parse(attrs.ngModel);
$.fn.slider.Constructor.prototype.disable = function () {
this.picker.off();
}
$.fn.slider.Constructor.prototype.enable = function () {
if (this.touchCapable) {
// Touch: Bind touch events:
this.picker.on({
touchstart: $.proxy(this.mousedown, this)
});
} else {
this.picker.on({
mousedown: $.proxy(this.mousedown, this)
});
}
}
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(attrs.formater) options.formater = $scope.$eval(attrs.formater);
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);
var updateEvent = attrs.updateEvent || 'slide';
slider.on(updateEvent, function(ev) {
model.assign($scope, ev.value);
$timeout(function() {
$scope.$apply();
});
});
$scope.$watch(attrs.ngModel, function(value) {
if(value || value === 0) {
slider.slider('setValue', value, false);
}
});
$scope.$watch(attrs.ngDisabled, function (value) {
if (value) {
slider.slider('disable');
} else {
slider.slider('enable');
}
});
}
};
}])
;