forked from danielcrisp/angular-rangeslider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.rangeSlider.js
453 lines (358 loc) · 17.6 KB
/
angular.rangeSlider.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
* Angular RangeSlider Directive
*
* Version: 0.0.3
*
* Author: Daniel Crisp, danielcrisp.com
*
* The rangeSlider has been styled to match the default styling
* of form elements styled using Twitter's Bootstrap
*
* Originally forked from https://github.com/leongersen/noUiSlider
*
*/
(function () {
'use strict';
/**
* RangeSlider, allows user to define a range of values using a slider
* Touch friendly.
* @directive
*/
angular.module('ui-rangeSlider', [])
.directive('rangeSlider', function($document, $filter, $log) {
// test for mouse, pointer or touch
var EVENT = window.navigator.msPointerEnabled ? 2 : 'ontouchend' in document ? 3 : 1,
eventNamespace = '.rangeSlider',
defaults = {
disabled: false,
orientation: 'horizontal',
step: 0,
decimalPlaces: 0,
showValues: true
},
onEvent = (EVENT === 1 ? 'mousedown' : EVENT === 2 ? 'MSPointerDown' : 'touchstart') + eventNamespace + 'X',
moveEvent = (EVENT === 1 ? 'mousemove' : EVENT === 2 ? 'MSPointerMove' : 'touchmove') + eventNamespace,
offEvent = (EVENT === 1 ? 'mouseup' : EVENT === 2 ? 'MSPointerUp' : 'touchend') + eventNamespace,
// get standarised clientX and clientY
client = function (f) {
try {
return [(f.clientX || f.originalEvent.clientX || f.originalEvent.touches[0].clientX), (f.clientY || f.originalEvent.clientY || f.originalEvent.touches[0].clientY)];
} catch (e) {
return ['x', 'y'];
}
},
restrict = function (value) {
// normalize so it can't move out of bounds
return (value < 0 ? 0 : (value > 100 ? 100 : value));
},
isNumber = function (n) {
// console.log(n);
return !isNaN(parseFloat(n)) && isFinite(n);
};
return {
restrict: 'A',
replace: true,
template: ['<div class="range-slider">',
'<div class="runner">',
'<div class="handle handle-min"><i></i></div>',
'<div class="handle handle-max"><i></i></div>',
'<div class="join"></div>',
'</div>',
'<div class="value value-min" ng-show="showValues">{{filteredModelMin}}</div>',
'<div class="value value-max" ng-show="showValues">{{filteredModelMax}}</div>',
'</div>'].join(''),
scope: {
disabled: '=?',
min: '=',
max: '=',
modelMin: '=?',
modelMax: '=?',
orientation: '@', // options: horizontal | vertical | vertical left | vertical right
step: '@',
decimalPlaces: '@',
filter: '@',
filterOptions: '@',
showValues: '@',
pinHandle: '@'
},
link: function(scope, element, attrs, controller) {
/**
* FIND ELEMENTS
*/
var $slider = angular.element(element),
handles = [element.find('.handle-min'), element.find('.handle-max')],
join = element.find('.join'),
pos = 'left',
posOpp = 'right',
orientation = 0,
allowedRange = [0, 0],
range = 0;
// filtered
scope.filteredModelMin = scope.modelMin;
scope.filteredModelMax = scope.modelMax;
/**
* FALL BACK TO DEFAULTS FOR SOME ATTRIBUTES
*/
attrs.$observe('disabled', function (val) {
if (!angular.isDefined(val)) {
scope.disabled = defaults.disabled;
}
scope.$watch('disabled', setDisabledStatus);
});
attrs.$observe('orientation', function (val) {
if (!angular.isDefined(val)) {
scope.orientation = defaults.orientation;
}
// add class to element
$slider.addClass(scope.orientation);
// update pos
if (scope.orientation === 'vertical' || scope.orientation === 'vertical left' || scope.orientation === 'vertical right') {
pos = 'top';
posOpp = 'bottom';
orientation = 1;
}
});
attrs.$observe('step', function (val) {
if (!angular.isDefined(val)) {
scope.step = defaults.step;
}
});
attrs.$observe('decimalPlaces', function (val) {
if (!angular.isDefined(val)) {
scope.decimalPlaces = defaults.decimalPlaces;
}
});
attrs.$observe('showValues', function (val) {
if (!angular.isDefined(val)) {
scope.showValues = defaults.showValues;
} else {
if (val === 'false') {
scope.showValues = false;
} else {
scope.showValues = true;
}
}
});
attrs.$observe('pinHandle', function (val) {
if (!angular.isDefined(val)) {
scope.pinHandle = null;
} else {
if (val === "min" || val === "max") {
scope.pinHandle = val;
} else {
scope.pinHandle = null;
}
}
scope.$watch('pinHandle', setPinHandle);
});
// listen for changes to values
scope.$watch('min', setMinMax);
scope.$watch('max', setMinMax);
scope.$watch('modelMin', setModelMinMax);
scope.$watch('modelMax', setModelMinMax);
/**
* HANDLE CHANGES
*/
function setPinHandle (status) {
if (status === "min") {
angular.element(handles[0]).hide();
angular.element(handles[1]).show();
} else if (status === "max") {
angular.element(handles[0]).show();
angular.element(handles[1]).hide();
} else {
angular.element(handles[0]).show();
angular.element(handles[1]).show();
}
}
function setDisabledStatus (status) {
if (status) {
$slider.addClass('disabled');
} else {
$slider.removeClass('disabled');
}
}
function setMinMax () {
if (scope.min > scope.max) {
throwError("min must be less than or equal to max");
}
// only do stuff when both values are ready
if (angular.isDefined(scope.min) && angular.isDefined(scope.max)) {
// make sure they are numbers
if (!isNumber(scope.min)) {
throwError("min must be a number");
}
if (!isNumber(scope.max)) {
throwError("max must be a number");
}
range = scope.max - scope.min;
allowedRange = [scope.min, scope.max];
// update models too
setModelMinMax();
}
}
function setModelMinMax () {
if (scope.modelMin > scope.modelMax) {
throwWarning("modelMin must be less than or equal to modelMax");
// reset values to correct
scope.modelMin = scope.modelMax;
}
// only do stuff when both values are ready
if (
(angular.isDefined(scope.modelMin) || scope.pinHandle === "min") &&
(angular.isDefined(scope.modelMax) || scope.pinHandle === "max")
) {
// make sure they are numbers
if (!isNumber(scope.modelMin)) {
if (scope.pinHandle !== "min") {
throwWarning("modelMin must be a number");
}
scope.modelMin = scope.min;
}
if (!isNumber(scope.modelMax)) {
if (scope.pinHandle !== "max") {
throwWarning("modelMax must be a number");
}
scope.modelMax = scope.max;
}
var handle1pos = restrict(((scope.modelMin - scope.min) / range) * 100),
handle2pos = restrict(((scope.modelMax - scope.min) / range) * 100);
// make sure the model values are within the allowed range
scope.modelMin = Math.max(scope.min, scope.modelMin);
scope.modelMax = Math.min(scope.max, scope.modelMax);
if (scope.filter) {
scope.filteredModelMin = $filter(scope.filter)(scope.modelMin, scope.filterOptions);
scope.filteredModelMax = $filter(scope.filter)(scope.modelMax, scope.filterOptions);
} else {
scope.filteredModelMin = scope.modelMin;
scope.filteredModelMax = scope.modelMax;
}
// check for no range
if (scope.min === scope.max && scope.modelMin == scope.modelMax) {
// reposition handles
angular.element(handles[0]).css(pos, '0%');
angular.element(handles[1]).css(pos, '100%');
// reposition join
angular.element(join).css(pos, '0%').css(posOpp, '0%');
} else {
// reposition handles
angular.element(handles[0]).css(pos, handle1pos + '%');
angular.element(handles[1]).css(pos, handle2pos + '%');
// reposition join
angular.element(join).css(pos, handle1pos + '%').css(posOpp, (100 - handle2pos) + '%');
}
}
}
function handleMove(index) {
var $handle = handles[index];
// on mousedown / touchstart
$handle.bind(onEvent, function (event) {
var handleDownClass = (index === 0 ? 'handle-min' : 'handle-max') + '-down',
unbind = $handle.add($document).add('body'),
modelValue = (index === 0 ? scope.modelMin : scope.modelMax) - scope.min,
originalPosition = (modelValue / range) * 100,
originalClick = client(event),
previousClick = originalClick,
previousProposal = false;
// stop user accidentally selecting stuff
angular.element('body').bind('selectstart' + eventNamespace, function () {
return false;
});
// only do stuff if we are disabled
if (!scope.disabled) {
// add down class
$handle.addClass('down');
$slider.addClass('focus ' + handleDownClass);
// add touch class for MS styling
angular.element('body').addClass('TOUCH');
// listen for mousemove / touchmove document events
$document.bind(moveEvent, function (e) {
// prevent default
e.preventDefault();
var currentClick = client(e),
movement,
proposal,
other,
per,
otherModelPosition = (((index === 0 ? scope.modelMax : scope.modelMin) - scope.min) / range) * 100;
if (currentClick[0] === "x") {
return;
}
// calculate deltas
currentClick[0] -= originalClick[0];
currentClick[1] -= originalClick[1];
// has movement occurred on either axis?
movement = [
(previousClick[0] !== currentClick[0]), (previousClick[1] !== currentClick[1])
];
// propose a movement
proposal = originalPosition + ((currentClick[orientation] * 100) / (orientation ? $slider.height() : $slider.width()));
// normalize so it can't move out of bounds
proposal = restrict(proposal);
// check which handle is being moved and add / remove margin
if (index === 0) {
proposal = proposal > otherModelPosition ? otherModelPosition : proposal;
} else if (index === 1) {
proposal = proposal < otherModelPosition ? otherModelPosition : proposal;
}
if (scope.step > 0) {
// only change if we are within the extremes, otherwise we get strange rounding
if (proposal < 100 && proposal > 0) {
per = (scope.step / range) * 100;
proposal = Math.round(proposal / per) * per;
}
}
if (proposal > 95 && index === 0) {
$handle.css('z-index', '3');
} else {
$handle.css('z-index', '');
}
if (movement[orientation] && proposal != previousProposal) {
if (index === 0) {
// update model as we slide
scope.modelMin = parseFloat((((proposal * range) / 100) + scope.min)).toFixed(scope.decimalPlaces);
} else if (index === 1) {
scope.modelMax = parseFloat((((proposal * range) / 100) + scope.min)).toFixed(scope.decimalPlaces);
}
// update angular
scope.$apply();
previousProposal = proposal;
}
previousClick = currentClick;
}).bind(offEvent, function () {
unbind.off(eventNamespace);
angular.element('body').removeClass('TOUCH');
// remove down class
$handle.removeClass('down');
// remove active class
$slider.removeClass('focus ' + handleDownClass);
});
}
});
}
function throwError (message) {
scope.disabled = true;
throw new Error("RangeSlider: " + message);
}
function throwWarning (message) {
$log.warn(message);
}
/**
* INIT
*/
$slider
// disable selection
.bind('selectstart' + eventNamespace, function (event) {
return false;
})
// stop propagation
.bind('click', function (event) {
event.stopPropagation();
});
// bind events to each handle
handleMove(0);
handleMove(1);
}
};
});
}());