-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPieceElement.js
83 lines (76 loc) · 2.67 KB
/
PieceElement.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
/**
* @typedef PieceElmentSubEls
* @prop {HTMLDivElement} container
* @prop {HTMLDivElement} grid
* @prop {HTMLSpanElement} countInput
* @prop {HTMLDivElement} countUp
* @prop {HTMLDivElement} countDown
* @prop {HTMLSpanElement} rotatableToggle
* @prop {HTMLSpanElement} flipableToggle
* @prop {HTMLSpanElement} delete
*/
export default class PieceElement {
/** @type {import("./Piece.js").default} */
parent = null;
/** @type {PieceElmentSubEls} */
els = {};
/**
* @param {import("./Piece.js").default} parent
*/
constructor(parent) {
this.parent = parent;
// initialize
/** @type {HTMLTemplateElement} */
const templateEl = document.getElementById("template__piece-list__item");
/** @type {HTMLDivElement} */
const container = templateEl.content.firstElementChild.cloneNode(true);
this.els = {
container,
grid: container.getElementsByClassName("piece-list__item__grid")[0],
countInput: container.getElementsByClassName("piece-list__count__input")[0],
countUp: container.getElementsByClassName("piece-list__count__up")[0],
countDown: container.getElementsByClassName("piece-list__count__down")[0],
rotatableToggle: container.getElementsByClassName("piece-rotatable")[0],
flipableToggle: container.getElementsByClassName("piece-flipable")[0],
delete: container.getElementsByClassName("piece-delete")[0],
};
this.els.countUp.addEventListener("click", () => {
this.parent.count++;
});
this.els.countDown.addEventListener("click", () => {
this.parent.count--;
});
this.els.rotatableToggle.addEventListener("click", () => {
this.parent.isRotatable = !this.parent.isRotatable;
this.update();
});
this.els.flipableToggle.addEventListener("click", () => {
this.parent.isFlipable = !this.parent.isFlipable;
this.update();
});
this.els.delete.addEventListener("click", () => {
this.parent.parent.remove(this.parent);
});
this.els.countInput.addEventListener("input", () => {
const value = parseInt(this.els.countInput.innerText);
if (isNaN(value) || 0 > value || value > 10000) {
this.els.countInput.innerText = this.parent.count;
return;
}
this.parent.count = value;
});
}
update() {
this.els.countInput.innerText = this.parent.count;
if (this.parent.isRotatable) {
this.els.rotatableToggle.classList.add("active");
} else {
this.els.rotatableToggle.classList.remove("active");
}
if (this.parent.isFlipable) {
this.els.flipableToggle.classList.add("active");
} else {
this.els.flipableToggle.classList.remove("active");
}
}
}