]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/static/js/upload.js
view_mail.js: remove upload from local array when deleted.
[citadel.git] / webcit-ng / static / js / upload.js
index 28c374edb114302bb8c3217ae21d3b96cbe44e57..257ff75624b31d7ff801fbffc21af4b6a803e259 100644 (file)
@@ -10,6 +10,15 @@ var uploads_in_progress = 0;
 var uploads = [] ;                                                             // everything the user has uploaded
 
 
+// Remove the upload window completely (even if it's hidden)
+function deactivate_uploads() {
+        upload_window = document.getElementById('ctdl-upload');
+       if (upload_window) {
+               upload_window.remove();
+       }
+}
+
+
 // Turn the specified div into a place where we can upload.  (Note: permanently changes the drag-and-drop behavior of that div.)
 function activate_uploads(parent_div) {
                document.getElementById(parent_div).innerHTML += `
@@ -65,6 +74,22 @@ function handle_upload_files(files) {
 }
 
 
+// Delete an uploaded item from the list
+delete_upload = async(ref) => {
+
+       response = await fetch(
+               "/ctdl/p/" + ref, { method: "DELETE" }
+       );
+
+       if (response.ok) {                                                                      // If the server accepted the delete...
+               var el = document.getElementById(ref);                                          // ...remove it from the screen...
+               el.parentNode.removeChild(el);
+               uploads = uploads.filter((r) => r.ref != ref);                                  // ...remove it from the array...
+               document.getElementById("num_attachments").innerHTML = uploads.length;          // ...and update our count
+       }
+}
+
+
 function upload_file(file) {
        var url = '/ctdl/p/';
        var xhr = new XMLHttpRequest();
@@ -84,14 +109,15 @@ function upload_file(file) {
                        // Add these uploads to the displayed list
                        j_response.forEach((item) => {
                                let new_upl = document.createElement("li");
-                               // item["ref"] is what we need
-                               new_upl.innerHTML = `<a href="xxx">❌</a>`
+                               new_upl.setAttribute("id", item["ref"]);        // set the element id to the upload reference
+                               new_upl.innerHTML = `<i class="fa-solid fa-circle-xmark" style="color:red" onClick="delete_upload('` + item["ref"] + `')"></i>`
+                               + `&nbsp;`
                                + item["uploadfilename"] + " (" + item["contenttype"] + ", " + item["contentlength"] + " " + _("bytes") + ")";
                                document.getElementById("ctdl-upload_list").appendChild(new_upl);
-                       });
 
-                       // append it to the global list of uploads
-                       uploads.push(j_response);
+                               // append it to the global list of uploads
+                               uploads.push(item);
+                       });
                        document.getElementById("num_attachments").innerHTML = uploads.length;
                }
                else if (xhr.readyState == 4 && xhr.status != 200) {
@@ -142,13 +168,29 @@ function show_or_hide_upload_window() {
 }
 
 
+// Helper function for flush_uploads()
+flush_one_upload = async(ref) => {
+       response = await fetch(
+               "/ctdl/p/" + ref, { method: "DELETE" }
+       );
+       // We don't have any interest in the server response.
+}
+
+
 // Flush all uploaded files and close the window
 function flush_uploads() {
-        document.getElementById('ctdl-upload').style.display='none';
+        upload_window = document.getElementById('ctdl-upload');
+
+       if (upload_window) {
+               upload_window.style.display='none';
+       }
 
-        // FIXME tell the server to delete the files
+        // tell the server to delete the files
        uploads.forEach(u => {
-               console.log(u.ref);
+               flush_one_upload(u.ref);
        });
         uploads=[];
+       document.getElementById("num_attachments").innerHTML = uploads.length;
+
+       deactivate_uploads();   // this makes the window get destroyed too
 }