-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
129 lines (111 loc) · 4.03 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
127
128
129
const subCount = document.querySelector("#subCount");
const addCount = document.querySelector("#addCount");
const subBonus = document.querySelector("#subBonus");
const addBonus = document.querySelector("#addBonus");
const updateMultiplier = (value) => {
subCount.disabled = value === 1;
[...document.querySelectorAll(".multiplier")].forEach((x) => {
x.textContent = value === 1 ? "" : value;
});
};
const updateAdditioner = (value) => {
[...document.querySelectorAll(".additioner")].forEach((x) => {
x.textContent = value === 0 ? "" : forceSymbol(value);
});
};
addBonus.onclick = () => {
const previous = parseInt(document.querySelector("#bonus").textContent);
const value = previous + 1;
document.querySelector("#bonus").textContent =
value === 0 ? 0 : forceSymbol(value);
updateAdditioner(value);
};
subBonus.onclick = () => {
const previous = parseInt(document.querySelector("#bonus").textContent);
const value = previous - 1;
document.querySelector("#bonus").textContent =
value === 0 ? 0 : forceSymbol(value);
updateAdditioner(value);
};
addCount.onclick = () => {
const previous = parseInt(document.querySelector("#count").textContent);
const value = previous + 1;
document.querySelector("#count").textContent = `${value}d`;
updateMultiplier(value);
};
subCount.onclick = () => {
const previous = parseInt(document.querySelector("#count").textContent);
const value = previous - 1;
document.querySelector("#count").textContent = `${value}d`;
updateMultiplier(value);
};
const displayDialog = (multiplier, additioner, value, rolls) => {
const formatBonus = additioner === 0 ? "" : forceSymbol(additioner);
const formatInput = `${multiplier}d${value}${formatBonus}`;
favDialog.querySelector(".ask").textContent = formatInput;
favDialog.querySelector(".sum").textContent = sum(rolls) + additioner;
favDialog.querySelector(".rolls").textContent = rolls.join(", ");
favDialog.showModal();
};
document.querySelector("#count").ondblclick = (e) => {
e.target.textContent = "1d";
updateMultiplier(1);
};
document.querySelector("#bonus").ondblclick = (e) => {
e.target.textContent = "0";
updateAdditioner(0);
};
[...document.querySelectorAll(".dice")].forEach((d) => {
const value = parseInt(d.dataset.value);
d.onclick = () => {
const multiplier = parseInt(document.querySelector("#count").textContent);
const additioner = parseInt(document.querySelector("#bonus").textContent);
const rolls = repeat(multiplier, () => roll(value));
d.firstElementChild.classList.add("wait");
d.firstElementChild.classList.add("roll");
setTimeout(() => {
displayDialog(multiplier, additioner, value, rolls);
d.firstElementChild.classList.remove("wait");
d.firstElementChild.classList.remove("roll");
}, 450);
};
});
updateMultiplier(1);
updateAdditioner(0);
const closeDialog = (event) => {
if (event.target.contains(favDialog)) {
favDialog.close();
}
};
document.addEventListener("click", closeDialog);
let deferredPrompt;
const addBtn = document.querySelector(".add-button");
addBtn.style.display = "none";
window.addEventListener("beforeinstallprompt", (e) => {
// Prevent Chrome 67 and earlier from automatically showing the prompt
e.preventDefault();
// Stash the event so it can be triggered later.
deferredPrompt = e;
// Update UI to notify the user they can add to home screen
addBtn.style.display = "block";
addBtn.addEventListener("click", (e) => {
// hide our user interface that shows our A2HS button
addBtn.style.display = "none";
// Show the prompt
deferredPrompt.prompt();
// Wait for the user to respond to the prompt
deferredPrompt.userChoice.then((choiceResult) => {
if (choiceResult.outcome === "accepted") {
console.log("User accepted the A2HS prompt");
} else {
console.log("User dismissed the A2HS prompt");
}
deferredPrompt = null;
});
});
});
if ("serviceWorker" in navigator) {
window.addEventListener("load", function () {
navigator.serviceWorker.register("/sw.js");
});
}