When forwarding an email, pre-load its attachments.
[citadel.git] / webcit-ng / static / js / upload.js
index 54713f5dd9c6493564aad330e6a0453d9b3d1911..3f6a33519f4372f64f0102c0e3d1b3eda048577a 100644 (file)
@@ -97,11 +97,25 @@ delete_upload = async(ref) => {
                el.parentNode.removeChild(el);
                uploads = uploads.filter((r) => r.ref != ref);                                  // ...remove it from the array...
                update_attachment_count();
-               // document.getElementById(attachment_counter_divs[0]).innerHTML = uploads.length;      // ...and update our count
        }
 }
 
 
+// When an upload (or reupload) is complete, call this function to add it to the on-screen attachments list.
+function add_upload_to_displayed_list(item) {
+       let new_upl = document.createElement("li");
+       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(item);
+       update_attachment_count();
+}
+
+
 function upload_file(file) {
        var url = '/ctdl/p/';
        var xhr = new XMLHttpRequest();
@@ -119,19 +133,7 @@ function upload_file(file) {
                        var j_response = JSON.parse(xhr.response);
 
                        // Add these uploads to the displayed list
-                       j_response.forEach((item) => {
-                               let new_upl = document.createElement("li");
-                               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(item);
-                       });
-                       update_attachment_count();
-                       // document.getElementById(attachment_counter_divs[0]).innerHTML = uploads.length;
+                       j_response.forEach((item) => add_upload_to_displayed_list(item));
                }
                else if (xhr.readyState == 4 && xhr.status != 200) {
                        // remove the "uploading in progress" message (there was an error, so just let it disappear)
@@ -204,7 +206,20 @@ function flush_uploads() {
        });
         uploads=[];
        update_attachment_count();
-       // document.getElementById(attachment_counter_divs[0]).innerHTML = uploads.length;
 
        deactivate_uploads();   // this makes the window get destroyed too
 }
+
+
+// Preload a forwarded message's attachments into the new copy currently being composed.
+function forward_attachments(msgnum) {
+       fetch(
+               "/ctdl/p/" + msgnum, { method: "GET" }
+       )
+       .then(response => response.json())
+       .then(j => {
+               j.forEach((item) => {
+                       add_upload_to_displayed_list(item);
+               })
+       })
+}