This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
266 lines (231 loc) · 7.92 KB
/
index.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
const backBtn = document.querySelector(".back-btn");
const closeModal = document.querySelector(".close-modal");
const bookmarkBtn = document.querySelector(".bookmark");
const typeSelection = document.querySelectorAll(".type-selection");
const pledgeInput = document.querySelectorAll(".pledge-input");
const gotItBtn = document.querySelector(".got-it");
const hamburgerBtn = document.querySelector(".hamburger-menu");
const navigationMenu = document.querySelector(".menu");
const selectReward = document.querySelectorAll(".select-reward");
const pledgeName = document.querySelectorAll(".info .item-header h3");
const modal = document.querySelector(".modal");
const modalSuccess = document.querySelector(".modal-success");
const item2 = document.querySelector("#item-2");
const item2Selection = document.querySelector("#item-2 .type-selection");
const item3 = document.querySelector("#item-3");
const item3Selection = document.querySelector("#item-2 .type-selection");
const item4 = document.querySelector("#item-4");
const item4Selection = document.querySelector("#item-2 .type-selection");
const forms = document.querySelectorAll(".form");
// Data
// let amountBacked = 89914;
let amountBackedTotal = 89914;
let totalBackers = 5007;
let bambooCapacity = 101;
let blackCapacity = 64;
let amountPledged = 0;
function showModal() {
document.body.classList.add("show-modal");
}
function hideModal() {
document.body.classList.remove("show-modal");
}
function uncompleteModal() {
modal.classList.remove("completed");
}
function completeModal() {
modal.classList.add("completed");
}
function scrollToItem() {
modalSuccess.scrollIntoView(true);
}
//Back btn
backBtn.addEventListener("click", showModal);
//Close Modal
closeModal.addEventListener("click", () => {
hideModal();
uncompleteModal();
});
//Toggle bookmark class
bookmarkBtn.addEventListener("click", () => {
bookmarkBtn.classList.toggle("bookmarked");
});
//Type selection
//Choose pledge
pledgeName.forEach((item) => {
item.addEventListener("click", () => {
item.parentElement.previousElementSibling.click();
});
});
forms.forEach((form) => {
form.addEventListener("submit", (e) => {
e.preventDefault();
const targetInput = e.target.querySelector(".pledge-input");
if (targetInput.value === targetInput.defaultValue) {
if (localStorage) {
localStorage.setItem("amountPledged", +targetInput.defaultValue + +localStorage.getItem("amountPledged"));
}
else {
localStorage.setItem("amountPledged", +targetInput.defaultValue + amountPledged);
}
}
else {
if (localStorage) {
localStorage.setItem("amountPledged", +targetInput.value + +localStorage.getItem("amountPledged"));
}
else {
localStorage.setItem("amountPledged", +targetInput.value + amountPledged);
}
}
if (localStorage) {
AnimateIncreaseNum(document.getElementById("increment-one"), 0, amountBackedTotal + +localStorage.getItem("amountPledged"));
}
else {
AnimateIncreaseNum(document.getElementById("increment-one"), 0, amountBackedTotal + amountPledged);
}
//! Bamboo Capacity
if (+localStorage.getItem("bambooCapacityStorage") > 0 && e.target.querySelector(`[aria-label="Bamboo"]`) !== null) {
localStorage.setItem("bambooCapacityStorage", +localStorage.getItem("bambooCapacityStorage") - 1);
// document.querySelector(".bamboo-capacity").textContent = +localStorage.getItem("bambooCapacityStorage");
}
//! Black Capacity
if (+ localStorage.getItem("blackCapacityStorage") > 0 && e.target.querySelector(`[aria-label="Black"]`) !== null) {
localStorage.setItem("blackCapacityStorage", +localStorage.getItem("blackCapacityStorage") - 1);
}
//! Black Capacity
if (+localStorage.getItem("blackCapacityStorage") === 0 && e.target.querySelector(`[aria-label="Black"]`) !== null) {
localStorage.setItem("blackCapacityStorage", blackCapacity - 1);
}
//! Bamboo Capacity
if (+ localStorage.getItem("bambooCapacityStorage") === 0 && e.target.querySelector(`[aria-label="Bamboo"]`) !== null) {
localStorage.setItem("bambooCapacityStorage", bambooCapacity - 1);
}
// ! Update HTML if capacity changed
if (+localStorage.getItem("bambooCapacityStorage") === 0) {
document.querySelectorAll(".bamboo-capacity").forEach(span => span.textContent = bambooCapacity);
}
else {
document.querySelectorAll(".bamboo-capacity").forEach(span => span.textContent = +localStorage.getItem("bambooCapacityStorage"));
}
// ! Update HTML if capacity changed
if (+localStorage.getItem("blackCapacityStorage") === 0) {
document.querySelectorAll(".black-capacity").forEach(span => span.textContent = blackCapacity);
}
else {
document.querySelectorAll(".black-capacity").forEach(span => span.textContent = +localStorage.getItem("blackCapacityStorage"));
}
completeModal();
scrollToItem();
});
})
//'Got It' Btn
gotItBtn.addEventListener("click", () => {
hideModal();
uncompleteModal();
typeSelection.forEach((button) => {
if (button.checked) {
button.checked = false;
button.parentElement.parentElement.classList.remove("item-selected");
}
});
});
//Hamburger menu
hamburgerBtn.addEventListener("click", () => {
navigationMenu.classList.toggle("active");
});
const getReward = (activeItem, passiveItem) => {
passiveItem.querySelector(".type-selection").checked = false;
passiveItem.classList.remove("item-selected");
activeItem.scrollIntoView(true);
activeItem.querySelector(".type-selection").checked = true;
activeItem.classList.add("item-selected");
}
const toggleClass = (on, off) => {
off.classList.remove("item-selected");
on.classList.add("item-selected");
}
//Reward selection
selectReward.forEach((reward) => {
reward.addEventListener("click", () => {
showModal();
if (reward.id == "reward-1") {
getReward(item2, item3);
}
if (reward.id == "reward-2") {
getReward(item3, item2);
}
typeSelection.forEach((button) => {
button.addEventListener("change", (e) => {
if (e.target.value == "bamboo") {
toggleClass(item2, item3);
}
else if (e.target.value == "black") {
toggleClass(item3, item2);
}
if (button.id == "support") {
completeModal();
scrollToItem();
}
});
});
});
});
// Numbers Animation
function AnimateIncreaseNum(
target,
start,
end,
increment = 200,
dollar = true
) {
let animationEvent = setInterval(() => {
start += increment;
if (start >= end) {
clearInterval(animationEvent);
start = end;
}
if (dollar) {
target.innerHTML = `${start}`;
} else {
target.innerHTML = `${start}`;
}
}, 1);
}
let Scroll100Flag = true;
window.addEventListener("scroll", () => {
if (Scroll100Flag) {
if (window.scrollY >= 50) {
Scroll100Flag = false;
if (localStorage) {
AnimateIncreaseNum(document.getElementById("increment-one"), 0, amountBackedTotal + +localStorage.getItem("amountPledged"));
}
else {
AnimateIncreaseNum(document.getElementById("increment-one"), 0, amountBackedTotal + amountPledged);
}
AnimateIncreaseNum(
document.getElementById("increment-two"),
0,
totalBackers,
(increment = 10),
(dollar = false)
);
AnimateIncreaseNum(
document.getElementById("increment-three"),
0,
56,
(increment = 1),
(dollar = false)
);
}
}
});
window.addEventListener("load", () => {
if (localStorage) {
document.getElementById("increment-one").textContent = amountBackedTotal + +localStorage.getItem("amountPledged");
}
else {
AnimateIncreaseNum(document.getElementById("increment-one"), 0, amountBackedTotal + amountPledged);
}
document.getElementById("increment-two").textContent = totalBackers;
document.getElementById("increment-three").textContent = 56;
})