From 738b6ae5832fb7c2992ec0dc52399e4dbf3d096d Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 13 Feb 2016 19:24:36 -0500 Subject: Add file drag and drop and asynchronous upload --- assets/js/src/App.js | 2 + assets/js/src/FileUpload.js | 124 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 assets/js/src/FileUpload.js (limited to 'assets/js/src') diff --git a/assets/js/src/App.js b/assets/js/src/App.js index d44d9f81..33b3c6b1 100644 --- a/assets/js/src/App.js +++ b/assets/js/src/App.js @@ -9,6 +9,7 @@ function App() { this.task = new Task(); this.project = new Project(); this.subtask = new Subtask(); + this.file = new FileUpload(this); this.keyboardShortcuts(); this.chosen(); this.poll(); @@ -39,6 +40,7 @@ App.prototype.listen = function() { this.task.listen(); this.swimlane.listen(); this.subtask.listen(); + this.file.listen(); this.search.focus(); this.autoComplete(); this.datePicker(); diff --git a/assets/js/src/FileUpload.js b/assets/js/src/FileUpload.js new file mode 100644 index 00000000..a8816bcd --- /dev/null +++ b/assets/js/src/FileUpload.js @@ -0,0 +1,124 @@ +function FileUpload(app) { + this.app = app; + this.files = []; + this.currentFile = 0; +} + +FileUpload.prototype.listen = function() { + var dropzone = document.getElementById("file-dropzone"); + var self = this; + + if (dropzone) { + dropzone.ondragover = dropzone.ondragenter = function(e) { + e.stopPropagation(); + e.preventDefault(); + } + + dropzone.ondrop = function(e) { + e.stopPropagation(); + e.preventDefault(); + self.files = e.dataTransfer.files; + self.show(); + $("#file-error-max-size").hide(); + } + + $(document).on("click", "#file-browser", function(e) { + e.preventDefault(); + $("#file-form-element").get(0).click(); + }); + + $(document).on("click", "#file-upload-button", function(e) { + e.preventDefault(); + self.currentFile = 0; + self.checkFiles(); + }); + + $("#file-form-element").change(function() { + self.files = document.getElementById("file-form-element").files; + self.show(); + $("#file-error-max-size").hide(); + }); + } +}; + +FileUpload.prototype.show = function() { + $("#file-list").remove(); + + if (this.files.length > 0) { + $("#file-upload-button").prop("disabled", false); + $("#file-dropzone-inner").hide(); + + var ul = jQuery("