-
Notifications
You must be signed in to change notification settings - Fork 38
/
InputStepper.js
72 lines (65 loc) · 1.75 KB
/
InputStepper.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
class PhotonInputStepper extends HTMLElement {
constructor() {
super();
/*
const shadow = this.attachShadow({mode: 'open'});
const addButton = document.createElement("button");
const subButton = document.createElement("button");
shadow.append(`
<style>
@import url( 'css/input-stepper.css' )
</style>
`);
shadow.appendChild(addButton);
shadow.appendChild(subButton);*/
this.addEventListener("click", this.__handleClick);
}
get buttons() {
const buttons = this.getElementsByTagName("button");
return {
add: buttons[0],
sub: buttons[1]
};
}
get for() {
return document.querySelector(this.getAttribute("for"));
}
set for(value) {
this.setAttribute("for", value);
}
get min() {
return parseFloat(this.for.min) || PhotonInputStepper.__defaultMin;
}
get max() {
return parseFloat(this.for.max) || PhotonInputStepper.__defaultMax;
}
get step() {
return parseFloat(this.for.step) || PhotonInputStepper.__defaultStep;
}
get value() {
return parseFloat(this.for.value);
}
__handleClick(event) {
const button = event.target.closest("button");
if (button === this.buttons.add) {
this.add(this.step);
}
else if (button === this.buttons.sub) {
this.add(-this.step);
}
this.for.dispatchEvent(new Event("input", {
cancelable: true,
bubbles: true
}));
}
add(number) {
var newValue = this.value + number;
if (newValue > this.max) newValue = this.max;
if (newValue < this.min) newValue = this.min;
this.for.value = newValue;
}
}
PhotonInputStepper.__defaultStep = 1;
PhotonInputStepper.__defaultMax = Infinity;
PhotonInputStepper.__defaultMin = -Infinity;
module.exports = PhotonInputStepper;