summaryrefslogtreecommitdiff
path: root/static/script.js
diff options
context:
space:
mode:
Diffstat (limited to 'static/script.js')
-rw-r--r--static/script.js52
1 files changed, 52 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})();