2021-09-22 03:57:33 +02:00
|
|
|
<template>
|
2022-08-06 15:25:31 +02:00
|
|
|
<div class="relative rounded-sm" :style="{ height: width * bookCoverAspectRatio + 'px', width: width + 'px', maxWidth: width + 'px', minWidth: width + 'px' }" @mouseover="isHovering = true" @mouseleave="isHovering = false">
|
|
|
|
<div class="w-full h-full relative overflow-hidden">
|
2021-12-04 00:05:13 +01: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" />
|
2021-09-22 03:57:33 +02:00
|
|
|
</div>
|
|
|
|
<img ref="cover" :src="cover" @error="imageError" @load="imageLoaded" class="w-full h-full absolute top-0 left-0" :class="showCoverBg ? 'object-contain' : 'object-cover'" />
|
2021-10-09 18:09:06 +02:00
|
|
|
|
|
|
|
<a v-if="!imageFailed && showOpenNewTab && isHovering" :href="cover" @click.stop target="_blank" class="absolute bg-primary flex items-center justify-center shadow-sm rounded-full hover:scale-110 transform duration-100" :style="{ top: sizeMultiplier * 0.5 + 'rem', right: sizeMultiplier * 0.5 + 'rem', width: 2.5 * sizeMultiplier + 'rem', height: 2.5 * sizeMultiplier + 'rem' }">
|
|
|
|
<span class="material-icons" :style="{ fontSize: sizeMultiplier * 1.75 + 'rem' }">open_in_new</span>
|
|
|
|
</a>
|
2021-09-22 03:57:33 +02:00
|
|
|
</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" class="mb-2" :style="{ height: 64 * sizeMultiplier + 'px' }" />
|
|
|
|
<p class="text-center font-book text-error" :style="{ fontSize: sizeMultiplier + 'rem' }">Invalid Cover</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-08-06 15:25:31 +02:00
|
|
|
|
|
|
|
<p v-if="!imageFailed" class="absolute -bottom-5 left-0 right-0 mx-auto text-xs text-gray-300 text-center">{{ resolution }}</p>
|
2021-09-22 03:57:33 +02:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
src: String,
|
|
|
|
width: {
|
|
|
|
type: Number,
|
|
|
|
default: 120
|
2021-10-09 18:09:06 +02:00
|
|
|
},
|
2021-12-03 02:02:38 +01:00
|
|
|
showOpenNewTab: Boolean,
|
|
|
|
bookCoverAspectRatio: Number
|
2021-09-22 03:57:33 +02:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
imageFailed: false,
|
2021-10-09 18:09:06 +02:00
|
|
|
showCoverBg: false,
|
|
|
|
isHovering: false,
|
|
|
|
naturalHeight: 0,
|
|
|
|
naturalWidth: 0
|
2021-09-22 03:57:33 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
cover() {
|
|
|
|
this.imageFailed = false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
cover() {
|
|
|
|
return this.src
|
|
|
|
},
|
|
|
|
sizeMultiplier() {
|
|
|
|
return this.width / 120
|
|
|
|
},
|
|
|
|
placeholderCoverPadding() {
|
|
|
|
return 0.8 * this.sizeMultiplier
|
2022-08-06 15:25:31 +02:00
|
|
|
},
|
|
|
|
resolution() {
|
|
|
|
return `${this.naturalWidth}x${this.naturalHeight}px`
|
2021-09-22 03:57:33 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
setCoverBg() {
|
|
|
|
if (this.$refs.coverBg) {
|
|
|
|
this.$refs.coverBg.style.backgroundImage = `url("${this.src}")`
|
|
|
|
}
|
|
|
|
},
|
|
|
|
imageLoaded() {
|
|
|
|
if (this.$refs.cover) {
|
|
|
|
var { naturalWidth, naturalHeight } = this.$refs.cover
|
2021-10-09 18:09:06 +02:00
|
|
|
this.naturalHeight = naturalHeight
|
|
|
|
this.naturalWidth = naturalWidth
|
|
|
|
|
2021-09-22 03:57:33 +02:00
|
|
|
var aspectRatio = naturalHeight / naturalWidth
|
2021-12-03 02:02:38 +01:00
|
|
|
var arDiff = Math.abs(aspectRatio - this.bookCoverAspectRatio)
|
2021-09-22 03:57:33 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
imageError(err) {
|
|
|
|
console.error('ImgError', err)
|
|
|
|
this.imageFailed = true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {}
|
|
|
|
}
|
|
|
|
</script>
|