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

144 lines
4.1 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
2022-04-21 16:06:45 +00:00
<section id="playlistWrapper" v-if="playlist">
2022-04-15 17:00:08 +00:00
<ScreenHeader>
2022-04-15 14:24:30 +00:00
{{ playlist.name }}
2022-04-15 17:00:08 +00:00
<ControlsToggler v-if="playlist.populated" :showing-controls="showingControls" @toggleControls="toggleControls"/>
2022-04-15 14:24:30 +00:00
<template v-slot:meta>
2022-04-23 21:24:02 +00:00
<span class="meta" v-if="songs.length">
{{ pluralize(songs.length, 'song') }}
2022-04-15 14:24:30 +00:00
2022-04-23 21:24:02 +00:00
{{ duration }}
<template v-if="allowDownload">
2022-04-15 14:24:30 +00:00
2022-04-23 21:24:02 +00:00
<a href role="button" title="Download all songs in playlist" @click.prevent="download">Download All</a>
2022-04-15 14:24:30 +00:00
</template>
</span>
</template>
<template v-slot:controls>
2022-04-15 17:00:08 +00:00
<SongListControls
2022-04-15 14:24:30 +00:00
v-if="playlist.populated && (!isPhone || showingControls)"
@playAll="playAll"
@playSelected="playSelected"
@deletePlaylist="destroy"
:songs="playlist.songs"
:config="songListControlConfig"
:selectedSongs="selectedSongs"
/>
</template>
2022-04-15 17:00:08 +00:00
</ScreenHeader>
2022-04-15 14:24:30 +00:00
<template v-if="playlist.populated">
2022-04-15 17:00:08 +00:00
<SongList
2022-04-21 16:06:45 +00:00
v-if="songs.length"
ref="songList"
:items="songs"
2022-04-15 14:24:30 +00:00
type="playlist"
@press:delete="removeSelected"
@press:enter="onPressEnter"
2022-04-15 14:24:30 +00:00
/>
<ScreenEmptyState v-else>
2022-04-15 14:24:30 +00:00
<template v-slot:icon>
<i class="fa fa-file-o"></i>
</template>
<template v-if="playlist.is_smart">
No songs match the playlist's
<a @click.prevent="editSmartPlaylist">criteria</a>.
</template>
<template v-else>
The playlist is currently empty.
<span class="d-block secondary">
Drag songs into its name in the sidebar
or use the &quot;Add To&quot; button to fill it up.
</span>
</template>
</ScreenEmptyState>
2022-04-15 14:24:30 +00:00
</template>
</section>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
import { defineAsyncComponent, nextTick, ref, toRef } from 'vue'
import { alerts, eventBus, pluralize } from '@/utils'
2022-04-24 08:50:45 +00:00
import { playlistStore, commonStore } from '@/stores'
import { downloadService } from '@/services'
2022-04-15 17:00:08 +00:00
import { useSongList } from '@/composables'
import { difference } from 'lodash'
2022-04-15 17:00:08 +00:00
2022-04-21 16:06:45 +00:00
const ScreenHeader = defineAsyncComponent(() => import('@/components/ui/ScreenHeader.vue'))
const ScreenEmptyState = defineAsyncComponent(() => import('@/components/ui/ScreenEmptyState.vue'))
2022-04-15 17:00:08 +00:00
2022-04-21 16:06:45 +00:00
const playlist = ref<Playlist>()
2022-04-15 17:00:08 +00:00
const {
SongList,
SongListControls,
ControlsToggler,
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
selectedSongs,
showingControls,
songListControlConfig,
isPhone,
onPressEnter,
2022-04-15 17:00:08 +00:00
playAll,
playSelected,
toggleControls
2022-04-21 16:06:45 +00:00
} = useSongList(ref(playlist.value?.songs || []), { deletePlaylist: true })
2022-04-15 14:24:30 +00:00
2022-04-24 08:50:45 +00:00
const allowDownload = toRef(commonStore.state, 'allowDownload')
2022-04-15 17:00:08 +00:00
const destroy = () => eventBus.emit('PLAYLIST_DELETE', playlist.value)
2022-04-21 16:06:45 +00:00
const download = () => downloadService.fromPlaylist(playlist.value!)
2022-04-15 17:00:08 +00:00
const editSmartPlaylist = () => eventBus.emit('MODAL_SHOW_EDIT_SMART_PLAYLIST_FORM', playlist.value)
const removeSelected = () => {
if (!selectedSongs.value.length) return
playlistStore.removeSongs(playlist.value!, selectedSongs.value)
songs.value = difference(songs.value, selectedSongs.value)
alerts.success(`Removed ${pluralize(selectedSongs.value.length, 'song')} from "${playlist.value!.name}."`)
}
2022-04-15 17:00:08 +00:00
/**
* Fetch a playlist's content from the server, populate it, and use it afterwards.
*/
const populate = async (_playlist: Playlist) => {
await playlistStore.fetchSongs(_playlist)
playlist.value = _playlist
2022-04-21 16:06:45 +00:00
songs.value = playlist.value.songs
2022-04-15 17:00:08 +00:00
await nextTick()
songList.value?.sort()
}
2022-04-15 14:24:30 +00:00
eventBus.on({
LOAD_MAIN_CONTENT (view: MainViewName, playlistFromRoute: Playlist) {
if (view !== 'Playlist') {
return
}
if (playlistFromRoute.populated) {
playlist.value = playlistFromRoute
songs.value = playlist.value.songs
} else {
populate(playlistFromRoute)
}
},
'SMART_PLAYLIST_UPDATED': (updated: Playlist) => updated === playlist.value && populate(updated)
2022-04-15 17:00:08 +00:00
}
)
2022-04-15 14:24:30 +00:00
</script>
<style lang="scss">
#playlistWrapper {
.none {
padding: 16px 24px;
}
}
</style>