akaunting/resources/assets/js/components/AkauntingDropzoneFileUpload.vue

293 lines
12 KiB
Vue
Raw Normal View History

<template>
2022-11-07 17:36:32 +03:00
<div :id="'dropzone-' + _uid" class="dropzone dz-clickable" :class="[preview == 'single' ? 'dropzone-single': 'dropzone-multiple', singleWidthClasses ? 'w-full': 'sm:w-37']">
<div class="fallback">
<div class="custom-file">
<input type="file" class="custom-file-input" :id="'projectCoverUploads' + _uid" :multiple="multiple">
<label class="custom-file-label" :for="'projectCoverUploads' + _uid">{{ textChooseFile }}</label>
2021-01-22 14:12:24 +03:00
</div>
</div>
2021-01-29 17:25:28 +03:00
<div v-if="preview == 'single'" class="dz-preview dz-preview-single" :class="previewClasses" ref="previewSingle">
<div class="dz-preview-cover">
<img class="dz-preview-img" data-dz-thumbnail>
2022-06-01 10:15:55 +03:00
<span class="material-icons hidden" data-dz-thumbnail-image>crop_original</span>
<span class="material-icons-outlined avatar hidden">file_present</span>
<span class="material-icons-outlined avatar hidden" data-dz-thumbnail-pdf>picture_as_pdf</span>
<span class="material-icons-outlined avatar hidden" data-dz-thumbnail-word>content_paste</span>
2022-06-02 17:16:34 +03:00
<span class="material-icons-outlined avatar hidden" data-dz-thumbnail-excel>table_chart</span>
2022-06-01 10:15:55 +03:00
<span class="mb-1 text-sm ml-3 text-gray-500 hidden" data-dz-name>...</span>
2022-08-05 19:04:38 +03:00
<div class="gap-x-1 relative">
<button data-dz-remove="true" class="absolute group right-0">
2022-11-16 17:49:40 +03:00
<span class="material-icons-outlined text-base text-gray-300 px-1.5 py-1 rounded-lg group-hover:bg-gray-100">delete</span>
2022-08-05 19:04:38 +03:00
</button>
</div>
</div>
</div>
<ul v-else class="dz-preview dz-preview-multiple list-group list-group-lg list-group-flush" :class="previewClasses" ref="previewMultiple">
2022-06-01 10:15:55 +03:00
<li class="list-group-item border-b py-4">
<div class="flex items-center justify-between">
<div class="flex items-center">
<div class="avatar">
2022-06-02 16:29:49 +03:00
<img class="avatar-img h-full rounded object-cover" data-dz-thumbnail>
2022-06-01 10:15:55 +03:00
<span class="material-icons hidden" data-dz-thumbnail-image>crop_original</span>
<span class="material-icons-outlined display-3 hidden" data-dz-thumbnail-pdf>picture_as_pdf</span>
<span class="material-icons-outlined hidden" data-dz-thumbnail-word>content_paste</span>
<span class="material-icons-outlined hidden" data-dz-thumbnail-excel>table_chart</span>
</div>
2022-06-01 10:15:55 +03:00
<div class="col text-gray-500 ml-3">
2022-06-30 11:24:00 +03:00
<h4 class="w-56 lg:w-96 text-sm mb-1 truncate" data-dz-name>...</h4>
2021-01-22 14:12:24 +03:00
2022-06-01 10:15:55 +03:00
<p class="text-xs text-muted mb-0" data-dz-size>...</p>
</div>
</div>
2022-11-16 17:49:40 +03:00
<div class="flex flex-col gap-x-1">
2022-08-05 19:04:38 +03:00
<button data-dz-remove="true" class="group">
2022-11-16 17:49:40 +03:00
<span class="material-icons-outlined text-base text-gray-300 px-1.5 py-1 rounded-lg group-hover:bg-gray-100">delete</span>
</button>
2022-08-05 19:04:38 +03:00
<a href="#" type="button" class="group hidden" data-dz-download>
2022-11-16 17:49:40 +03:00
<span class="material-icons text-base text-gray-300 px-1.5 py-1 rounded-lg group-hover:bg-gray-100">download</span>
2021-01-30 21:20:46 +03:00
</a>
</div>
</div>
<div class="text-red text-sm mt-1 block" data-dz-errormessage></div>
</li>
</ul>
</div>
</template>
<script>
import Dropzone from 'dropzone';
Dropzone.autoDiscover = false;
export default {
name: 'akaunting-dropzone-file-upload',
props: {
2021-01-22 16:01:48 +03:00
textDropFile: {
type: String,
default: 'Drop files here to upload',
description: 'Drop file text'
},
textChooseFile: {
type: String,
default: 'Choose File',
description: 'Choose file text'
},
options: {
2022-06-01 10:15:55 +03:00
type: [Object, Array],
default: () => ({})
},
value: [String, Object, Array, File],
url: {
type: String,
default: 'http:'
},
multiple: {
type: Boolean,
default: false,
description: 'Multiple file Upload'
},
previewClasses: [String, Object, Array],
2022-06-01 10:15:55 +03:00
singleWidthClasses: {
type: [Boolean, String],
default: false
},
2021-01-29 17:25:28 +03:00
preview: {
type: String,
default: function () {
return this.multiple ? 'multiple' : 'single'
},
},
2021-01-30 21:20:46 +03:00
attachments: {
type: Array,
default: () => ([])
},
},
model: {
prop: 'value',
event: 'change'
},
data() {
return {
files: [],
configurations: this.options,
}
},
methods: {
async initDropzone() {
let self = this;
2021-01-29 17:25:28 +03:00
let preview = this.preview == 'single' ? this.$refs.previewSingle : this.$refs.previewMultiple;
2021-01-22 14:12:24 +03:00
if (this.configurations.maxFiles === undefined && this.multiple == false) {
2021-02-08 18:26:30 +03:00
this.configurations.maxFiles = 1;
2021-01-22 14:12:24 +03:00
}
if (this.configurations.acceptedFiles === undefined) {
2021-02-08 18:26:30 +03:00
this.configurations.acceptedFiles = 'image/*';
}
let finalOptions = {
2021-02-08 18:26:30 +03:00
...self.configurations,
url: this.url,
previewsContainer: preview,
previewTemplate: preview.innerHTML,
dictDefaultMessage: this.textDropFile,
autoProcessQueue: false,
init: function () {
let dropzone = this;
dropzone.on('addedfile', function (file) {
self.files.push(file);
if (self.configurations.maxFiles == 1) {
self.$emit('change', file);
} else {
self.$emit('change', self.files);
}
if (file.type.indexOf("image") == -1) {
2021-02-11 17:54:21 +03:00
let ext = file.name.split('.').pop();
2022-06-01 10:15:55 +03:00
file.previewElement.querySelector("[data-dz-thumbnail]").classList.add("hidden");
file.previewElement.querySelector("[data-dz-name]").classList.remove("hidden");
2021-02-11 17:54:21 +03:00
if (ext == "pdf") {
2022-06-01 10:15:55 +03:00
file.previewElement.querySelector("[data-dz-thumbnail-pdf]").classList.remove("hidden");
} else if ((ext.indexOf("doc") != -1) || (ext.indexOf("docx") != -1)) {
2022-06-01 10:15:55 +03:00
file.previewElement.querySelector("[data-dz-thumbnail-word]").classList.remove("hidden");
} else if ((ext.indexOf("xls") != -1) || (ext.indexOf("xlsx") != -1)) {
2022-06-01 10:15:55 +03:00
file.previewElement.querySelector("[data-dz-thumbnail-excel]").classList.remove("hidden");
} else {
2022-06-01 10:15:55 +03:00
file.previewElement.querySelector("[data-dz-thumbnail-image]").classList.remove("hidden");
}
}
2022-06-02 16:29:49 +03:00
self.onMaxFilesReached(self);
2021-02-08 18:26:30 +03:00
}),
2021-01-22 14:12:24 +03:00
2021-02-08 18:26:30 +03:00
dropzone.on('removedfile', function (file) {
let index = self.files.findIndex(f => f.name === file.name)
2021-02-08 18:26:30 +03:00
if (index !== -1) {
self.files.splice(index, 1);
}
2021-01-22 14:12:24 +03:00
2021-02-08 18:26:30 +03:00
self.$emit('change', self.files);
2021-01-22 14:12:24 +03:00
2021-02-08 18:26:30 +03:00
if (self.multiple) {
this.enable();
}
}),
2021-01-30 21:20:46 +03:00
2021-02-08 18:26:30 +03:00
dropzone.on('maxfilesexceeded', function(file) {
this.removeAllFiles('notCancel');
this.addFile(file);
}),
2021-01-30 21:20:46 +03:00
2021-02-08 18:26:30 +03:00
dropzone.on('maxfilesreached', function(file) {
if (self.multiple) {
this.disable();
}
2021-01-30 21:20:46 +03:00
})
2021-02-08 18:26:30 +03:00
if (self.attachments.length) {
setTimeout(() => {
self.attachments.forEach(async (attachment) => {
var mockFile = {
id: attachment.id,
name: attachment.name,
size: attachment.size,
type: attachment.type,
download: attachment.downloadPath,
2021-02-10 12:07:33 +03:00
dropzone: 'edit',
};
dropzone.emit("addedfile", mockFile);
dropzone.options.thumbnail.call(dropzone, mockFile, attachment.path);
// Make sure that there is no progress bar, etc...
dropzone.emit("complete", mockFile);
});
self.files.forEach(async (attachment) => {
if (attachment.download) {
attachment.previewElement.querySelector("[data-dz-download]").href = attachment.download;
2022-06-01 10:15:55 +03:00
attachment.previewElement.querySelector("[data-dz-download]").classList.remove("hidden");
}
2021-02-08 18:26:30 +03:00
});
2022-06-02 16:29:49 +03:00
self.onMaxFilesReached(self);
2021-02-08 18:26:30 +03:00
}, 100);
}
}
};
this.dropzone = new Dropzone(this.$el, finalOptions);
2021-01-22 14:12:24 +03:00
preview.innerHTML = '';
2021-01-30 21:20:46 +03:00
},
2022-06-02 16:29:49 +03:00
onMaxFilesReached(arg) {
if (arg.preview == 'single' || arg.attachments.length == 1) {
arg.$nextTick(() => {
document.querySelector("#dropzone-" + arg._uid).classList.add("dz-max-files-reached");
});
}
}
},
async mounted() {
this.initDropzone();
},
2021-05-25 19:52:39 +03:00
2022-06-02 17:16:34 +03:00
watch: {
attachments: function (attachments) {
attachments.forEach((attachment) => {
if (attachment.length != undefined) {
var mockFile = {
id: attachment[0].id,
name: attachment[0].name,
size: attachment[0].size,
type: attachment[0].type,
download: attachment[0].downloadPath,
dropzone: 'edit',
};
this.dropzone.emit("addedfile", mockFile);
this.dropzone.options.thumbnail.call(this.dropzone, mockFile, attachment[0].path);
// Make sure that there is no progress bar, etc...
this.dropzone.emit("complete", mockFile);
this.files.forEach(async (attachment) => {
if (attachment.download) {
attachment.previewElement.querySelector("[data-dz-download]").href = attachment.download;
attachment.previewElement.querySelector("[data-dz-download]").classList.remove("hidden");
}
});
2021-05-25 19:52:39 +03:00
2022-06-02 17:16:34 +03:00
this.onMaxFilesReached(this);
}
2022-06-02 17:16:34 +03:00
}, this);
},
2021-05-25 19:52:39 +03:00
},
}
</script>
<style>
2022-06-01 10:15:55 +03:00
.avatar.hidden {
display: none;
}
</style>