mirror of
https://0xacab.org/jvoisin/mat2-web.git
synced 2025-05-14 12:12:33 -04:00
Implement drag'n'drop support
This commit is contained in:
parent
f436da0539
commit
32f2411400
3 changed files with 57 additions and 0 deletions
52
static/script.js
Normal file
52
static/script.js
Normal file
|
@ -0,0 +1,52 @@
|
|||
"use strict";
|
||||
|
||||
(function () {
|
||||
const dropZone = document.body;
|
||||
if (!dropZone) {
|
||||
return;
|
||||
}
|
||||
|
||||
const hoverClassName = "hover";
|
||||
|
||||
// Handle drag* events to handle style
|
||||
// Add the css you want when the class "hover" is present
|
||||
dropZone.addEventListener("dragenter", function (e) {
|
||||
e.preventDefault();
|
||||
dropZone.classList.add(hoverClassName);
|
||||
});
|
||||
|
||||
dropZone.addEventListener("dragover", function (e) {
|
||||
e.preventDefault();
|
||||
dropZone.classList.add(hoverClassName);
|
||||
});
|
||||
|
||||
dropZone.addEventListener("dragleave", function (e) {
|
||||
e.preventDefault();
|
||||
dropZone.classList.remove(hoverClassName);
|
||||
});
|
||||
|
||||
// This is the most important event, the event that gives access to files
|
||||
dropZone.addEventListener("drop", function (e) {
|
||||
e.preventDefault();
|
||||
dropZone.classList.remove(hoverClassName);
|
||||
|
||||
const files = Array.from(e.dataTransfer.files);
|
||||
if (files.length > 0) {
|
||||
const data = new FormData();
|
||||
for (const file of files) {
|
||||
data.append('file', file);
|
||||
}
|
||||
|
||||
fetch('/', {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
})
|
||||
.then(response => response.text())
|
||||
.then(body => { // Yes, this is ugly
|
||||
document.open()
|
||||
document.write(body)
|
||||
document.close()
|
||||
})
|
||||
}
|
||||
});
|
||||
})();
|
|
@ -57,3 +57,7 @@ details[open] > summary:before {
|
|||
.fakelink:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.hover {
|
||||
background-color: #f1f1f1;
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
<link rel="stylesheet" href="{{ url_for('static', filename='skeleton.css') }}" type="text/css" />
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" type="text/css" />
|
||||
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.png') }}">
|
||||
<script defer src="{{url_for('static', filename='script.js') }}"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue