-
Notifications
You must be signed in to change notification settings - Fork 4
/
kartta-slider.js
60 lines (56 loc) · 1.77 KB
/
kartta-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
function createKarttaSlider(options) {
function createElement(tag, attrs, text) {
const el = document.createElement(tag);
if (attrs != undefined) {
for (const [attr, value] of Object.entries(attrs)) {
el.setAttribute(attr, value);
}
}
if (text) {
el.innerHTML = text;
}
return el;
}
function createTextNode(text) {
return document.createTextNode(text);
}
// <div class="kartta-slider-container">
// <div class="kartta-slider-text">Year: <label class="kartta-year-label">1850</label></div>
// <input class="kartta-year-slider" type="range" min="1800" max="1950" step="10" value="1850" />
// </div>
const container = createElement("div", {
class: "kartta-slider-container"
});
const labelWrapper = createElement("div", {
class: "kartta-slider-text"
});
labelWrapper.appendChild(createTextNode("Year:"));
const label = createElement("label", {
class: "kartta-year-label"
}, options.value);
labelWrapper.appendChild(label);
container.appendChild(labelWrapper);
const input = createElement("input", {
class: "kartta-year-slider",
type: "range",
min: options.minValue,
max: options.maxValue,
step: options.stepSize,
value: options.value
});
container.appendChild(input);
const handleChange = (e) => {
const year = e.target.value;
label.innerText = year;
if (options.change) {
options.change(year);
}
};
input.addEventListener('change', handleChange);
input.addEventListener('input', handleChange);
if (options.domElementToReplace) {
options.domElementToReplace.parentNode.insertBefore(container, options.domElementToReplace);
options.domElementToReplace.parentNode.removeChild(options.domElementToReplace);
}
return container;
}