-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
236 lines (189 loc) · 6.73 KB
/
index.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
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
// let editIcon = document.querySelectorAll('.edit-icon-gallery');
let dropdown = document.getElementById('list-holder');
let container = document.querySelector('.container');
let imagesContainer = document.querySelectorAll('.img-container');
let revertBtn = document.getElementById('revert-btn');
let imgSrc = document.getElementById('src-input');
let imgDetail = document.getElementById('detail-input');
let addBtn = document.querySelector('.add-btn');
//Aquire list items which will provide class names
let original = document.getElementById('#original');
let big = document.getElementById("#big");
let horizontal = document.getElementById("#horizontal");
let vertical = document.getElementById("#vertical");
let large = document.getElementById('#large');
let horizontalLarge = document.getElementById('#horizontal-large');
let verticalLarge = document.getElementById('#vertical-large');
let space = document.getElementById('#space');
let spaceHorizontal = document.getElementById('#space-horizontal');
let spaceVertical = document.getElementById('#space-vertical');
let xl = document.getElementById('#xl');
let horizontalExtraLarge = document.getElementById('#horizontal-xl');
let verticalExtraLarge = document.getElementById('#vertical-xl');
let xxl = document.getElementById('#xxl');
let xxxl = document.getElementById('#xxxl');
for(i = 1;i<=localStorage.length;i++)
{
var innerDiv = document.createElement('div');
innerDiv.className = `img-container ${(JSON.parse(localStorage.getItem(i))).appliedClass}`;
innerDiv.setAttribute('value',(JSON.parse(localStorage.getItem(i))).imageDetail);
innerDiv.id = i;
var innerImage = document.createElement('img');
innerImage.className = 'gallery-images';
innerImage.src = (JSON.parse(localStorage.getItem(i))).srcValue;
innerImage.alt = `image_${i}`;
//edit icon injection
var editIconAnchor = document.createElement('a');
editIconAnchor.className = "edit-icon-gallery";
editIconAnchor.href = "#";
var editIconI = document.createElement('i');
editIconI.className = "fas fa-edit";
//delete icon injection
var deleteIconAnchor = document.createElement('a');
deleteIconAnchor.className = "delete-icon-gallery";
deleteIconAnchor.href = "#";
var deleteIconI = document.createElement('i');
deleteIconI.className = "fas fa-trash-alt";
//Appending
editIconAnchor.appendChild(editIconI);
innerDiv.appendChild(innerImage);
innerDiv.appendChild(editIconAnchor);
deleteIconAnchor.append(deleteIconI);
innerDiv.appendChild(deleteIconAnchor);
//final append
container.appendChild(innerDiv)
}
revertBtn.addEventListener('click',(e)=>{
e.preventDefault();
for(i = 1;i<=localStorage.length;i++){
let storageObject = JSON.parse(localStorage.getItem(i));
storageObject.appliedClass = "";
localStorage.setItem(i, JSON.stringify(storageObject));
}
location.reload();
})
//take input details
addBtn.addEventListener('click',(e) =>{
e.preventDefault();
let srcValue = imgSrc.value;
let imageDetail = imgDetail.value;
let storageId = localStorage.length + 1;
let storageObject = {
srcValue:srcValue,
imageDetail:imageDetail,
appliedClass: "",
}
localStorage.setItem(storageId, JSON.stringify(storageObject));
console.log((JSON.parse(localStorage.getItem(localStorage.length))));
location.reload();
})
let editIcon = document.querySelectorAll('.edit-icon-gallery');
let deleteIcon = document.querySelectorAll('.delete-icon-gallery');
editIcon.forEach(icon =>{
icon.addEventListener('click',(event) =>{
event.preventDefault();
icon.parentNode.appendChild(dropdown);
dropdown.classList.toggle('add');
});
})
deleteIcon.forEach(icon =>{
icon.addEventListener('click',() =>{
let parent = icon.parentElement;
let delatableObject = JSON.stringify(localStorage.getItem(parent.id));
let storageObjects = JSON.parse(JSON.stringify(localStorage));
for(i = 1;i<= localStorage.length;i++){
if(delatableObject === JSON.stringify(storageObjects[i])){
for(j = i;j<=localStorage.length;j++){
storageObjects[j] = storageObjects[j+1];
}
delete storageObjects[localStorage.length];
break;
}
}
let count = localStorage.length;
localStorage.clear();
for(i = 1;i<=count-1;i++){
localStorage.setItem(i,storageObjects[i])
}
location.reload();
})
})
original.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(original);
})
big.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(big);
})
horizontal.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(horizontal);
})
vertical.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(vertical);
})
large.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(large);
})
horizontalLarge.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(horizontalLarge);
})
verticalLarge.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(verticalLarge);
})
space.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(space);
})
spaceHorizontal.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(spaceHorizontal);
})
spaceVertical.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(spaceVertical);
})
xl.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(xl);
})
horizontalExtraLarge.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(horizontalExtraLarge);
})
verticalExtraLarge.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(verticalExtraLarge);
})
xxl.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(xxl);
})
xxxl.addEventListener("click",(e)=>{
e.preventDefault();
updateStorage(xxxl);
})
function updateStorage(addableClass){
let parent = addableClass.parentElement.parentElement;
let srcValue = JSON.parse(localStorage.getItem(parent.id)).srcValue;
let imageDetail = JSON.parse(localStorage.getItem(parent.id)).imageDetail;
let storageObject = {
srcValue:srcValue,
imageDetail:imageDetail,
appliedClass: "",
}
if(parent.classList.contains(addableClass.innerHTML.toLowerCase()) === false){
storageObject.appliedClass = addableClass.innerHTML.toLowerCase();
localStorage.setItem(parent.id,JSON.stringify(storageObject));
}
else{
storageObject.appliedClass = "";
localStorage.setItem(parent.id,JSON.stringify(storageObject));
}
location.reload();
}