opnform-host-nginx/client/components/open/tables/components/OpenFile.vue

83 lines
2.0 KiB
Vue
Raw Normal View History

2023-12-09 15:47:03 +01:00
<template>
<p class="text-xs">
2024-01-13 19:57:39 +01:00
<span v-for="file in parsedFiles" :key="file.file_url"
2023-12-09 15:47:03 +01:00
class="whitespace-nowrap rounded-md transition-colors hover:decoration-none"
2024-01-13 19:57:39 +01:00
:class="{'open-file text-gray-700 dark:text-gray-300 truncate':!file.is_image, 'open-file-img':file.is_image}"
2023-12-09 15:47:03 +01:00
>
<a class="text-gray-700 dark:text-gray-300" :href="file.file_url" target="_blank"
rel="nofollow"
>
2024-01-13 19:57:39 +01:00
<div v-if="file.is_image" class="w-8 h-8">
<img class="object-cover h-full w-full rounded" :src="file.file_url" @error="failedImages.push(file.file_url)"/>
2023-12-09 15:47:03 +01:00
</div>
<span v-else
class="py-1 px-2"
>
2024-01-13 19:57:39 +01:00
<a :href="file.file_url" target="_blank" download>{{ file.displayed_file_name }}</a>
</span>
2023-12-09 15:47:03 +01:00
</a>
</span>
</p>
</template>
<script>
export default {
components: {},
props: {
value: {
type: Array,
required: false
}
},
data() {
2024-01-13 19:57:39 +01:00
return {
failedImages: []
}
2023-12-09 15:47:03 +01:00
},
2024-01-13 19:57:39 +01:00
computed: {
parsedFiles() {
2024-01-18 11:37:04 +01:00
return this.value ? this.value.map((file) => {
2024-01-13 19:57:39 +01:00
return {
file_name: file.file_name,
file_url: file.file_url,
displayed_file_name: this.displayedFileName(file.file_name),
is_image: !this.failedImages.includes(file.file_url) && this.isImage(file.file_name)
}
2024-01-18 11:37:04 +01:00
}): []
2024-01-13 19:57:39 +01:00
}
2023-12-09 15:47:03 +01:00
},
methods: {
2024-01-13 19:57:39 +01:00
isImage(fileName) {
2023-12-09 15:47:03 +01:00
return ['png', 'gif', 'jpg', 'jpeg', 'tif'].some((suffix) => {
2024-01-13 19:57:39 +01:00
return fileName && fileName.endsWith(suffix)
2023-12-09 15:47:03 +01:00
})
},
displayedFileName(fileName) {
const extension = fileName.substr(fileName.lastIndexOf(".") + 1)
const filename = fileName.substr(0, fileName.lastIndexOf("."))
2024-01-13 19:57:39 +01:00
if (filename.length > 10) {
return filename.substr(0, 10) + '[...].' + extension
2023-12-09 15:47:03 +01:00
}
return filename + '.' + extension
}
}
}
</script>
<style lang="scss">
.open-file {
max-width: 120px;
background-color: #e3e2e0;
}
.dark {
.open-file {
background-color: #5a5a5a;
}
}
</style>