-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
68 lines (61 loc) · 2.09 KB
/
main.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
// define global variables to track currently displayed image
var selectedImageIndex;
function uploadImage() {
var formData = new FormData(document.getElementById('uploadForm'));
fetch('http://localhost:3000/upload', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if(data.success){
getImageGallery();
} else {
alert('Image Upload Fail');
}
})
.catch(error => console.error('Error:', error));
}
function getImageGallery() {
fetch('http://localhost:3000/gallery')
.then(response => response.json())
.then(data => {
let gallery = document.getElementById('gallery');
gallery.innerHTML = '';
data.forEach((img, index) => {
let imageElement = document.createElement('div');
imageElement.classList.add('w-32', 'h-32', 'relative', 'bg-center', 'bg-cover', 'rounded-md', 'border', 'border-white');
imageElement.setAttribute('style', `background-image: url('${img.url}')`);
imageElement.setAttribute('onclick', `showModal(${index}, '${img.url}')`);
gallery.appendChild(imageElement);
});
});
}
function deleteImage(index) {
fetch(`http://localhost:3000/delete/${index}`, { method: 'DELETE' })
.then(response => response.json())
.then(data => {
if(data.success){
getImageGallery();
hideModal();
}
});
}
function copyToClipboard(text) {
var dummy = document.createElement("textarea");
document.body.appendChild(dummy);
dummy.value = text;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
alert('Copied to clipboard');
}
function showModal(index, url) {
document.getElementById('modalImage').src = url;
document.getElementById('modal').classList.remove('hidden');
selectedImageIndex = index;
}
function hideModal() {
document.getElementById('modal').classList.add('hidden');
}
getImageGallery();