-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgallery.js
More file actions
171 lines (118 loc) · 3.29 KB
/
gallery.js
File metadata and controls
171 lines (118 loc) · 3.29 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
const GALLERY_KEY = "galleryItems";
let galleryAdmin = false;
let currentEditIndex = null;
let newImageData = null;
/* GET DATA */
function getGalleryItems(){
return JSON.parse(localStorage.getItem(GALLERY_KEY)) || [];
}
/* SAVE */
function saveGalleryItems(items){
localStorage.setItem(GALLERY_KEY, JSON.stringify(items));
}
/* RENDER */
function renderGallery(){
const container = document.getElementById("galleryContainer");
container.innerHTML = "";
const items = getGalleryItems();
items.forEach((item,index)=>{
container.innerHTML += `
<div class="gallery-item ${index % 2 ? "reverse" : ""}">
<div class="image-box">
<img src="${item.image}">
${
galleryAdmin
? `
<button class="delete-btn" onclick="deleteGalleryItem(${index})">×</button>
<button class="edit-btn" onclick="editGalleryText(${index})">✎</button>
`
: ""
}
</div>
<div class="text-box">
<h3>${item.title}</h3>
<p>${item.text}</p>
</div>
</div>
`;
});
}
/* ADD ITEM */
function addGalleryItem(e){
const file = e.target.files[0];
if(!file) return;
const reader = new FileReader();
reader.onload = ()=>{
newImageData = reader.result;
currentEditIndex = null;
document.getElementById("editTitleInput").value = "";
document.getElementById("editTextInput").value = "";
document.getElementById("editGalleryModal").style.display = "block";
};
reader.readAsDataURL(file);
}
/* DELETE */
function deleteGalleryItem(index){
const items = getGalleryItems();
items.splice(index,1);
saveGalleryItems(items);
renderGallery();
}
function editGalleryText(index){
const items = getGalleryItems();
currentEditIndex = index;
document.getElementById("editTitleInput").value =
items[index].title;
document.getElementById("editTextInput").value =
items[index].text;
document.getElementById("editGalleryModal").style.display = "block";
}
/* ADMIN */
function enableAdmin(){
galleryAdmin = true;
document.getElementById("galleryAdminPanel").style.display="flex";
renderGallery();
}
function disableAdmin(){
galleryAdmin = false;
document.getElementById("galleryAdminPanel").style.display="none";
renderGallery();
}
/* DOT MENU */
function toggleAdminMenu(){
const panel = document.getElementById("adminMenuPanel");
panel.style.display =
panel.style.display === "block" ? "none" : "block";
}
function closeGalleryEdit(){
document.getElementById("editGalleryModal").style.display = "none";
}
function saveGalleryEdit(){
const title = document.getElementById("editTitleInput").value;
const text = document.getElementById("editTextInput").value;
const items = getGalleryItems();
if(currentEditIndex === null){
items.push({
image: newImageData,
title,
text
});
}else{
items[currentEditIndex].title = title;
items[currentEditIndex].text = text;
}
saveGalleryItems(items);
renderGallery();
closeGalleryEdit();
}
function replaceGalleryImage(e){
const file = e.target.files[0];
if(!file) return;
const reader = new FileReader();
reader.onload = ()=>{
newImageData = reader.result;
};
reader.readAsDataURL(file);
}
/* INIT */
renderGallery();