-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
401 lines (345 loc) Β· 17.5 KB
/
script.js
File metadata and controls
401 lines (345 loc) Β· 17.5 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
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
const GEMINI_API_KEY = "AIzaSyAug3QzeXsavkWaGzuMNTGafLmLSva9dZU";
const EDEN_API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMjI3ZWUwNjItZjE4ZS00NjFjLTg0OTgtNGU3MDc2MDFjMjMwIiwidHlwZSI6ImFwaV90b2tlbiJ9.sftmqvfpn7Jwvts9-4a-vFi3qK-QQOZuKRcBzhbRL3Y";
const GEMINI_API_URL = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-002:generateContent?key=${GEMINI_API_KEY}`;
const EDEN_API_URL = "https://api.edenai.run/v2/image/generation";
document.addEventListener("DOMContentLoaded", () => {
const generateBtn = document.getElementById("generate-btn");
const flashcardsContainer = document.querySelector(".flashcards-container");
const templateSelect = document.getElementById("template-select");
const themeToggle = document.getElementById("theme-toggle");
const themeIcon = document.getElementById("theme-icon");
const htmlElement = document.documentElement;
const userInput = document.getElementById("user-input");
const cardCount = document.getElementById("card-count");
// Add template and color selectors to the DOM
const templateSelectorHTML = `
<div class="template-controls animate__animated animate__fadeIn">
<h4 class="mb-3">Choose Your Style</h4>
<div class="template-selector">
<div class="template-option active" data-template="classic">
<span class="template-icon">ποΈ</span>
<span class="template-name">Classic</span>
</div>
<div class="template-option" data-template="modern">
<span class="template-icon">π</span>
<span class="template-name">Modern</span>
</div>
<div class="template-option" data-template="vibrant">
<span class="template-icon">β¨</span>
<span class="template-name">Vibrant</span>
</div>
<div class="template-option" data-template="gradient">
<span class="template-icon">π</span>
<span class="template-name">Gradient</span>
</div>
</div>
<div class="color-selector" id="classic-colors">
<div class="color-option color-classic-sage active" data-color="sage"></div>
<div class="color-option color-classic-mint" data-color="mint"></div>
<div class="color-option color-classic-cream" data-color="cream"></div>
<div class="color-option color-classic-lavender" data-color="lavender"></div>
</div>
<div class="color-selector" id="modern-colors" style="display: none;">
<div class="color-option color-modern-blue active" data-color="blue"></div>
<div class="color-option color-modern-purple" data-color="purple"></div>
<div class="color-option color-modern-coral" data-color="coral"></div>
<div class="color-option color-modern-slate" data-color="slate"></div>
</div>
<div class="color-selector" id="vibrant-colors" style="display: none;">
<div class="color-option color-vibrant-yellow active" data-color="yellow"></div>
<div class="color-option color-vibrant-teal" data-color="teal"></div>
<div class="color-option color-vibrant-pink" data-color="pink"></div>
<div class="color-option color-vibrant-lime" data-color="lime"></div>
</div>
<div class="color-selector" id="gradient-colors" style="display: none;">
<div class="color-option color-gradient-sunset active" data-color="sunset"></div>
<div class="color-option color-gradient-ocean" data-color="ocean"></div>
<div class="color-option color-gradient-forest" data-color="forest"></div>
<div class="color-option color-gradient-berry" data-color="berry"></div>
</div>
<div class="color-selector" id="illustrated-colors" style="display: none;">
<div class="color-option color-illustrated-light active" data-color="light"></div>
<div class="color-option color-illustrated-dark" data-color="dark"></div>
<div class="color-option color-illustrated-nature" data-color="nature"></div>
<div class="color-option color-illustrated-tech" data-color="tech"></div>
</div>
</div>
`;
// Insert template controls after the user input container
const userInputContainer = document.querySelector(".user-input-container");
userInputContainer.insertAdjacentHTML('afterend', templateSelectorHTML);
// Function to set the theme
function setTheme(theme) {
document.documentElement.setAttribute("data-theme", theme);
localStorage.setItem("theme", theme);
themeIcon.textContent = theme === "light" ? "βοΈ Light" : "π Dark";
}
// Load theme on page load
function loadTheme() {
const savedTheme = localStorage.getItem("theme") || "light";
setTheme(savedTheme);
}
// Toggle theme
function toggleTheme() {
const currentTheme = localStorage.getItem("theme") || "light";
const newTheme = currentTheme === "light" ? "dark" : "light";
setTheme(newTheme);
}
// Event Listeners
themeToggle.addEventListener("click", toggleTheme);
window.addEventListener("load", loadTheme);
// Template and Color Selection
let currentTemplate = "classic";
let currentColor = "sage";
document.querySelectorAll('.template-option').forEach(option => {
option.addEventListener('click', () => {
document.querySelectorAll('.template-option').forEach(opt => opt.classList.remove('active'));
option.classList.add('active');
currentTemplate = option.dataset.template;
document.querySelectorAll('.color-selector').forEach(selector => selector.style.display = 'none');
document.getElementById(`${currentTemplate}-colors`).style.display = 'flex';
const defaultColorElement = document.querySelector(`#${currentTemplate}-colors .color-option`);
currentColor = defaultColorElement.dataset.color;
document.querySelectorAll('.color-option').forEach(opt => opt.classList.remove('active'));
defaultColorElement.classList.add('active');
updateFlashcardStyles();
});
});
document.querySelectorAll('.color-selector').forEach(selector => {
selector.addEventListener('click', (event) => {
if (event.target.classList.contains('color-option')) {
document.querySelectorAll('.color-option').forEach(opt => opt.classList.remove('active'));
event.target.classList.add('active');
currentColor = event.target.dataset.color;
updateFlashcardStyles();
}
});
});
function updateFlashcardStyles() {
document.querySelectorAll('.flashcard').forEach(card => {
card.className = card.className
.replace(/classic|modern|vibrant|gradient/g, '')
.replace(/sage|mint|cream|lavender|blue|purple|coral|slate|yellow|teal|pink|lime|sunset|ocean|forest|berry|light|dark|nature|tech/g, '')
.trim();
card.classList.add('flashcard', currentTemplate, currentColor);
if (currentTemplate === 'illustrated') {
refreshIllustratedBackground(card);
}
});
}
// Generate Image with Eden AI
async function generateEdenImage(prompt) {
const stylePrompt = `${prompt} - cartoon style, vibrant colors, flat design, digital illustration, no realism, educational diagram, white background`;
try {
const response = await fetch(EDEN_API_URL, {
method: "POST",
headers: {
"Authorization": `Bearer ${EDEN_API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
providers: "replicate",
text: stylePrompt,
resolution: "512x512",
num_images: 1,
parameters: {
style_preset: "digital-art",
guidance_scale: 10,
steps: 90
}
})
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
return data.replicate?.items?.[0]?.image_resource_url || null;
} catch (error) {
console.error("Eden AI Error:", error);
return null;
}
}
// Generate Flashcards
generateBtn.addEventListener("click", async () => {
const userInputValue = userInput.value.trim();
const cardCountValue = parseInt(cardCount.value) || 5;
if (!userInputValue) {
alert("Please enter a topic.");
return;
}
generateBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Generating...';
generateBtn.disabled = true;
try {
const response = await fetch(GEMINI_API_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
contents: [{
parts: [{
text: `You are an AI trained in Electric Vehicle diagnostics. Generate exactly ${cardCountValue} structured flashcards for: "${userInputValue}".
Format strictly as follows:
---
Title: [EV_ERROR_CODE_OR_FAULT_CATEGORY] - [TECHNICAL_COMPONENT]
Instruction: [MAX 25 WORDS: Code meaning, causes (physical/software), vehicle impact, safety alerts]
Detail: [MAX 15 WORDS: DIY steps, tools needed, professional help triggers]
Icon: [RELEVANT_EMOJI_FOR_VISUAL_REPRESENTATION]
---
Example Output:
Title: P0AA6 - Battery Isolation Fault
Instruction: High voltage isolation failure. Causes: coolant intrusion, damaged wiring. Impact: propulsion shutdown. Safety: wear Class 0 gloves before inspection.
Detail: Check battery seals, measure isolation resistance (>500Ξ©), replace damaged modules.
Icon: β‘β οΈ
Only return ${cardCountValue} flashcards in this exact format without commentary`
}]
}]
})
});
if (!response.ok) throw new Error(`API request failed: ${response.status}`);
const geminiData = await response.json();
const flashcardText = geminiData.candidates?.[0]?.content?.parts?.[0]?.text;
if (!flashcardText) throw new Error("No flashcards generated by Gemini");
// Improved parsing with error handling
const flashcards = flashcardText
.split(/---\n/g)
.filter(rawCard => rawCard.trim())
.map(rawCard => {
const cardData = {};
const lines = rawCard.split('\n').filter(line => line.trim());
lines.forEach(line => {
const [key, ...value] = line.split(/:(.+)/);
if (key && value) cardData[key.trim()] = value.join('').trim();
});
return {
title: cardData.Title || "Untitled",
instruction: cardData.Instruction || "No instruction provided",
detail: cardData.Detail || "No details available",
icon: cardData.Icon || "β οΈ"
};
});
// Enhanced image generation with quality parameters
const flashcardsWithImages = await Promise.all(
flashcards.map(async (card, index) => {
await new Promise(resolve => setTimeout(resolve, index * 1500));
const imagePrompt = `Photorealistic 3D technical illustration of ${card.title}:
- EV workshop environment
- No text/annotations
- Cycles render engine
- relavant to topic
- Professional lighting`;
return {
...card,
image: await generateEdenImage(imagePrompt)
};
})
);
displayFlashcards(flashcardsWithImages);
} catch (error) {
console.error("Error:", error);
alert(`Error: ${error.message}`);
} finally {
generateBtn.innerText = "Generate Flashcards";
generateBtn.disabled = false;
}
});
//Prevent righ click
document.oncontextmenu = () => {
alert("Don't try right click")
return false
}
document.onkeydown = e => {
//Prevent F12 key
if (e.key == "F12") {
alert("Don't try to inspect element")
return false
}
//Prevent showing page source by ctrl + U
if (e.ctrlKey && e.key == "u") {
alert("Don't try to view page sources")
return false
}
}
// Display Flashcards
function displayFlashcards(flashcards) {
flashcardsContainer.innerHTML = "";
if (flashcards.length === 0) {
flashcardsContainer.innerHTML = `<div class="alert alert-warning">No flashcards generated. Try again!</div>`;
return;
}
flashcards.forEach((card, index) => {
const flashcard = document.createElement("div");
flashcard.className = `flashcard animate__animated animate__fadeIn ${currentTemplate} ${currentColor}`;
flashcard.style.animationDelay = `${index * 0.1}s`;
const imageHTML = card.image ?
`<img src="${card.image}" class="card-image" alt="${card.title}" onerror="this.classList.add('d-none')">` :
`<div class="image-placeholder">Image not available</div>`;
flashcard.innerHTML = `
<div class="card-image-container">
${imageHTML}
</div>
<div class="card-header">${card.title}</div>
<div class="card-body">
<p class="instruction">${card.instruction}</p>
<p class="detail">${card.detail}</p>
</div>
`;
flashcardsContainer.appendChild(flashcard);
});
}
// Apply Template Function
function applyTemplate(template) {
const flashcards = document.querySelectorAll(".flashcard");
flashcards.forEach((flashcard) => {
flashcard.className = "flashcard animate__animated animate__fadeIn";
flashcard.classList.add(template);
});
}
});
// Handle login form submission
document.getElementById('loginForm').addEventListener('submit', async (e) => {
e.preventDefault();
const email = document.getElementById('loginEmail').value;
const password = document.getElementById('loginPassword').value;
try {
const response = await fetch('/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password }),
});
const result = await response.json();
alert(result.message);
// Redirect to another page on successful login
if (response.ok) {
window.location.href = '/dashboard'; // Replace with your desired redirect URL
}
} catch (error) {
console.error('Error:', error);
alert('An error occurred. Please try again.');
}
});
// Handle signup form submission
document.getElementById('signupForm').addEventListener('submit', async (e) => {
e.preventDefault();
const name = document.getElementById('signupName').value;
const email = document.getElementById('signupEmail').value;
const password = document.getElementById('signupPassword').value;
const confirmPassword = document.getElementById('signupConfirmPassword').value;
// Validate password match
if (password !== confirmPassword) {
alert('Passwords do not match.');
return;
}
try {
const response = await fetch('/signup', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, email, password }),
});
const result = await response.json();
alert(result.message);
// Redirect to another page on successful signup
if (response.ok) {
window.location.href = '/login'; // Redirect to login page
}
} catch (error) {
console.error('Error:', error);
alert('An error occurred. Please try again.');
}
});