-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24-script.js
146 lines (120 loc) · 7.23 KB
/
24-script.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
document.addEventListener("DOMContentLoaded", function() {
initImageContainers();
attachEventHandlers();
});
function initImageContainers() {
var imageContainers = [
{ imageId: 'image-principale', pathId: 'image_path1', oldTextId: 'ancien-texte1', newTextId: 'nouveau-texte1', deleteButtonId: 'delete-button1', deleteFlagId: 'delete-flag1' },
{ imageId: 'image-secondaires2', pathId: 'image_path2', oldTextId: 'ancien-texte2', newTextId: 'nouveau-texte2', deleteButtonId: 'delete-button2', deleteFlagId: 'delete-flag2' },
{ imageId: 'image-secondaires3', pathId: 'image_path3', oldTextId: 'ancien-texte3', newTextId: 'nouveau-texte3', deleteButtonId: 'delete-button3', deleteFlagId: 'delete-flag3' },
{ imageId: 'image-secondaires4', pathId: 'image_path4', oldTextId: 'ancien-texte4', newTextId: 'nouveau-texte4', deleteButtonId: 'delete-button4', deleteFlagId: 'delete-flag4' },
{ imageId: 'image-secondaires5', pathId: 'image_path5', oldTextId: 'ancien-texte5', newTextId: 'nouveau-texte5', deleteButtonId: 'delete-button5', deleteFlagId: 'delete-flag5' },
];
imageContainers.forEach(function(container) {
initImageContainer(container.imageId, container.pathId, container.oldTextId, container.newTextId, container.deleteButtonId, container.deleteFlagId);
});
}
function initImageContainer(imageId, fileInputId, oldTextId, newTextId, deleteButtonId, deleteFlagId) {
var image = document.getElementById(imageId);
var fileInput = document.getElementById(fileInputId);
var oldText = document.getElementById(oldTextId);
var newText = document.getElementById(newTextId);
var deleteButton = document.getElementById(deleteButtonId);
var hasImage = image.src && image.complete && image.naturalHeight !== 0;
image.style.display = hasImage ? 'block' : 'none';
oldText.style.display = hasImage ? 'block' : 'none';
newText.style.display = hasImage ? 'none' : 'none';
deleteButton.style.display = hasImage ? 'block' : 'none';
fileInput.style.display = hasImage ? 'none' : 'block';
document.getElementById(deleteFlagId).value = hasImage ? 'false' : 'true';
}
function attachEventHandlers() {
var imagePaths = [
{ pathId: 'image_path1', imageId: 'image-principale', oldTextId: 'ancien-texte1', newTextId: 'nouveau-texte1', deleteButtonId: 'delete-button1', deleteFlagId: 'delete-flag1' },
{ pathId: 'image_path2', imageId: 'image-secondaires2', oldTextId: 'ancien-texte2', newTextId: 'nouveau-texte2', deleteButtonId: 'delete-button2', deleteFlagId: 'delete-flag2' },
{ pathId: 'image_path3', imageId: 'image-secondaires3', oldTextId: 'ancien-texte3', newTextId: 'nouveau-texte3', deleteButtonId: 'delete-button3', deleteFlagId: 'delete-flag3' },
{ pathId: 'image_path4', imageId: 'image-secondaires4', oldTextId: 'ancien-texte4', newTextId: 'nouveau-texte4', deleteButtonId: 'delete-button4', deleteFlagId: 'delete-flag4' },
{ pathId: 'image_path5', imageId: 'image-secondaires5', oldTextId: 'ancien-texte5', newTextId: 'nouveau-texte5', deleteButtonId: 'delete-button5', deleteFlagId: 'delete-flag5' },
];
imagePaths.forEach(function(path) {
// Gestionnaire pour le chargement des images
document.getElementById(path.pathId).addEventListener('change', function(event) {
createFileUploadHandler(path.imageId, 'image-defaut', path.newTextId, path.oldTextId, path.deleteButtonId, path.deleteFlagId)(event);
});
// Gestionnaire pour la suppression des images
var deleteButton = document.getElementById(path.deleteButtonId);
if (deleteButton) {
deleteButton.addEventListener('click', function(event) {
removeImage(event, path.imageId, 'image-defaut', path.newTextId, path.oldTextId, path.deleteButtonId, path.deleteFlagId);
});
}
});
}
function createFileUploadHandler(imageId, defaultImageId, newTextId, oldTextId, deleteButtonId, deleteFlagId) {
return function(event) {
var reader = new FileReader();
reader.onload = function(e) {
// Afficher la nouvelle image
var image = document.getElementById(imageId);
image.src = e.target.result;
image.style.display = 'block';
// Masquer le texte "image actuelle" et afficher le texte "nouvelle image"
var oldText = document.getElementById(oldTextId);
var newText = document.getElementById(newTextId);
oldText.style.display = 'none';
newText.style.display = 'block';
// Masquer le champ de saisie du fichier
var fileInput = document.getElementById(deleteFlagId.replace('delete-flag', 'image_path'));
fileInput.style.display = 'none';
// Masquer l'image par défaut si elle existe
var defaultImage = document.getElementById(defaultImageId);
defaultImage.style.display = 'none';
// Afficher le bouton de suppression
var deleteButton = document.getElementById(deleteButtonId);
// Création dynamique du bouton de suppression si nécessaire
var deleteButton = document.getElementById(deleteButtonId);
if (!deleteButton) {
deleteButton = document.createElement('button');
deleteButton.id = deleteButtonId;
deleteButton.className = 'delete-button';
deleteButton.innerHTML = '<img src="/images/delete.png" alt="Supprimer" />';
deleteButton.onclick = function(event) {
removeImage(event, imageId, defaultImageId, newTextId, oldTextId, deleteButtonId, deleteFlagId);
};
// Insérer le bouton dans le DOM
image.parentNode.insertBefore(deleteButton, image.nextSibling);
}
deleteButton.style.display = 'block';
// Réinitialiser le drapeau de suppression
document.getElementById(deleteFlagId).value = 'false';
};
reader.readAsDataURL(event.target.files[0]);
};
}
function removeImage(event, imageId, defaultImageId, newTextId, oldTextId, deleteButtonId, deleteFlagId, fileInputId) {
event.preventDefault();
// Cachez tous les éléments sauf le champ de saisie du fichier
var elementsToHide = [imageId, defaultImageId, newTextId, oldTextId, deleteButtonId].map(function(id) {
return document.getElementById(id);
});
elementsToHide.forEach(function(element) {
if (element) {
element.style.display = 'none';
}
});
// Réinitialisez et affichez le champ de saisie du fichier
var fileInput = document.getElementById(fileInputId);
if (!fileInput) {
// Créez un nouveau champ de saisie de fichier s'il n'existe pas
fileInput.value = '';
fileInput.style.display = 'block';
// Insérez le champ de saisie de fichier dans le DOM
var container = document.getElementById(imageId).parentNode;
container.appendChild(fileInput);
}
// Si le champ de saisie du fichier existe, réinitialisez-le simplement
fileInput.value = '';
fileInput.style.display = 'block';
// Indiquez que l'image a été supprimée
document.getElementById(deleteFlagId).value = 'true';
}