-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
59 lines (55 loc) · 1.63 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
document.querySelectorAll('.truthlie-container button').forEach(button => {
button.addEventListener('click', () => {
// parent container
const container = button.closest('.truthlie-container');
const buttons = container.querySelectorAll('button');
var activeButtons = 0;
var truths = 0;
// check if all buttons are in active state
buttons.forEach(btn => {
if (btn.classList.contains('active')) {
activeButtons += 1;
if (btn.classList.contains('truth')) {
truths += 1;
}
}
});
if (activeButtons == 3){
buttons.forEach(btn => {
toggleButton(btn, false);
});
return;
}
// if lie or if 2 truths are active, reveal all buttons
if (button.classList.contains('lie') || (truths == 1 && !button.classList.contains('active'))) {
buttons.forEach(btn => {
toggleButton(btn, true);
});
}
// else, activate selected button
else {
toggleButton(button, true);
}
});
});
function toggleButton(btn, makeActive) {
if (makeActive) {
if (btn.classList.contains('truth')) {
btn.style.borderColor = "#fff";
btn.style.backgroundColor = "#4CAF50";
btn.style.textShadow = "2px 2px 2px rgba(0, 0, 0, 0.7)";
}
else if (btn.classList.contains('lie')) {
btn.style.borderColor = "#fff";
btn.style.backgroundColor = "#E53935";
btn.style.textShadow = "2px 2px 2px rgba(0, 0, 0, 0.7)";
}
btn.classList.add('active');
}
else {
btn.style.borderColor = "";
btn.style.backgroundColor = "";
btn.style.textShadow = "";
btn.classList.remove('active');
}
}