-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathenable-slider.js
executable file
·927 lines (793 loc) · 31.1 KB
/
enable-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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
'use strict';
/*******************************************************************************
* enable-slider.js - Implementation of the ARIA slider role UI.
*
* Based on code by the Open Ajax Alliance
* Original code available at:
* https://web.archive.org/web/20170715191225/http://oaa-accessibility.org/example/32/
*
* Refactored by Zoltan Hawryluk <[email protected]>
* Part of the Enable accessible component library.
* Version 1.0 written May 17, 2021
*
* More information about this script available at:
* https://www.useragentman.com/enable/slider.php
*
* Released under the MIT License.
******************************************************************************/
import { interpolate, htmlToDomNode } from '../modules/interpolate.js';
/**
* keyCodes() is an object to contain key code values for the application
*/
const keyCodes = function () {
// Define values for keycodes
this.backspace = 8;
this.tab = 9;
this.enter = 13;
this.esc = 27;
this.space = 32;
this.pageup = 33;
this.pagedown = 34;
this.end = 35;
this.home = 36;
this.left = 37;
this.up = 38;
this.right = 39;
this.down = 40;
}; // end keyCodes
/**
*
* slider() is a class to define an ARIA-enabled slider widget. The class
* will create needed handles and define ARIA attributes for the slider
*
* @param {string} container_id - the containing div for the slider
* @param {boolean} vert - true if the slider is vertical; false if horizontal
* @param {integer} inc - the increment value for the slider
* @param {integer} jump - the large increment value for the slider (pgUp/pgDown keys)
* @param {boolean} showVals - true if the slider should display the value of the handles
* @param {boolean} range - true if the slider is a range slider
* @param {integer} val1 - val1 specifies the initial value of the slider or of the first
* slide handle if this is a range enableSlider. Must be >= min.
* @param {integer} val2 - val2 specifies the initial value of the second slide handle.
* Ignored if range is false (i.e. not a range slider). Must be <= max.
*/
const enableSlider = function (
container_id,
vert,
min,
max,
inc,
jump,
showVals,
range,
val1,
val2,
) {
this.init = function () {
// define slider object properties
this.keys = new keyCodes();
this.id = container_id;
this.$template = document.getElementById(
'enable-slider__handle--template',
);
this.template = this.$template.innerHTML;
this.className = 'enable-slider';
this.$container = document.getElementById(container_id);
this.vert = vert;
this.range = range;
this.showVals = showVals;
// Store the size of the slider
this.width = this.$container.offsetWidth;
this.height = this.$container.offsetHeight;
// Store the page position of the slider
this.offset = this.$container.getBoundingClientRect();
this.left = Math.round(this.offset.left);
this.top = Math.round(this.offset.top);
// Store the minimum and maximum and initial values
this.min = min;
this.max = max;
this.inc = inc;
this.jump = jump;
this.val1 = val1;
this.domParser = new DOMParser();
// If range is true, store the second value
if (range) {
this.val2 = val2;
}
/////////////// Create handles /////////////////
this.$handle1 = undefined;
this.$handle2 = undefined;
if (!range) {
// Create the handle
this.$handle1 = this.createHandle(val1);
} else {
// create the range highlight div
this.createRangeDiv();
// Create the first handle
this.$handle1 = this.createHandle(val1, 1);
// create the second handle
this.$handle2 = this.createHandle(val2, 2);
}
};
/**
* createHandle() creates a handle for the enableSlider. It defines ARIA attributes for
* the handle and positions it at the specified value in the slider range. if showVals is true,
* create and position divs to display the handle value.
*
* @param {integer} val - the initial value of the handle
* @param {integer} num - the handle number. (optional)
* @returns { object } - the object pointer of the newly created handle
*/
this.createHandle = function (val, num) {
const index = num === undefined ? '' : num;
const id = this.id + '_handle' + index;
const ariaLabel = this.id + '_label' + index;
const ariaDesc = this.id + '_desc' + index;
// slider HTML. If it doesn't exist in the DOM, we create it for you.
const handle = interpolate(this.template, {
id: id,
classNameRoot: this.className,
arialabelledby: ariaLabel,
ariadescribedby: ariaDesc,
valuemin: this.min,
valuemax: this.max,
valuenow: val === undefined ? this.min : val,
});
this.$container.appendChild(htmlToDomNode(handle));
const $handle = document.getElementById(id);
const $handleButton = $handle.querySelector(
'.enable-slider__handle-button',
);
const $incrementor = $handle.querySelector(
'.enable-slider__increase .enable-slider__button-label',
);
const $decrementor = $handle.querySelector(
'.enable-slider__decrease .enable-slider__button-label',
);
// position handle
this.positionHandle($handle, $handleButton, val);
// bind handlers
this.bindHandlers($handle, $decrementor, $incrementor);
return $handle;
}; // end createHandle()
/**
* createRangeDiv() creates a div for the highlight of a range enableSlider.
* It sets the initial top or left position to match that of the slider container.
*/
this.createRangeDiv = function () {
const id = this.id + '_range';
const range =
'<div id="' +
id +
'" class="' +
this.className +
'__slider-range"></div>';
// Store the div object
this.$rangeDiv = htmlToDomNode(range);
// Create the range div
this.$container.appendChild(this.$rangeDiv);
const rangeStyle = this.$rangeDiv.style;
const containerStyle = getComputedStyle(this.$container, null);
if (!this.vert) {
// horizontal slider
rangeStyle.top = 0;
rangeStyle.height = containerStyle.height;
} else {
// vertical slider
rangeStyle.left = 0;
rangeStyle.width = containerStyle.width;
}
}; // end createRangeDiv()
/**
* positionHandle() is a member function to position a handle
* at the specified value for the enableSlider. If showVal is true,
* it also positions and updates the displayed value container.
*
* @param { HTMLElement } $handle - DOM node of the handle container to be manipulated
* @param { HTMLElement } $handleButton - DOM node of the handle button.
* @param {integer} val - the new value of the slider
*/
this.positionHandle = ($handle, $handleButton, val) => {
const handleHeight = $handle.offsetHeight; // the total height of the handle
const handleWidth = $handle.offsetWidth; // the total width of the handle
let xPos; // calculated horizontal position of the handle;
let yPos; // calculated vertical position of the handle;
let valPos; //calculated new pixel position for the value;
const { offsetWidth, offsetHeight } = this.$container;
let didChange;
if (!this.vert) {
// horizontal slider
// calculate the horizontal pixel position of the specified value
valPos = ((val - this.min) / (this.max - this.min)) * offsetWidth;
xPos = Math.round(valPos - handleWidth / 2);
yPos = Math.round(offsetHeight / 2 - handleHeight / 2);
} else {
// vertical slider
// calculate the vertical pixel position of the specified value
valPos = ((val - this.min) / (this.max - this.min)) * offsetHeight;
xPos = Math.round(offsetWidth / 2 - handleWidth / 2);
yPos = Math.round(valPos - handleHeight / 2);
}
// Set the position of the handle
$handle.style.top = yPos + 'px';
$handle.style.left = xPos + 'px';
// Set the aria-valuenow position of the handle
$handleButton.setAttribute('aria-valuenow', val);
// Update the stored handle values
if (/1$/.test($handle.getAttribute('id'))) {
// first handle
didChange = this.val1 !== val;
if (didChange) {
this.val1 = val;
}
} else {
// second handle
didChange = this.val2 !== val;
if (didChange) {
this.val2 = val;
}
}
// if range is true, set the position of the range div
if (this.range) {
this.positionRangeDiv();
}
// if showVal is true, update the value container
if (this.showVals) {
this.updateValBox($handle, $handleButton, Math.round(valPos));
}
if (didChange) {
$handle.dispatchEvent(
new CustomEvent('enable-slider-change', {
bubbles: true,
detail: {
value: () =>
$handleButton.getAttribute('aria-valuenow'),
},
}),
);
}
}; // end positionHandle()
/**
* positionRangeDiv() is a member function to reposition the range div
* when a handle is moved/
*/
this.positionRangeDiv = () => {
let pos; //calculated new range start position;
let size; //calculated new range size;
const { offsetWidth, offsetHeight } = this.$container;
if (!this.vert) {
// Horizontal slider
// calculate the range start position
pos = Math.round(
((this.val1 - this.min) / (this.max - this.min)) * offsetWidth,
);
// calculate the new range width
size =
Math.round(
((this.val2 - this.min) / (this.max - this.min)) *
offsetWidth,
) - pos;
// set the new range position
this.$rangeDiv.style.left = pos + 'px';
// set the new range width
this.$rangeDiv.style.width = size + 'px';
} else {
// calculate the range start position
pos = Math.round(
((this.val1 - this.min) / (this.max - this.min)) * offsetHeight,
);
// calculate the new range width
size =
Math.round(
((this.val2 - this.min) / (this.max - this.min)) *
offsetHeight,
) - pos;
// set the new range position
this.$rangeDiv.style.top = pos + 'px';
// set the new range width
this.$rangeDiv.style.height = size + 'px';
}
}; // end positionRangeDiv()
/**
* updateValBox() is a member function to reposition a handle value box
* and update its contents
*
* @param { HTMLElement } $handle - DOM node of the handle container to be manipulated
* @param { HTMLElement } $handleButton - DOM node of the handle button.
* @param {integer} valPos - the pixel position of the slider value
*/
this.updateValBox = function ($handle, $handleButton, valPos) {
const $valBox = document.getElementById(
$handle.getAttribute('id') + '_val',
);
let xPos; // horizontal pixel position of the box
let yPos; // vertical pixel position of the box
// Set the position of the handle
if (!this.vert) {
const boxWidth = $valBox.offsetWidth;
yPos = $handle.style.top;
// Adjust the horizontal position to center the value box on the value position
xPos = Math.round(valPos - boxWidth / 2) + 'px';
} else {
const boxHeight = $valBox.offsetHeight;
xPos = $handle.style.left;
// Adjust the vertical position to center the value box on the value position
yPos = Math.round(valPos - boxHeight / 2) + 'px';
}
// Set the position of the value box
$valBox.style.top = yPos;
$valBox.style.left = xPos;
// Set the text in the box to the handle value
$valBox.innerHTML = $handleButton.getAttribute('aria-valuenow');
}; // end updateValBox()
/**
* bindHandlers() is a member function to bind event handlers to a slider handle
*
* @param {object} $handle - the object pointer of the handle to bind handlers to
*/
this.bindHandlers = ($handle, $decrementor, $incrementor) => {
$decrementor.addEventListener('click', (e) => {
return this.handleDecrementorClick($handle, e);
});
$incrementor.addEventListener('click', (e) => {
return this.handleIncrementorClick($handle, e);
});
$handle.addEventListener('keydown', (e) => {
return this.handleKeyDown($handle, e);
});
$handle.addEventListener('keypress', (e) => {
return this.handleKeyPress($handle, e);
});
$handle.addEventListener('focus', (e) => {
return this.handleFocus($handle, e);
});
$handle.addEventListener('blur', (e) => {
return this.handleBlur($handle, e);
});
$handle.addEventListener('pointerdown', (e) => {
return this.handlePointerDown(
$handle,
$incrementor,
$decrementor,
e,
);
});
// Use passive: true for touchstart event listeners
$handle.addEventListener(
'touchstart',
(e) => {
return this.handlePointerDown(
$handle,
$incrementor,
$decrementor,
e,
);
},
{ passive: true },
);
window.addEventListener('resize', this.handleResize);
window.addEventListener('orientationchange', this.handleResize);
}; // end bindHandlers()
/**
* handleKeyDown() is a member function to process keydown events for a slider handle
*
* @param { HTMLElement } $handle - DOM node of the handle container to be manipulated
* @param {object} evt - the event object associated witthe the event
*
* @returns { boolean } true if propagating; false if consuming event
*/
this.handleKeyDown = function ($handle, evt) {
const $handleButton = $handle.querySelector(
'.enable-slider__handle-button',
);
if (evt.ctrlKey || evt.shiftKey || evt.altKey) {
// Do nothing
return true;
}
switch (evt.keyCode) {
case this.keys.home: {
// move the handle to the slider minimum
if (!this.range) {
this.positionHandle($handle, $handleButton, this.min);
} else {
if (/1$/.test($handle.getAttribute('id'))) {
// handle 1 - move to the min value
this.positionHandle($handle, $handleButton, this.min);
} else {
// handle 2 - move to the position of handle 1
this.positionHandle($handle, $handleButton, this.val1);
}
}
evt.stopPropagation;
evt.preventDefault();
return false;
}
case this.keys.end: {
if (!this.range) {
// move the handle to the slider maximum
this.positionHandle($handle, $handleButton, this.max);
} else {
if (/1$/.test($handle.getAttribute('id'))) {
// handle 1 - move to the position of handle 2
this.positionHandle($handle, $handleButton, this.val2);
} else {
// handle 2 - move to the max value
this.positionHandle($handle, $handleButton, this.max);
}
}
evt.stopPropagation;
evt.preventDefault();
return false;
}
case this.keys.pageup: {
// Decrease by jump value
const newVal =
$handleButton.getAttribute('aria-valuenow') - this.jump;
let stopVal = this.min; // where to stop moving
if (this.range) {
// if this is handle 2, stop when we reach the value
// for handle 1
if (/2$/.test($handle.getAttribute('id'))) {
stopVal = this.val1;
}
}
// move the handle one jump increment toward the slider minimum
// If value is less than stopVal, set at stopVal instead
this.positionHandle(
$handle,
$handleButton,
newVal > stopVal ? newVal : stopVal,
);
evt.stopPropagation;
evt.preventDefault();
return false;
}
case this.keys.pagedown: {
// Increase by jump value
const newVal =
parseInt($handleButton.getAttribute('aria-valuenow')) +
this.jump;
let stopVal = this.max; // where to stop moving
if (this.range) {
// if this is handle 1, stop when we reach the value
// for handle 2
if (/1$/.test($handle.getAttribute('id'))) {
stopVal = this.val2;
}
}
// move the handle one jump increment toward the slider maximum
// If value is greater than maximum, set at maximum instead
this.positionHandle(
$handle,
$handleButton,
newVal < stopVal ? newVal : stopVal,
);
evt.stopPropagation;
evt.preventDefault();
return false;
}
case this.keys.left:
case this.keys.up: {
// decrement
this.handleDecrementorClick($handle, evt);
return false;
}
case this.keys.right:
case this.keys.down: {
// increment
this.handleIncrementorClick($handle, evt);
return false;
}
} // end switch
return true;
}; // end handleKeyDown
/**
* handleKeyPress() is a member function to process keypress events for a slider handle. Needed for
* browsers that perform window scrolling on keypress rather than keydown events.
*
* @param {object} $handle - the object associated with the event
* @param {object} evt - the event object associated witthe the event
* @returns { boolean } true if propagating; false if consuming event
*/
this.handleKeyPress = function ($handle, evt) {
if (evt.ctrlKey || evt.shiftKey || evt.altKey) {
// Do nothing
return true;
}
switch (evt.keyCode) {
case this.keys.home:
case this.keys.pageup:
case this.keys.end:
case this.keys.pagedown:
case this.keys.left:
case this.keys.up:
case this.keys.right:
case this.keys.down: {
// Consume the event
evt.stopPropagation;
return false;
}
} // end switch
return true;
}; // end handleKeyDown
/**
* handleFocus() is a member function to process focus events for a slider handle
*
* @param {object} $handle - the object associated with the event
* @param {object} evt - the event object associated with the the event
* @returns { boolean } true if propagating; false if consuming event
*/
this.handleFocus = function ($handle) {
$handle.setAttribute(
'src',
'js/modules/images/slider_' +
(this.vert ? 'v' : 'h') +
'-focus.png',
);
$handle.classList.add('focus');
$handle.style.zIndex = '20';
return true;
}; // end handleFocus()
/**
* handleBlur() is a member function to process blur events for a slider handle
*
* @param {object} $handle - the object associated with the event
* @param {object} evt - the event object associated witthe the event
* @returns { boolean } true if propagating; false if consuming event
*/
this.handleBlur = function ($handle) {
$handle.setAttribute(
'src',
'js/modules/images/slider_' + (this.vert ? 'v' : 'h') + '.png',
);
$handle.classList.remove('focus');
$handle.style.zIndex = '10';
return true;
}; // end handleBlur()
/**
* handlePointerDown() is a member function to process pointerdown events
* for a slider handle. The function binds a pointermove handler
*
* @param {object} $handle - the object associated with the event
* @param {object} evt - the event object associated witthe the event
* @returns { boolean } true if propagating; false if consuming event
*/
this.handlePointerDown = function (
$handle,
$incrementor,
$decrementor,
evt,
) {
if (
evt.target &&
(evt.target === $decrementor || evt.target === $incrementor)
) {
return;
}
document.body.classList.add(`${this.className}--cannot-select-text`);
this.$container.classList.add(`${this.className}--is-moving`);
// remove focus highlight from all other slider handles on the page
const $hsliderHandleAll = document.querySelectorAll(
`${this.className}--horizontal ${this.className}__handle`,
);
$hsliderHandleAll.forEach(
($hsliderHandle) => ($hsliderHandle.style.zIndex = '0'),
);
const $vsliderHandleAll = document.querySelectorAll(
`${this.className}--vertical ${this.className}__handle`,
);
$vsliderHandleAll.forEach(
($vsliderHandle) => ($vsliderHandle.style.zIndex = '10'),
);
// Set focus to the clicked handle
$handle.focus();
const pointerMoveEvent = (e) => {
this.handlePointerMove($handle, e);
};
const pointerUpEvent = (e) => {
document.body.classList.remove(
`${this.className}--cannot-select-text`,
);
this.$container.classList.remove(`${this.className}--is-moving`);
// unbind the event listeners to release the pointer
document.body.removeEventListener('pointermove', pointerMoveEvent);
document.body.removeEventListener('pointerup', pointerUpEvent);
document.body.removeEventListener('touchmove', pointerMoveEvent);
document.body.removeEventListener('touchend', pointerUpEvent);
e.stopPropagation;
return false;
};
// bind a pointermove event handler to the document to capture the pointer
document.body.addEventListener('pointermove', pointerMoveEvent);
document.body.addEventListener('touchmove', pointerMoveEvent);
//bind a pointerup event handler to the document to capture the pointer
document.body.addEventListener('pointerup', pointerUpEvent);
document.body.addEventListener('touchend', pointerUpEvent);
evt.stopPropagation();
evt.preventDefault();
return false;
}; // end handlePointerDown()
/**
* handleDecrementorClick() - Event handler fired when clicking on the decrementor
* button. This button should only be visible to mobile screen reader users.
*
* @param { HTMLElement } $handle - DOM node of the handle container to be manipulated
* @param { HTMLElement } $handleButton - DOM node of the handle button.
* @param { Event } evt - the click event object
*/
this.handleDecrementorClick = function ($handle, evt) {
const $handleButton = $handle.querySelector(
'.enable-slider__handle-button',
);
const newVal = $handleButton.getAttribute('aria-valuenow') - this.inc;
let stopVal = this.min; // where to stop moving
if (this.range) {
// if this is handle 2, stop when we reach the value
// for handle 1
if (/2$/.test($handle.getAttribute('id'))) {
stopVal = this.val1;
}
}
// move the handle one jump increment toward the stopVal
// If value is less than stopVal, set at stopVal instead
this.positionHandle(
$handle,
$handleButton,
newVal > stopVal ? newVal : stopVal,
);
evt.stopPropagation;
evt.preventDefault();
};
/**
* handleIncrementorClick() - Event handler fired when clicking on the incrementor
* button. This button should only be visible to mobile screen reader users.
*
* @param { HTMLElement } $handle - DOM node of the handle container to be manipulated
* @param { Event } evt - the click event object
*/
this.handleIncrementorClick = function ($handle, evt) {
const $handleButton = $handle.querySelector(
'.enable-slider__handle-button',
);
const newVal =
parseInt($handleButton.getAttribute('aria-valuenow')) + this.inc;
let stopVal = this.max; // where to stop moving
if (this.range) {
// if this is handle 1, stop when we reach the value
// for handle 2
if (/1$/.test($handle.getAttribute('id'))) {
stopVal = this.val2;
}
}
// move the handle one increment toward the slider maximum
// If value is greater than maximum, set at maximum instead
this.positionHandle(
$handle,
$handleButton,
newVal < stopVal ? newVal : stopVal,
);
evt.stopPropagation;
evt.preventDefault();
};
/**
* handleResize(): the event that fires when the page is resized or onorientationchange.
* This redraws the slider, which is necessary if the sliders dimensions are given
* in relative units, such as % or vw.
*
* @param { Event } evt - the resize event object
*/
this.handleResize = () => {
const { positionHandle, positionRangeDiv, $handle1, $handle2 } = this;
const $handleButton1 = $handle1.getElementsByClassName(
'enable-slider__handle-button',
)[0];
const $handleButton2 = $handle2
? $handle2.getElementsByClassName('enable-slider__handle-button')[0]
: null;
positionHandle(
$handle1,
$handleButton1,
parseInt($handleButton1.getAttribute('aria-valuenow')),
);
if ($handle2) {
positionHandle(
$handle2,
$handleButton2,
parseInt($handleButton2.getAttribute('aria-valuenow')),
);
}
if (this.range) {
positionRangeDiv();
}
};
/**
* handlePointerMove() is a member function to process pointermove
* events for a slider handle.
*
* @param { HTMLElement } $handle - DOM node of the handle container to be manipulated
* @param { Event } evt - the event object associated witthe the event
* @returns { boolean } true if propagating; false if consuming event
*/
this.handlePointerMove = function ($handle, evt) {
const $handleButton = $handle.querySelector(
'.enable-slider__handle-button',
);
const {
left: containerLeft,
top: containerTop,
width: containerWidth,
height: containerHeight,
} = this.$container.getBoundingClientRect();
let newVal;
let startVal = this.min;
let stopVal = this.max;
if (this.range) {
// if this is handle 1, set stopVal to be the value
// for handle 2
if (/1$/.test($handle.getAttribute('id'))) {
stopVal = this.val2;
} else {
// This is handle 2: Set startVal to be the value
// for handle 1
startVal = this.val1;
}
}
if (!this.vert) {
// horizontal slider
// Calculate the new slider value based on the horizontal pixel position of the pointer using the container width
const relativeX = evt.clientX - containerLeft;
newVal = Math.round(
(relativeX / containerWidth) * (this.max - this.min) + this.min,
);
} else {
// vertical slider
// Calculate the new slider value based on the vertical pixel position of the pointer
const relativeY = evt.clientY - containerTop;
newVal = Math.round(
(relativeY / containerHeight) * (this.max - this.min) +
this.min,
);
}
// Snap the value to the nearest increment
newVal = Math.round(newVal / this.inc) * this.inc;
// Clamp the value within the allowed range
newVal = Math.max(startVal, Math.min(newVal, stopVal));
this.positionHandle($handle, $handleButton, newVal);
evt.stopPropagation();
evt.preventDefault();
return false;
}; // end handlePointerMove
this.init();
};
const enableSliders = new (function () {
this.list = [];
this.add = ($root) => {
const { min, max, inc, jump, showVals, range, val1, val2 } =
$root.dataset;
if (!$root.id) {
$root.id = 'enable-slider__' + i;
}
this.list.push(
new enableSlider(
$root.id,
$root.classList.contains('enable-slider--vertical'),
parseFloat(min),
parseFloat(max),
parseFloat(inc),
parseFloat(jump),
showVals === 'true',
range === 'true',
parseFloat(val1),
parseFloat(val2),
),
);
};
this.init = () => {
const $roots = document.querySelectorAll('.enable-slider');
for (let i = 0; i < $roots.length; i++) {
const $root = $roots[i];
this.add($root);
}
};
})();
export default enableSliders;