1 // Handle any tasks which require uploading files to the server (such as attachments)
2 // h/t https://www.smashingmagazine.com/2018/01/drag-drop-file-uploader-vanilla-js/ which inspired the design of this module
4 // Copyright (c) 2016-2023 by the citadel.org team
6 // This program is open source software. Use, duplication, or
7 // disclosure are subject to the GNU General Public License v3.
9 var uploads_in_progress = 0;
10 var uploads = [] ; // everything the user has uploaded
13 // Remove the upload window completely (even if it's hidden)
14 function deactivate_uploads() {
15 upload_window = document.getElementById('ctdl-upload');
17 upload_window.remove();
22 // Turn the specified div into a place where we can upload. (Note: permanently changes the drag-and-drop behavior of that div.)
23 function activate_uploads(parent_div) {
24 document.getElementById(parent_div).innerHTML += `
25 <div class="ctdl-upload" id="ctdl-upload">
26 <div id="ctdl_attachments_title" class="ctdl-compose-attachments-title">
27 <div><h1><i class="fa fa-paperclip" style="color:grey"></i>` + _("Attachments:") + ` <span id="num_attachments">` + uploads.length + `</span></h1></div>
28 <div><h1><i class="fas fa-window-close" style="color:red" onClick="show_or_hide_upload_window()"></i></h1></div>
31 <ul id="ctdl-upload_list">
34 <div id="drop-area" class="ctdl-upload-drop-area">
35 <form class="ctdl-upload-form">
36 <p>${_("Drop files here to upload")}</p>
37 <input type="file" id="fileElem" multiple accept="*/*" onChange="handle_upload_files(this.files)">
43 // activate drag and drop
44 let dropArea = document.getElementById(parent_div);
45 ;['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
46 dropArea.addEventListener(eventName, upload_prevent_defaults, false)
48 ;["dragenter", "dragover"].forEach(eventName => {
49 dropArea.addEventListener(eventName, upload_highlight, false)
51 ;['dragleave', 'drop'].forEach(eventName => {
52 dropArea.addEventListener(eventName, upload_unhighlight, false)
54 dropArea.addEventListener('drop', upload_handle_drop, false);
58 // prevent drag and drop events from propagating up through the DOM
59 function upload_prevent_defaults(e) {
65 function upload_handle_drop(e) {
66 let dt = e.dataTransfer;
68 handle_upload_files(files);
72 function handle_upload_files(files) {
73 ([...files]).forEach(upload_file)
77 // Delete an uploaded item from the list
78 delete_upload = async(ref) => {
80 response = await fetch(
81 "/ctdl/p/" + ref, { method: "DELETE" }
84 if (response.ok) { // If the server accepted the delete...
85 var el = document.getElementById(ref); // ...remove it from the screen...
86 el.parentNode.removeChild(el);
87 uploads = uploads.filter((r) => r.ref != ref); // ...remove it from the array...
88 document.getElementById("num_attachments").innerHTML = uploads.length; // ...and update our count
93 function upload_file(file) {
95 var xhr = new XMLHttpRequest();
96 var formData = new FormData();
97 xhr.open('POST', url, true);
99 xhr.addEventListener('readystatechange', function(e) {
100 if (xhr.readyState == 4 && xhr.status == 200) {
101 // remove the "uploading in progress" message
102 let li = document.getElementById("ctdl_uploading_" + uploads_in_progress.toString());
103 li.parentNode.removeChild(li);
104 uploads_in_progress -= 1;
106 // The response body will be a JSON array of completed uploads.
107 var j_response = JSON.parse(xhr.response);
109 // Add these uploads to the displayed list
110 j_response.forEach((item) => {
111 let new_upl = document.createElement("li");
112 new_upl.setAttribute("id", item["ref"]); // set the element id to the upload reference
113 new_upl.innerHTML = `<i class="fa-solid fa-circle-xmark" style="color:red" onClick="delete_upload('` + item["ref"] + `')"></i>`
115 + item["uploadfilename"] + " (" + item["contenttype"] + ", " + item["contentlength"] + " " + _("bytes") + ")";
116 document.getElementById("ctdl-upload_list").appendChild(new_upl);
118 // append it to the global list of uploads
121 document.getElementById("num_attachments").innerHTML = uploads.length;
123 else if (xhr.readyState == 4 && xhr.status != 200) {
124 // remove the "uploading in progress" message (there was an error, so just let it disappear)
125 let li = document.getElementById("ctdl_uploading_" + uploads_in_progress.toString());
126 li.parentNode.removeChild(li);
127 uploads_in_progress -= 1;
131 formData.append('file', file);
133 uploads_in_progress += 1;
135 // Make an "uploading in progress" message appear in the uploads list!
136 progress = document.createElement("li");
137 progress.setAttribute("id", "ctdl_uploading_" + uploads_in_progress.toString());
138 progress.innerHTML = `<img src="/ctdl/s/images/dotcrawl.gif" /> ` + _("Processing dropped files...");
139 document.getElementById("ctdl-upload_list").appendChild(progress);
143 // called when the user drags a file into the upload area
144 function upload_highlight(e) {
145 let dropArea = document.getElementById("ctdl-upload");
146 dropArea.classList.add('highlight')
148 document.getElementById("ctdl-upload").style.display = "block"; /* also make it appear */
152 // called when the user is no longer dragging a file into the upload area
153 function upload_unhighlight(e) {
154 let dropArea = document.getElementById("ctdl-upload");
155 dropArea.classList.remove('highlight')
159 // Show or hide the attachments window in the composer
160 function show_or_hide_upload_window() {
162 if (document.getElementById("ctdl-upload").style.display == "block") {
163 document.getElementById("ctdl-upload").style.display = "none"; /* turn it off */
166 document.getElementById("ctdl-upload").style.display = "block"; /* turn it on */
171 // Helper function for flush_uploads()
172 flush_one_upload = async(ref) => {
173 response = await fetch(
174 "/ctdl/p/" + ref, { method: "DELETE" }
176 // We don't have any interest in the server response.
180 // Flush all uploaded files and close the window
181 function flush_uploads() {
182 upload_window = document.getElementById('ctdl-upload');
185 upload_window.style.display='none';
188 // tell the server to delete the files
189 uploads.forEach(u => {
190 flush_one_upload(u.ref);
193 document.getElementById("num_attachments").innerHTML = uploads.length;
195 deactivate_uploads(); // this makes the window get destroyed too