-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmingallery.html
598 lines (507 loc) · 15.9 KB
/
admingallery.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
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta information -->
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Image Gallery with Navigation</title>
<!-- External stylesheet for Font Awesome icons -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"
/>
<!-- Internal stylesheet for custom styles -->
<style>
/* CSS Styles */
p {
margin: 0;
padding: 0;
font-family: 'Barlow', sans-serif;
}
* {
box-sizing: border-box;
}
nav {
background-color: #333;
color: white;
padding: 10px;
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
}
.nav-links {
display: flex;
align-items: center;
}
.nav-links a {
color: white;
text-decoration: none;
margin: 0 10px;
}
#home {
padding: 8px 16px;
text-decoration: none;
cursor: pointer;
position: absolute;
top: 15px;
font-size: 20px;
font-weight: 600;
}
#home:hover {
color: #4caf50;
}
#postButton {
position: absolute;
top: 15px;
right: 310px;
}
#galleryButton {
position: absolute;
top: 15px;
right: 210px;
}
#messageButton {
padding: 8px 16px;
background-color: #4caf50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
position: absolute;
top: 15px;
right: 100px;
}
/* Adjust the button styling */
button {
background-color: #4caf50;
color: #fff;
padding: 8px 16px; /* Adjust the padding as needed */
border: none;
border-radius: 4px;
cursor: pointer;
}
#logoutButton {
padding: 8px 16px;
background-color: #4caf50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
position: absolute;
top: 15px;
right: 10px;
}
.gallery-container {
max-width: 100%;
overflow-x: auto;
white-space: nowrap;
}
.gallery {
display: flex;
flex-wrap: wrap; /* Allow images to wrap to the next line */
gap: 25px;
padding: 16px;
}
.gallery-item {
position: relative;
width: 200px;
height: 200px;
overflow: hidden;
border-radius: 8px;
}
.gallery img {
width: 100%;
height: 100%;
object-fit: cover;
cursor: pointer;
transition: transform 0.3s ease-in-out;
}
.gallery img:hover {
transform: scale(1.1);
}
.delete-icon {
position: absolute;
top: 8px;
right: 8px;
color: #ff0e0e;
cursor: pointer;
font-size: 16px;
display: none;
}
.gallery-item:hover .delete-icon {
display: block;
}
/* Lightbox styles */
.lightbox {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.8);
justify-content: center;
align-items: center;
}
.lightbox img {
max-width: 80%;
max-height: 80%;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
order: 1;
}
.close {
order: 2;
margin-top: calc(-80vh);
cursor: pointer;
color: #e00000;
font-size: 30px;
}
/* Navigation arrows */
.nav-arrow {
position: absolute;
top: 50%;
transform: translateY(-50%);
font-size: 24px;
color: #fff;
cursor: pointer;
}
.prev {
left: 16px;
}
.next {
right: 16px;
}
/* File upload styles */
.add-image-container {
padding: 16px;
text-align: center;
}
#addImageInput {
display: none;
}
#addImageButton {
background-color: #4caf50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
margin-top: 45px;
margin-bottom: 16px;
}
/* Responsive Styles */
@media (max-width: 768px) {
.gallery-item {
width: 100%;
height: auto;
margin-bottom: 16px;
}
.close {
font-size: 22px;
font-weight: bold;
margin-top: calc(-60vw);
}
}
</style>
</head>
<body>
<!-- Logout button on the right side -->
<a id="home" href="/dashboard.html">VWF</a>
<div class="nav-links">
<a href="/posts.html"><button id="postButton">Post</button></a>
<a href="/admingallery.html"
><button id="galleryButton">Gallery</button></a
>
<button id="logoutButton">Logout</button>
<a href="/message.html"><button id="messageButton">Message</button></a>
</div>
<!-- Add Image Container -->
<div class="add-image-container">
<!-- File input for uploading images -->
<input
type="file"
id="addImageInput"
accept="image/*"
multiple
onchange="addImage(event)"
/>
<!-- Button to trigger file input -->
<button
id="addImageButton"
onclick="document.getElementById('addImageInput').click()"
>
Upload Image(s)
</button>
</div>
<!-- Container for displaying the image gallery -->
<div class="gallery-container">
<!-- Dynamic gallery content will be added here -->
<div class="gallery" id="imageGallery"></div>
</div>
<!-- Modal for displaying enlarged image -->
<div class="lightbox" id="imageModal">
<!-- Close button for the modal -->
<span class="close" id="closeModal">×</span>
<!-- Navigation arrows within the modal -->
<!-- Image content within the modal -->
<img class="modal-content" id="modalImage" />
<div class="nav-arrow prev" onclick="prevImageInModal()">❮</div>
<div class="nav-arrow next" onclick="nextImageInModal()">❯</div>
</div>
<!-- External configuration script -->
<script src="./config.js"></script>
<!-- External Font Awesome JavaScript library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js"></script>
<!-- Inline JavaScript code for custom functionality -->
<script>
// Global variables for backend and frontend URLs
const backendUrl = window.config.backendUrl;
const frontendUrl = window.config.frontendUrl;
const token = localStorage.getItem('token');
async function checkAuthentication() {
try {
const response = await fetch(`${backendUrl}/check-auth`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
credentials: 'include',
});
if (!response.ok) {
console.log(
'Authentication failed. Response status:',
response.status
);
console.log('Response text:', await response.text());
if (response.status === 401) {
// Handle token refresh or ask the user to log in again
console.log(
'Token expired or invalid. Redirecting to login page.'
);
localStorage.removeItem('token');
} else {
// Handle other cases as needed
console.log(
'Other authentication failure. Redirecting to login page.'
);
localStorage.removeItem('token');
}
}
} catch (error) {
console.error('Error fetching data:', error);
console.log('Redirecting to login page due to an error');
localStorage.removeItem('token');
}
if (
!localStorage.getItem('token') &&
window.location.pathname !== '/admin.html'
) {
console.log('Redirecting to login page');
window.location.href = `${frontendUrl}/admin.html`;
return;
}
}
// Call the checkAuthentication function when the page loads
checkAuthentication();
// DOM elements for modal and image display
const modal = document.getElementById('imageModal');
const modalImage = document.getElementById('modalImage');
const closeModalButton = document.getElementById('closeModal');
// Index of the currently displayed image
let currentImageIndex = 0;
// Function to handle image upload
async function addImage(event) {
const files = event.target.files;
if (files.length === 0) {
return;
}
const formData = new FormData();
for (const file of files) {
const maxUploadSize = 10 * 1024 * 1024;
if (file.size > maxUploadSize) {
alert('Please upload image below 10 MB only');
return 1;
}
formData.append('images', file);
}
try {
const response = await fetch(`${backendUrl}/api/v1/gallery`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
},
body: formData,
});
if (response.ok) {
const result = await response.json();
fetchImages();
} else {
console.error('Failed to upload image');
}
} catch (error) {
console.error('Error during fetch:', error);
}
}
// Array to store gallery images
let galleryImages = [];
// Function to fetch and display images from the backend
async function fetchImages() {
try {
const response = await fetch(`${backendUrl}/api/v1/gallery`);
if (response.ok) {
let galleryData = await response.json();
let imageGallery;
imageGallery = document.getElementById('imageGallery');
imageGallery.innerHTML = '';
if (galleryData && galleryData.gallery) {
galleryData = galleryData.gallery.filter(
(g) => g.images.length > 0
);
const allImages = galleryData.map((g) => g.images).flat();
allImages.forEach((image, index) => {
galleryImages.push({
index: index,
img_name: image.img_name,
asset_id: image.asset_id,
});
const galleryItem = document.createElement('div');
galleryItem.className = 'gallery-item';
// Set 'assetId' dataset
galleryItem.dataset.assetId = image.asset_id;
const img = document.createElement('img');
img.src =
'https://res.cloudinary.com/drtm8uqcp/image/upload/' +
image.img_name;
img.alt = `Image ${index + 1}`;
img.addEventListener('click', () => {
showImageInModal(image, index);
});
const deleteIcon = document.createElement('i');
deleteIcon.className = 'fas fa-trash delete-icon';
galleryItem.appendChild(img);
galleryItem.appendChild(deleteIcon);
imageGallery.appendChild(galleryItem);
});
// Event delegation for delete icons
imageGallery.addEventListener('click', function (event) {
const deleteIcon = event.target.closest('.delete-icon');
if (deleteIcon) {
const galleryItem = event.target.closest('.gallery-item');
if (galleryItem) {
const assetId = galleryItem.dataset.assetId;
deleteImage(assetId);
}
}
});
}
}
} catch (error) {
console.error('Error during fetch:', error);
}
}
// Function to show an image in the modal
function showImageInModal(image, index) {
currentImageIndex = index;
const galleryItem = document.createElement('div');
galleryItem.className = 'gallery-item';
const img = document.createElement('img');
img.src =
'https://res.cloudinary.com/drtm8uqcp/image/upload/' + image.img_name;
img.alt = `Image ${index + 1}`;
modalImage.src = img.src;
modal.style.display = 'flex';
}
// Function to close the modal
function closeModal() {
modal.style.display = 'none';
}
// Function to delete an image
async function deleteImage(assetId) {
try {
const response = await fetch(
`${backendUrl}/api/v1/gallery/${assetId}`,
{
method: 'DELETE',
headers: {
Authorization: `Bearer ${token}`,
},
}
);
if (response.ok) {
fetchImages(); // Assuming fetchImages is defined and handles updating the UI.
} else {
console.error('Error deleting image:', response.statusText);
}
} catch (error) {
console.error('Error during fetch:', error);
}
}
// Function to show the next image in the modal
function nextImageInModal() {
currentImageIndex++;
if (currentImageIndex >= galleryImages.length) {
currentImageIndex = 0;
}
const image = galleryImages.find(
(image) => image.index == currentImageIndex
);
showImageInModal(image, currentImageIndex);
}
// Function to show the previous image in the modal
function prevImageInModal() {
currentImageIndex--;
if (currentImageIndex < 0) {
currentImageIndex = galleryImages.length - 1;
}
const image = galleryImages.find(
(image) => image.index == currentImageIndex
);
showImageInModal(image, currentImageIndex);
}
// Event listener for keyboard shortcuts
document.addEventListener('keydown', function (event) {
if (modal.style.display === 'flex') {
if (event.key === 'ArrowRight') {
nextImageInModal();
} else if (event.key === 'ArrowLeft') {
prevImageInModal();
} else if (event.key === 'Escape') {
closeModal();
}
}
});
// Event listener for closing the modal on button click
closeModalButton.addEventListener('click', closeModal);
// Event listener for closing the modal on overlay click
modal.addEventListener('click', function (event) {
if (event.target === modal) {
closeModal();
}
});
async function logout() {
try {
const response = await fetch(`${backendUrl}/logout`, {
method: 'GET',
credentials: 'include',
});
if (response.ok) {
// Redirect to the login page after successful logout
localStorage.setItem('token', '');
window.location.href = `${frontendUrl}/admin.html`;
} else {
console.error('Error logging out:', response.statusText);
}
} catch (error) {
console.error('Error logging out:', error);
}
}
// Add click event listener to the logout button
const logoutButton = document.getElementById('logoutButton');
logoutButton.addEventListener('click', logout);
// Event listener for executing code when the DOM is loaded
document.addEventListener('DOMContentLoaded', fetchImages);
</script>
</body>
</html>