1 | 'use strict';
|
2 |
|
3 | import $ from 'jquery';
|
4 | import { Keyboard } from './foundation.util.keyboard';
|
5 | import { Move } from './foundation.util.motion';
|
6 | import { GetYoDigits, rtl as Rtl } from './foundation.util.core';
|
7 |
|
8 | import { Plugin } from './foundation.plugin';
|
9 |
|
10 | import { Touch } from './foundation.util.touch';
|
11 |
|
12 | import { Triggers } from './foundation.util.triggers';
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | class Slider extends Plugin {
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | _setup(element, options) {
|
31 | this.$element = element;
|
32 | this.options = $.extend({}, Slider.defaults, this.$element.data(), options);
|
33 | this.className = 'Slider';
|
34 |
|
35 |
|
36 | Touch.init($);
|
37 | Triggers.init($);
|
38 |
|
39 | this._init();
|
40 |
|
41 | Keyboard.register('Slider', {
|
42 | 'ltr': {
|
43 | 'ARROW_RIGHT': 'increase',
|
44 | 'ARROW_UP': 'increase',
|
45 | 'ARROW_DOWN': 'decrease',
|
46 | 'ARROW_LEFT': 'decrease',
|
47 | 'SHIFT_ARROW_RIGHT': 'increase_fast',
|
48 | 'SHIFT_ARROW_UP': 'increase_fast',
|
49 | 'SHIFT_ARROW_DOWN': 'decrease_fast',
|
50 | 'SHIFT_ARROW_LEFT': 'decrease_fast',
|
51 | 'HOME': 'min',
|
52 | 'END': 'max'
|
53 | },
|
54 | 'rtl': {
|
55 | 'ARROW_LEFT': 'increase',
|
56 | 'ARROW_RIGHT': 'decrease',
|
57 | 'SHIFT_ARROW_LEFT': 'increase_fast',
|
58 | 'SHIFT_ARROW_RIGHT': 'decrease_fast'
|
59 | }
|
60 | });
|
61 | }
|
62 |
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 | _init() {
|
69 | this.inputs = this.$element.find('input');
|
70 | this.handles = this.$element.find('[data-slider-handle]');
|
71 |
|
72 | this.$handle = this.handles.eq(0);
|
73 | this.$input = this.inputs.length ? this.inputs.eq(0) : $(`#${this.$handle.attr('aria-controls')}`);
|
74 | this.$fill = this.$element.find('[data-slider-fill]').css(this.options.vertical ? 'height' : 'width', 0);
|
75 |
|
76 | var isDbl = false,
|
77 | _this = this;
|
78 | if (this.options.disabled || this.$element.hasClass(this.options.disabledClass)) {
|
79 | this.options.disabled = true;
|
80 | this.$element.addClass(this.options.disabledClass);
|
81 | }
|
82 | if (!this.inputs.length) {
|
83 | this.inputs = $().add(this.$input);
|
84 | this.options.binding = true;
|
85 | }
|
86 |
|
87 | this._setInitAttr(0);
|
88 |
|
89 | if (this.handles[1]) {
|
90 | this.options.doubleSided = true;
|
91 | this.$handle2 = this.handles.eq(1);
|
92 | this.$input2 = this.inputs.length > 1 ? this.inputs.eq(1) : $(`#${this.$handle2.attr('aria-controls')}`);
|
93 |
|
94 | if (!this.inputs[1]) {
|
95 | this.inputs = this.inputs.add(this.$input2);
|
96 | }
|
97 | isDbl = true;
|
98 |
|
99 |
|
100 | this._setInitAttr(1);
|
101 | }
|
102 |
|
103 |
|
104 | this.setHandles();
|
105 |
|
106 | this._events();
|
107 | }
|
108 |
|
109 | setHandles() {
|
110 | if(this.handles[1]) {
|
111 | this._setHandlePos(this.$handle, this.inputs.eq(0).val(), true, () => {
|
112 | this._setHandlePos(this.$handle2, this.inputs.eq(1).val(), true);
|
113 | });
|
114 | } else {
|
115 | this._setHandlePos(this.$handle, this.inputs.eq(0).val(), true);
|
116 | }
|
117 | }
|
118 |
|
119 | _reflow() {
|
120 | this.setHandles();
|
121 | }
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 | _pctOfBar(value) {
|
128 | var pctOfBar = percent(value - this.options.start, this.options.end - this.options.start)
|
129 |
|
130 | switch(this.options.positionValueFunction) {
|
131 | case "pow":
|
132 | pctOfBar = this._logTransform(pctOfBar);
|
133 | break;
|
134 | case "log":
|
135 | pctOfBar = this._powTransform(pctOfBar);
|
136 | break;
|
137 | }
|
138 |
|
139 | return pctOfBar.toFixed(2)
|
140 | }
|
141 |
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 | _value(pctOfBar) {
|
148 | switch(this.options.positionValueFunction) {
|
149 | case "pow":
|
150 | pctOfBar = this._powTransform(pctOfBar);
|
151 | break;
|
152 | case "log":
|
153 | pctOfBar = this._logTransform(pctOfBar);
|
154 | break;
|
155 | }
|
156 | var value = (this.options.end - this.options.start) * pctOfBar + this.options.start;
|
157 |
|
158 | return value
|
159 | }
|
160 |
|
161 | |
162 |
|
163 |
|
164 |
|
165 |
|
166 | _logTransform(value) {
|
167 | return baseLog(this.options.nonLinearBase, ((value*(this.options.nonLinearBase-1))+1))
|
168 | }
|
169 |
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 | _powTransform(value) {
|
176 | return (Math.pow(this.options.nonLinearBase, value) - 1) / (this.options.nonLinearBase - 1)
|
177 | }
|
178 |
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | _setHandlePos($hndl, location, noInvert, cb) {
|
190 |
|
191 | if (this.$element.hasClass(this.options.disabledClass)) {
|
192 | return;
|
193 | }
|
194 |
|
195 | location = parseFloat(location);
|
196 |
|
197 |
|
198 | if (location < this.options.start) { location = this.options.start; }
|
199 | else if (location > this.options.end) { location = this.options.end; }
|
200 |
|
201 | var isDbl = this.options.doubleSided;
|
202 |
|
203 | if (isDbl) {
|
204 | if (this.handles.index($hndl) === 0) {
|
205 | var h2Val = parseFloat(this.$handle2.attr('aria-valuenow'));
|
206 | location = location >= h2Val ? h2Val - this.options.step : location;
|
207 | } else {
|
208 | var h1Val = parseFloat(this.$handle.attr('aria-valuenow'));
|
209 | location = location <= h1Val ? h1Val + this.options.step : location;
|
210 | }
|
211 | }
|
212 |
|
213 |
|
214 |
|
215 | if (this.options.vertical && !noInvert) {
|
216 | location = this.options.end - location;
|
217 | }
|
218 |
|
219 | var _this = this,
|
220 | vert = this.options.vertical,
|
221 | hOrW = vert ? 'height' : 'width',
|
222 | lOrT = vert ? 'top' : 'left',
|
223 | handleDim = $hndl[0].getBoundingClientRect()[hOrW],
|
224 | elemDim = this.$element[0].getBoundingClientRect()[hOrW],
|
225 |
|
226 | pctOfBar = this._pctOfBar(location),
|
227 |
|
228 | pxToMove = (elemDim - handleDim) * pctOfBar,
|
229 |
|
230 | movement = (percent(pxToMove, elemDim) * 100).toFixed(this.options.decimal);
|
231 |
|
232 | location = parseFloat(location.toFixed(this.options.decimal));
|
233 |
|
234 | var css = {};
|
235 |
|
236 | this._setValues($hndl, location);
|
237 |
|
238 |
|
239 | if (isDbl) {
|
240 | var isLeftHndl = this.handles.index($hndl) === 0,
|
241 |
|
242 | dim,
|
243 |
|
244 | handlePct = ~~(percent(handleDim, elemDim) * 100);
|
245 |
|
246 | if (isLeftHndl) {
|
247 |
|
248 | css[lOrT] = `${movement}%`;
|
249 |
|
250 | dim = parseFloat(this.$handle2[0].style[lOrT]) - movement + handlePct;
|
251 |
|
252 |
|
253 | if (cb && typeof cb === 'function') { cb(); }
|
254 | } else {
|
255 |
|
256 | var handlePos = parseFloat(this.$handle[0].style[lOrT]);
|
257 |
|
258 |
|
259 | dim = movement - (isNaN(handlePos) ? (this.options.initialStart - this.options.start)/((this.options.end-this.options.start)/100) : handlePos) + handlePct;
|
260 | }
|
261 |
|
262 | css[`min-${hOrW}`] = `${dim}%`;
|
263 | }
|
264 |
|
265 | this.$element.one('finished.zf.animate', function() {
|
266 | |
267 |
|
268 |
|
269 |
|
270 | _this.$element.trigger('moved.zf.slider', [$hndl]);
|
271 | });
|
272 |
|
273 |
|
274 | var moveTime = this.$element.data('dragging') ? 1000/60 : this.options.moveTime;
|
275 |
|
276 | Move(moveTime, $hndl, function() {
|
277 |
|
278 |
|
279 |
|
280 | if (isNaN(movement)) {
|
281 | $hndl.css(lOrT, `${pctOfBar * 100}%`);
|
282 | }
|
283 | else {
|
284 | $hndl.css(lOrT, `${movement}%`);
|
285 | }
|
286 |
|
287 | if (!_this.options.doubleSided) {
|
288 |
|
289 | _this.$fill.css(hOrW, `${pctOfBar * 100}%`);
|
290 | } else {
|
291 |
|
292 | _this.$fill.css(css);
|
293 | }
|
294 | });
|
295 |
|
296 |
|
297 | |
298 |
|
299 |
|
300 |
|
301 | clearTimeout(_this.timeout);
|
302 | _this.timeout = setTimeout(function(){
|
303 | _this.$element.trigger('changed.zf.slider', [$hndl]);
|
304 | }, _this.options.changedDelay);
|
305 | }
|
306 |
|
307 | |
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 | _setInitAttr(idx) {
|
314 | var initVal = (idx === 0 ? this.options.initialStart : this.options.initialEnd)
|
315 | var id = this.inputs.eq(idx).attr('id') || GetYoDigits(6, 'slider');
|
316 | this.inputs.eq(idx).attr({
|
317 | 'id': id,
|
318 | 'max': this.options.end,
|
319 | 'min': this.options.start,
|
320 | 'step': this.options.step
|
321 | });
|
322 | this.inputs.eq(idx).val(initVal);
|
323 | this.handles.eq(idx).attr({
|
324 | 'role': 'slider',
|
325 | 'aria-controls': id,
|
326 | 'aria-valuemax': this.options.end,
|
327 | 'aria-valuemin': this.options.start,
|
328 | 'aria-valuenow': initVal,
|
329 | 'aria-orientation': this.options.vertical ? 'vertical' : 'horizontal',
|
330 | 'tabindex': 0
|
331 | });
|
332 | }
|
333 |
|
334 | |
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 | _setValues($handle, val) {
|
342 | var idx = this.options.doubleSided ? this.handles.index($handle) : 0;
|
343 | this.inputs.eq(idx).val(val);
|
344 | $handle.attr('aria-valuenow', val);
|
345 | }
|
346 |
|
347 | |
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 | _handleEvent(e, $handle, val) {
|
359 | var value, hasVal;
|
360 | if (!val) {
|
361 | e.preventDefault();
|
362 | var _this = this,
|
363 | vertical = this.options.vertical,
|
364 | param = vertical ? 'height' : 'width',
|
365 | direction = vertical ? 'top' : 'left',
|
366 | eventOffset = vertical ? e.pageY : e.pageX,
|
367 | halfOfHandle = this.$handle[0].getBoundingClientRect()[param] / 2,
|
368 | barDim = this.$element[0].getBoundingClientRect()[param],
|
369 | windowScroll = vertical ? $(window).scrollTop() : $(window).scrollLeft();
|
370 |
|
371 |
|
372 | var elemOffset = this.$element.offset()[direction];
|
373 |
|
374 |
|
375 |
|
376 | if (e.clientY === e.pageY) { eventOffset = eventOffset + windowScroll; }
|
377 | var eventFromBar = eventOffset - elemOffset;
|
378 | var barXY;
|
379 | if (eventFromBar < 0) {
|
380 | barXY = 0;
|
381 | } else if (eventFromBar > barDim) {
|
382 | barXY = barDim;
|
383 | } else {
|
384 | barXY = eventFromBar;
|
385 | }
|
386 | var offsetPct = percent(barXY, barDim);
|
387 |
|
388 | value = this._value(offsetPct);
|
389 |
|
390 |
|
391 | if (Rtl() && !this.options.vertical) {value = this.options.end - value;}
|
392 |
|
393 | value = _this._adjustValue(null, value);
|
394 |
|
395 | hasVal = false;
|
396 |
|
397 | if (!$handle) {
|
398 | var firstHndlPos = absPosition(this.$handle, direction, barXY, param),
|
399 | secndHndlPos = absPosition(this.$handle2, direction, barXY, param);
|
400 | $handle = firstHndlPos <= secndHndlPos ? this.$handle : this.$handle2;
|
401 | }
|
402 |
|
403 | } else {
|
404 | value = this._adjustValue(null, val);
|
405 | hasVal = true;
|
406 | }
|
407 |
|
408 | this._setHandlePos($handle, value, hasVal);
|
409 | }
|
410 |
|
411 | |
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 | _adjustValue($handle, value) {
|
419 | var val,
|
420 | step = this.options.step,
|
421 | div = parseFloat(step/2),
|
422 | left, prev_val, next_val;
|
423 | if (!!$handle) {
|
424 | val = parseFloat($handle.attr('aria-valuenow'));
|
425 | }
|
426 | else {
|
427 | val = value;
|
428 | }
|
429 | left = val % step;
|
430 | prev_val = val - left;
|
431 | next_val = prev_val + step;
|
432 | if (left === 0) {
|
433 | return val;
|
434 | }
|
435 | val = val >= prev_val + div ? next_val : prev_val;
|
436 | return val;
|
437 | }
|
438 |
|
439 | |
440 |
|
441 |
|
442 |
|
443 |
|
444 | _events() {
|
445 | this._eventsForHandle(this.$handle);
|
446 | if(this.handles[1]) {
|
447 | this._eventsForHandle(this.$handle2);
|
448 | }
|
449 | }
|
450 |
|
451 |
|
452 | |
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 | _eventsForHandle($handle) {
|
459 | var _this = this,
|
460 | curHandle,
|
461 | timer;
|
462 |
|
463 | this.inputs.off('change.zf.slider').on('change.zf.slider', function(e) {
|
464 | var idx = _this.inputs.index($(this));
|
465 | _this._handleEvent(e, _this.handles.eq(idx), $(this).val());
|
466 | });
|
467 |
|
468 | if (this.options.clickSelect) {
|
469 | this.$element.off('click.zf.slider').on('click.zf.slider', function(e) {
|
470 | if (_this.$element.data('dragging')) { return false; }
|
471 |
|
472 | if (!$(e.target).is('[data-slider-handle]')) {
|
473 | if (_this.options.doubleSided) {
|
474 | _this._handleEvent(e);
|
475 | } else {
|
476 | _this._handleEvent(e, _this.$handle);
|
477 | }
|
478 | }
|
479 | });
|
480 | }
|
481 |
|
482 | if (this.options.draggable) {
|
483 | this.handles.addTouch();
|
484 |
|
485 | var $body = $('body');
|
486 | $handle
|
487 | .off('mousedown.zf.slider')
|
488 | .on('mousedown.zf.slider', function(e) {
|
489 | $handle.addClass('is-dragging');
|
490 | _this.$fill.addClass('is-dragging');
|
491 | _this.$element.data('dragging', true);
|
492 |
|
493 | curHandle = $(e.currentTarget);
|
494 |
|
495 | $body.on('mousemove.zf.slider', function(e) {
|
496 | e.preventDefault();
|
497 | _this._handleEvent(e, curHandle);
|
498 |
|
499 | }).on('mouseup.zf.slider', function(e) {
|
500 | _this._handleEvent(e, curHandle);
|
501 |
|
502 | $handle.removeClass('is-dragging');
|
503 | _this.$fill.removeClass('is-dragging');
|
504 | _this.$element.data('dragging', false);
|
505 |
|
506 | $body.off('mousemove.zf.slider mouseup.zf.slider');
|
507 | });
|
508 | })
|
509 |
|
510 | .on('selectstart.zf.slider touchmove.zf.slider', function(e) {
|
511 | e.preventDefault();
|
512 | });
|
513 | }
|
514 |
|
515 | $handle.off('keydown.zf.slider').on('keydown.zf.slider', function(e) {
|
516 | var _$handle = $(this),
|
517 | idx = _this.options.doubleSided ? _this.handles.index(_$handle) : 0,
|
518 | oldValue = parseFloat(_this.inputs.eq(idx).val()),
|
519 | newValue;
|
520 |
|
521 |
|
522 | Keyboard.handleKey(e, 'Slider', {
|
523 | decrease: function() {
|
524 | newValue = oldValue - _this.options.step;
|
525 | },
|
526 | increase: function() {
|
527 | newValue = oldValue + _this.options.step;
|
528 | },
|
529 | decrease_fast: function() {
|
530 | newValue = oldValue - _this.options.step * 10;
|
531 | },
|
532 | increase_fast: function() {
|
533 | newValue = oldValue + _this.options.step * 10;
|
534 | },
|
535 | min: function() {
|
536 | newValue = _this.options.start;
|
537 | },
|
538 | max: function() {
|
539 | newValue = _this.options.end;
|
540 | },
|
541 | handled: function() {
|
542 | e.preventDefault();
|
543 | _this._setHandlePos(_$handle, newValue, true);
|
544 | }
|
545 | });
|
546 | |
547 |
|
548 |
|
549 |
|
550 | });
|
551 | }
|
552 |
|
553 | |
554 |
|
555 |
|
556 | _destroy() {
|
557 | this.handles.off('.zf.slider');
|
558 | this.inputs.off('.zf.slider');
|
559 | this.$element.off('.zf.slider');
|
560 |
|
561 | clearTimeout(this.timeout);
|
562 | }
|
563 | }
|
564 |
|
565 | Slider.defaults = {
|
566 | |
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 | start: 0,
|
573 | |
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 | end: 100,
|
580 | |
581 |
|
582 |
|
583 |
|
584 |
|
585 |
|
586 | step: 1,
|
587 | |
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 | initialStart: 0,
|
594 | |
595 |
|
596 |
|
597 |
|
598 |
|
599 |
|
600 | initialEnd: 100,
|
601 | |
602 |
|
603 |
|
604 |
|
605 |
|
606 |
|
607 | binding: false,
|
608 | |
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 | clickSelect: true,
|
615 | |
616 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 | vertical: false,
|
622 | |
623 |
|
624 |
|
625 |
|
626 |
|
627 |
|
628 | draggable: true,
|
629 | |
630 |
|
631 |
|
632 |
|
633 |
|
634 |
|
635 | disabled: false,
|
636 | |
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 | doubleSided: false,
|
643 | |
644 |
|
645 |
|
646 |
|
647 | |
648 |
|
649 |
|
650 |
|
651 |
|
652 |
|
653 | decimal: 2,
|
654 | |
655 |
|
656 |
|
657 |
|
658 | |
659 |
|
660 |
|
661 |
|
662 |
|
663 |
|
664 | moveTime: 200,
|
665 | |
666 |
|
667 |
|
668 |
|
669 |
|
670 |
|
671 | disabledClass: 'disabled',
|
672 | |
673 |
|
674 |
|
675 |
|
676 |
|
677 |
|
678 | invertVertical: false,
|
679 | |
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 | changedDelay: 500,
|
686 | |
687 |
|
688 |
|
689 |
|
690 |
|
691 |
|
692 | nonLinearBase: 5,
|
693 | |
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 | positionValueFunction: 'linear',
|
700 | };
|
701 |
|
702 | function percent(frac, num) {
|
703 | return (frac / num);
|
704 | }
|
705 | function absPosition($handle, dir, clickPos, param) {
|
706 | return Math.abs(($handle.position()[dir] + ($handle[param]() / 2)) - clickPos);
|
707 | }
|
708 | function baseLog(base, value) {
|
709 | return Math.log(value)/Math.log(base)
|
710 | }
|
711 |
|
712 | export {Slider};
|