audiobookshelf/client/components/widgets/ItemSlider.vue

208 lines
6.6 KiB
Vue
Raw Normal View History

2022-05-09 01:21:46 +02:00
<template>
<div class="w-full">
<div class="flex items-center py-3e">
2022-05-09 01:21:46 +02:00
<slot />
<div class="flex-grow" />
<button cy-id="leftScrollButton" v-if="isScrollable" class="w-8e h-8e mx-1e flex items-center justify-center rounded-full" :class="canScrollLeft ? 'hover:bg-white hover:bg-opacity-5 text-gray-300 hover:text-white' : 'text-white text-opacity-40 cursor-text'" @click="scrollLeft">
<span class="material-icons" :style="{ fontSize: 1.5 + 'em' }">chevron_left</span>
2022-05-09 01:21:46 +02:00
</button>
<button cy-id="rightScrollButton" v-if="isScrollable" class="w-8e h-8e mx-1e flex items-center justify-center rounded-full" :class="canScrollRight ? 'hover:bg-white hover:bg-opacity-5 text-gray-300 hover:text-white' : 'text-white text-opacity-40 cursor-text'" @click="scrollRight">
<span class="material-icons" :style="{ fontSize: 1.5 + 'em' }">chevron_right</span>
2022-05-09 01:21:46 +02:00
</button>
</div>
2024-06-03 08:04:03 +02:00
<div cy-id="slider" ref="slider" class="w-full overflow-y-hidden overflow-x-auto no-scroll" style="scroll-behavior: smooth" @scroll="scrolled">
<div class="flex space-x-4e">
2022-05-09 01:21:46 +02:00
<template v-for="(item, index) in items">
2024-06-03 08:04:03 +02:00
<div cy-id="item" ref="item" :key="itemKeyFunc(item)">
<component :is="componentName" :ref="itemRefFunc(item)" :index="index" :[itemPropName]="item" :bookshelf-view="bookshelfView" :continue-listening-shelf="continueListeningShelf" class="relative" @edit="editFunc" @editPodcast="editItem" @select="selectItem" @hook:updated="setScrollVars" />
</div>
2022-05-09 01:21:46 +02:00
</template>
</div>
</div>
</div>
</template>
<script>
export default {
props: {
items: {
type: Array,
default: () => []
},
bookshelfView: {
type: Number,
default: 1
},
2024-06-03 08:04:03 +02:00
shelfId: {
type: String,
default: ''
},
continueListeningShelf: {
type: Boolean,
default: false
},
type: {
type: String,
default: 'book'
}
2022-05-09 01:21:46 +02:00
},
data() {
return {
isScrollable: false,
canScrollLeft: false,
canScrollRight: false,
2024-06-03 08:04:03 +02:00
clientWidth: 0,
shelfOptionsByType: {
episode: {
component: 'cards-lazy-book-card',
itemPropName: 'book-mount',
itemIdFunc: (item) => item.recentEpisode.id
},
series: {
component: 'cards-lazy-series-card',
itemPropName: 'series-mount',
itemIdFunc: (item) => item.id
},
authors: {
component: 'cards-author-card',
itemPropName: 'author',
itemIdFunc: (item) => item.id
},
narrators: {
component: 'cards-narrator-card',
itemPropName: 'narrator',
itemIdFunc: (item) => item.name
},
book: {
component: 'cards-lazy-book-card',
itemPropName: 'book-mount',
itemIdFunc: (item) => item.id
},
podcast: {
component: 'cards-lazy-book-card',
itemPropName: 'book-mount',
itemIdFunc: (item) => item.id
}
}
2022-05-09 01:21:46 +02:00
}
},
computed: {
2024-06-03 08:04:03 +02:00
isSelectionMode() {
return this.$store.getters['globals/getIsBatchSelectingMediaItems']
2022-05-09 01:21:46 +02:00
},
2024-06-03 08:04:03 +02:00
options() {
return this.shelfOptionsByType[this.type]
},
2024-06-03 08:04:03 +02:00
itemIdFunc() {
return this.options.itemIdFunc
2022-05-09 01:21:46 +02:00
},
2024-06-03 08:04:03 +02:00
itemKeyFunc() {
return (item) => this.itemIdFunc(item) + this.shelfId
2022-05-09 01:21:46 +02:00
},
2024-06-03 08:04:03 +02:00
itemRefFunc() {
return (item) => `slider-item-${this.itemIdFunc(item)}`
2022-05-09 01:21:46 +02:00
},
2024-06-03 08:04:03 +02:00
componentName() {
return this.options.component
},
itemPropName() {
return this.options.itemPropName
},
editFunc() {
switch (this.type) {
case 'episode':
return this.editEpisode
case 'authors':
return this.editAuthor
default:
return this.editItem
}
2022-05-09 01:21:46 +02:00
}
},
methods: {
clearSelectedEntities() {
this.updateSelectionMode(false)
},
2024-06-03 08:04:03 +02:00
editEpisode({ libraryItem, episode }) {
this.$store.commit('setSelectedLibraryItem', libraryItem)
this.$store.commit('globals/setSelectedEpisode', episode)
this.$store.commit('globals/setShowEditPodcastEpisodeModal', true)
},
editAuthor(author) {
this.$store.commit('globals/showEditAuthorModal', author)
},
2022-05-09 01:21:46 +02:00
editItem(libraryItem) {
var itemIds = this.items.map((e) => e.id)
this.$store.commit('setBookshelfBookIds', itemIds)
this.$store.commit('showEditModal', libraryItem)
},
selectItem(payload) {
this.$emit('selectEntity', payload)
2022-05-09 01:21:46 +02:00
},
itemSelectedEvt() {
this.updateSelectionMode(this.isSelectionMode)
},
updateSelectionMode(val) {
const selectedMediaItems = this.$store.state.globals.selectedMediaItems
2022-05-09 01:21:46 +02:00
this.items.forEach((item) => {
2024-06-03 08:04:03 +02:00
let component = this.$refs[this.itemRefFunc(item)]
2022-05-09 01:21:46 +02:00
if (!component || !component.length) return
component = component[0]
component.setSelectionMode(val)
component.selected = selectedMediaItems.some((i) => i.id === item.id)
2022-05-09 01:21:46 +02:00
})
},
scrolled() {
this.setScrollVars()
},
scrollRight() {
if (!this.canScrollRight) return
const slider = this.$refs.slider
if (!slider) return
2024-06-03 08:04:03 +02:00
const scrollAmount = this.clientWidth
2022-05-09 01:21:46 +02:00
const maxScrollLeft = slider.scrollWidth - slider.clientWidth
const newScrollLeft = Math.min(maxScrollLeft, slider.scrollLeft + scrollAmount)
slider.scrollLeft = newScrollLeft
},
scrollLeft() {
if (!this.canScrollLeft) return
const slider = this.$refs.slider
if (!slider) return
2024-06-03 08:04:03 +02:00
const scrollAmount = this.clientWidth
2022-05-09 01:21:46 +02:00
const newScrollLeft = Math.max(0, slider.scrollLeft - scrollAmount)
slider.scrollLeft = newScrollLeft
},
setScrollVars() {
const slider = this.$refs.slider
if (!slider) return
const { scrollLeft, scrollWidth, clientWidth } = slider
2024-06-03 08:04:03 +02:00
const scrollRemaining = Math.abs(scrollLeft + clientWidth - scrollWidth)
2022-05-09 01:21:46 +02:00
this.clientWidth = clientWidth
this.isScrollable = scrollWidth > clientWidth
2024-06-03 08:04:03 +02:00
this.canScrollRight = scrollRemaining >= 1
2022-05-09 01:21:46 +02:00
this.canScrollLeft = scrollLeft > 0
}
},
updated() {
this.setScrollVars()
},
mounted() {
2024-06-03 08:04:03 +02:00
this.setScrollVars()
2024-06-24 23:37:02 +02:00
if (['book', 'podcast', 'episode'].includes(this.type)) {
this.$eventBus.$on('bookshelf_clear_selection', this.clearSelectedEntities)
this.$eventBus.$on('item-selected', this.itemSelectedEvt)
2024-06-03 08:04:03 +02:00
}
2022-05-09 01:21:46 +02:00
},
beforeDestroy() {
2024-06-24 23:37:02 +02:00
if (['book', 'podcast', 'episode'].includes(this.type)) {
this.$eventBus.$off('bookshelf_clear_selection', this.clearSelectedEntities)
this.$eventBus.$off('item-selected', this.itemSelectedEvt)
2024-06-03 08:04:03 +02:00
}
2022-05-09 01:21:46 +02:00
}
}
2024-06-03 08:04:03 +02:00
</script>