partage

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.z3bra.org/partage.git
Log | Files | Refs | README | LICENSE

dz.js (2414B)


      1 // Handle drag and drop into a dropzone_element div:
      2 // send the files as a POST request to the server
      3 "use strict";
      4 
      5 // Only start once the DOM tree is ready
      6 if(document.readyState === "complete") {
      7     setupzone();
      8 } else {
      9     document.addEventListener("DOMContentLoaded", setupzone);
     10 }
     11 
     12 function setupzone() {
     13     let dropzone = document.getElementById("dropzone");
     14     let fileinput = document.getElementById("filebox");
     15     let fallbackform = document.getElementById("fallbackform");
     16 
     17     fallbackform.style.display = "none";
     18 
     19     dropzone.className = "dropzone";
     20     dropzone.innerHTML = "Click or drop file(s)";
     21 
     22     dropzone.onclick = function() {
     23         fileinput.click()
     24 	return false;
     25     }
     26 
     27     dropzone.ondragover = function() {
     28         this.className = "dropzone dragover";
     29         return false;
     30     }
     31     
     32     dropzone.ondragleave = function() {
     33         this.className = "dropzone";
     34         return false;
     35     }
     36 
     37     dropzone.ondrop = function(e) {
     38         // Stop browser from simply opening that was just dropped
     39         e.preventDefault();  
     40         // Restore original dropzone appearance
     41         this.className = "dropzone";
     42         sendfiles(e.dataTransfer.files)
     43     }
     44 
     45     fileinput.onchange = function(e) {
     46         sendfiles(this.files)
     47     }
     48 }
     49 
     50 function sendfiles(files) {
     51     let uploads = document.getElementById("uploads");
     52     let progressbar = document.createElement("progress");
     53     let uploadlist = document.createElement("ul");
     54     let formData = new FormData(), xhr = new XMLHttpRequest();
     55 
     56     uploads.appendChild(progressbar);
     57     uploads.appendChild(uploadlist);
     58 
     59     formData.append("expiry", 10);
     60     for(let i=0; i < files.length; i++) {
     61         formData.append("file", files[i]);
     62     }
     63 
     64     // triggers periodically
     65     xhr.upload.onprogress = function(e) {
     66         // e.loaded - how many bytes downloaded
     67         // e.lengthComputable = true if the server sent Content-Length header
     68         // e.total - total number of bytes (if lengthComputable)
     69 
     70     }
     71 
     72     xhr.onreadystatechange = function() {
     73         if(xhr.readyState === XMLHttpRequest.DONE) {
     74 		progressbar.remove();
     75 		this.response.split(/\r?\n/).forEach(function(link) {
     76 			let li = document.createElement("li");
     77 			li.innerHTML = `<a href="${link}">${link}</a>`;
     78 			uploadlist.appendChild(li);
     79 		});
     80         }
     81     }
     82 
     83     xhr.open('POST', window.location.href, true); // async = true
     84     xhr.send(formData); 
     85 }