-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
126 lines (116 loc) · 4.35 KB
/
app.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { initialGame } from "./config/initialGame.js";
import { piecesImages } from "./config/piecesImages.js";
import { q, qAll } from "./helpers.js";
import {
mouseEnter, mouseLeave, pieceClick, blockChangeSides, restart, undoMove, toggleUndoButton, contextmenu
} from "./src/listeners.js";
export let whiteSide = false; // start with false but with first render became true
export const toggleChangeSideButton = (unblock) => {
const button = q("#change-sides");
if (unblock) {
button.classList.remove("disabled");
button.disabled = false;
return;
}
button.classList.add("disabled");
button.disabled = true;
};
const resetGame = () => {
toggleUndoButton(false);
blockChangeSides(true);
toggleChangeSideButton(true);
restart();
qAll(".piece-box").forEach((piece) => {
if (piece.firstChild) piece.removeChild(piece.firstChild);
piece.classList.remove("piece-pointer", "piece-clicked", "piece-premove", "piece-endmove", "piece-startmove");
piece.removeEventListener("mouseenter", (el) => mouseEnter(el, piece));
piece.removeEventListener("mouseleave", (el) => mouseLeave(el, piece));
piece.removeEventListener("click", (el) => pieceClick(el, piece));
piece.removeEventListener("contextmenu", (el) => contextmenu(el, piece));
});
const backdrop = q(".winner-backdrop");
backdrop.classList.add("hidden");
renderGame.renderPieces();
};
const renderSide = (reversed = false) => {
const leftPanel = q(".left-panel");
const bottomPanel = q(".bottom-panel");
let numbers = ["8", "7", "6", "5", "4", "3", "2", "1"];
let letters = ["A", "B", "C", "D", "E", "F", "G", "H"];
while (leftPanel.firstChild || bottomPanel.firstChild) {
leftPanel.removeChild(leftPanel.firstChild);
bottomPanel.removeChild(bottomPanel.firstChild);
}
const main = q("#main");
if (main.classList) main.classList.remove("reversed");
if (reversed) {
q("#main").classList.add("reversed");
numbers = numbers.reverse();
letters = letters.reverse();
}
numbers.forEach((number) => {
const div = document.createElement("div");
div.classList.add("number");
const text = document.createTextNode(number);
div.appendChild(text);
leftPanel.appendChild(div);
});
letters.forEach((letter) => {
const div = document.createElement("div");
div.classList.add("letter");
const text = document.createTextNode(letter);
div.appendChild(text);
bottomPanel.appendChild(div);
});
whiteSide = !whiteSide
};
const renderGame = {
renderPieces() {
const gameSetup = initialGame;
renderSide();
this.placePiece(gameSetup);
this.pieceAddEvents();
this.blockDragEvent();
toggleUndoButton(true);
},
placePiece(gameSetup) {
for (const piecePosition in gameSetup) {
const pieceType = gameSetup[piecePosition];
const imgLocation = piecesImages[pieceType];
const imgEl = document.createElement("img");
imgEl.classList.add("piece");
imgEl.setAttribute("piece-type", pieceType);
imgEl.src = `${imgLocation}`;
q(`#${piecePosition}`).append(imgEl);
};
},
pieceAddEvents() {
qAll(".piece-box").forEach((piece) => {
if (piece.hasChildNodes()) {
piece.classList.add("piece-pointer");
}
piece.addEventListener("mouseenter", (el) => mouseEnter(el, piece));
piece.addEventListener("mouseleave", (el) => mouseLeave(el, piece));
piece.addEventListener("click", (el) => pieceClick(el, piece));
piece.addEventListener("contextmenu", (el) => contextmenu(el, piece));
});
},
resetAddEvents() {
},
blockDragEvent() {
qAll(".piece-box > img").forEach((img) => img.setAttribute("draggable", false));
}
};
window.addEventListener('DOMContentLoaded', () => {
renderGame.renderPieces();
});
q("#change-sides").addEventListener("click", () => renderSide(whiteSide));
q("#reset").addEventListener("click", () => {
resetGame();
resetGame();
});
q("#undo-move").addEventListener("click", () => {
const piece = q(".piece-endmove");
const activePiece = q(".piece-startmove");
undoMove(piece, activePiece);
});