linx-server/static/js/upload.js

102 lines
3.7 KiB
JavaScript
Raw Normal View History

// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
2015-09-30 23:37:00 -04:00
Dropzone.options.dropzone = {
2015-10-01 12:46:34 -04:00
init: function() {
var dzone = document.getElementById("dzone");
dzone.style.display = "block";
2015-10-01 12:46:34 -04:00
},
2015-09-30 23:37:00 -04:00
addedfile: function(file) {
var upload = document.createElement("div");
upload.className = "upload";
2015-10-04 03:14:21 -04:00
var fileLabel = document.createElement("span");
fileLabel.innerHTML = file.name;
file.fileLabel = fileLabel;
upload.appendChild(fileLabel);
2015-09-30 23:37:00 -04:00
2015-10-04 03:14:21 -04:00
var fileActions = document.createElement("div");
fileActions.className = "right";
file.fileActions = fileActions;
upload.appendChild(fileActions);
var cancelAction = document.createElement("span");
cancelAction.className = "cancel";
cancelAction.innerHTML = "Cancel";
cancelAction.addEventListener('click', function(ev) {
this.removeFile(file);
}.bind(this));
file.cancelActionElement = cancelAction;
fileActions.appendChild(cancelAction);
var progress = document.createElement("span");
file.progressElement = progress;
fileActions.appendChild(progress);
2015-09-30 23:37:00 -04:00
file.uploadElement = upload;
2015-10-04 03:14:21 -04:00
2015-09-30 23:37:00 -04:00
document.getElementById("uploads").appendChild(upload);
},
uploadprogress: function(file, p, bytesSent) {
p = parseInt(p);
2015-10-04 03:14:21 -04:00
file.progressElement.innerHTML = p + "%";
2015-09-30 23:37:00 -04:00
file.uploadElement.setAttribute("style", 'background-image: -webkit-linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%); background-image: -moz-linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%); background-image: -ms-linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%); background-image: -o-linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%); background-image: linear-gradient(left, #F2F4F7 ' + p + '%, #E2E2E2 ' + p + '%)');
},
2015-09-30 23:55:03 -04:00
sending: function(file, xhr, formData) {
formData.append("randomize", document.getElementById("randomize").checked);
formData.append("expires", document.getElementById("expires").selectedOptions[0].value);
},
2015-09-30 23:37:00 -04:00
success: function(file, resp) {
2015-10-04 03:14:21 -04:00
file.fileActions.removeChild(file.progressElement);
var fileLabelLink = document.createElement("a");
fileLabelLink.href = resp.url;
fileLabelLink.target = "_blank";
fileLabelLink.innerHTML = resp.url;
file.fileLabel.innerHTML = "";
file.fileLabelLink = fileLabelLink;
file.fileLabel.appendChild(fileLabelLink);
var deleteAction = document.createElement("span");
deleteAction.innerHTML = "Delete";
deleteAction.className = "cancel";
deleteAction.addEventListener('click', function(ev) {
2015-09-30 23:37:00 -04:00
xhr = new XMLHttpRequest();
xhr.open("DELETE", resp.url, true);
xhr.setRequestHeader("Linx-Delete-Key", resp.delete_key);
2015-09-30 23:37:00 -04:00
xhr.onreadystatechange = function(file) {
2015-10-04 03:14:21 -04:00
if (xhr.readyState == 4 && xhr.status === 200) {
var text = document.createTextNode("Deleted ");
file.fileLabel.insertBefore(text, file.fileLabelLink);
file.fileLabel.className = "deleted";
file.fileActions.removeChild(file.cancelActionElement);
2015-09-30 23:37:00 -04:00
}
}.bind(this, file);
xhr.send();
2015-10-04 03:14:21 -04:00
});
file.fileActions.removeChild(file.cancelActionElement);
file.cancelActionElement = deleteAction;
file.fileActions.appendChild(deleteAction);
2015-09-30 23:37:00 -04:00
},
error: function(file, resp, xhrO) {
2015-10-04 03:14:21 -04:00
file.fileActions.removeChild(file.cancelActionElement);
file.fileActions.removeChild(file.progressElement);
2015-09-30 23:37:00 -04:00
if (file.status === "canceled") {
2015-10-04 17:39:29 -04:00
file.fileLabel.innerHTML = file.name + ": Canceled ";
2015-09-30 23:37:00 -04:00
}
else {
2015-10-04 17:39:29 -04:00
file.fileLabel.innerHTML = file.name + ": " + resp.error;
2015-09-30 23:37:00 -04:00
}
2015-10-04 03:14:21 -04:00
file.fileLabel.className = "error";
2015-09-30 23:37:00 -04:00
},
2015-10-01 12:46:34 -04:00
2015-09-30 23:37:00 -04:00
maxFilesize: 4096,
previewsContainer: "#uploads",
parallelUploads: 5,
headers: {"Accept": "application/json"},
dictDefaultMessage: "Click or Drop file(s)",
dictFallbackMessage: ""
};
// @end-license