2021-12-04 19:56:29 -06:00
|
|
|
<template>
|
|
|
|
<div class="relative rounded-sm overflow-hidden" :style="{ height: height + 'px', width: width + 'px', maxWidth: width + 'px', minWidth: width + 'px' }">
|
2023-02-09 18:13:30 -06:00
|
|
|
<div class="w-full h-full relative" :class="{ 'bg-bg': !noBg }">
|
2021-12-04 19:56:29 -06:00
|
|
|
<div v-show="showCoverBg" class="absolute top-0 left-0 w-full h-full overflow-hidden rounded-sm bg-primary">
|
|
|
|
<div class="absolute cover-bg" ref="coverBg" />
|
|
|
|
</div>
|
|
|
|
|
2023-02-09 18:13:30 -06:00
|
|
|
<img v-if="fullCoverUrl" ref="cover" :src="fullCoverUrl" loading="lazy" @error="imageError" @load="imageLoaded" class="w-full h-full absolute top-0 left-0 z-10 duration-300 transition-opacity" :style="{ opacity: imageReady ? 1 : 0 }" :class="(showCoverBg && hasCover) || noBg ? 'object-contain' : 'object-fill'" />
|
2022-04-17 17:54:13 -05:00
|
|
|
|
2022-03-23 17:59:14 -05:00
|
|
|
<div v-show="loading && libraryItem" class="absolute top-0 left-0 h-full w-full flex items-center justify-center">
|
2023-02-12 12:16:38 -06:00
|
|
|
<p class="text-center" :style="{ fontSize: 0.75 * sizeMultiplier + 'rem' }">{{ title }}</p>
|
2021-12-04 19:56:29 -06:00
|
|
|
<div class="absolute top-2 right-2">
|
2022-03-23 17:59:14 -05:00
|
|
|
<widgets-loading-spinner />
|
2021-12-04 19:56:29 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div v-if="imageFailed" class="absolute top-0 left-0 right-0 bottom-0 w-full h-full bg-red-100" :style="{ padding: placeholderCoverPadding + 'rem' }">
|
|
|
|
<div class="w-full h-full border-2 border-error flex flex-col items-center justify-center">
|
|
|
|
<img src="/Logo.png" loading="lazy" class="mb-2" :style="{ height: 64 * sizeMultiplier + 'px' }" />
|
2023-02-12 12:16:38 -06:00
|
|
|
<p class="text-centertext-error" :style="{ fontSize: titleFontSize + 'rem' }">Invalid Cover</p>
|
2021-12-04 19:56:29 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div v-if="!hasCover" class="absolute top-0 left-0 right-0 bottom-0 w-full h-full flex items-center justify-center z-10" :style="{ padding: placeholderCoverPadding + 'rem' }">
|
|
|
|
<div>
|
2023-02-12 12:16:38 -06:00
|
|
|
<p class="text-centertruncate leading-none origin-center" style="color: rgb(247 223 187); font-size: 0.8rem" :style="{ transform: `scale(${sizeMultiplier})` }">{{ titleCleaned }}</p>
|
2021-12-04 19:56:29 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div v-if="!hasCover" class="absolute left-0 right-0 w-full flex items-center justify-center z-10" :style="{ padding: placeholderCoverPadding + 'rem', bottom: authorBottom + 'rem' }">
|
2023-02-12 12:16:38 -06:00
|
|
|
<p class="text-centertruncate leading-none origin-center" style="color: rgb(247 223 187); opacity: 0.75; font-size: 0.6rem" :style="{ transform: `scale(${sizeMultiplier})` }">{{ authorCleaned }}</p>
|
2021-12-04 19:56:29 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2022-04-03 17:07:26 -05:00
|
|
|
import { Capacitor } from '@capacitor/core'
|
|
|
|
|
2021-12-04 19:56:29 -06:00
|
|
|
export default {
|
|
|
|
props: {
|
2022-03-23 17:59:14 -05:00
|
|
|
libraryItem: {
|
2021-12-04 19:56:29 -06:00
|
|
|
type: Object,
|
|
|
|
default: () => {}
|
|
|
|
},
|
|
|
|
width: {
|
|
|
|
type: Number,
|
|
|
|
default: 120
|
|
|
|
},
|
|
|
|
bookCoverAspectRatio: Number,
|
2022-12-04 16:30:18 -06:00
|
|
|
downloadCover: String,
|
2023-02-09 18:13:30 -06:00
|
|
|
raw: Boolean,
|
|
|
|
noBg: Boolean
|
2021-12-04 19:56:29 -06:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
loading: true,
|
|
|
|
imageFailed: false,
|
|
|
|
showCoverBg: false,
|
|
|
|
imageReady: false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
cover() {
|
|
|
|
this.imageFailed = false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2022-04-03 17:07:26 -05:00
|
|
|
isLocal() {
|
|
|
|
if (!this.libraryItem) return false
|
|
|
|
return this.libraryItem.isLocal
|
|
|
|
},
|
2022-04-07 18:46:58 -05:00
|
|
|
localCover() {
|
2023-06-19 14:03:29 -05:00
|
|
|
return this.libraryItem?.coverContentUrl || null
|
2022-04-07 18:46:58 -05:00
|
|
|
},
|
2021-12-04 19:56:29 -06:00
|
|
|
squareAspectRatio() {
|
|
|
|
return this.bookCoverAspectRatio === 1
|
|
|
|
},
|
|
|
|
height() {
|
|
|
|
return this.width * this.bookCoverAspectRatio
|
|
|
|
},
|
2022-03-23 17:59:14 -05:00
|
|
|
media() {
|
|
|
|
if (!this.libraryItem) return {}
|
|
|
|
return this.libraryItem.media || {}
|
|
|
|
},
|
|
|
|
mediaMetadata() {
|
|
|
|
return this.media.metadata || {}
|
2021-12-04 19:56:29 -06:00
|
|
|
},
|
|
|
|
title() {
|
2022-03-23 17:59:14 -05:00
|
|
|
return this.mediaMetadata.title || 'No Title'
|
2021-12-04 19:56:29 -06:00
|
|
|
},
|
|
|
|
titleCleaned() {
|
|
|
|
if (this.title.length > 60) {
|
|
|
|
return this.title.slice(0, 57) + '...'
|
|
|
|
}
|
|
|
|
return this.title
|
|
|
|
},
|
2022-03-23 17:59:14 -05:00
|
|
|
authors() {
|
|
|
|
return this.mediaMetadata.authors || []
|
|
|
|
},
|
2021-12-04 19:56:29 -06:00
|
|
|
author() {
|
2022-03-23 17:59:14 -05:00
|
|
|
return this.authors.map((au) => au.name).join(', ')
|
2021-12-04 19:56:29 -06:00
|
|
|
},
|
|
|
|
authorCleaned() {
|
|
|
|
if (this.author.length > 30) {
|
|
|
|
return this.author.slice(0, 27) + '...'
|
|
|
|
}
|
|
|
|
return this.author
|
|
|
|
},
|
|
|
|
placeholderUrl() {
|
|
|
|
return '/book_placeholder.jpg'
|
|
|
|
},
|
|
|
|
fullCoverUrl() {
|
2022-04-03 17:07:26 -05:00
|
|
|
if (this.isLocal) {
|
2022-04-07 18:46:58 -05:00
|
|
|
if (this.localCover) return Capacitor.convertFileSrc(this.localCover)
|
2022-04-03 17:07:26 -05:00
|
|
|
return this.placeholderUrl
|
|
|
|
}
|
2021-12-04 19:56:29 -06:00
|
|
|
if (this.downloadCover) return this.downloadCover
|
2022-03-23 17:59:14 -05:00
|
|
|
if (!this.libraryItem) return null
|
2021-12-04 19:56:29 -06:00
|
|
|
var store = this.$store || this.$nuxt.$store
|
2022-12-04 16:30:18 -06:00
|
|
|
return store.getters['globals/getLibraryItemCoverSrc'](this.libraryItem, this.placeholderUrl, this.raw)
|
2021-12-04 19:56:29 -06:00
|
|
|
},
|
|
|
|
cover() {
|
2022-03-23 17:59:14 -05:00
|
|
|
return this.media.coverPath || this.placeholderUrl
|
2021-12-04 19:56:29 -06:00
|
|
|
},
|
|
|
|
hasCover() {
|
2022-05-22 17:49:10 -05:00
|
|
|
return (!!this.media.coverPath && !this.isLocal) || this.localCover || this.downloadCover
|
2021-12-04 19:56:29 -06:00
|
|
|
},
|
|
|
|
sizeMultiplier() {
|
2022-04-16 13:36:30 -05:00
|
|
|
var baseSize = this.squareAspectRatio ? 128 : 96
|
2021-12-04 19:56:29 -06:00
|
|
|
return this.width / baseSize
|
|
|
|
},
|
|
|
|
titleFontSize() {
|
|
|
|
return 0.75 * this.sizeMultiplier
|
|
|
|
},
|
|
|
|
authorFontSize() {
|
|
|
|
return 0.6 * this.sizeMultiplier
|
|
|
|
},
|
|
|
|
placeholderCoverPadding() {
|
2022-04-16 13:36:30 -05:00
|
|
|
if (this.sizeMultiplier < 0.5) return 0
|
|
|
|
return this.sizeMultiplier
|
2021-12-04 19:56:29 -06:00
|
|
|
},
|
|
|
|
authorBottom() {
|
|
|
|
return 0.75 * this.sizeMultiplier
|
|
|
|
},
|
|
|
|
userToken() {
|
|
|
|
return this.$store.getters['user/getToken']
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
setCoverBg() {
|
|
|
|
if (this.$refs.coverBg) {
|
|
|
|
this.$refs.coverBg.style.backgroundImage = `url("${this.fullCoverUrl}")`
|
|
|
|
}
|
|
|
|
},
|
|
|
|
imageLoaded() {
|
|
|
|
this.loading = false
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.imageReady = true
|
|
|
|
})
|
2023-02-09 18:13:30 -06:00
|
|
|
if (!this.noBg && this.$refs.cover && this.cover !== this.placeholderUrl) {
|
2021-12-04 19:56:29 -06:00
|
|
|
var { naturalWidth, naturalHeight } = this.$refs.cover
|
|
|
|
var aspectRatio = naturalHeight / naturalWidth
|
|
|
|
var arDiff = Math.abs(aspectRatio - this.bookCoverAspectRatio)
|
|
|
|
|
|
|
|
// If image aspect ratio is <= 1.45 or >= 1.75 then use cover bg, otherwise stretch to fit
|
|
|
|
if (arDiff > 0.15) {
|
|
|
|
this.showCoverBg = true
|
|
|
|
this.$nextTick(this.setCoverBg)
|
|
|
|
} else {
|
|
|
|
this.showCoverBg = false
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 17:02:18 -06:00
|
|
|
|
|
|
|
this.$emit('imageLoaded', this.fullCoverUrl)
|
2021-12-04 19:56:29 -06:00
|
|
|
},
|
|
|
|
imageError(err) {
|
|
|
|
this.loading = false
|
|
|
|
console.error('ImgError', err)
|
|
|
|
this.imageFailed = true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|