koel/resources/assets/js/components/song/list.vue

547 lines
14 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
<div
class="song-list-wrap main-scroll-wrap"
:class="type"
ref="wrapper"
tabindex="0"
@keydown.delete.prevent.stop="handleDelete"
@keydown.enter.prevent.stop="handleEnter"
@keydown.a.prevent="handleA"
>
2022-04-15 17:00:08 +00:00
<div class="song-list-header" :class="mergedConfig.sortable ? 'sortable' : 'unsortable'">
<span @click="sort('song.track')" class="track-number" v-if="mergedConfig.columns.includes('track')">
#
<i class="fa fa-angle-down" v-show="primarySortField === 'song.track' && sortOrder === 'Asc'"></i>
<i class="fa fa-angle-up" v-show="primarySortField === 'song.track' && sortOrder === 'Desc'"></i>
</span>
<span @click="sort('song.title')" class="title" v-if="mergedConfig.columns.includes('title')">
Title
<i class="fa fa-angle-down" v-show="primarySortField === 'song.title' && sortOrder === 'Asc'"></i>
<i class="fa fa-angle-up" v-show="primarySortField === 'song.title' && sortOrder === 'Desc'"></i>
</span>
<span
@click="sort(['song.album.artist.name', 'song.album.name', 'song.track'])"
class="artist"
v-if="mergedConfig.columns.includes('artist')"
>
Artist
<i class="fa fa-angle-down" v-show="primarySortField === 'song.album.artist.name' && sortOrder === 'Asc'"></i>
<i class="fa fa-angle-up" v-show="primarySortField === 'song.album.artist.name' && sortOrder === 'Desc'"></i>
</span>
<span
@click="sort(['song.album.name', 'song.track'])"
class="album"
v-if="mergedConfig.columns.includes('album')"
>
Album
<i class="fa fa-angle-down" v-show="primarySortField === 'song.album.name' && sortOrder === 'Asc'"></i>
<i class="fa fa-angle-up" v-show="primarySortField === 'song.album.name' && sortOrder === 'Desc'"></i>
</span>
<span @click="sort('song.length')" class="time" v-if="mergedConfig.columns.includes('length')">
Time
<i class="fa fa-angle-down" v-show="primarySortField === 'song.length' && sortOrder === 'Asc'"></i>
<i class="fa fa-angle-up" v-show="primarySortField === 'song.length' && sortOrder === 'Desc'"></i>
</span>
<span class="favorite"></span>
<span class="play"></span>
</div>
<RecycleScroller
2022-04-15 14:24:30 +00:00
class="scroller"
:items="songProxies"
2022-04-15 17:00:08 +00:00
:item-size="35"
key-field="id"
v-slot="{ item }"
2022-04-15 14:24:30 +00:00
>
2022-04-15 17:00:08 +00:00
<SongItem :item="item" :columns="mergedConfig.columns"/>
</RecycleScroller>
2022-04-15 14:24:30 +00:00
</div>
</template>
<script lang="ts">
2022-04-15 17:00:08 +00:00
export default {
name: 'SongList'
}
</script>
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
import isMobile from 'ismobilejs'
import {
computed,
defineAsyncComponent,
getCurrentInstance,
nextTick,
onMounted,
PropType,
ref,
toRefs,
watch
} from 'vue'
import { RecycleScroller } from 'vue-virtual-scroller'
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'
import { $, eventBus, orderBy, startDragging } from '@/utils'
import { favoriteStore, playlistStore, queueStore } from '@/stores'
2022-04-15 14:24:30 +00:00
import { playback } from '@/services'
import router from '@/router'
type SortField = 'song.track'
| 'song.disc'
| 'song.title'
| 'song.album.artist.name'
| 'song.album.name'
| 'song.length'
2022-04-15 17:00:08 +00:00
type SortOrder = 'Asc' | 'Desc' | 'None'
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
interface SongRow {
props: {
item: SongProxy
}
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
const SongItem = defineAsyncComponent(() => import('@/components/song/item.vue'))
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
// @ts-ignore
getCurrentInstance()!.__IS_SONG_LIST__ = true
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const props = defineProps({
items: {
type: Array as PropType<Song[]>,
required: true
},
playlist: {
type: Object as PropType<Playlist>
2022-04-15 14:24:30 +00:00
},
2022-04-15 17:00:08 +00:00
type: {
type: String as PropType<SongListType>,
default: 'all-songs'
},
config: {
type: Object as PropType<Partial<SongListConfig>>,
default: () => ({})
}
})
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const { items, playlist, type, config } = toRefs(props)
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const lastSelectedRow = ref<SongRow>()
const sortFields = ref<SortField[]>([])
const sortOrder = ref<SortOrder>('None')
const songProxies = ref<SongProxy[]>([])
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const allowSongReordering = computed(() => type.value === 'queue')
const selectedSongs = computed(() => songProxies.value.filter(row => row.selected).map(row => row.song))
const primarySortField = computed(() => sortFields.value.length === 0 ? null : sortFields.value[0])
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const mergedConfig = computed((): SongListConfig => {
return Object.assign({
sortable: true,
columns: ['track', 'title', 'artist', 'album', 'length']
}, config.value)
})
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
/**
* Since song objects themselves are shared by all song lists, we can't use them directly to
* determine their selection status (selected/unselected). Therefore, for each song list, we
* maintain an array of "song proxies," each containing the song itself and the "selected" flag.
* To comply with virtual-scroller, a "type" attribute also presents.
*/
const generateSongProxies = () => {
// Since this method re-generates the song wrappers, we need to keep track of the
// selected songs manually.
const selectedSongIds = selectedSongs.value.map(song => song.id)
return items.value.map(song => ({
id: song.id,
song,
selected: selectedSongIds.includes(song.id)
}))
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const nextSortOrder = computed<SortOrder>(() => {
if (sortOrder.value === 'None') return 'Asc'
if (sortOrder.value === 'Asc') return 'Desc'
return 'None'
})
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const sort = (field: SortField | SortField[] = [], order: SortOrder | null = null) => {
// there are certain circumstances where sorting is simply disallowed, e.g. in Queue
if (!mergedConfig.value.sortable) {
return
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
sortFields.value = ([] as SortField[]).concat(field)
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
if (!sortFields.value.length && ['album', 'artist'].includes(type.value)) {
// by default, sort Album/Artist by track numbers for a more friendly UX
sortFields.value.push('song.track')
order = 'Asc'
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
if (sortFields.value.includes('song.track') && !sortFields.value.includes('song.disc')) {
// Track numbers should always go in conjunction with disc numbers.
sortFields.value.push('song.disc')
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
sortOrder.value = order === null ? nextSortOrder.value : order
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
songProxies.value = sortOrder.value === 'None'
? generateSongProxies()
: orderBy(songProxies.value, sortFields.value, sortOrder.value === 'Desc')
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const render = () => {
mergedConfig.value.sortable || (sortFields.value = [])
songProxies.value = generateSongProxies()
sort(sortFields.value, sortOrder.value)
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
watch(items, () => render())
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
watch(selectedSongs, () => eventBus.emit('SET_SELECTED_SONGS', selectedSongs, getCurrentInstance()?.parent))
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const handleDelete = () => {
if (!selectedSongs.value.length) {
return
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
switch (type.value) {
case 'queue':
queueStore.unqueue(selectedSongs.value)
break
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
case 'favorites':
favoriteStore.unlike(selectedSongs.value)
break
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
case 'playlist':
playlistStore.removeSongs(playlist!.value!, selectedSongs.value)
break
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
default:
return
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
clearSelection()
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const handleEnter = (event: DragEvent) => {
if (!selectedSongs.value.length) {
return
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
if (selectedSongs.value.length === 1) {
// Just play the song
playback.play(selectedSongs.value[0])
return
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
switch (type.value) {
case 'queue':
// Play the first song selected if we're in Queue screen.
playback.play(selectedSongs.value[0])
break
default:
//
// --------------------------------------------------------------------
// For other screens, follow this map:
//
// • Enter: Queue songs to bottom
// • Shift+Enter: Queues song to top
// • Cmd/Ctrl+Enter: Queues song to bottom and play the first selected song
// • Cmd/Ctrl+Shift+Enter: Queue songs to top and play the first queued song
// --------------------------------------------------------------------
//
if (event.shiftKey) {
queueStore.queueToTop(selectedSongs.value)
} else {
queueStore.queue(selectedSongs.value)
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
if (event.ctrlKey || event.metaKey) {
playback.play(selectedSongs.value[0])
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
router.go('queue')
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
break
}
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
/**
* Select all (filtered) rows in the current list.
*/
const selectAllRows = () => songProxies.value.forEach(row => (row.selected = true))
const clearSelection = () => songProxies.value.forEach(row => (row.selected = false))
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const handleA = (event: KeyboardEvent) => (event.ctrlKey || event.metaKey) && selectAllRows()
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const rowClicked = (rowVm: SongRow, event: MouseEvent) => {
// If we're on a touch device, or if Ctrl/Cmd key is pressed, just toggle selection.
if (isMobile.any) {
toggleRow(rowVm)
return
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
if (event.ctrlKey || event.metaKey) {
toggleRow(rowVm)
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
if (event.button === 0) {
if (!(event.ctrlKey || event.metaKey || event.shiftKey)) {
clearSelection()
toggleRow(rowVm)
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
if (event.shiftKey && lastSelectedRow.value) {
selectRowsBetween(lastSelectedRow.value, rowVm)
}
}
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const toggleRow = (rowVm: SongRow) => {
rowVm.props.item.selected = !rowVm.props.item.selected
lastSelectedRow.value = rowVm
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const selectRowsBetween = (firstRowVm: SongRow, secondRowVm: SongRow) => {
const indexes = [
songProxies.value.indexOf(firstRowVm.props.item),
songProxies.value.indexOf(secondRowVm.props.item)
]
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
indexes.sort((a, b) => a - b)
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
for (let i = indexes[0]; i <= indexes[1]; ++i) {
songProxies.value[i].selected = true
}
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
/**
* Enable dragging songs by capturing the dragstart event on a table row.
* Even though the event is triggered on one row only, we'll collect other
* selected rows, if any, as well.
*/
const dragStart = (rowVm: SongRow, event: DragEvent) => {
// If the user is dragging an unselected row, clear the current selection.
if (!rowVm.props.item.selected) {
clearSelection()
rowVm.props.item.selected = true
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
startDragging(event, selectedSongs.value, 'Song')
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
/**
* Add a "droppable" class and set the drop effect when other songs are dragged over a row.
*/
const allowDrop = (event: DragEvent) => {
if (!allowSongReordering.value) {
return
}
$.addClass((event.target as Element).parentElement, 'droppable')
event.dataTransfer!.dropEffect = 'move'
return false
}
/**
* Perform reordering songs upon dropping if the current song list is of type Queue.
*/
const handleDrop = (rowVm: SongRow, event: DragEvent) => {
if (
!allowSongReordering.value ||
!event.dataTransfer!.getData('application/x-koel.text+plain') ||
!selectedSongs.value.length
) {
return removeDroppableState(event)
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
queueStore.move(selectedSongs.value, rowVm.props.item.song)
return removeDroppableState(event)
}
const removeDroppableState = (event: DragEvent) => {
$.removeClass((event.target as Element).parentElement, 'droppable')
return false
}
const openContextMenu = async (rowVm: SongRow, event: MouseEvent) => {
// If the user is right-clicking an unselected row,
// clear the current selection and select it instead.
if (!rowVm.props.item.selected) {
clearSelection()
toggleRow(rowVm)
}
await nextTick()
eventBus.emit('SONG_CONTEXT_MENU_REQUESTED', event, selectedSongs.value)
}
const getAllSongsWithSort = () => songProxies.value.map(proxy => proxy.song)
onMounted(() => items.value && render())
defineExpose({
rowClicked,
dragStart,
allowDrop,
handleDrop,
removeDroppableState,
openContextMenu,
getAllSongsWithSort
2022-04-15 14:24:30 +00:00
})
</script>
<style lang="scss">
.song-list-wrap {
position: relative;
padding: 8px 24px;
.song-list-header {
position: absolute;
top: 0;
left: 0;
right: 0;
background: var(--color-bg-secondary);
z-index: 1;
2022-04-15 17:00:08 +00:00
display: flex;
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
div.droppable {
2022-04-15 14:24:30 +00:00
border-bottom-width: 3px;
border-bottom-color: var(--color-green);
}
2022-04-15 17:00:08 +00:00
.song-list-header span, .song-item span {
2022-04-15 14:24:30 +00:00
text-align: left;
padding: 8px;
vertical-align: middle;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
&.time {
2022-04-15 17:00:08 +00:00
flex-basis: 96px;
2022-04-15 14:24:30 +00:00
padding-right: 24px;
text-align: right;
}
&.track-number {
2022-04-15 17:00:08 +00:00
flex-basis: 66px;
2022-04-15 14:24:30 +00:00
padding-left: 24px;
}
&.artist {
2022-04-15 17:00:08 +00:00
flex-basis: 23%;
2022-04-15 14:24:30 +00:00
}
&.album {
2022-04-15 17:00:08 +00:00
flex-basis: 27%;
2022-04-15 14:24:30 +00:00
}
&.favorite {
2022-04-15 17:00:08 +00:00
flex-basis: 36px;
2022-04-15 14:24:30 +00:00
}
&.play {
display: none;
@media (hover: none) {
display: block;
position: absolute;
top: 8px;
right: 4px;
}
}
2022-04-15 17:00:08 +00:00
&.title {
flex: 1;
}
2022-04-15 14:24:30 +00:00
}
2022-04-15 17:00:08 +00:00
.song-list-header {
2022-04-15 14:24:30 +00:00
color: var(--color-text-secondary);
letter-spacing: 1px;
text-transform: uppercase;
cursor: pointer;
i {
color: var(--color-highlight);
font-size: 1.2rem;
}
}
2022-04-15 17:00:08 +00:00
.unsortable span {
2022-04-15 14:24:30 +00:00
cursor: default;
}
.scroller {
overflow: auto;
position: absolute;
top: 35px;
left: 0;
bottom: 0;
right: 0;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
.item-container {
position: absolute;
left: 0;
right: 0;
min-height: 100%;
}
.item {
margin-bottom: 0;
}
}
@media only screen and (max-width: 768px) {
2022-04-15 17:00:08 +00:00
.song-list-header {
2022-04-15 14:24:30 +00:00
display: none;
}
.scroller {
top: 0;
.item-container {
left: 12px;
right: 12px;
width: calc(100vw - 24px);
}
}
2022-04-15 17:00:08 +00:00
.song-item {
2022-04-15 14:24:30 +00:00
padding: 8px 32px 8px 4px;
position: relative;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
color: var(--color-text-secondary);
width: 100%;
}
2022-04-15 17:00:08 +00:00
.song-item span {
2022-04-15 14:24:30 +00:00
display: none;
padding: 0;
vertical-align: bottom;
color: var(--color-text-primary);
&.artist, &.title {
display: inline;
}
&.artist {
color: var(--color-text-secondary);
font-size: 0.9rem;
padding: 0 4px;
}
}
}
}
</style>