2023-04-18 23:20:02 +02:00
|
|
|
import {Component} from './component';
|
2023-04-24 17:19:20 +02:00
|
|
|
import {Clipboard} from '../services/clipboard';
|
2020-06-30 23:12:45 +02:00
|
|
|
|
2022-11-16 14:04:22 +01:00
|
|
|
export class Dropzone extends Component {
|
2023-04-18 23:20:02 +02:00
|
|
|
|
2020-06-30 23:12:45 +02:00
|
|
|
setup() {
|
|
|
|
this.container = this.$el;
|
|
|
|
this.url = this.$opts.url;
|
2020-07-04 17:53:02 +02:00
|
|
|
this.successMessage = this.$opts.successMessage;
|
|
|
|
this.removeMessage = this.$opts.removeMessage;
|
2023-04-24 17:19:20 +02:00
|
|
|
this.uploadLimit = Number(this.$opts.uploadLimit); // TODO - Use
|
|
|
|
this.uploadLimitMessage = this.$opts.uploadLimitMessage; // TODO - Use
|
|
|
|
this.timeoutMessage = this.$opts.timeoutMessage; // TODO - Use
|
|
|
|
// window.uploadTimeout // TODO - Use
|
|
|
|
// TODO - Click-to-upload buttons/areas
|
|
|
|
// TODO - Drop zone highlighting of existing element
|
|
|
|
// (Add overlay via additional temp element).
|
|
|
|
|
|
|
|
this.setupListeners();
|
2020-06-30 23:12:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-24 17:19:20 +02:00
|
|
|
setupListeners() {
|
|
|
|
this.container.addEventListener('dragenter', event => {
|
|
|
|
this.container.style.border = '1px dotted tomato';
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.container.addEventListener('dragover', event => {
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
2020-06-30 23:12:45 +02:00
|
|
|
|
2023-04-24 17:19:20 +02:00
|
|
|
const reset = () => {
|
|
|
|
this.container.style.border = null;
|
2023-04-18 23:20:02 +02:00
|
|
|
};
|
2020-06-30 23:12:45 +02:00
|
|
|
|
2023-04-24 17:19:20 +02:00
|
|
|
this.container.addEventListener('dragend', event => {
|
|
|
|
reset();
|
|
|
|
});
|
2020-07-04 17:53:02 +02:00
|
|
|
|
2023-04-24 17:19:20 +02:00
|
|
|
this.container.addEventListener('dragleave', event => {
|
|
|
|
reset();
|
|
|
|
});
|
2020-07-04 17:53:02 +02:00
|
|
|
|
2023-04-24 17:19:20 +02:00
|
|
|
this.container.addEventListener('drop', event => {
|
|
|
|
event.preventDefault();
|
|
|
|
const clipboard = new Clipboard(event.dataTransfer);
|
|
|
|
const files = clipboard.getFiles();
|
|
|
|
for (const file of files) {
|
|
|
|
this.createUploadFromFile(file);
|
|
|
|
}
|
2020-06-30 23:12:45 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-24 17:19:20 +02:00
|
|
|
/**
|
|
|
|
* @param {File} file
|
|
|
|
* @return {Upload}
|
|
|
|
*/
|
|
|
|
createUploadFromFile(file) {
|
|
|
|
const {dom, status} = this.createDomForFile(file);
|
|
|
|
this.container.append(dom);
|
|
|
|
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('file', file, file.name);
|
|
|
|
|
|
|
|
// TODO - Change to XMLHTTPRequest so we can track progress.
|
|
|
|
const uploadPromise = window.$http.post(this.url, formData);
|
2020-06-30 23:12:45 +02:00
|
|
|
|
2023-04-24 17:19:20 +02:00
|
|
|
const upload = {
|
|
|
|
file,
|
|
|
|
dom,
|
|
|
|
markError(message) {
|
|
|
|
status.setAttribute('data-status', 'error');
|
|
|
|
status.textContent = message;
|
|
|
|
},
|
|
|
|
markSuccess(message) {
|
|
|
|
status.setAttribute('data-status', 'success');
|
|
|
|
status.textContent = message;
|
|
|
|
},
|
2023-04-18 23:20:02 +02:00
|
|
|
};
|
2020-06-30 23:12:45 +02:00
|
|
|
|
2023-04-24 17:19:20 +02:00
|
|
|
uploadPromise.then(returnData => {
|
|
|
|
upload.markSuccess(returnData.statusText);
|
|
|
|
}).catch(err => {
|
|
|
|
upload.markError(err?.data?.message || err.message);
|
|
|
|
});
|
|
|
|
|
|
|
|
return upload;
|
2020-06-30 23:12:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-24 17:19:20 +02:00
|
|
|
/**
|
|
|
|
* @param {File} file
|
|
|
|
* @return {{image: Element, dom: Element, progress: Element, label: Element, status: Element}}
|
|
|
|
*/
|
|
|
|
createDomForFile(file) {
|
|
|
|
const dom = document.createElement('div');
|
|
|
|
const label = document.createElement('div');
|
|
|
|
const status = document.createElement('div');
|
|
|
|
const progress = document.createElement('div');
|
|
|
|
const image = document.createElement('img');
|
|
|
|
|
|
|
|
dom.classList.add('dropzone-file-item');
|
|
|
|
status.classList.add('dropzone-file-item-status');
|
|
|
|
progress.classList.add('dropzone-file-item-progress');
|
|
|
|
|
|
|
|
image.src = ''; // TODO - file icon
|
|
|
|
label.innerText = file.name;
|
|
|
|
|
|
|
|
if (file.type.startsWith('image/')) {
|
|
|
|
image.src = URL.createObjectURL(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
dom.append(image, label, progress, status);
|
|
|
|
return {
|
|
|
|
dom, label, image, progress, status,
|
|
|
|
};
|
2020-06-30 23:12:45 +02:00
|
|
|
}
|
2023-04-18 23:20:02 +02:00
|
|
|
|
|
|
|
}
|
2023-04-24 17:19:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef Upload
|
|
|
|
* @property {File} file
|
|
|
|
* @property {Element} dom
|
|
|
|
* @property {function(String)} markError
|
|
|
|
* @property {function(String)} markSuccess
|
|
|
|
*/
|