-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventhandler.js
88 lines (80 loc) · 2.49 KB
/
eventhandler.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
"use strict";
const Eventhandler = (function () {
const getElement = (id) => document.getElementById(`${id}`);
const ElementsToToggleDisplay = document.querySelectorAll(
"div.toggleJS, button.toggleJS"
);
let playerScore = 0;
let computerScore = 0;
//divs
const container = getElement("container");
const startDiv = getElement("startDiv");
const inGameDiv = getElement("inGameDiv");
const header = getElement("header");
const formDiv = getElement("formDiv");
const playerName = getElement("player");
const score = getElement("score");
const menu = getElement("menu");
const mobileMenu = getElement("mobileMenu");
const gameStatus = getElement("gameStatus");
const title = getElement("title");
//buttons
const startBtn = getElement("startBtn");
const newGameBtn = getElement("newGameBtn");
const levelBtn = getElement("levelBtn");
const resestBtn = getElement("resetBtn");
//submit
const input = getElement("nameForm");
startBtn.addEventListener("click", () => {
[startBtn, startDiv, formDiv].forEach((el) => el.classList.toggle("hide"));
/* ElementsToToggleDisplay.forEach((el) => el.classList.toggle("hide")); */
});
input.addEventListener("change", () => {
playerName.textContent = `${
input.value.charAt(0).toUpperCase() + input.value.slice(1) //capitalize first letter
}`;
ElementsToToggleDisplay.forEach((el) => el.classList.toggle("hide"));
GameMechanics.render();
GameMechanics.announceTurn();
});
const setMobileMenuView = () => {
container.appendChild(menu);
//mediaquery macht den rest
};
const incrScore = (comTurn) => {
if (comTurn) {
computerScore++;
} else {
playerScore++;
}
score.innerText = `${playerScore}:${computerScore}`;
};
const addListenerNewGame = (func) => {
newGameBtn.addEventListener("click", func);
};
levelBtn.addEventListener("click", () => computer.adjustLevel(levelBtn));
resestBtn.addEventListener("click", function () {
computerScore = 0;
playerScore = 0;
score.innerText = `${playerScore}:${computerScore}`;
});
const addListenerForMarks = (field) => {
field.addEventListener("click", GameMechanics.setSign);
};
const mql = window.matchMedia("(max-width: 977px)");
mql.onchange = (e) => {
if (e.matches) {
setMobileMenuView();
} else {
title.append(menu);
}
};
mql.onchange(mql);
return {
setMobileMenuView,
addListenerForMarks,
incrScore,
addListenerNewGame,
gameStatus,
};
})();