diff options
| author | jvoisin | 2019-02-23 13:07:41 +0100 |
|---|---|---|
| committer | jvoisin | 2019-02-23 13:07:41 +0100 |
| commit | 32f241140011803cc6933e0e5e5e5850b77a019d (patch) | |
| tree | fa9ef134503bb6e63b1e2b9ff19bc254c914cc56 /static | |
| parent | f436da05399199af59ff074e3450c584f35aa7e0 (diff) | |
Implement drag'n'drop support
Diffstat (limited to 'static')
| -rw-r--r-- | static/script.js | 52 | ||||
| -rw-r--r-- | static/style.css | 4 |
2 files changed, 56 insertions, 0 deletions
diff --git a/static/script.js b/static/script.js new file mode 100644 index 0000000..1f88d95 --- /dev/null +++ b/static/script.js | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | "use strict"; | ||
| 2 | |||
| 3 | (function () { | ||
| 4 | const dropZone = document.body; | ||
| 5 | if (!dropZone) { | ||
| 6 | return; | ||
| 7 | } | ||
| 8 | |||
| 9 | const hoverClassName = "hover"; | ||
| 10 | |||
| 11 | // Handle drag* events to handle style | ||
| 12 | // Add the css you want when the class "hover" is present | ||
| 13 | dropZone.addEventListener("dragenter", function (e) { | ||
| 14 | e.preventDefault(); | ||
| 15 | dropZone.classList.add(hoverClassName); | ||
| 16 | }); | ||
| 17 | |||
| 18 | dropZone.addEventListener("dragover", function (e) { | ||
| 19 | e.preventDefault(); | ||
| 20 | dropZone.classList.add(hoverClassName); | ||
| 21 | }); | ||
| 22 | |||
| 23 | dropZone.addEventListener("dragleave", function (e) { | ||
| 24 | e.preventDefault(); | ||
| 25 | dropZone.classList.remove(hoverClassName); | ||
| 26 | }); | ||
| 27 | |||
| 28 | // This is the most important event, the event that gives access to files | ||
| 29 | dropZone.addEventListener("drop", function (e) { | ||
| 30 | e.preventDefault(); | ||
| 31 | dropZone.classList.remove(hoverClassName); | ||
| 32 | |||
| 33 | const files = Array.from(e.dataTransfer.files); | ||
| 34 | if (files.length > 0) { | ||
| 35 | const data = new FormData(); | ||
| 36 | for (const file of files) { | ||
| 37 | data.append('file', file); | ||
| 38 | } | ||
| 39 | |||
| 40 | fetch('/', { | ||
| 41 | method: 'POST', | ||
| 42 | body: data, | ||
| 43 | }) | ||
| 44 | .then(response => response.text()) | ||
| 45 | .then(body => { // Yes, this is ugly | ||
| 46 | document.open() | ||
| 47 | document.write(body) | ||
| 48 | document.close() | ||
| 49 | }) | ||
| 50 | } | ||
| 51 | }); | ||
| 52 | })(); | ||
diff --git a/static/style.css b/static/style.css index 0d78cbd..3f1a5da 100644 --- a/static/style.css +++ b/static/style.css | |||
| @@ -57,3 +57,7 @@ details[open] > summary:before { | |||
| 57 | .fakelink:hover { | 57 | .fakelink:hover { |
| 58 | text-decoration: underline; | 58 | text-decoration: underline; |
| 59 | } | 59 | } |
| 60 | |||
| 61 | .hover { | ||
| 62 | background-color: #f1f1f1; | ||
| 63 | } | ||
