-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.mjs
75 lines (65 loc) · 1.97 KB
/
view.mjs
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
import { PIECES } from "./constants.mjs";
class View {
constructor() {
const x0 = document.getElementById("x--0");
const x1 = document.getElementById("x--1");
const x2 = document.getElementById("x--2");
const x3 = document.getElementById("x--3");
const x4 = document.getElementById("x--4");
const x5 = document.getElementById("x--5");
const x6 = document.getElementById("x--6");
const x7 = document.getElementById("x--7");
const x8 = document.getElementById("x--8");
const o0 = document.getElementById("o--0");
const o1 = document.getElementById("o--1");
const o2 = document.getElementById("o--2");
const o3 = document.getElementById("o--3");
const o4 = document.getElementById("o--4");
const o5 = document.getElementById("o--5");
const o6 = document.getElementById("o--6");
const o7 = document.getElementById("o--7");
const o8 = document.getElementById("o--8");
this.x = [x0, x1, x2, x3, x4, x5, x6, x7, x8];
this.o = [o0, o1, o2, o3, o4, o5, o6, o7, o8];
this.resultX = document.getElementById("result-x");
this.resultO = document.getElementById("result-o");
this.resultDraw = document.getElementById("result-draw");
}
#asyncLabelListener(checkbox) {
const label = checkbox.labels[0];
return new Promise((resolve) => {
label.addEventListener("animationend", resolve, {
once: true,
});
});
}
renderBoardResult(winnerPiece) {
switch (winnerPiece) {
case PIECES.X:
this.resultX.style.display = "block";
return;
case PIECES.O:
this.resultO.style.display = "block";
return;
default:
this.resultDraw.style.display = "block";
}
}
renderPiece(ind, piece) {
let checkbox;
switch (piece) {
case PIECES.X:
checkbox = this.x[ind];
break;
case PIECES.O:
checkbox = this.o[ind];
break;
default:
throw new Error("Piece does not exit, check your code!");
}
const listener = this.#asyncLabelListener(checkbox);
checkbox.checked = true;
return listener;
}
}
export default View;