2020-06-30 23:12:45 +02:00
|
|
|
import DropZoneLib from "dropzone";
|
|
|
|
import {fadeOut} from "../services/animations";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dropzone
|
|
|
|
* @extends {Component}
|
|
|
|
*/
|
|
|
|
class Dropzone {
|
|
|
|
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;
|
2021-11-14 23:03:22 +01:00
|
|
|
this.uploadLimit = Number(this.$opts.uploadLimit);
|
2020-07-04 17:53:02 +02:00
|
|
|
this.uploadLimitMessage = this.$opts.uploadLimitMessage;
|
|
|
|
this.timeoutMessage = this.$opts.timeoutMessage;
|
2020-06-30 23:12:45 +02:00
|
|
|
|
|
|
|
const _this = this;
|
|
|
|
this.dz = new DropZoneLib(this.container, {
|
|
|
|
addRemoveLinks: true,
|
2020-07-04 17:53:02 +02:00
|
|
|
dictRemoveFile: this.removeMessage,
|
2020-06-30 23:12:45 +02:00
|
|
|
timeout: Number(window.uploadTimeout) || 60000,
|
2021-11-14 23:03:22 +01:00
|
|
|
maxFilesize: this.uploadLimit,
|
2020-06-30 23:12:45 +02:00
|
|
|
url: this.url,
|
|
|
|
withCredentials: true,
|
|
|
|
init() {
|
|
|
|
this.dz = this;
|
|
|
|
this.dz.on('sending', _this.onSending.bind(_this));
|
|
|
|
this.dz.on('success', _this.onSuccess.bind(_this));
|
|
|
|
this.dz.on('error', _this.onError.bind(_this));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onSending(file, xhr, data) {
|
|
|
|
|
|
|
|
const token = window.document.querySelector('meta[name=token]').getAttribute('content');
|
|
|
|
data.append('_token', token);
|
|
|
|
|
2020-07-04 17:53:02 +02:00
|
|
|
xhr.ontimeout = (e) => {
|
2020-06-30 23:12:45 +02:00
|
|
|
this.dz.emit('complete', file);
|
2020-07-04 17:53:02 +02:00
|
|
|
this.dz.emit('error', file, this.timeoutMessage);
|
2020-06-30 23:12:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onSuccess(file, data) {
|
|
|
|
this.$emit('success', {file, data});
|
2020-07-04 17:53:02 +02:00
|
|
|
|
|
|
|
if (this.successMessage) {
|
|
|
|
window.$events.emit('success', this.successMessage);
|
|
|
|
}
|
|
|
|
|
2020-06-30 23:12:45 +02:00
|
|
|
fadeOut(file.previewElement, 800, () => {
|
|
|
|
this.dz.removeFile(file);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onError(file, errorMessage, xhr) {
|
|
|
|
this.$emit('error', {file, errorMessage, xhr});
|
|
|
|
|
|
|
|
const setMessage = (message) => {
|
|
|
|
const messsageEl = file.previewElement.querySelector('[data-dz-errormessage]');
|
|
|
|
messsageEl.textContent = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xhr && xhr.status === 413) {
|
2020-07-04 17:53:02 +02:00
|
|
|
setMessage(this.uploadLimitMessage);
|
2020-06-30 23:12:45 +02:00
|
|
|
} else if (errorMessage.file) {
|
|
|
|
setMessage(errorMessage.file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeAll() {
|
|
|
|
this.dz.removeAllFiles(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Dropzone;
|