-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
201 lines (184 loc) · 5.76 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
// Define the default list
const defaultData = [
{ front: "dog", back: "Hund" },
{ front: "cat", back: "Katze" },
{ front: "bird", back: "Vogel" },
];
// Function to get the data from URL parameters or use the default data
async function getDataFromURL() {
const params = new URLSearchParams(window.location.search);
const dataParam = params.get('data');
const dataURLParam = params.get('dataseturl');
if (dataParam) {
try {
// Decode and parse the JSON string into a JavaScript object
return JSON.parse(decodeURIComponent(dataParam));
} catch (e) {
console.error("Failed to parse JSON from URL", e);
// Return default data in case of error
return defaultData;
}
} else if (dataURLParam) {
try {
const response = await fetch(decodeURIComponent(dataURLParam));
if (!response.ok) {
throw new Error("Network response was not ok");
}
const data = await response.json();
return data;
} catch (e) {
console.error("Failed to fetch data from URL", e);
// Return default data in case of error
return defaultData;
}
} else {
// Return default data if no data or dataseturl parameter is found
return defaultData;
}
}
var data;
// Usage example
(async () => {
data = await getDataFromURL();
let learningList = [...data]; // Clone the data;
let currentIndex = 0;
let currentSide = "front";
const cardElement = document.getElementById("card");
const cardText = document.getElementById("card-text");
const nextButton = document.getElementById("next-button");
const beforeButton = document.getElementById("before-button");
const progressText = document.getElementById("progress-text");
const progressBar = document.getElementById("progress-bar");
function calculatePercentage(part, whole) {
if (whole === 0) {
throw new Error("The whole number cannot be zero.");
}
return (part / whole) * 100;
}
function updateProgress() {
progressText.innerText = `${learningList.length} / ${data.length}`;
var percent = calculatePercentage(data.length - learningList.length,data.length)
progressBar.style.width = `${percent}%`;
}
function updateCard() {
let newText; // Define newText here so it's available throughout the function
if (learningList.length == 0) {
newText = "Fertig!"; // Assign an empty string to newText if learningList is empty
// Pass in the id of an element
confetti({
particleCount: 100,
spread: 70,
origin: { y: 0.6 },
});
nextButton.style.display = "none";
beforeButton.style.display = "none";
progressText.style.display = "none";
progressBar.style.display = "none";
} else {
newText = learningList[currentIndex][currentSide]; // Assign the value based on the current side
}
cardText.innerText = newText;
if (currentSide == "front") {
cardElement.classList.remove("flipped");
} else {
cardElement.classList.add("flipped");
}
updateProgress();
}
function flipCard() {
if (currentSide == "front") {
currentSide = "back";
} else {
currentSide = "front";
}
updateCard();
}
function nextCard() {
cardText.classList.add("switch");
setTimeout(() => {
cardText.classList.remove("switch");
}, 500);
setTimeout(() => {
updateCard();
}, 200);
}
cardElement.addEventListener("click", (event) => {
flipCard();
});
nextButton.addEventListener("click", (event) => {
currentSide = "front";
learningList.shift();
nextCard();
});
beforeButton.addEventListener("click", (event) => {
currentSide = "front";
learningList.push(learningList[0]);
learningList.shift();
nextCard();
});
updateCard();
})();
const driver = window.driver.js.driver;
const driverObj = driver({
showButtons: ["next", "previous", "close"],
nextBtnText: "Weiter",
prevBtnText: "Zurück",
doneBtnText: "Schließen",
progressText: "{{current}} von {{total}}",
showProgress: true,
steps: [
{
element: ".card",
popover: {
title: "Das sind deine Karteikarten",
description:
"Wenn du auf sie klicks / tippst, kannst du die andere Seite sehen",
side: "bottom",
align: "start",
},
},
{
element: "#next-button",
popover: {
title: "Wenn du eine vokabel schon gut kannst klickst / tippst du hier",
description: "Sie wird dann (in dieser runde) nicht mehr abgefragt",
side: "bottom",
align: "start",
},
},
{
element: "#before-button",
popover: {
title:
"Wenn du eine vokabel noch nicht so gut kannst klickst / tippst du hier",
description: "Sie wird dann am ende noch einmal abgefragt",
side: "bottom",
align: "start",
},
},
{
element: "#progress-text",
popover: {
title:
"Hier siehst du, wie viel karten du noch abgefragt wirst",
side: "bottom",
align: "start",
},
},
{
element: ".create-button",
popover: {
title:
"Hier kannst du dein eigenes Lernset erstellen",
side: "bottom",
align: "start",
},
},
{
popover: {
title:
"Viel Spaß!",
},
},
],
});