-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslider.js
381 lines (360 loc) · 15.5 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
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
// Generated by CoffeeScript 1.7.1
(function() {
var MODULE_NAME, SLIDER_TAG, angularize, gap, halfWidth, hide, inputEvents, module, offset, offsetLeft, pixelize, qualifiedDirectiveDefinition, roundStep, show, sliderDirective, width;
MODULE_NAME = 'ui.slider';
SLIDER_TAG = 'slider';
angularize = function(element) {
return angular.element(element);
};
pixelize = function(position) {
return "" + position + "px";
};
hide = function(element) {
return element.css({
opacity: 0
});
};
show = function(element) {
return element.css({
opacity: 1
});
};
offset = function(element, position) {
return element.css({
left: position
});
};
halfWidth = function(element) {
return element[0].offsetWidth / 2;
};
offsetLeft = function(element) {
return element[0].offsetLeft;
};
width = function(element) {
return element[0].offsetWidth;
};
gap = function(element1, element2) {
return offsetLeft(element2) - offsetLeft(element1) - width(element1);
};
roundStep = function(value, precision, step, floor) {
var decimals, remainder, roundedValue, steppedValue;
if (floor == null) {
floor = 0;
}
if (step == null) {
step = 1 / Math.pow(10, precision);
}
remainder = (value - floor) % step;
steppedValue = remainder > (step / 2) ? value + step - remainder : value - remainder;
decimals = Math.pow(10, precision);
roundedValue = steppedValue * decimals / decimals;
return roundedValue.toFixed(precision);
};
inputEvents = {
mouse: {
start: 'mousedown',
move: 'mousemove',
end: 'mouseup'
},
touch: {
start: 'touchstart',
move: 'touchmove',
end: 'touchend'
}
};
sliderDirective = function($timeout) {
return {
restrict: 'E',
scope: {
floor: '@',
ceiling: '@',
values: '=?',
range: '@',
step: '@',
highlight: '@',
precision: '@',
buffer: '@',
dragstop: '@',
enableSlider: '@',
showHandle: '@',
sliderControl: '=',
ngModel: '=?',
ngModelLow: '=?',
ngModelHigh: '=?'
},
template: '<div class="bar"><div class="selection"></div></div>\n<div class="handle low" ng-hide="(!enableSlider && !showHandle)"></div><div class="handle high" ng-hide="(!enableSlider && !showHandle)"></div>\n<div class="bubble limit low">{{ values.length ? ( values[floor || 0] || floor ) : floor }}</div>\n<div class="bubble limit high">{{ values.length ? ( values[ceiling || values.length - 1] || ceiling ) : ceiling }}</div>\n<div class="bubble value low">{{ values.length ? ( values[local.ngModelLow || local.ngModel] || local.ngModelLow || local.ngModel ) : local.ngModelLow || local.ngModel}}</div>\n<div class="bubble value high">{{ values.length ? ( values[local.ngModelHigh] || local.ngModelHigh ) : local.ngModelHigh }}</div>',
compile: function(element, attributes) {
var bar, ceilBub, e, flrBub, high, highBub, low, lowBub, maxPtr, minPtr, range, selection, watchables, _i, _len, _ref, _ref1;
range = (attributes.ngModel == null) && (attributes.ngModelLow != null) && (attributes.ngModelHigh != null);
_ref = (function() {
var _i, _len, _ref, _results;
_ref = element.children();
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
e = _ref[_i];
_results.push(angularize(e));
}
return _results;
})(), bar = _ref[0], minPtr = _ref[1], maxPtr = _ref[2], flrBub = _ref[3], ceilBub = _ref[4], lowBub = _ref[5], highBub = _ref[6];
selection = angularize(bar.children()[0]);
if (!range) {
_ref1 = [maxPtr, highBub];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
element = _ref1[_i];
element.remove();
}
if (!attributes.highlight) {
selection.remove();
}
//if (scope.enableSlider && !scope.showHandle) {
//minPtr.css('display', 'none');
//maxPtr.css('display', 'none');
//}
}
low = range ? 'ngModelLow' : 'ngModel';
high = 'ngModelHigh';
watchables = ['floor', 'ceiling', 'values', low];
if (range) {
watchables.push(high);
}
return {
post: function(scope, element, attributes) {
var barWidth, boundToInputs, dimensions, handleHalfWidth, maxOffset, maxValue, minOffset, minValue, ngDocument, offsetRange, updateDOM, valueRange, w, _j, _len1s;
scope.local = {};
scope.local[low] = scope[low];
scope.local[high] = scope[high];
boundToInputs = false;
ngDocument = angularize(document);
handleHalfWidth = barWidth = minOffset = maxOffset = minValue = maxValue = valueRange = offsetRange = void 0;
dimensions = function() {
var value, _j, _len1, _ref2;
if (scope.step == null) {
scope.step = 1;
}
if (scope.floor == null) {
scope.floor = 0;
}
if (scope.precision == null) {
scope.precision = 0;
}
if (scope.enableSlider == null) {
scope.enableSlider = true;
}
else {
scope.enableSlider = (scope.enableSlider == 'true' || (typeof scope.enableSlider === 'boolean' && scope.enableSlider)) ? true : false;
}
attributes.$observe('enableSlider', function(value) {
scope.enableSlider = (value == 'true' || (typeof value === 'boolean' && value)) ? true : false;
});
if (scope.showHandle == null) {
scope.showHandle = false;
}
else {
scope.showHandle = ((typeof scope.showHandle === 'boolean' && !scope.showHandle) || scope.showHandle == 'false') ? false : true;
}
attributes.$observe('showHandle', function(value) {
scope.showHandle = ((typeof value === 'boolean' && !value) || value == 'false') ? false : true;
});
if (angular.isDefined(scope.sliderControl)) {
scope.internalSliderControl = scope.sliderControl;
scope.internalSliderControl.updateSlider = function() {
updateDOM();
};
}
if (!range) {
scope.ngModelLow = scope.ngModel;
}
if ((_ref2 = scope.values) != null ? _ref2.length : void 0) {
if (scope.ceiling == null) {
scope.ceiling = scope.values.length - 1;
}
}
scope.local[low] = scope[low];
scope.local[high] = scope[high];
for (_j = 0, _len1 = watchables.length; _j < _len1; _j++) {
value = watchables[_j];
if (typeof value === 'number') {
scope[value] = roundStep(parseFloat(scope[value]), parseInt(scope.precision), parseFloat(scope.step), parseFloat(scope.floor));
}
}
handleHalfWidth = halfWidth(minPtr);
barWidth = width(bar);
minOffset = 0;
maxOffset = barWidth - width(minPtr);
minValue = parseFloat(scope.floor);
maxValue = parseFloat(scope.ceiling);
valueRange = maxValue - minValue;
return offsetRange = maxOffset - minOffset;
};
updateDOM = function() {
var bindToInputEvents, fitToBar, percentOffset, percentToOffset, percentValue, setBindings, setPointers;
dimensions();
percentOffset = function(offset) {
return ((offset - minOffset) / offsetRange) * 100;
};
percentValue = function(value) {
return ((value - minValue) / valueRange) * 100;
};
percentToOffset = function(percent) {
return pixelize((percent * (offsetRange-3.5) / 100));
};
fitToBar = function(element) {
return offset(element, pixelize(Math.min(Math.max(0, offsetLeft(element)), barWidth - width(element))));
};
setPointers = function() {
var newHighValue, newLowValue;
offset(ceilBub, pixelize(barWidth - width(ceilBub)));
newLowValue = percentValue(scope.local[low]);
offset(minPtr, percentToOffset(newLowValue));
offset(lowBub, pixelize(offsetLeft(minPtr) - (halfWidth(lowBub)) + handleHalfWidth));
offset(selection, pixelize(offsetLeft(minPtr) + handleHalfWidth));
switch (true) {
case range:
newHighValue = percentValue(scope.local[high]);
offset(maxPtr, percentToOffset(newHighValue));
offset(highBub, pixelize(offsetLeft(maxPtr) - (halfWidth(highBub)) + handleHalfWidth));
return selection.css({
width: percentToOffset(newHighValue - newLowValue)
});
case attributes.highlight === 'right':
return selection.css({
width: percentToOffset(110 - newLowValue)
});
case attributes.highlight === 'left':
// To fill the gap between handle and selection
selection.css({
width: percentToOffset(newLowValue + (handleHalfWidth / 10))
});
return offset(selection, 0);
}
};
bindToInputEvents = function(handle, bubble, ref, events) {
var currentRef, onEnd, onMove, onStart;
currentRef = ref;
onEnd = function() {
bubble.removeClass('active');
handle.removeClass('active');
ngDocument.unbind(events.move);
ngDocument.unbind(events.end);
if (scope.dragstop) {
scope[high] = scope.local[high];
scope[low] = scope.local[low];
}
return currentRef = ref;
};
onMove = function(event) {
var eventX, newOffset, newPercent, newValue;
eventX = event.clientX || event.touches[0].clientX;
newOffset = eventX - element[0].getBoundingClientRect().left - handleHalfWidth;
newOffset = Math.max(Math.min(newOffset, maxOffset), minOffset);
newPercent = percentOffset(newOffset);
newValue = minValue + (valueRange * newPercent / 100.0);
if (range) {
switch (currentRef) {
case low:
if (newValue > scope.local[high]) {
currentRef = high;
minPtr.removeClass('active');
lowBub.removeClass('active');
maxPtr.addClass('active');
highBub.addClass('active');
setPointers();
} else if (scope.buffer > 0) {
newValue = Math.min(newValue, scope.local[high] - scope.buffer);
}
break;
case high:
if (newValue < scope.local[low]) {
currentRef = low;
maxPtr.removeClass('active');
highBub.removeClass('active');
minPtr.addClass('active');
lowBub.addClass('active');
setPointers();
} else if (scope.buffer > 0) {
newValue = Math.max(newValue, parseInt(scope.local[low]) + parseInt(scope.buffer));
}
}
}
newValue = roundStep(newValue, parseInt(scope.precision), parseFloat(scope.step), parseFloat(scope.floor));
scope.local[currentRef] = newValue;
if (!scope.dragstop) {
scope[currentRef] = newValue;
}
setPointers();
return scope.$apply();
};
onStart = function(event) {
if (!scope.enableSlider) { return; }
dimensions();
bubble.addClass('active');
handle.addClass('active');
setPointers();
event.stopPropagation();
event.preventDefault();
ngDocument.bind(events.move, onMove);
return ngDocument.bind(events.end, onEnd);
};
return handle.bind(events.start, onStart);
};
// Added this function to let the user to adjust the slider value
// by clicking on the bar itself. This method will only work with
// value slider not with the range slider.
bindClickEvents = function() {
var onClick;
onClick = function(event) {
if (!scope.enableSlider) { return; }
var eventX, newOffset, newPercent, newValue;
eventX = event.clientX || event.touches[0].clientX;
newOffset = eventX - element[0].getBoundingClientRect().left - handleHalfWidth;
newOffset = Math.max(Math.min(newOffset, maxOffset), minOffset);
newPercent = percentOffset(newOffset);
newValue = minValue + (valueRange * newPercent / 100.0);
newValue = roundStep(newValue, parseInt(scope.precision), parseFloat(scope.step), parseFloat(scope.floor));
scope.local[low] = newValue;
if (!scope.dragstop) {
scope[low] = newValue;
}
setPointers();
return scope.$apply();
}
return bar.bind('click', onClick);
};
setBindings = function() {
var bind, inputMethod, _j, _len1, _ref2, _results;
boundToInputs = true;
bind = function(method) {
bindToInputEvents(minPtr, lowBub, low, inputEvents[method]);
return bindToInputEvents(maxPtr, highBub, high, inputEvents[method]);
};
_ref2 = ['touch', 'mouse'];
_results = [];
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
inputMethod = _ref2[_j];
_results.push(bind(inputMethod));
}
_results.push(bindClickEvents());
return _results;
};
if (!boundToInputs && scope.enableSlider) {
setBindings();
}
return setPointers();
};
$timeout(updateDOM);
for (_j = 0, _len1 = watchables.length; _j < _len1; _j++) {
w = watchables[_j];
scope.$watch(w, updateDOM, true);
}
return window.addEventListener("resize", updateDOM);
}
};
}
};
};
qualifiedDirectiveDefinition = ['$timeout', sliderDirective];
module = function(window, angular) {
return angular.module(MODULE_NAME, []).directive(SLIDER_TAG, qualifiedDirectiveDefinition);
};
module(window, window.angular);
}).call(this);