-
Notifications
You must be signed in to change notification settings - Fork 0
/
modal.js
397 lines (317 loc) · 13.7 KB
/
modal.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
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
import {displayProjects} from "./projets.js";
import {addProject, deleteProject, getAllProject} from "./services/works-service.js";
import {getAllCategories} from "./services/categories-service.js";
// Récupération des éléments HTML des modales
const modalGallery = document.getElementById("modalProjets");
const modalAjoutPhoto = document.getElementById("modalAjouterPhoto");
const span = document.getElementsByClassName("close")[0];
const span2 = document.getElementsByClassName("close2")[0];
// Récupération des projets via l'API
const projectRequest = await getAllProject();
// Récupération des catégories via l'API
const categories = await getAllCategories();
// Stockage des projets dans une variable
let projects = [];
projects = projectRequest;
// Stockage des éléments HTML pour le header et le footer de la modale Edit
let modalHeader = document.querySelector(".modal-header");
let modalFooter = document.querySelector(".modal-footer");
// Initialisation des variables pour les boutons d'ajout et suppression de projets
let ajouterPhoto;
let deleteGallery;
// Ouverture des modales
export function modalOpen() {
firstModal();
secondModal();
}
// Fermeture des modales via la croix
export function modalClose() {
span.onclick = function () {
modalGallery.style.display = "none";
resetModal();
}
span2.onclick = function () {
modalAjoutPhoto.style.display = "none";
resetModal();
}
}
// Fermeture des modales via clic hors modale
export function modalCloseOutside() {
window.onclick = function (event) {
if (event.target === modalGallery) {
modalGallery.style.display = "none";
} else if (event.target === modalAjoutPhoto) {
modalAjoutPhoto.style.display = "none";
}
}
}
// Pour éviter duplication des boutons
function refreshModalButtons() {
modalFooter.innerHTML = ""
ajouterPhoto = document.createElement("button");
ajouterPhoto.innerText = "Ajouter une photo";
modalFooter.appendChild(ajouterPhoto);
deleteGallery = document.createElement("a");
deleteGallery.innerText = "Supprimer la galerie";
modalFooter.appendChild(deleteGallery);
console.log("Refreshed Buttons")
}
// Création de la modale Gallerie
function createModal() {
modalGallery.style.display = "block";
resetModal();
const modalBody = document.querySelector(".modal-body");
let imgModal;
let editModal;
let deleteModal;
let moveModal;
modalBody.innerHTML = ""
console.log('projects', projects)
// Création de la galerie modale
projects.forEach((projet) => {
// Affiche uniquement les projets non deleted
// Création d'un <post> pour la modale
let modalForm = document.createElement("post");
modalForm.setAttribute("data-id", projet.id);
imgModal = document.createElement("img");
imgModal.src = projet.imageUrl;
editModal = document.createElement("p");
editModal.textContent = "éditer";
deleteModal = document.createElement("i");
deleteModal.setAttribute("class", "delete-modal");
deleteModal.innerHTML = `<i class="fa-solid fa-trash-can"></i>`;
moveModal = document.createElement('i');
moveModal.innerHTML = `<i class="fa-solid fa-arrows-up-down-left-right"></i>`;
moveModal.style.visibility = "hidden";
// Apparition du logo déplacement lors du survol
modalForm.addEventListener("mouseenter", (event) => {
const currentCard = event.currentTarget;
const currentMoveModal = currentCard.querySelector('i');
currentMoveModal.style.visibility = "visible";
});
// Disparition du logo déplacement
modalForm.addEventListener("mouseleave", (event) => {
const currentCard = event.currentTarget;
const currentMoveModal = currentCard.querySelector('i');
currentMoveModal.style.visibility = "hidden";
});
modalBody.appendChild(modalForm); // <post> et son contenu assigné au body de la modale
modalForm.appendChild(imgModal);
modalForm.appendChild(editModal);
modalForm.appendChild(moveModal);
modalForm.appendChild(deleteModal);
modalBody.style.display = "grid";
modalBody.style.gridTemplateColumns = "repeat(5, 1fr)";
modalBody.style.gridTemplateRows = "repeat(5, 1fr)";
modalBody.style.gridGap = "10px 10px";
modalBody.style.width = "430px";
modalBody.style.height = "450px";
modalBody.style.margin = "46px auto";
modalBody.style.borderBottom = "1px solid #B3B3B3";
imgModal.style.width = "78px";
imgModal.style.height = "104px"
deleteModal.style.position = "relative";
deleteModal.style.bottom = "117px";
deleteModal.style.left = "40px";
deleteModal.style.fontSize = "9px";
deleteModal.style.color = "white";
deleteModal.style.background = "black";
deleteModal.style.border = "4px solid black";
moveModal.style.position = "relative";
moveModal.style.bottom = "117px";
moveModal.style.left = "36px";
moveModal.style.fontSize = "9px";
moveModal.style.color = "white";
moveModal.style.background = "black";
moveModal.style.border = "4px solid black";
// Comportement du logo de suppression de projet
deleteModal.onclick = async function () {
projects = await deleteProject(projet.id, projects, modalBody, modalForm)
}
}
);
}
// Reset de la modale à son ouverture et sa fermeture
function resetModal() {
const modalBody = document.querySelector(".modal-body");
modalBody.innerHTML = "";
}
// Appel de la première modale
function firstModal() {
refreshModalButtons();
createModal();
console.log("Modale édition ouverte");
}
// Création modale d'ajout photo
function secondModal() {
// Comportement flèche retour en arrière
let backArrow = document.getElementById("back");
backArrow.onclick = function () {
modalAjoutPhoto.style.display = "none";
modalGallery.style.display = "block";
};
// Comportement bouton Ajouter
ajouterPhoto.addEventListener("click", function () {
console.log("Modale Ajout ouverte / Modale Edit fermée")
let modalImgInput;
let modal2Body;
let imgInputForm;
let modal2Footer = document.querySelector(".modal-footer2")
modal2Footer.innerHTML = "";
let modal2 = document.getElementById("modalAjouterPhoto");
let modalImgLabel;
modalImgLabel = document.createElement("label");
modalImgLabel.setAttribute("for", "img-project");
modalImgLabel.innerHTML = "+ Ajouter Photo";
// Icône visible tant qu'aucune image n'est uploadée
let modalImgEmpty;
modalImgEmpty = document.createElement("i");
modalImgEmpty.innerHTML = `<i class="fa-sharp fa-regular fa-image"></i>`;
let modalImgResult = document.createElement("img");
modalImgResult.setAttribute("id", "imgInput");
modalImgResult.style.display = "none";
modalImgInput = document.createElement("input");
modalImgInput.setAttribute("type", "file");
modalImgInput.setAttribute("required", "");
modalImgInput.setAttribute("id", "img-project");
modalImgInput.style.display = "none";
modalImgInput.onchange = () => {
const [file] = modalImgInput.files;
if (file) {
modalImgEmpty.style.display = "none";
modalImgResult.style.display = "block";
modalImgLabel.style.display = "none";
modalImgDesc.style.display = "none";
modalImgResult.src = URL.createObjectURL(file);
}
}
let modalImgDesc;
modalImgDesc = document.createElement("p");
modalImgDesc.innerText = "jpg, png : 4Mo max"
modalHeader = document.querySelector(".modal-header");
modal2Body = document.querySelector(".modal-body2");
modalFooter = document.querySelector(".modal-footer");
modal2Body.innerHTML = "";
imgInputForm = document.createElement("div");
imgInputForm.setAttribute("class", "image-input");
imgInputForm.appendChild(modalImgInput);
imgInputForm.appendChild(modalImgResult);
imgInputForm.appendChild(modalImgEmpty);
imgInputForm.appendChild(modalImgLabel);
imgInputForm.appendChild(modalImgDesc)
let imgTitleInput;
let imgTitleLabel;
let imgTitle;
imgTitleInput = document.createElement("div");
imgTitleInput.setAttribute("id", "image-titre");
imgTitleLabel = document.createElement("label");
imgTitleLabel.setAttribute("for", "titre");
imgTitleLabel.innerText = "Titre"
imgTitle = document.createElement("input");
imgTitle.setAttribute("name", "titre");
imgTitle.setAttribute("id", "titre-project");
imgTitle.setAttribute("required", "");
imgTitleInput.appendChild(imgTitleLabel)
imgTitleInput.appendChild(imgTitle);
let imgCategoriesInput;
let imgCategoriesLabel
let imgCategories;
let imgCategoriesList;
imgCategoriesInput = document.createElement("div");
imgCategoriesInput.setAttribute("id", "image-categorie");
imgCategoriesLabel = document.createElement("label");
imgCategoriesLabel.setAttribute("for", "categorie");
imgCategoriesLabel.innerText = "Catégorie"
imgCategories = document.createElement("select");
imgCategories.setAttribute("id", "categ-project");
imgCategories.setAttribute("name", "categorie");
imgCategories.setAttribute("required", "");
createDefaultOption(imgCategories);
// Parcourir les catégories
categories.forEach((categorie) => {
console.log("categorie", categorie);
imgCategoriesList = document.createElement("option");
imgCategoriesList.setAttribute("class", "test-cate");
imgCategoriesList.setAttribute("value", `${categorie.id}`);
imgCategoriesList.setAttribute("id", `${categorie.id}`);
imgCategoriesList.innerHTML = `${categorie.name}`;
imgCategories.appendChild(imgCategoriesList);
})
imgCategoriesInput.appendChild(imgCategoriesLabel)
imgCategoriesInput.appendChild(imgCategories);
modalGallery.style.display = "none";
modal2.style.display = "block";
modal2Body.appendChild(imgInputForm);
modal2Body.appendChild(imgTitleInput);
modal2Body.appendChild(imgCategoriesInput);
let modalImgValidButton = document.createElement("button");
modalImgValidButton.setAttribute("id", "valider-button")
modalImgValidButton.setAttribute("class", "disabled")
modalImgValidButton.innerText = "Valider";
modalImgValidButton.disabled = true;
modal2Footer.appendChild(modalImgValidButton)
modalImgValidButton.addEventListener("click", function () {
newWork();
})
imgCategoriesInput.addEventListener("change", function () {
validateCondition();
})
imgTitleInput.addEventListener("keyup", function () {
validateCondition();
})
modalImgInput.addEventListener("change", function () {
validateCondition();
})
})
}
// Création de l'option défaut du dropdown
function createDefaultOption(imgCategories) {
const imgCategoriesList = document.createElement("option");
imgCategoriesList.innerHTML = `Selectionner une catégorie`
imgCategories.appendChild(imgCategoriesList);
}
// Envoi d'un nouveau projet
function newWork() {
let tok = sessionStorage.getItem("token");
let titleProject = document.getElementById("titre-project").value;
let imgProject = document.getElementById("img-project").files[0];
let categProject = document.getElementById("categ-project").value;
console.log("title", titleProject);
console.log("cate", categProject);
console.log("img", imgProject);
const formData = new FormData();
formData.append("image", imgProject);
formData.append("title", titleProject);
formData.append("category", categProject);
console.log("formdata", formData);
addProject(formData).then(response => {
if (response.ok) {
modalAjoutPhoto.style.display = "none";
console.log("Nouveau projet ajouté")
response.json().then(newProject => {
console.log("newProject", newProject);
projects.push(newProject)
displayProjects(newProject, true);
})
}
})
.catch((error) => {
console.error("Error : ", error);
errorMessage.textContent = "Une erreur est survenue";
});
}
// Condition pour que le bouton Valider soit cliquable
function validateCondition() {
const titreInput = document.getElementById("titre-project");
const cateInput = document.getElementById("categ-project");
const imgInput = document.getElementById("img-project");
const validButton = document.getElementById("valider-button");
if (titreInput?.value && cateInput?.value > 0 && imgInput?.files.length > 0) {
validButton.disabled = false;
validButton.classList.add("enabled");
validButton.classList.remove("disabled");
} else {
validButton.disabled = true;
validButton.classList.remove("enabled");
}
}