vikunja-frontend/src/components/tasks/partials/attachments.vue

262 lines
5.6 KiB
Vue
Raw Normal View History

2019-11-24 14:16:24 +01:00
<template>
<div class="attachments">
<h3>
<span class="icon is-grey">
<icon icon="paperclip"/>
2019-11-24 14:16:24 +01:00
</span>
{{ $t('task.attachment.title') }}
2019-11-24 14:16:24 +01:00
</h3>
<input
:disabled="attachmentService.loading"
@change="uploadNewAttachment()"
id="files"
multiple
ref="files"
type="file"
v-if="editEnabled"
/>
<progress
:value="attachmentService.uploadProgress"
class="progress is-primary"
max="100"
v-if="attachmentService.uploadProgress > 0"
>
{{ attachmentService.uploadProgress }}%
</progress>
2019-11-24 14:16:24 +01:00
<div class="files" v-if="attachments.length > 0">
<a
class="attachment"
v-for="a in attachments"
:key="a.id"
@click="viewOrDownload(a)"
>
<div class="filename">{{ a.file.name }}</div>
<div class="info">
<p class="collapses">
<i18n path="task.attachment.createdBy">
<span v-tooltip="formatDate(a.created)">
{{ formatDateSince(a.created) }}
</span>
<user
:avatar-size="24"
:user="a.createdBy"
:is-inline="true"
/>
</i18n>
<span>
{{ a.file.getHumanSize() }}
</span>
<span v-if="a.file.mime">
{{ a.file.mime }}
</span>
</p>
<p>
<a
@click.prevent.stop="downloadAttachment(a)"
v-tooltip="$t('task.attachment.downloadTooltip')"
>
{{ $t('misc.download') }}
2019-11-24 14:16:24 +01:00
</a>
<a
@click.stop="copyUrl(a)"
v-tooltip="$t('task.attachment.copyUrlTooltip')"
>
{{ $t('task.attachment.copyUrl') }}
</a>
<a
@click.prevent.stop="() => {attachmentToDelete = a; showDeleteModal = true}"
v-if="editEnabled"
v-tooltip="$t('task.attachment.deleteTooltip')"
>
{{ $t('misc.delete') }}
2019-11-24 14:16:24 +01:00
</a>
</p>
</div>
</a>
</div>
<x-button
v-if="editEnabled"
:disabled="attachmentService.loading"
@click="$refs.files.click()"
class="mb-4"
icon="cloud-upload-alt"
type="secondary"
:shadow="false"
>
{{ $t('task.attachment.upload') }}
</x-button>
2019-11-24 14:16:24 +01:00
<!-- Dropzone -->
<div
:class="{ hidden: !showDropzone }"
class="dropzone"
v-if="editEnabled"
>
2019-11-24 14:16:24 +01:00
<div class="drop-hint">
<div class="icon">
<icon icon="cloud-upload-alt"/>
2019-11-24 14:16:24 +01:00
</div>
<div class="hint">{{ $t('task.attachment.drop') }}</div>
2019-11-24 14:16:24 +01:00
</div>
</div>
<!-- Delete modal -->
<transition name="modal">
<modal
@close="showDeleteModal = false"
v-if="showDeleteModal"
@submit="deleteAttachment()"
>
<template #header><span>{{ $t('task.attachment.delete') }}</span></template>
<template #text>
<p>{{ $t('task.attachment.deleteText1', {filename: attachmentToDelete.file.name}) }}<br/>
<strong>{{ $t('task.attachment.deleteText2') }}</strong></p>
</template>
</modal>
</transition>
<transition name="modal">
<modal
@close="
() => {
showImageModal = false
attachmentImageBlobUrl = null
}
"
v-if="showImageModal"
>
<img :src="attachmentImageBlobUrl" alt=""/>
</modal>
</transition>
2019-11-24 14:16:24 +01:00
</div>
</template>
<script>
import AttachmentService from '../../../services/attachment'
import AttachmentModel from '../../../models/attachment'
import User from '../../misc/user'
import {mapState} from 'vuex'
import copy from 'copy-to-clipboard'
2019-11-24 14:16:24 +01:00
import { uploadFiles, generateAttachmentUrl } from '@/helpers/attachments'
export default {
name: 'attachments',
components: {
User,
},
data() {
return {
attachmentService: AttachmentService,
showDropzone: false,
2019-11-24 14:16:24 +01:00
showDeleteModal: false,
attachmentToDelete: AttachmentModel,
showImageModal: false,
attachmentImageBlobUrl: null,
}
},
props: {
taskId: {
required: true,
type: Number,
2019-11-24 14:16:24 +01:00
},
initialAttachments: {
type: Array,
2019-11-24 14:16:24 +01:00
},
editEnabled: {
default: true,
2019-11-24 14:16:24 +01:00
},
},
created() {
this.attachmentService = new AttachmentService()
},
computed: mapState({
attachments: (state) => state.attachments.attachments,
}),
mounted() {
document.addEventListener('dragenter', (e) => {
e.stopPropagation()
e.preventDefault()
this.showDropzone = true
})
2019-11-24 14:16:24 +01:00
window.addEventListener('dragleave', (e) => {
e.stopPropagation()
e.preventDefault()
this.showDropzone = false
})
2019-11-24 14:16:24 +01:00
document.addEventListener('dragover', (e) => {
e.stopPropagation()
e.preventDefault()
this.showDropzone = true
})
2019-11-24 14:16:24 +01:00
document.addEventListener('drop', (e) => {
e.stopPropagation()
e.preventDefault()
2019-11-24 14:16:24 +01:00
let files = e.dataTransfer.files
this.uploadFiles(files)
this.showDropzone = false
})
},
methods: {
downloadAttachment(attachment) {
this.attachmentService.download(attachment)
2019-11-24 14:16:24 +01:00
},
uploadNewAttachment() {
if (this.$refs.files.files.length === 0) {
return
}
2019-11-24 14:16:24 +01:00
this.uploadFiles(this.$refs.files.files)
},
uploadFiles(files) {
uploadFiles(this.attachmentService, this.taskId, files)
},
deleteAttachment() {
this.attachmentService
.delete(this.attachmentToDelete)
.then((r) => {
this.$store.commit(
'attachments/removeById',
this.attachmentToDelete.id,
)
2021-06-22 22:07:57 +02:00
this.success(r)
})
.catch((e) => {
2021-06-22 22:07:57 +02:00
this.error(e)
})
.finally(() => {
this.showDeleteModal = false
})
2019-11-24 14:16:24 +01:00
},
viewOrDownload(attachment) {
if (
attachment.file.name.endsWith('.jpg') ||
attachment.file.name.endsWith('.png') ||
attachment.file.name.endsWith('.bmp') ||
attachment.file.name.endsWith('.gif')
) {
this.showImageModal = true
this.attachmentService.getBlobUrl(attachment).then((url) => {
this.attachmentImageBlobUrl = url
})
} else {
this.downloadAttachment(attachment)
}
},
copyUrl(attachment) {
copy(generateAttachmentUrl(this.taskId, attachment.id))
},
},
}
2019-11-24 14:16:24 +01:00
</script>