From db01f3646ae2e3ab3fb527a8361a52e4f8aae095 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 13 May 2020 11:13:26 +1000 Subject: [PATCH] Enable drag-and-drop attachment upload for Part --- .../InvenTree/static/script/inventree/inventree.js | 8 ++++++++ InvenTree/part/templates/part/attachments.html | 14 ++++++++++++++ InvenTree/templates/attachment_table.html | 4 +++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/static/script/inventree/inventree.js b/InvenTree/InvenTree/static/script/inventree/inventree.js index 5995034241..45565f1d6a 100644 --- a/InvenTree/InvenTree/static/script/inventree/inventree.js +++ b/InvenTree/InvenTree/static/script/inventree/inventree.js @@ -140,10 +140,13 @@ function enableDragAndDrop(element, url, options) { url - URL to POST the file to options - object with following possible values: label - Label of the file to upload (default='file') + data - Other form data to upload success - Callback function in case of success error - Callback function in case of error */ + data = options.data || {}; + $(element).on('drop', function(event) { var transfer = event.originalEvent.dataTransfer; @@ -152,6 +155,11 @@ function enableDragAndDrop(element, url, options) { var formData = new FormData(); + // Add the extra data + for (var key in data) { + formData.append(key, data[key]); + } + if (isFileTransfer(transfer)) { formData.append(label, transfer.files[0]); diff --git a/InvenTree/part/templates/part/attachments.html b/InvenTree/part/templates/part/attachments.html index 965645b748..049ef0cd7a 100644 --- a/InvenTree/part/templates/part/attachments.html +++ b/InvenTree/part/templates/part/attachments.html @@ -16,6 +16,20 @@ {% block js_ready %} {{ block.super }} + enableDragAndDrop( + '#attachment-dropzone', + "{% url 'part-attachment-create' %}", + { + data: { + part: {{ part.id }}, + }, + label: 'attachment', + success: function(data, status, xhr) { + location.reload(); + } + } + ); + $("#new-attachment").click(function() { launchModalForm("{% url 'part-attachment-create' %}?part={{ part.id }}", { diff --git a/InvenTree/templates/attachment_table.html b/InvenTree/templates/attachment_table.html index 090ae566f6..71664a3ccc 100644 --- a/InvenTree/templates/attachment_table.html +++ b/InvenTree/templates/attachment_table.html @@ -6,6 +6,7 @@ +
@@ -37,4 +38,5 @@ {% endfor %} -
\ No newline at end of file + +
\ No newline at end of file