]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/static/js/upload.js
upload.js: moved the delete-an-attachment skeleton code to its own function
[citadel.git] / webcit-ng / static / js / upload.js
index 96e02b972e7a53b90b556155c293723140ac91cf..ac72bbee8e8afc96f0ead6e61d499e767bd1201f 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,19 @@ 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, remove it from the screen
+               // FIXME do the delete
+       }
+}
+
+
 function upload_file(file) {
        var url = '/ctdl/p/';
        var xhr = new XMLHttpRequest();
@@ -85,14 +107,14 @@ function upload_file(file) {
                        j_response.forEach((item) => {
                                let new_upl = document.createElement("li");
                                // item["ref"] is what we need
-                               new_upl.innerHTML = `<i class="fa-solid fa-circle-xmark" style="color:red" onClick="alert('` + item["ref"] + `')"></i>`
+                               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) {
@@ -145,11 +167,17 @@ function show_or_hide_upload_window() {
 
 // 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
        uploads.forEach(u => {
                console.log(u.ref);
        });
         uploads=[];
+
+       deactivate_uploads();   // this makes the window get destroyed too
 }