-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
488 lines (418 loc) · 16.8 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="/favicon.ico">
<title>Recipe Generator</title>
<style>
#overlay {
display: none; /* Hidden by default */
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent background */
z-index: 1000; /* High z-index to cover other elements */
display: flex;
justify-content: center; /* Center horizontally */
align-items: center; /* Center vertically */
flex-direction: column; /* Stack elements vertically */
}
/* Spinner styling */
#loadingSpinner {
display: none; /* Hidden by default */
margin: 20px auto;
border: 4px solid #f3f3f3;
border-top: 4px solid #3498db;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 2s linear infinite;
}
/* Response box styling */
#response {
display: none; /* Hidden by default */
position: fixed; /* Fixed to the viewport */
top: 50%; /* Center vertically */
left: 50%; /* Center horizontally */
transform: translate(-50%, -50%); /* Perfectly center */
background: #fff; /* White background */
padding: 20px; /* Add padding */
border-radius: 12px; /* Rounded corners */
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2); /* Soft shadow */
z-index: 1500; /* Ensure it's above other elements */
width: 320px; /* Set a width */
text-align: center; /* Center-align text */
font-family: Arial, sans-serif; /* Use a clean font */
font-size: 16px; /* Readable font size */
color: #333; /* Neutral text color */
}
#response button {
position: absolute; /* Position relative to the box */
top: 10px; /* Offset from the top */
right: 10px; /* Offset from the right */
background: transparent; /* No background */
border: none; /* No border */
font-size: 18px; /* Slightly larger font */
color: #999; /* Neutral color */
cursor: pointer; /* Pointer cursor */
transition: color 0.3s ease; /* Smooth hover effect */
}
#response button:hover {
color: #333; /* Darker color on hover */
}
/* Spinner animation */
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
body {
font-family: Arial, sans-serif;
margin: 40px;
}
h1, h2 {
color: #333;
}
form {
margin-bottom: 30px;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
label {
display: block;
margin-top: 10px;
font-weight: bold;
}
input[type="text"],
input[type="url"],
textarea,
input[type="file"] {
width: 100%;
padding: 8px;
margin-top: 5px;
box-sizing: border-box;
}
button {
margin-top: 15px;
padding: 10px 20px;
background-color: #28a745;
border: none;
color: white;
cursor: pointer;
border-radius: 3px;
}
button:hover {
background-color: #218838;
}
#dropZone {
border: 2px dashed #ccc;
border-radius: 5px;
padding: 20px;
text-align: center;
cursor: pointer;
}
#dropZone.dragover {
border-color: #28a745;
}
#previewImage {
margin-top: 15px;
max-width: 100%;
max-height: 200px;
border-radius: 5px;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
</head>
<body>
<!-- Language Toggle Button -->
<button id="languageToggleButton" onclick="toggleLanguage()">Switch to English</button>
<h1 id="heading">Rezept-Generator</h1>
<!-- Generate by Link Form -->
<form id="linkForm">
<h2 id="generateByLinkTitle">Generieren aus Link</h2>
<label for="url" id="urlLabel">Rezept-Link:</label>
<input type="url" id="url" name="url" required>
<button type="button" onclick="submitByLink()" id="generateRecipeLinkBtn">Rezept generieren</button>
</form>
<!-- Generate by Image Form -->
<form id="imageForm" enctype="multipart/form-data">
<h2 id="generateByImageTitle">Generieren aus Bild</h2>
<div
id="dropZone"
ondrop="dropHandler(event);"
ondragover="dragOverHandler(event);"
onclick="triggerFileInput()">
Bild auswählen
</div>
<input
type="file"
id="image"
name="image"
accept="image/*"
style="display: none;"
required
onchange="previewImage()">
<label for="url" id="recipenameByImageLabel">Rezeptname (optional):</label>
<input type="text" id="recipename-byimage" name="recipename">
<button type="button" onclick="submitByImage()" id="generateRecipeImageBtn">Rezept generieren</button>
</form>
<!-- Transform Recipe Form -->
<form id="transformForm">
<h2 id="transformRecipeTitle">Generieren aus Text</h2>
<label for="transformRecipeName" id="transformRecipeNameLabel">Rezeptname (optional):</label>
<input type="text" id="transformRecipeName" name="transformRecipeName">
<label for="recipeText" id="recipeTextLabel">Rezept:</label>
<textarea id="recipeText" name="recipeText" rows="6" required></textarea>
<button type="button" onclick="submitTransform()" id="transformRecipeBtn">Rezept transformieren</button>
</form>
<!-- Generate by Name Form -->
<form id="nameForm">
<h2 id="generateByName-Title">Neues Rezept generieren aus Name</h2>
<label for="recipename" id="recipenameLabel">Rezeptname:</label>
<input type="text" id="recipename" name="recipename" required>
<label for="details" id="detailsLabel">Details (optional):</label>
<textarea id="details" name="generateByName-details" rows="4" placeholder="Füge Details oder Vorlieben hinzu"></textarea>
<button type="button" onclick="submitByName()" id="generateRecipeBtn">Rezept generieren</button>
</form>
<!-- Response Section -->
<div id="response">
<button onclick="closeResponse()">×</button> <!-- Close Button -->
<p id="responseText"></p> <!-- Message Content -->
</div>
<!-- Response Section -->
<div id="response">
<button onclick="closeResponse()">×</button> <!-- Close Button -->
<div id="responseText"></div> <!-- Message Content -->
<div id="responseMarkdown" style="margin-top: 20px;"></div> <!-- Rendered Markdown Content -->
<button onclick="acceptRecipe()">Accept</button>
<button onclick="denyRecipe()">Deny</button>
</div>
<!-- Loading Spinner -->
<div id="overlay">
<div id="loadingSpinner"></div>
</div>
<script>
document.getElementById("overlay").style.display = "none";
let isGerman = true; // Default language is German
// Function to toggle language
function toggleLanguage() {
if (isGerman) {
// Switch to English
document.getElementById("languageToggleButton").innerText = "Zu deutsch wechseln";
document.getElementById("heading").innerText = "Recipe Generator";
document.getElementsByName("generateByName-details")[0].placeholder="Add details or preferences";
document.getElementById("generateByName-Title").innerText = "Generate by Name";
document.getElementById("recipenameLabel").innerText = "Recipe Name:";
document.getElementById("detailsLabel").innerText = "Details (optional):";
document.getElementById("generateRecipeBtn").innerText = "Generate Recipe";
document.getElementById("generateByLinkTitle").innerText = "Generate by Link";
document.getElementById("urlLabel").innerText = "Recipe Link:";
document.getElementById("generateRecipeLinkBtn").innerText = "Generate Recipe";
document.getElementById("generateByImageTitle").innerText = "Generate by Image";
document.getElementById("dropZone").innerText = "Drag and drop an image here, or click to select one.";
document.getElementById("recipenameByImageLabel").innerText = "Recipe Name (optional):";
document.getElementById("generateRecipeImageBtn").innerText = "Generate Recipe";
document.getElementById("transformRecipeTitle").innerText = "Transform Recipe";
document.getElementById("transformRecipeNameLabel").innerText = "Recipe Name (optional):";
document.getElementById("recipeTextLabel").innerText = "Recipe Text:";
document.getElementById("transformRecipeBtn").innerText = "Transform Recipe";
} else {
// Switch to German
document.getElementById("languageToggleButton").innerText = "Switch to English";
document.getElementById("heading").innerText = "Rezept-Generator";
document.getElementById("generateByName-Title").innerText = "Generieren aus Name";
document.getElementsByName("generateByName-details")[0].placeholder="Füge Details oder Vorlieben hinzu";
document.getElementById("recipenameLabel").innerText = "Rezeptname:";
document.getElementById("detailsLabel").innerText = "Details (optional):";
document.getElementById("generateRecipeBtn").innerText = "Rezept generieren";
document.getElementById("generateByLinkTitle").innerText = "Generieren aus Link";
document.getElementById("urlLabel").innerText = "Rezept-Link:";
document.getElementById("generateRecipeLinkBtn").innerText = "Rezept generieren";
document.getElementById("generateByImageTitle").innerText = "Generieren aus Bild";
document.getElementById("dropZone").innerText = "Bild auswählen";
document.getElementById("recipenameByImageLabel").innerText = "Rezeptname (optional):";
document.getElementById("generateRecipeImageBtn").innerText = "Rezept generieren";
document.getElementById("transformRecipeTitle").innerText = "Generieren aus Text";
document.getElementById("transformRecipeNameLabel").innerText = "Rezeptname (optional):";
document.getElementById("recipeTextLabel").innerText = "Rezept:";
document.getElementById("transformRecipeBtn").innerText = "Rezept generieren";
}
isGerman = !isGerman; // Toggle language state
}
function dragOverHandler(event) {
event.preventDefault();
event.dataTransfer.dropEffect = "copy";
document.getElementById('dropZone').classList.add('dragover');
}
function dropHandler(event) {
event.preventDefault();
document.getElementById('dropZone').classList.remove('dragover');
const files = event.dataTransfer.files;
if (files.length > 0) {
document.getElementById('image').files = files;
updateFileName();
previewImage();
}
}
function triggerFileInput() {
document.getElementById('image').click();
}
function displayResponse(message, isError = false, markdownContent = '') {
const response = document.getElementById('response');
const spinner = document.getElementById('loadingSpinner');
const responseText = document.getElementById('responseText');
const responseMarkdown = document.getElementById('responseMarkdown');
spinner.style.display = 'none'; // Hide spinner
response.style.display = 'block'; // Show response box
if (isError) {
responseText.textContent = `Error: ${message}`;
responseMarkdown.innerHTML = ''; // No markdown for errors
} else {
responseText.textContent = 'Recipe generated successfully!';
responseMarkdown.innerHTML = marked(markdownContent); // Render markdown
}
}
function closeResponse() {
const overlay = document.getElementById('overlay');
overlay.style.display = 'none'; // Hide overlay
document.getElementById('response').style.display = 'none'; // Hide response box
}
function showLoading() {
const overlay = document.getElementById('overlay');
const spinner = document.getElementById('loadingSpinner');
overlay.style.display = 'flex'; // Show overlay
spinner.style.display = 'block'; // Show spinner
}
function hideLoading() {
const overlay = document.getElementById('overlay');
const spinner = document.getElementById('loadingSpinner');
overlay.style.display = 'none'; // Hide overlay
spinner.style.display = 'none'; // Hide spinner
}
function previewImage() {
const fileInput = document.getElementById('image');
const file = fileInput.files[0];
if (file) {
document.getElementById('dropZone').innerHTML = '';
const reader = new FileReader();
reader.onload = function(event) {
const image = document.createElement('img');
image.id = 'previewImage';
image.src = event.target.result;
const dropZone = document.getElementById('dropZone');
// Remove any previous preview image
const existingPreview = document.getElementById('previewImage');
if (existingPreview) {
existingPreview.remove();
}
dropZone.appendChild(image); // Add the new image preview
};
reader.readAsDataURL(file);
}
}
const apiBaseUrl = window.location.hostname === 'localhost' || window.location.hostname === '192.168.10.163'
? 'http://192.168.10.24:8080' // Local environment
: 'https://recipe-generator.ili16.de'; // Production environment
// Function to handle Generate by Name
async function submitByName() {
const recipename = document.getElementById('recipename').value.trim();
const details = document.getElementById('details').value.trim();
if (!recipename) {
alert('Recipe name is required.');
return;
}
const payload = { Recipename: recipename, Details: details };
try {
const response = await fetch(`${apiBaseUrl}/api/v1/generate/by-name`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
const result = await response.text();
displayResponse(result);
} catch (error) {
displayResponse('Error: ' + error.message);
}
}
// Function to handle Generate by Link
async function submitByLink() {
const url = document.getElementById('url').value.trim();
if (!url) {
alert('Recipe link is required.');
return;
}
const payload = { URL: url, IsGerman: isGerman };
showLoading();
try {
const response = await fetch(`${apiBaseUrl}/api/v1/generate/by-link`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
const result = await response.text();
displayResponse(result);
} catch (error) {
displayResponse('Error: ' + error.message);
}
}
// Function to handle Generate by Image
async function submitByImage() {
const fileInput = document.getElementById('image'); // Get the file input element
const file = fileInput.files[0]; // Retrieve the selected file
if (!file) {
alert('Please upload an image.');
return;
}
const recipeName = document.getElementById('recipename-byimage').value.trim();
if (recipeName && recipeName.length > 25) {
alert('Recipe name cannot be longer than 25 characters.');
return;
}
const formData = new FormData();
formData.append('image', file);
if (recipeName) {
formData.append('recipename', recipeName);
}
formData.append('isGerman', isGerman);
try {
const response = await fetch(`${apiBaseUrl}/api/v1/generate/by-image`, {
method: 'POST',
body: formData,
});
const result = await response.text();
displayResponse(result); // Display the result
} catch (error) {
displayResponse('Error: ' + error.message);
}
}
// Function to handle Transform Recipe
async function submitTransform() {
const transformRecipeName = document.getElementById('transformRecipeName').value.trim();
const recipeText = document.getElementById('recipeText').value.trim();
if (!recipeText) {
alert('Recipe text is required.');
return;
}
const payload = {
Recipename: transformRecipeName || null, // If transformRecipeName is empty, use null
RecipeText: recipeText,
IsGerman: isGerman
};
try {
const response = await fetch(`${apiBaseUrl}/api/v1/transform`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
const result = await response.text();
displayResponse(result);
} catch (error) {
displayResponse('Error: ' + error.message);
}
}
</script>
</body>
</html>