koel/resources/assets/js/components/screens/AllSongsScreen.vue

114 lines
2.9 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
<section id="songsWrapper">
2022-07-17 08:58:05 +00:00
<ScreenHeader :layout="headerLayout">
2022-04-15 14:24:30 +00:00
All Songs
<ControlsToggle v-model="showingControls"/>
2022-04-15 14:24:30 +00:00
2022-07-16 09:52:39 +00:00
<template v-slot:thumbnail>
<ThumbnailStack :thumbnails="thumbnails"/>
</template>
<template v-if="totalSongCount" v-slot:meta>
2022-07-10 17:15:56 +00:00
<span>{{ pluralize(totalSongCount, 'song') }}</span>
<span>{{ totalDuration }}</span>
2022-04-15 14:24:30 +00:00
</template>
<template v-slot:controls>
2022-04-15 17:00:08 +00:00
<SongListControls
2022-07-11 17:03:38 +00:00
v-if="totalSongCount && (!isPhone || showingControls)"
2022-04-15 14:24:30 +00:00
@playAll="playAll"
@playSelected="playSelected"
/>
</template>
2022-04-15 17:00:08 +00:00
</ScreenHeader>
2022-04-15 14:24:30 +00:00
2022-07-30 15:08:20 +00:00
<SongListSkeleton v-if="showSkeletons"/>
2022-07-16 09:52:39 +00:00
<SongList
2022-07-30 15:08:20 +00:00
v-else
2022-07-16 09:52:39 +00:00
ref="songList"
@sort="sort"
@scroll-breakpoint="onScrollBreakpoint"
@press:enter="onPressEnter"
@scrolled-to-end="fetchSongs"
/>
2022-04-15 14:24:30 +00:00
</section>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
import { computed, ref, toRef } from 'vue'
import { pluralize, requireInjection, secondsToHis } from '@/utils'
2022-06-10 10:47:46 +00:00
import { commonStore, queueStore, songStore } from '@/stores'
import { playbackService } from '@/services'
import { useScreen, useSongList } from '@/composables'
import { RouterKey } from '@/symbols'
2022-04-21 16:06:45 +00:00
import ScreenHeader from '@/components/ui/ScreenHeader.vue'
2022-07-30 15:08:20 +00:00
import SongListSkeleton from '@/components/ui/skeletons/SongListSkeleton.vue'
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
const totalSongCount = toRef(commonStore.state, 'song_count')
const totalDuration = computed(() => secondsToHis(commonStore.state.song_length))
2022-04-15 17:00:08 +00:00
const {
SongList,
SongListControls,
2022-06-10 10:47:46 +00:00
ControlsToggle,
2022-07-16 09:52:39 +00:00
ThumbnailStack,
headerLayout,
thumbnails,
2022-04-21 16:06:45 +00:00
songs,
2022-04-15 17:00:08 +00:00
songList,
2022-04-23 21:24:02 +00:00
duration,
2022-04-15 17:00:08 +00:00
showingControls,
isPhone,
onPressEnter,
2022-04-15 17:00:08 +00:00
playSelected,
2022-07-16 09:52:39 +00:00
onScrollBreakpoint
} = useSongList(toRef(songStore.state, 'songs'), 'Songs')
2022-06-10 10:47:46 +00:00
const router = requireInjection(RouterKey)
2022-06-10 10:47:46 +00:00
let initialized = false
2022-07-30 15:08:20 +00:00
const loading = ref(false)
2022-06-10 10:47:46 +00:00
let sortField: SongListSortField = 'title' // @todo get from query string
let sortOrder: SortOrder = 'asc'
const page = ref<number | null>(1)
const moreSongsAvailable = computed(() => page.value !== null)
2022-07-30 15:08:20 +00:00
const showSkeletons = computed(() => loading.value && songs.value.length === 0)
2022-06-10 10:47:46 +00:00
const sort = async (field: SongListSortField, order: SortOrder) => {
page.value = 1
songStore.state.songs = []
sortField = field
sortOrder = order
await fetchSongs()
}
const fetchSongs = async () => {
2022-07-30 15:08:20 +00:00
if (!moreSongsAvailable.value || loading.value) return
2022-06-10 10:47:46 +00:00
2022-07-30 15:08:20 +00:00
loading.value = true
2022-07-24 11:47:18 +00:00
page.value = await songStore.paginate(sortField, sortOrder, page.value!)
2022-07-30 15:08:20 +00:00
loading.value = false
2022-06-10 10:47:46 +00:00
}
const playAll = async (shuffle: boolean) => {
if (shuffle) {
await queueStore.fetchRandom()
} else {
await queueStore.fetchInOrder(sortField, sortOrder)
}
await playbackService.playFirstInQueue()
router.go('queue')
2022-06-10 10:47:46 +00:00
}
useScreen('Songs').onScreenActivated(async () => {
if (!initialized) {
2022-06-10 10:47:46 +00:00
initialized = true
await fetchSongs()
2022-06-10 10:47:46 +00:00
}
})
2022-04-15 14:24:30 +00:00
</script>