Skip to content

Commit 8404b22

Browse files
committed
Add button for deleting uploads
1 parent 7f91672 commit 8404b22

File tree

5 files changed

+51
-19
lines changed

5 files changed

+51
-19
lines changed

conf/language.de.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ button_save : Speichern
1717
button_send : Senden
1818
button_clone: Feld hinzufügen
1919
button_upload: Dateien hochladen
20-
button_upload_replace: Dateien ersetzen
20+
button_upload_replace: Dateien löschen
2121
uploaded_file: Hochgeladene Dateien
2222
uploaded_original: Urspünglicher Deteiname
2323
upload_info: "Die ausgewählten Dateien werden beim Speichern oder Senden hochgeladen und geprüft:"

conf/language.default.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ button_save : Save
1717
button_send : Send
1818
button_clone: Copy field
1919
button_upload: Upload files
20-
button_upload_replace: Replace uploaded files
20+
button_upload_replace: Delete uploaded files
2121
uploaded_file: Saved file
2222
uploaded_original: Original filename
2323
upload_info: "The selected files will be uploaded and checked after saving or sending the form:"

conf/language.fr.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ button_save : Économiser
1717
button_send : Envoyer
1818
button_clone: Copier le champ
1919
button_upload: Télécharger le fichier
20-
button_upload_replace: Remplacer le fichier téléchargé
20+
button_upload_replace: Supprimer le fichier téléchargé
2121
uploaded_file: Fichier enregistré
2222
upload_info: Le fichier sélectionné sera téléchargé et vérifié après l'enregistrement ou l'envoi du formulaire.
2323
upload_error: Le téléchargement est trop important!

conf/language.it.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ button_save : Risparmiare
1717
button_send : Inviare
1818
button_clone: Campo di copia
1919
button_upload: Caricare il file
20-
button_upload_replace: Sostituire il file caricato
20+
button_upload_replace: Eliminare il file caricato
2121
uploaded_file: File salvato
2222
upload_info: Il file selezionato verrà caricato e controllato dopo il salvataggio o l'invio del modulo.
2323
upload_error: Il caricamento è troppo grande!

js_src/components/UploadComponent.js

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export class UploadComponent extends BaseComponent {
88
#currentFiles;
99

1010
html() {
11+
// not using U.createField() because we need a different structure
1112
const field = document.createElement("div");
1213
field.classList.add("field", "file", "dropzone");
1314

@@ -27,13 +28,13 @@ export class UploadComponent extends BaseComponent {
2728
control.appendChild(U.modalHint(this.config));
2829
control.appendChild(U.modal(this.config));
2930
}
30-
3131
field.appendChild(control);
3232

33-
const fileLabel = document.createElement('label');
34-
fileLabel.classList.add('file-label');
33+
// input container must be a label to trigger upload
34+
const fileInputContainer = document.createElement("label");
35+
fileInputContainer.classList.add("buttons", "file-label");
3536

36-
control.appendChild(fileLabel);
37+
control.appendChild(fileInputContainer);
3738

3839
// actual file input and Bulma call-to-action (cta) element
3940
const input = document.createElement("input");
@@ -48,11 +49,11 @@ export class UploadComponent extends BaseComponent {
4849
input.dataset['max'] = this.config.validation.filesize;
4950
}
5051

51-
fileLabel.appendChild(input);
52+
fileInputContainer.appendChild(input);
5253

5354
const cta = document.createElement("span");
5455
cta.classList.add("file-cta");
55-
fileLabel.appendChild(cta);
56+
fileInputContainer.appendChild(cta);
5657

5758
const labelSpan = document.createElement("span");
5859
labelSpan.classList.add("file-label");
@@ -64,14 +65,39 @@ export class UploadComponent extends BaseComponent {
6465
infoContainer.classList.add("notification", "hidden", "is-warning");
6566
control.appendChild(infoContainer);
6667

67-
// upload status from state
68-
this.#showStatus(infoContainer);
69-
7068
const errorContainer = document.createElement("div");
7169
errorContainer.classList.add("notification", "hidden", "is-danger");
7270
control.appendChild(errorContainer);
7371

74-
// drag & drop handling
72+
// create upload status info from state
73+
this.#showStatus(infoContainer);
74+
75+
// button to delete existing uploads
76+
const deleteButton = document.createElement("button");
77+
deleteButton.type = "button";
78+
deleteButton.classList.add("button");
79+
deleteButton.innerText = U.getLang("button_upload_replace");
80+
81+
deleteButton.addEventListener("click", e => {
82+
this.myState.value = null;
83+
input.value = null;
84+
this.render();
85+
});
86+
87+
infoContainer.appendChild(deleteButton);
88+
89+
this.attachDragAndDropHandlers(field, input);
90+
91+
return field;
92+
}
93+
94+
/**
95+
* Add drag & drop handlers
96+
*
97+
* @param {HTMLElement} field
98+
* @param {HTMLElement} input
99+
*/
100+
attachDragAndDropHandlers(field, input) {
75101
field.addEventListener("drop", e => {
76102
e.preventDefault();
77103
e.stopPropagation();
@@ -94,8 +120,6 @@ export class UploadComponent extends BaseComponent {
94120
e.stopPropagation();
95121
e.target.classList.remove("highlight");
96122
});
97-
98-
return field;
99123
}
100124

101125
/** @override */
@@ -134,13 +158,21 @@ export class UploadComponent extends BaseComponent {
134158
if (!this.myState.value) {
135159
return;
136160
}
137-
const uploads = document.createElement("ul");
138-
infoNotification.appendChild(uploads);
161+
162+
const oldUploads = infoNotification.querySelector("ul");
163+
const newUploads = document.createElement("ul");
164+
139165
for (const fileInfo of this.myState.value) {
140-
uploads.insertAdjacentHTML(
166+
newUploads.insertAdjacentHTML(
141167
"beforeend",
142168
`<li><a href="${fileInfo.content}" download="${fileInfo.file}">${fileInfo.file}</a></li>`)
143169
}
170+
171+
if (oldUploads !== null) {
172+
infoNotification.replaceChild(newUploads, oldUploads);
173+
} else {
174+
infoNotification.appendChild(newUploads);
175+
}
144176
infoNotification.classList.remove("hidden");
145177
}
146178

0 commit comments

Comments
 (0)