forked from SC5/nouislider-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnouislider.js
124 lines (116 loc) · 4.41 KB
/
nouislider.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
angular.module('ya.nouislider', []).value('noUiSliderConfig', {}).directive('noUiSlider', function(noUiSliderConfig, $timeout) {
noUiSliderConfig = noUiSliderConfig || {}
function handlesCount(value) {
if (angular.isUndefined(value)) return 0
return angular.isArray(value) && value.length == 2 ? 2 : 1
}
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
return {
restrict: 'A',
require: 'ngModel',
scope: {
ngDisabled: '=',
noUiSlider: '=',
noUiSliderLib: '=',
noUiSliderEvents: '=',
noUiSliderDebounce: '@',
noUiSliderTrigger: '@'
},
link: function(scope, elements, attrs, ngModel) {
var initialized = false,
previousValue = undefined,
debouncedSet = undefined,
element = elements[0];
scope.$on('$destroy', function() {
element.noUiSlider.off('slide set change update');
});
function tryToInit() {
var value = ngModel.$viewValue,
options = angular.extend({}, noUiSliderConfig, scope.noUiSlider, {start: value}),
noUiSlider = scope.noUiSliderLib ? scope.noUiSliderLib : window.noUiSlider;
if (angular.isDefined(options.start) && angular.isDefined(options.range)) {
previousValue = angular.copy(value);
if (!initialized) {
noUiSlider.create(element, options);
angular.forEach(scope.noUiSliderEvents, function(handler, event) {
element.noUiSlider.on(event, handler);
});
element.noUiSlider.on((scope.noUiSliderTrigger || 'update'), function(value) {
// NOTE: Reading the value using .get() because handler always returns an array
value = element.noUiSlider.get();
var valueIsArray = angular.isArray(value);
if (valueIsArray) value = value[0];
value = parseFloat(value) || 0;
value = Math.min(Math.max(value, scope.noUiSlider.range.min), scope.noUiSlider.range.max);
newValue = valueIsArray ? [value] : value;
ngModel.$setViewValue(newValue);
});
// Disable the field per request
element.removeAttribute('disabled');
if (scope.ngDisabled) {
element.setAttribute('disabled', true);
}
}
debouncedSet = debounce(element.noUiSlider.set, (scope.noUiSliderDebounce ? parseInt(scope.noUiSliderDebounce) : 0));
initialized = true;
}
}
ngModel.$render = function() {
if (!initialized) return
var value = ngModel.$viewValue,
newValue = undefined
if (handlesCount(value) == 2) {
value[0] = Math.max(value[0], scope.noUiSlider.range.min)
value[1] = Math.min(value[1], scope.noUiSlider.range.max)
var fromNotChanged = value[0] == previousValue[0],
toNotChanged = value[1] == previousValue[1]
previousValue = angular.copy(value)
if (value[0] > value[1]) {
if (fromNotChanged) value[1] = value[0]
if (toNotChanged) value[0] = value[1]
if (value[0] > value[1]) value[1] = value[0]
}
newValue = [fromNotChanged ? null : value[0], toNotChanged ? null : value[1]]
} else {
var valueIsArray = angular.isArray(value)
if (valueIsArray) value = value[0]
value = parseFloat(value) || 0
value = Math.min(Math.max(value, scope.noUiSlider.range.min), scope.noUiSlider.range.max)
newValue = valueIsArray ? [value] : value
ngModel.$setViewValue(newValue);
}
debouncedSet(newValue);
}
scope.$watch(function() {
return scope.noUiSlider
}, function() {
tryToInit()
}, true)
scope.$watch(function() {
return ngModel.$viewValue
}, function() {
ngModel.$render()
}, true)
scope.$watch(function() {
return handlesCount(ngModel.$viewValue)
}, function(newValue) {
if (angular.isDefined(newValue)) {
tryToInit()
}
}, true)
}
}
})