-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrange-slider.js
142 lines (116 loc) · 4.2 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
/**
* License: MIT
* Credits: https://stackoverflow.com/a/44384948
* Repo: https://github.com/swissup/range-slider
*/
class RangeSlider extends HTMLElement {
connectedCallback() {
setTimeout(() => this.addMarkup(), 0);
this.addEventListener('touchstart', this.onMouseDown.bind(this), { passive: true });
this.addEventListener('mousedown', this.onMouseDown.bind(this));
this.addEventListener('touchend', this.onMouseUp.bind(this));
this.addEventListener('mouseup', this.onMouseUp.bind(this));
this.addEventListener('input', this.onInput.bind(this));
}
get value() {
return [this.min.valueAsNumber, this.max.valueAsNumber].sort((a, b) => a - b);
}
set value(value) {
this.min.value = Math.min(...value);
this.max.value = Math.max(...value);
this.minOutput.value = this.min.value;
this.maxOutput.value = this.max.value;
}
addMarkup() {
if (!this.querySelector(':nth-child(1)')) {
this.insertAdjacentHTML('afterbegin', [
'<input class="range" type="range"/>',
'<input class="range" type="range"/>',
'<input class="filler" disabled type="range"/>',
].join(''));
}
this.insertAdjacentHTML('beforeend', '<output></output><output></output>');
this.initMinMaxElements();
let mapping = {
step: this.getAttribute('step') || 1,
min: this.getAttribute('min') || 0,
max: this.getAttribute('max') || 100
};
for (let [key, value] of Object.entries(mapping)) {
this.min[key] = value;
this.max[key] = value;
};
let value = (this.getAttribute('value') || '').split('-').filter((val) => val);
if (value.length) {
this.min.value = value[0];
this.max.value = value[1] || this.max.max;
} else {
this.min.value = this.min.min;
this.max.value = this.max.max;
}
this.minOutput.value = this.min.value;
this.maxOutput.value = this.max.value;
}
initMinMaxElements() {
this.min = this.querySelector(':nth-child(1)');
this.max = this.querySelector(':nth-child(2)');
let name = this.getAttribute('name');
if (name) {
this.min.name = name + '[min]';
this.max.name = name + '[max]';
}
[this.minOutput, this.maxOutput] = this.querySelectorAll('output');
}
/**
* Allow to move min above max and vice versa
*/
onMouseDown() {
this.constraint = (this.max.valueAsNumber - this.min.valueAsNumber) > Math.min(this.min.step, 1) * 5;
}
/**
* If min thumb was moved above max (or vice versa) - swap them
*/
onMouseUp() {
if (this.constraint) {
return;
}
this.constraint = true;
if (this.min.valueAsNumber < this.max.valueAsNumber) {
return;
}
this.insertBefore(this.max, this.min);
this.insertBefore(this.maxOutput, this.minOutput);
this.initMinMaxElements();
}
/**
* Contstrain thumbs inside their min/max values
*/
onInput(event) {
if (this.constraint) {
let el = event.target;
if (el === this.min) {
if (el.valueAsNumber > this.max.valueAsNumber) {
el.value = this.max.value;
}
} else if (el.valueAsNumber < this.min.valueAsNumber) {
el.value = this.min.value;
}
}
if (event.target === this.min) {
this.minOutput.value = this.min.value;
} else {
this.maxOutput.value = this.max.value;
}
this.dispatchEvent(new Event('range:input', {
bubbles: true
}));
}
disconnectedCallback() {
this.removeEventListener('touchstart', this.onMouseDown);
this.removeEventListener('mousedown', this.onMouseDown);
this.removeEventListener('touchend', this.onMouseUp);
this.removeEventListener('mouseup', this.onMouseUp);
this.removeEventListener('input', this.onInput);
}
}
customElements.define('range-slider', RangeSlider);