-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
155 lines (126 loc) · 3.79 KB
/
main.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Alert on reload
window.onbeforeunload = function (ev) {
var ev = ev || window.event;
const warning = 'Do you want to leave this page? Your counters will not be saved.';
if (ev) {
ev.returnValue = warning;
}
return warning;
};
// Life counter
class LifeCounter {
constructor($el) {
this.$el = $el;
this.$buttons = $el.querySelectorAll('.life__button');
this.$life = $el.querySelector('.life__input');
let lifeCount = parseInt(this.$life.value, 10);
lifeCount = isNaN(lifeCount) ? 0 : lifeCount;
this.$buttons.forEach(($button) => {
$button.addEventListener('click', (e) => {
e.preventDefault();
const number = parseInt($button.dataset.count);
lifeCount = lifeCount + number;
this.$life.value = lifeCount;
});
});
}
}
// Secondary counters
class SecondaryCounters {
constructor($el) {
this.$el = $el;
this.$counters = $el.querySelectorAll('.counter__item');
this.$counters.forEach(($counter) => {
const $input = $counter.querySelector('.counter__input');
const $buttons = $counter.querySelectorAll('.counter__button');
let count = parseInt($input.value, 10);
count = isNaN(count) ? 0 : count;
$buttons.forEach(($button) => {
$button.addEventListener('click', (e) => {
e.preventDefault();
const number = parseInt($button.dataset.count);
count = count + number;
$input.value = count;
});
});
});
}
}
// Delete button
class DeleteButton {
constructor($el) {
this.$el = $el;
this.$player = $el.closest('.player');
this.$el.addEventListener('click', () => {
this.$player.remove();
});
}
}
// Reset button
class ResetButton {
constructor($el, $input, life) {
this.$el = $el;
this.$input = $input;
this.life = life;
this.$player = $el.closest('.player');
this.$counters = this.$player.querySelectorAll('.counter__input');
this.$el.addEventListener('click', (e) => {
e.preventDefault();
$input.value = life;
this.$counters.forEach(($counter) => {
$counter.value = 0;
});
new LifeCounter(this.$player);
new SecondaryCounters(this.$player);
});
}
}
// Add button
class AddButtons {
constructor($el) {
this.$el = $el;
this.$buttons = this.$el.querySelectorAll('.add-button');
this.$content = document.querySelector('.content');
this.maxPlayers = 6;
this.$buttons.forEach(($button) => {
const startingLife = $button.dataset.life;
$button.addEventListener('click', () => {
const currentPlayers = document.querySelectorAll('.player').length;
if(currentPlayers < this.maxPlayers) {
new Player(this.$content, startingLife);
}
});
});
}
}
new AddButtons(document.querySelector('.intro'));
// Player
class Player {
constructor($el, life) {
this.$el = $el;
this.life = life;
this.$content = document.querySelector('.content');
this.$intro = document.querySelector('.intro');
// Include player template
this.playerTemplate = document.querySelector('.player-template');
this.clone = document.importNode(this.playerTemplate.content, true);
this.$el.appendChild(this.clone);
// Get current player
this.$currentPlayer = this.$content.querySelector('.player:last-of-type');
// Set life count
this.$currentLife = this.$currentPlayer.querySelector('.life__input');
this.$currentLife.value = this.life;
// Initialize delete button
this.$currentDeleteButton = this.$currentPlayer.querySelector('.player__delete');
new DeleteButton(this.$currentDeleteButton);
// Initialize reset button
this.$currentResetButton = this.$currentPlayer.querySelector('.counter__reset');
new ResetButton(this.$currentResetButton, this.$currentLife, this.life);
// Initialize counters
new LifeCounter(this.$currentPlayer);
new SecondaryCounters(this.$currentPlayer);
// Insert template
this.$el.appendChild(this.$intro);
this.$currentPlayer.focus();
}
}