-
Notifications
You must be signed in to change notification settings - Fork 38
/
group.js
62 lines (54 loc) · 1.37 KB
/
group.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
class ButtonGroup extends HTMLElement {
constructor() {
super();
this.__typeHandler = {
absolute: btn => this.__typeAbsoluteHandle(btn),
relative: btn => this.__typeRelativeHandle(btn),
};
this.addEventListener("click", this.__handleClick);
}
activate(button) {
let typeHandler = this.__typeHandler[this.type];
typeHandler(button);
}
__handleClick(event) {
const button = event.target.closest(".btn");
if (button) {
this.activate(button);
button.dispatchEvent(new CustomEvent("activate", {
bubbles: true,
detail: {
button: button
}
}));
}
}
__typeAbsoluteHandle(button) {
for (let button of this.buttons) {
button.classList.remove("active");
}
button.classList.add("active");
}
__typeRelativeHandle(button) {
if (button.classList.contains("active")) {
button.classList.remove("active");
}
else {
button.classList.add("active");
}
}
attributeChangedCallback(attr, oldValue, newValue) {
}
get buttons() {
return this.getElementsByClassName("btn");
}
get type() {
return this.getAttribute("type") || "relative";
}
set type(value) {
return this.setAttribute("type", value);
}
}
ButtonGroup.observedAttributes = ["type"];
customElements.define("btn-group", ButtonGroup);
module.exports = ButtonGroup;