-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.js
More file actions
101 lines (90 loc) · 3.07 KB
/
module.js
File metadata and controls
101 lines (90 loc) · 3.07 KB
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
export const fetchData = async () => {
const req = await fetch("data.json")
const res = await req.json()
return res
}
export const showDialog = () => {
const dialog = document.getElementsByClassName('dialog')[0]
dialog.showModal()
dialog.focus({ preventScroll: true });
dialog.scrollTop = 0;
}
export const timeout = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
export const handleDialog = (showDialog) => {
const dialog = document.getElementsByClassName('dialog')[0]
const whatBtn = document.getElementsByClassName("what")[0]
whatBtn.addEventListener("click", () => {
showDialog()
})
dialog.addEventListener("click", (event) => {
if (event.target === dialog) {
dialog.close();
}
});
const closeWhatBtn = document.getElementsByClassName("close-what")[0]
closeWhatBtn.addEventListener("click", () => {
dialog.close()
})
}
export const makingBox = (targetAmount, oneBoxValue, progress) => {
const noOfBoxes = targetAmount / oneBoxValue;
for (let i = 1; i <= noOfBoxes; i++) {
const img = document.createElement("img")
img.setAttribute("src", "/media/locked.png")
img.classList.add("img");
const box = document.createElement("div")
box.appendChild(img)
box.classList.add('box')
progress.appendChild(box)
}
}
export const modifyFilledAndNearBoxes = (filledBoxes, data) => {
for (let i = 0; i <= filledBoxes; i++) {
const box = document.getElementsByClassName(`box`)[i]
if (i < filledBoxes) {
const tooltipText = document.createElement("span")
tooltipText.classList.add("tooltiptext")
tooltipText.innerText = `${data[i].title}: ${data[i].description}`
box.appendChild(tooltipText)
box.setAttribute("id", "filled")
box.classList.add("tooltip")
} else {
box.setAttribute("class", "near")
}
}
}
export const getData = async (fetchData, oneBoxValue) => {
const fetchedData = await fetchData()
const data = fetchedData.flatMap((item) => {
if (item.amount > oneBoxValue) {
const correctItem = { ...item, amount: oneBoxValue }
const noOfItem = item.amount / oneBoxValue
return Array.from({ length: noOfItem }, () => ({ ...correctItem }))
} else {
return item
}
})
return data
}
export const getUpdates = async () => {
const req = await fetch("updates.json")
const res = await req.json()
return res
}
export const makingUpdates = (updates) => {
const updateTable = document.getElementsByClassName("updateTable")[0]
for (let i = 0; i < updates.length; i++) {
const update = document.createElement("li")
update.innerHTML = updates[i]
updateTable.appendChild(update)
}
}
export const runDialogForFirstTime = (showDialog) => {
const isVisited = localStorage.getItem("isVisited")
if (!isVisited) {
showDialog()
localStorage.setItem("isVisited", true)
}
}