-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMCSlider.js
More file actions
68 lines (63 loc) · 2.45 KB
/
MCSlider.js
File metadata and controls
68 lines (63 loc) · 2.45 KB
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
import { MCComponent } from "./Component.js";
// progress 存在的意义是可以自定义显示的value值,例如精确到小数点后几位
class MCSlider extends MCComponent {
#input = this.shadowRoot.querySelector("input");
#progress = this.shadowRoot.getElementById("progress");
constructor() {
super();
this.#input.addEventListener("input", e => {
e.preventDefault();
this.refresh();
this.dispatchEvent("valueChange", { global: true, data: this.#input.value });
});
};
onConnected() {
this.initVar();
};
refresh() {
const input = this.#input;
this.setAttribute("value", input.value);
if (!this.hasAttribute("progress"))
this.#progress.innerHTML = input.value;
};
initVar() {
const input = this.#input;
input.min = this.getAttribute("min") || 0;
input.max = this.getAttribute("max") || 100;
input.step = this.getAttribute("step") || 1;
input.value = this.getAttribute("value") || 50;
this.refresh();
};
static get observedAttributes() { return ["label", "echo", "prefix", "progress", "unit", "min", "step", "max", "value", "disabled"]; };
onAttrChanged(name, oldVal, newVal) {
if (name === "echo") {
this.shadowRoot.getElementById("default-echo").style.display = newVal? "none": null;
const se = this.shadowRoot.getElementById("specified-echo");
se.innerHTML = newVal;
se.style.display = newVal? null: "none";
}
else if (name === "label" || name === "prefix")
this.shadowRoot.querySelector(`[name=${name}]`).innerHTML = newVal;
else if (name === "unit")
this.shadowRoot.querySelector("[name=unit]").innerHTML = newVal || "%";
else if (name === "progress")
this.#progress.innerHTML = newVal;
else if (name === "disabled")
this.#input.disabled = newVal !== null;
else this.initVar();
};
};
MCSlider.setBorderAndWaitImg("button-disabled", ":host");
MCSlider.setBorderAndWaitImg("slider-thumb", "input::-webkit-slider-thumb", {
keepCenter: false,
});
MCSlider.setBorderAndWaitImg("slider-thumb", "input::-moz-range-thumb", {
keepCenter: false,
});
MCSlider.setBorderAndWaitImg("slider-thumb-hover", "input:not(:disabled)::-webkit-slider-thumb:hover", {
keepCenter: false,
});
MCSlider.asyncLoadAndDefine();
export {
MCSlider,
};