2023-04-18 17:20:02 -04:00
|
|
|
import {Component} from './component';
|
2023-04-24 11:19:20 -04:00
|
|
|
import {Clipboard} from '../services/clipboard';
|
2023-04-24 18:24:58 -04:00
|
|
|
import {
|
2023-04-25 08:10:25 -04:00
|
|
|
elem, getLoading, onSelect, removeLoading,
|
2023-04-24 18:24:58 -04:00
|
|
|
} from '../services/dom';
|
2020-06-30 17:12:45 -04:00
|
|
|
|
2022-11-16 08:04:22 -05:00
|
|
|
export class Dropzone extends Component {
|
2023-04-18 17:20:02 -04:00
|
|
|
|
2020-06-30 17:12:45 -04:00
|
|
|
setup() {
|
|
|
|
this.container = this.$el;
|
2023-04-24 18:24:58 -04:00
|
|
|
this.statusArea = this.$refs.statusArea;
|
2023-04-25 08:10:25 -04:00
|
|
|
this.dropTarget = this.$refs.dropTarget;
|
|
|
|
this.selectButtons = this.$manyRefs.selectButton || [];
|
2023-04-24 18:24:58 -04:00
|
|
|
|
2023-04-26 09:23:28 -04:00
|
|
|
this.isActive = true;
|
|
|
|
|
2020-06-30 17:12:45 -04:00
|
|
|
this.url = this.$opts.url;
|
2023-05-28 12:32:22 -04:00
|
|
|
this.method = (this.$opts.method || 'post').toUpperCase();
|
2020-07-04 11:53:02 -04:00
|
|
|
this.successMessage = this.$opts.successMessage;
|
2023-04-25 11:41:39 -04:00
|
|
|
this.errorMessage = this.$opts.errorMessage;
|
|
|
|
this.uploadLimitMb = Number(this.$opts.uploadLimit);
|
|
|
|
this.uploadLimitMessage = this.$opts.uploadLimitMessage;
|
2023-04-24 18:24:58 -04:00
|
|
|
this.zoneText = this.$opts.zoneText;
|
2023-04-25 11:41:39 -04:00
|
|
|
this.fileAcceptTypes = this.$opts.fileAccept;
|
2023-05-22 09:19:18 -04:00
|
|
|
this.allowMultiple = this.$opts.allowMultiple === 'true';
|
2023-04-24 11:19:20 -04:00
|
|
|
|
|
|
|
this.setupListeners();
|
2020-06-30 17:12:45 -04:00
|
|
|
}
|
|
|
|
|
2023-04-26 09:23:28 -04:00
|
|
|
/**
|
|
|
|
* Public method to allow external disabling/enabling of this drag+drop dropzone.
|
|
|
|
* @param {Boolean} active
|
|
|
|
*/
|
|
|
|
toggleActive(active) {
|
|
|
|
this.isActive = active;
|
|
|
|
}
|
|
|
|
|
2023-04-24 11:19:20 -04:00
|
|
|
setupListeners() {
|
2023-04-25 08:10:25 -04:00
|
|
|
onSelect(this.selectButtons, this.manualSelectHandler.bind(this));
|
2023-04-25 11:41:39 -04:00
|
|
|
this.setupDropTargetHandlers();
|
|
|
|
}
|
2023-04-25 08:10:25 -04:00
|
|
|
|
2023-04-25 11:41:39 -04:00
|
|
|
setupDropTargetHandlers() {
|
2023-04-24 18:24:58 -04:00
|
|
|
let depth = 0;
|
|
|
|
|
2023-04-25 11:41:39 -04:00
|
|
|
const reset = () => {
|
|
|
|
this.hideOverlay();
|
|
|
|
depth = 0;
|
|
|
|
};
|
|
|
|
|
2023-04-25 08:10:25 -04:00
|
|
|
this.dropTarget.addEventListener('dragenter', event => {
|
2023-04-24 11:19:20 -04:00
|
|
|
event.preventDefault();
|
2023-04-24 18:24:58 -04:00
|
|
|
depth += 1;
|
|
|
|
|
2023-04-26 09:23:28 -04:00
|
|
|
if (depth === 1 && this.isActive) {
|
2023-04-24 18:24:58 -04:00
|
|
|
this.showOverlay();
|
|
|
|
}
|
2023-04-24 11:19:20 -04:00
|
|
|
});
|
|
|
|
|
2023-04-25 08:10:25 -04:00
|
|
|
this.dropTarget.addEventListener('dragover', event => {
|
2023-04-24 11:19:20 -04:00
|
|
|
event.preventDefault();
|
|
|
|
});
|
2020-06-30 17:12:45 -04:00
|
|
|
|
2023-04-25 11:41:39 -04:00
|
|
|
this.dropTarget.addEventListener('dragend', reset);
|
|
|
|
this.dropTarget.addEventListener('dragleave', () => {
|
2023-04-24 18:24:58 -04:00
|
|
|
depth -= 1;
|
|
|
|
if (depth === 0) {
|
|
|
|
reset();
|
|
|
|
}
|
2023-04-24 11:19:20 -04:00
|
|
|
});
|
2023-04-25 08:10:25 -04:00
|
|
|
this.dropTarget.addEventListener('drop', event => {
|
2023-04-24 11:19:20 -04:00
|
|
|
event.preventDefault();
|
2023-04-24 18:24:58 -04:00
|
|
|
reset();
|
2023-04-26 09:23:28 -04:00
|
|
|
|
|
|
|
if (!this.isActive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-24 11:19:20 -04:00
|
|
|
const clipboard = new Clipboard(event.dataTransfer);
|
|
|
|
const files = clipboard.getFiles();
|
|
|
|
for (const file of files) {
|
|
|
|
this.createUploadFromFile(file);
|
|
|
|
}
|
2020-06-30 17:12:45 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-25 08:10:25 -04:00
|
|
|
manualSelectHandler() {
|
2023-05-22 09:19:18 -04:00
|
|
|
const input = elem('input', {
|
|
|
|
type: 'file',
|
|
|
|
style: 'left: -400px; visibility: hidden; position: fixed;',
|
|
|
|
accept: this.fileAcceptTypes,
|
|
|
|
multiple: this.allowMultiple ? '' : null,
|
|
|
|
});
|
2023-04-25 08:10:25 -04:00
|
|
|
this.container.append(input);
|
|
|
|
input.click();
|
2023-04-25 11:41:39 -04:00
|
|
|
input.addEventListener('change', () => {
|
2023-04-25 08:10:25 -04:00
|
|
|
for (const file of input.files) {
|
|
|
|
this.createUploadFromFile(file);
|
|
|
|
}
|
|
|
|
input.remove();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-24 18:24:58 -04:00
|
|
|
showOverlay() {
|
2023-04-25 08:10:25 -04:00
|
|
|
const overlay = this.dropTarget.querySelector('.dropzone-overlay');
|
2023-04-24 18:24:58 -04:00
|
|
|
if (!overlay) {
|
|
|
|
const zoneElem = elem('div', {class: 'dropzone-overlay'}, [this.zoneText]);
|
2023-04-25 08:10:25 -04:00
|
|
|
this.dropTarget.append(zoneElem);
|
2023-04-24 18:24:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hideOverlay() {
|
2023-04-25 08:10:25 -04:00
|
|
|
const overlay = this.dropTarget.querySelector('.dropzone-overlay');
|
2023-04-24 18:24:58 -04:00
|
|
|
if (overlay) {
|
|
|
|
overlay.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 11:19:20 -04:00
|
|
|
/**
|
|
|
|
* @param {File} file
|
|
|
|
* @return {Upload}
|
|
|
|
*/
|
|
|
|
createUploadFromFile(file) {
|
2023-04-25 11:41:39 -04:00
|
|
|
const {
|
|
|
|
dom, status, progress, dismiss,
|
|
|
|
} = this.createDomForFile(file);
|
2023-04-25 08:10:25 -04:00
|
|
|
this.statusArea.append(dom);
|
|
|
|
const component = this;
|
2023-04-24 11:19:20 -04:00
|
|
|
|
|
|
|
const upload = {
|
|
|
|
file,
|
|
|
|
dom,
|
2023-04-24 13:18:08 -04:00
|
|
|
updateProgress(percentComplete) {
|
|
|
|
progress.textContent = `${percentComplete}%`;
|
|
|
|
progress.style.width = `${percentComplete}%`;
|
|
|
|
},
|
2023-04-24 11:19:20 -04:00
|
|
|
markError(message) {
|
|
|
|
status.setAttribute('data-status', 'error');
|
|
|
|
status.textContent = message;
|
2023-04-24 18:24:58 -04:00
|
|
|
removeLoading(dom);
|
2023-04-25 11:41:39 -04:00
|
|
|
this.updateProgress(100);
|
2023-04-24 11:19:20 -04:00
|
|
|
},
|
|
|
|
markSuccess(message) {
|
|
|
|
status.setAttribute('data-status', 'success');
|
|
|
|
status.textContent = message;
|
2023-04-24 18:24:58 -04:00
|
|
|
removeLoading(dom);
|
2023-04-25 08:10:25 -04:00
|
|
|
setTimeout(dismiss, 2400);
|
|
|
|
component.$emit('upload-success', {
|
|
|
|
name: file.name,
|
|
|
|
});
|
2023-04-24 11:19:20 -04:00
|
|
|
},
|
2023-04-18 17:20:02 -04:00
|
|
|
};
|
2020-06-30 17:12:45 -04:00
|
|
|
|
2023-04-25 11:41:39 -04:00
|
|
|
// Enforce early upload filesize limit
|
|
|
|
if (file.size > (this.uploadLimitMb * 1000000)) {
|
|
|
|
upload.markError(this.uploadLimitMessage);
|
|
|
|
return upload;
|
|
|
|
}
|
|
|
|
|
2023-04-24 13:18:08 -04:00
|
|
|
this.startXhrForUpload(upload);
|
2023-04-24 11:19:20 -04:00
|
|
|
|
|
|
|
return upload;
|
2020-06-30 17:12:45 -04:00
|
|
|
}
|
|
|
|
|
2023-04-24 13:18:08 -04:00
|
|
|
/**
|
|
|
|
* @param {Upload} upload
|
|
|
|
*/
|
|
|
|
startXhrForUpload(upload) {
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('file', upload.file, upload.file.name);
|
2023-05-28 12:32:22 -04:00
|
|
|
if (this.method !== 'POST') {
|
|
|
|
formData.append('_method', this.method);
|
|
|
|
}
|
2023-04-25 11:41:39 -04:00
|
|
|
const component = this;
|
2023-04-24 13:18:08 -04:00
|
|
|
|
|
|
|
const req = window.$http.createXMLHttpRequest('POST', this.url, {
|
|
|
|
error() {
|
2023-04-25 11:41:39 -04:00
|
|
|
upload.markError(component.errorMessage);
|
2023-04-24 13:18:08 -04:00
|
|
|
},
|
|
|
|
readystatechange() {
|
|
|
|
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
|
2023-04-25 11:41:39 -04:00
|
|
|
upload.markSuccess(component.successMessage);
|
2023-04-24 13:18:08 -04:00
|
|
|
} else if (this.readyState === XMLHttpRequest.DONE && this.status >= 400) {
|
2024-05-18 16:18:15 -04:00
|
|
|
upload.markError(window.$http.formatErrorResponseText(this.responseText));
|
2023-04-24 13:18:08 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
req.upload.addEventListener('progress', evt => {
|
|
|
|
const percent = Math.min(Math.ceil((evt.loaded / evt.total) * 100), 100);
|
|
|
|
upload.updateProgress(percent);
|
|
|
|
});
|
|
|
|
|
|
|
|
req.setRequestHeader('Accept', 'application/json');
|
|
|
|
req.send(formData);
|
|
|
|
}
|
|
|
|
|
2023-04-24 11:19:20 -04:00
|
|
|
/**
|
|
|
|
* @param {File} file
|
2023-04-25 08:10:25 -04:00
|
|
|
* @return {{image: Element, dom: Element, progress: Element, status: Element, dismiss: function}}
|
2023-04-24 11:19:20 -04:00
|
|
|
*/
|
|
|
|
createDomForFile(file) {
|
2023-04-25 11:41:39 -04:00
|
|
|
const image = elem('img', {src: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M9.224 7.373a.924.924 0 0 0-.92.925l-.006 7.404c0 .509.412.925.921.925h5.557a.928.928 0 0 0 .926-.925v-5.553l-2.777-2.776Zm3.239 3.239V8.067l2.545 2.545z' style='fill:%23000;fill-opacity:.75'/%3E%3C/svg%3E"});
|
2023-04-24 18:24:58 -04:00
|
|
|
const status = elem('div', {class: 'dropzone-file-item-status'}, []);
|
|
|
|
const progress = elem('div', {class: 'dropzone-file-item-progress'});
|
|
|
|
const imageWrap = elem('div', {class: 'dropzone-file-item-image-wrap'}, [image]);
|
|
|
|
|
|
|
|
const dom = elem('div', {class: 'dropzone-file-item'}, [
|
|
|
|
imageWrap,
|
|
|
|
elem('div', {class: 'dropzone-file-item-text-wrap'}, [
|
|
|
|
elem('div', {class: 'dropzone-file-item-label'}, [file.name]),
|
|
|
|
getLoading(),
|
|
|
|
status,
|
|
|
|
]),
|
|
|
|
progress,
|
|
|
|
]);
|
2023-04-24 11:19:20 -04:00
|
|
|
|
|
|
|
if (file.type.startsWith('image/')) {
|
|
|
|
image.src = URL.createObjectURL(file);
|
|
|
|
}
|
|
|
|
|
2023-04-25 08:10:25 -04:00
|
|
|
const dismiss = () => {
|
|
|
|
dom.classList.add('dismiss');
|
2023-04-25 11:41:39 -04:00
|
|
|
dom.addEventListener('animationend', () => {
|
2023-04-25 08:10:25 -04:00
|
|
|
dom.remove();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
dom.addEventListener('click', dismiss);
|
|
|
|
|
2023-04-24 11:19:20 -04:00
|
|
|
return {
|
2023-04-25 08:10:25 -04:00
|
|
|
dom, progress, status, dismiss,
|
2023-04-24 11:19:20 -04:00
|
|
|
};
|
2020-06-30 17:12:45 -04:00
|
|
|
}
|
2023-04-18 17:20:02 -04:00
|
|
|
|
|
|
|
}
|
2023-04-24 11:19:20 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef Upload
|
|
|
|
* @property {File} file
|
|
|
|
* @property {Element} dom
|
2023-04-24 13:18:08 -04:00
|
|
|
* @property {function(Number)} updateProgress
|
2023-04-24 11:19:20 -04:00
|
|
|
* @property {function(String)} markError
|
|
|
|
* @property {function(String)} markSuccess
|
|
|
|
*/
|