-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
530 lines (436 loc) · 15.3 KB
/
script.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
window.addEventListener("DOMContentLoaded", start());
function start() {
const Utils = {
formatCurrency(value) {
const signal = Number(value) < 0 ? "-" : "";
const onlyDigitsInString = String(value).replace(/\D/g, "");
const formatedValue = Number(onlyDigitsInString) / 100;
const currency = formatedValue.toLocaleString("en-us", {
style: "currency",
currency: "USD",
});
return signal + currency;
},
sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
},
formatAmount(value) {
return Math.round(value * 100);
},
formatDate(date) {
const [year, month, day] = date.split("-");
return `${day}/${month}/${year}`;
},
};
async function LoadingAppAnimation() {
const timeline = new TimelineMax();
timeline
.fromTo(
".bg-loader",
1,
{ width: "100%" },
{ width: "0%", delay: 5, ease: Expo.easeInOut }
)
.fromTo(
"header",
2,
{ width: "0%", opacity: 0 },
{ width: "100%", opacity: 1, ease: Expo.easeInOut },
"-=1"
)
.fromTo(
"main.container",
0.7,
{ y: -50, opacity: 0 },
{ y: 0, opacity: 1, ease: Expo.easeInOut },
"-=0.5"
)
.fromTo(
".button.open-modal-download",
0.7,
{ y: -50, opacity: 0 },
{ y: 0, opacity: 1, ease: Expo.easeInOut },
"-=0.5"
)
.fromTo(
"footer p",
0.7,
{ y: -50, opacity: 0 },
{ y: 0, opacity: 1, ease: Expo.easeInOut },
"-=0.5"
);
await Utils.sleep(8 * 1000);
return;
}
function FormModalManipulation() {
const ModalElement = document.querySelector(".modal-overlay.form");
const openModalButton = document.querySelector(".button.new");
const cancelModalUse = document.querySelector(".button.cancel.form");
const ModalFunctions = {
toggle: () => () => ModalElement.classList.toggle("active"),
};
openModalButton.addEventListener("click", ModalFunctions.toggle());
cancelModalUse.addEventListener("click", ModalFunctions.toggle());
}
function Transactions() {
const Storage = {
get() {
return (
JSON.parse(localStorage.getItem("dev.finances:transactions")) || []
);
},
set(transactions) {
localStorage.setItem(
"dev.finances:transactions",
JSON.stringify(transactions)
);
},
};
const TransactionFunctions = {
all: Storage.get(),
add(transaction) {
TransactionFunctions.all.push(transaction);
App.reload();
},
remove(index) {
const transaction = TransactionFunctions.all[index];
TransactionFunctions.all.splice(index, 1);
App.reload();
return transaction;
},
incomes() {
let income = 0;
TransactionFunctions.all.forEach(({ amount }) => {
if (amount > 0) income += amount;
});
return income;
},
expenses() {
let expense = 0;
TransactionFunctions.all.forEach(({ amount }) => {
if (amount < 0) expense += amount;
});
return expense;
},
total() {
return TransactionFunctions.incomes() + TransactionFunctions.expenses();
},
};
const DOM = {
transactionsContainer: document.querySelector("#data-table tbody"),
addTransaction(transaction, index) {
const tr = document.createElement("tr");
tr.innerHTML = DOM.innerHtmlTransaction(transaction, index);
tr.dataset.index = index;
const image = tr.querySelector(`img#remove_${index}`);
image.addEventListener("click", () => {
const { description } = TransactionFunctions.remove(index);
window.NotificateUser(`The transaction ${description} was deleted`);
if (window.userAcceptedVoice) {
window.notificateUserByVoice(
`The transaction ${description} was deleted`
);
}
});
DOM.transactionsContainer.appendChild(tr);
},
innerHtmlTransaction({ description, amount, date }, index) {
if (!description && !amount && !date) return "";
const CSSClass = amount < 0 ? "expense" : "income";
const formatedAmount = Utils.formatCurrency(amount);
const html = `
<td class="description">${description}</td>
<td class="${CSSClass}">${formatedAmount}</td>
<td class="date">${date}</td>
<td>
<img src="./assets/minus.svg" alt="remover transação" id="remove_${index}" class="remove_transaction">
</td>
`;
return html;
},
updateBalance() {
document.getElementById(
"incomeDisplay"
).innerHTML = Utils.formatCurrency(TransactionFunctions.incomes());
document.getElementById(
"expenseDisplay"
).innerHTML = Utils.formatCurrency(TransactionFunctions.expenses());
document.getElementById(
"totalDisplay"
).innerHTML = Utils.formatCurrency(TransactionFunctions.total());
},
clearTransactions() {
DOM.transactionsContainer.innerHTML = "";
},
};
const App = {
init() {
TransactionFunctions.all.forEach(DOM.addTransaction);
DOM.updateBalance();
Storage.set(TransactionFunctions.all);
},
reload() {
DOM.clearTransactions();
App.init();
},
};
window.APP = App;
window.TransactionFunctions = TransactionFunctions;
}
function FormSubmitManipulation() {
const descriptionInput = document.querySelector("input#description");
const amountInput = document.querySelector("input#amount");
const dateInput = document.querySelector("input#date");
function getValues() {
return {
description: descriptionInput.value,
amount: amountInput.value,
date: dateInput.value,
};
}
function validateFields() {
const { description, amount, date } = getValues();
if (
description.trim() === "" ||
amount.trim() === "" ||
date.trim() === ""
) {
throw new Error("Fill all the input data, Please!");
}
}
function formatValues() {
const { description, amount, date } = getValues();
const formatedAmount = Utils.formatAmount(amount);
const formatedDate = Utils.formatDate(date);
return {
description,
amount: formatedAmount,
date: formatedDate,
};
}
function closeModal() {
removeErrors();
document.querySelector(".button.cancel.form").click();
}
async function showErrorToUser(error) {
console.error(error);
const errorContainer = document.querySelector(".input-group.error p");
errorContainer.innerHTML = error;
const errorLine = document.querySelector(".error-line");
while (errorLine.style.width !== "100%") {
const width = Number(errorLine.style.width.replace("%", ""));
await Utils.sleep(1);
errorLine.style.width = `${width + 1}%`;
}
}
async function removeErrors() {
const errorLine = document.querySelector(".error-line");
errorLine.style.width = "0%";
const errorContainer = document.querySelector(".input-group.error p");
errorContainer.innerHTML = "";
}
function clearFields() {
descriptionInput.value = "";
amountInput.value = "";
dateInput.value = "";
}
const formElement = document.querySelector("form");
formElement.addEventListener("submit", (event) => {
event.preventDefault();
try {
removeErrors();
validateFields();
const transaction = formatValues();
window.TransactionFunctions.add(transaction);
window.NotificateUser(
`The transction ${transaction.description} was added`
);
if (window.userAcceptedVoice) {
window.notificateUserByVoice(
`The transaction ${transaction.description} was added`
);
}
clearFields();
closeModal();
} catch (error) {
showErrorToUser(error.message);
}
});
}
function DarkMode() {
const darkModeCheckbox = document.querySelector("#switch");
function changeText(hasDarkMode,showNotification=true) {
const p = document.querySelector(".toggle p");
p.textContent = hasDarkMode ? "Dark Mode" : "Light Mode";
darkModeCheckbox.checked = p.textContent === "Dark Mode";
if(!showNotification) return;
window.NotificateUser(
`Selectd Theme:${p.textContent || "Light Mode"}`
);
if (window.userAcceptedVoice) {
window.notificateUserByVoice(
`Selectd Theme:${p.textContent || "Light Mode"}`
);
}
}
const onChangeButton = () => {
const html = document.querySelector("html");
const hasDarkMode = html.classList.toggle("dark-mode");
localStorage.setItem("dev.finances:mode", String(hasDarkMode));
changeText(hasDarkMode);
};
const hasDarkMode =
(localStorage.getItem("dev.finances:mode") || "false") === "false"
? false
: true;
changeText(hasDarkMode,false);
if (hasDarkMode) onChangeButton();
darkModeCheckbox.addEventListener("change", onChangeButton);
}
function Notifications(message) {
if (!("Notification" in window)) return;
if (!message) return;
if (Notification.permission === "granted") {
const notification = new Notification(message);
} else if (Notification.permission !== "denied") {
Notification.requestPermission((permission) => {
if (permission === "granted") {
const notification = new Notification(message);
}
});
}
}
function SquaresAnimation() {
const ulSquares = document.querySelector("ul.squares");
const random = (min, max) => Math.random() * (max - min) + min;
for (let squareIndex = 0; squareIndex <= 6; squareIndex++) {
const li = document.createElement("li");
const size = Math.floor(random(10, 120));
const bottom = Math.floor(random(1, 20));
const position = random(1, 99);
const delay = random(5, 0.1);
const duration = random(24, 12);
li.style.width = `${size}px`;
li.style.height = `${size}px`;
li.style.bottom = `${bottom}px`;
li.style.left = `${position}%`;
li.style.animationDelay = `${delay}s`;
li.style.animationDuration = `${duration}s`;
li.style.animationTimingFunction = `cubic-bezier(${Math.random()},${Math.random()},${Math.random()},${Math.random()})`;
ulSquares.appendChild(li);
}
}
function DownloadJSON() {
const downloadModal = document.querySelector(".modal-overlay.download");
const downloadButton = document.querySelector(
".button.open-modal-download"
);
const closeModalButton = document.querySelector(".button.cancel-download");
const saveJsonButton = document.querySelector(".button.download-json");
function getText() {
const array = [];
window.TransactionFunctions.all.forEach(
({ amount, description, date }) => {
const formatedAmount = Utils.formatCurrency(amount);
array.push({ formatedAmount, description, date });
}
);
array.push({
incomes: Utils.formatCurrency(TransactionFunctions.incomes()),
expenses: Utils.formatCurrency(TransactionFunctions.expenses()),
total: Utils.formatCurrency(TransactionFunctions.total()),
});
return array;
}
downloadButton.addEventListener("click", () => {
const transactionsInString = JSON.stringify(getText(), null, 4);
downloadModal.classList.add("active");
downloadModal.querySelector("pre").innerHTML = transactionsInString;
});
closeModalButton.addEventListener("click", () =>
downloadModal.classList.remove("active")
);
saveJsonButton.addEventListener("click", () => {
const transactionsInString = JSON.stringify(getText(), null, 4);
const blobOfFile = new Blob([transactionsInString], {
type: "application/json",
});
const anchora = document.createElement("a");
anchora.href = window.URL.createObjectURL(blobOfFile);
const filename = `transactions-${Date.now()}.json`;
anchora.download = filename;
document.body.appendChild(anchora);
anchora.click();
document.body.removeChild(anchora);
downloadModal.classList.remove("active");
window.NotificateUser(`O arquivo ${filename} foi baixado`);
if (window.userAcceptedVoice) {
window.notificateUserByVoice(`O arquivo json foi baixado`);
}
});
}
function VoiceNotificationsModalManipulation() {
const voiceNotificationsModal = document.querySelector(
".modal-overlay.acessibility"
);
const userAlreadyUseThisApp =
(localStorage.getItem("dev.finances:user_already_use_this_app") ||
"false") === "true"
? true
: false;
if (userAlreadyUseThisApp) {
window.userAcceptedVoice =
(localStorage.getItem("dev.finances:voicenotifications") || "false") ===
"true"
? true
: false;
return;
}
voiceNotificationsModal.classList.add("active");
const cancelVoiceButton = voiceNotificationsModal.querySelector(
".button.cancel_voice"
);
const acceptVoiceButton = voiceNotificationsModal.querySelector(
".button.accept_voice"
);
const userHaveClicked = () => {
localStorage.setItem("dev.finances:user_already_use_this_app", "true");
voiceNotificationsModal.classList.remove("active");
};
cancelVoiceButton.addEventListener("click", () => {
localStorage.setItem("dev.finances:voicenotifications", "false");
window.userAcceptedVoice = false;
userHaveClicked();
});
acceptVoiceButton.addEventListener("click", () => {
localStorage.setItem("dev.finances:voicenotifications", "true");
window.userAcceptedVoice = true;
userHaveClicked();
});
}
function VoiceNotifications(message) {
if (!"speechSynthesis" in window) return;
VoiceNotificationsModalManipulation();
if (!window.userAcceptedVoice) return;
// voice notification
if(!message) return;
const msg = new SpeechSynthesisUtterance();
msg.text = message;
msg.lang = "en";
window.speechSynthesis.speak(msg);
return;
}
return async () => {
await LoadingAppAnimation();
window.NotificateUser = Notifications;
window.notificateUserByVoice = VoiceNotifications;
VoiceNotifications();
SquaresAnimation();
DarkMode();
Transactions();
DownloadJSON();
FormModalManipulation();
FormSubmitManipulation();
window.APP.init();
};
}