-
Notifications
You must be signed in to change notification settings - Fork 0
/
range-slider.js
157 lines (137 loc) · 4.39 KB
/
range-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
'use strict';
class RangeSlider extends HTMLElement {
constructor() {
super();
let rangeStart = 0;
let rangeEnd = 1;
this.setRange = (rangeStart_, rangeEnd_) => {
rangeStart = rangeStart_;
rangeEnd = rangeEnd_;
updateRange();
}
this.onrangechanged = (start, end) => {};
const minRange = 0.001;
const orientation = this.dataset.orientation;
this.innerHTML = `
<div class="thumb-container">
<div class="thumb">
<span class="min"></span>
<span class="max"></span>
</div>
</div>
`;
const thumb = this.querySelector('.thumb');
const thumbMin = thumb.querySelector('.min');
const thumbMax = thumb.querySelector('.max');
function updateRange() {
const range = rangeEnd - rangeStart;
if (orientation === 'horizontal') {
thumb.style.width = (range * 100) + '%';
thumb.style.left = (rangeStart * 100) + '%';
} else if (orientation === 'vertical') {
thumb.style.height = (range * 100) + '%';
thumb.style.bottom = (rangeStart * 100) + '%';
}
}
updateRange();
this.getLengthPx = () => {
const boundingRect = this.getBoundingClientRect();
switch (orientation) {
case 'horizontal': return boundingRect.width;
case 'vertical': return boundingRect.height;
}
}
thumb.onpointerdown = downEvent => {
downEvent.preventDefault();
if (downEvent.button && downEvent.button > 0) {
return;
}
if (thumb.onpointermove || thumbMin.onpointermove || thumbMax.onpointermove) {
return;
}
thumb.setPointerCapture(downEvent.pointerId);
thumb.style.cursor = 'grabbing';
const lengthPx = this.getLengthPx();
let lastCursorPosition = orientation === 'horizontal' ? downEvent.pageX : downEvent.pageY;
const rangeStartOnMousedown = rangeStart;
const rangeEndOnMousedown = rangeEnd;
let deltaTotal = 0;
thumb.onpointermove = moveEvent => {
if (moveEvent.pointerId !== downEvent.pointerId) {
return;
}
const cursorPosition = orientation === 'horizontal' ? moveEvent.pageX : moveEvent.pageY;
const deltaPx = cursorPosition - lastCursorPosition;
lastCursorPosition = cursorPosition;
let delta = deltaPx / lengthPx;
if (orientation === 'vertical') {
delta = -delta;
}
deltaTotal += delta;
rangeStart = Math.max(rangeStartOnMousedown + deltaTotal, 0);
rangeEnd = Math.min(rangeEndOnMousedown + deltaTotal, 1);
updateRange();
this.onrangechanged(rangeStart, rangeEnd);
}
thumb.onlostpointercapture = lostCaptureEvent => {
if (lostCaptureEvent.pointerId !== downEvent.pointerId) {
return;
}
thumb.onpointermove = null;
thumb.onlostpointercapture = null;
thumb.style.cursor = null;
}
}
thumbMin.onpointerdown = thumbMax.onpointerdown = downEvent => {
downEvent.preventDefault();
downEvent.stopPropagation();
const handle = downEvent.target;
if (downEvent.button && downEvent.button > 0) {
return;
}
if (handle.onpointermove || thumb.onpointermove) {
return;
}
handle.setPointerCapture(downEvent.pointerId);
const lengthPx = this.getLengthPx();
const isMin = handle.classList.contains('min');
let lastCursorPosition = orientation === 'horizontal' ? downEvent.pageX : downEvent.pageY;
handle.onpointermove = moveEvent => {
if (moveEvent.pointerId !== downEvent.pointerId) {
return;
}
const cursorPosition = orientation === 'horizontal' ? moveEvent.pageX : moveEvent.pageY;
let delta = (cursorPosition - lastCursorPosition) / lengthPx;
if (orientation === 'vertical') {
delta = -delta;
}
if (isMin) {
rangeStart = Math.max(rangeStart + delta, 0);
rangeStart = Math.min(rangeStart, 1 - minRange);
rangeEnd = Math.max(rangeEnd, rangeStart + minRange);
} else {
rangeEnd = Math.min(rangeEnd + delta, 1);
rangeEnd = Math.max(rangeEnd, minRange);
rangeStart = Math.min(rangeStart, rangeEnd - minRange);
}
updateRange();
this.onrangechanged(rangeStart, rangeEnd);
lastCursorPosition = cursorPosition;
}
handle.onlostpointercapture = lostCaptureEvent => {
if (lostCaptureEvent.pointerId !== downEvent.pointerId) {
return;
}
handle.onpointermove = null;
handle.onlostpointercapture = null;
}
}
this.ondblclick = () => {
rangeStart = 0;
rangeEnd = 1;
updateRange();
this.onrangechanged(rangeStart, rangeEnd);
}
}
}
customElements.define('range-slider', RangeSlider);