2022-04-15 14:24:30 +00:00
|
|
|
<template>
|
|
|
|
<li
|
2022-09-03 08:32:09 +00:00
|
|
|
ref="el"
|
2022-09-12 11:11:56 +00:00
|
|
|
:class="{ droppable }"
|
2022-10-08 10:54:25 +00:00
|
|
|
class="playlist"
|
2022-05-08 18:43:52 +00:00
|
|
|
data-testid="playlist-sidebar-item"
|
2022-08-10 14:56:01 +00:00
|
|
|
draggable="true"
|
2022-09-08 05:06:49 +00:00
|
|
|
@contextmenu="onContextMenu"
|
2022-09-03 08:32:09 +00:00
|
|
|
@dragleave="onDragLeave"
|
|
|
|
@dragover="onDragOver"
|
|
|
|
@dragstart="onDragStart"
|
|
|
|
@drop="onDrop"
|
2022-05-08 18:43:52 +00:00
|
|
|
>
|
2022-09-08 05:06:49 +00:00
|
|
|
<a :class="{ active }" :href="url">
|
|
|
|
<icon v-if="isRecentlyPlayedList(list)" :icon="faClockRotateLeft" class="text-green" fixed-width/>
|
|
|
|
<icon v-else-if="isFavoriteList(list)" :icon="faHeart" class="text-maroon" fixed-width/>
|
2022-09-03 08:32:09 +00:00
|
|
|
<icon
|
2022-09-08 05:06:49 +00:00
|
|
|
v-else-if="list.is_smart"
|
2022-09-03 08:32:09 +00:00
|
|
|
:icon="faBoltLightning"
|
|
|
|
:mask="faFile"
|
|
|
|
fixed-width
|
|
|
|
transform="shrink-7 down-2"
|
|
|
|
/>
|
|
|
|
<icon v-else :icon="faMusic" :mask="faFile" fixed-width transform="shrink-7 down-2"/>
|
2022-09-08 05:06:49 +00:00
|
|
|
{{ list.name }}
|
2022-05-13 11:46:03 +00:00
|
|
|
</a>
|
2022-04-15 14:24:30 +00:00
|
|
|
</li>
|
|
|
|
</template>
|
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
<script lang="ts" setup>
|
2022-07-15 07:23:55 +00:00
|
|
|
import { faBoltLightning, faClockRotateLeft, faFile, faHeart, faMusic } from '@fortawesome/free-solid-svg-icons'
|
2022-09-08 05:06:49 +00:00
|
|
|
import { computed, ref, toRefs } from 'vue'
|
2022-09-03 08:32:09 +00:00
|
|
|
import { eventBus, pluralize, requireInjection } from '@/utils'
|
2022-06-10 10:47:46 +00:00
|
|
|
import { favoriteStore, playlistStore } from '@/stores'
|
2022-10-08 10:54:25 +00:00
|
|
|
import { MessageToasterKey, RouterKey } from '@/symbols'
|
2022-09-03 08:32:09 +00:00
|
|
|
import { useDraggable, useDroppable } from '@/composables'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-09-03 08:32:09 +00:00
|
|
|
const { startDragging } = useDraggable('playlist')
|
|
|
|
const { acceptsDrop, resolveDroppedSongs } = useDroppable(['songs', 'album', 'artist'])
|
|
|
|
|
2022-07-26 09:51:19 +00:00
|
|
|
const toaster = requireInjection(MessageToasterKey)
|
2022-10-08 10:54:25 +00:00
|
|
|
const router = requireInjection(RouterKey)
|
2022-09-12 11:11:56 +00:00
|
|
|
const droppable = ref(false)
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-09-08 05:06:49 +00:00
|
|
|
const props = defineProps<{ list: PlaylistLike }>()
|
|
|
|
const { list } = toRefs(props)
|
|
|
|
|
|
|
|
const isPlaylist = (list: PlaylistLike): list is Playlist => 'id' in list
|
|
|
|
const isFavoriteList = (list: PlaylistLike): list is FavoriteList => list.name === 'Favorites'
|
|
|
|
const isRecentlyPlayedList = (list: PlaylistLike): list is RecentlyPlayedList => list.name === 'Recently Played'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
const active = ref(false)
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
const url = computed(() => {
|
2022-09-08 05:06:49 +00:00
|
|
|
if (isPlaylist(list.value)) return `#!/playlist/${list.value.id}`
|
|
|
|
if (isFavoriteList(list.value)) return '#!/favorites'
|
|
|
|
if (isRecentlyPlayedList(list.value)) return '#!/recently-played'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-09-08 05:06:49 +00:00
|
|
|
throw new Error('Invalid playlist-like type.')
|
2022-04-15 17:00:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const contentEditable = computed(() => {
|
2022-09-08 05:06:49 +00:00
|
|
|
if (isRecentlyPlayedList(list.value)) return false
|
|
|
|
if (isFavoriteList(list.value)) return true
|
|
|
|
|
|
|
|
return !list.value.is_smart
|
2022-04-15 17:00:08 +00:00
|
|
|
})
|
|
|
|
|
2022-09-08 05:06:49 +00:00
|
|
|
const onContextMenu = (event: MouseEvent) => {
|
|
|
|
if (isPlaylist(list.value)) {
|
|
|
|
event.preventDefault()
|
|
|
|
eventBus.emit('PLAYLIST_CONTEXT_MENU_REQUESTED', event, list.value)
|
2022-04-15 17:00:08 +00:00
|
|
|
}
|
2022-09-03 08:32:09 +00:00
|
|
|
}
|
2022-04-15 17:00:08 +00:00
|
|
|
|
2022-09-08 05:06:49 +00:00
|
|
|
const onDragStart = (event: DragEvent) => isPlaylist(list.value) && startDragging(event, list.value)
|
2022-09-03 08:32:09 +00:00
|
|
|
|
|
|
|
const onDragOver = (event: DragEvent) => {
|
|
|
|
if (!contentEditable.value) return false
|
|
|
|
if (!acceptsDrop(event)) return false
|
|
|
|
|
|
|
|
event.preventDefault()
|
|
|
|
event.dataTransfer!.dropEffect = 'copy'
|
2022-09-12 11:11:56 +00:00
|
|
|
droppable.value = true
|
2022-09-03 08:32:09 +00:00
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-09-12 11:11:56 +00:00
|
|
|
const onDragLeave = () => (droppable.value = false)
|
2022-09-03 08:32:09 +00:00
|
|
|
|
|
|
|
const onDrop = async (event: DragEvent) => {
|
2022-09-12 11:11:56 +00:00
|
|
|
droppable.value = false
|
2022-09-03 08:32:09 +00:00
|
|
|
|
|
|
|
if (!contentEditable.value) return false
|
|
|
|
if (!acceptsDrop(event)) return false
|
|
|
|
|
|
|
|
const songs = await resolveDroppedSongs(event)
|
|
|
|
|
|
|
|
if (!songs?.length) return false
|
2022-04-15 17:00:08 +00:00
|
|
|
|
2022-09-08 05:06:49 +00:00
|
|
|
if (isFavoriteList(list.value)) {
|
2022-06-10 10:47:46 +00:00
|
|
|
await favoriteStore.like(songs)
|
2022-09-08 05:06:49 +00:00
|
|
|
} else if (isPlaylist(list.value)) {
|
|
|
|
await playlistStore.addSongs(list.value, songs)
|
|
|
|
toaster.value.success(`Added ${pluralize(songs, 'song')} into "${list.value.name}."`)
|
2022-04-15 17:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-08 10:54:25 +00:00
|
|
|
router.onRouteChanged(route => {
|
|
|
|
switch (route.screen) {
|
2022-04-15 17:00:08 +00:00
|
|
|
case 'Favorites':
|
2022-09-08 05:06:49 +00:00
|
|
|
active.value = isFavoriteList(list.value)
|
2022-04-15 17:00:08 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
case 'RecentlyPlayed':
|
2022-09-08 05:06:49 +00:00
|
|
|
active.value = isRecentlyPlayedList(list.value)
|
2022-04-15 17:00:08 +00:00
|
|
|
break
|
2022-09-08 05:06:49 +00:00
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
case 'Playlist':
|
2022-10-08 10:54:25 +00:00
|
|
|
active.value = (list.value as Playlist).id === parseInt(route.params!.id)
|
2022-04-15 17:00:08 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
default:
|
|
|
|
active.value = false
|
|
|
|
break
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.playlist {
|
|
|
|
user-select: none;
|
2022-07-15 15:23:12 +00:00
|
|
|
overflow: hidden;
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-09-03 08:32:09 +00:00
|
|
|
&.droppable {
|
|
|
|
box-shadow: inset 0 0 0 1px var(--color-accent);
|
|
|
|
border-radius: 4px;
|
|
|
|
cursor: copy;
|
|
|
|
}
|
|
|
|
|
2022-08-10 14:56:01 +00:00
|
|
|
::v-deep(a) {
|
2022-04-15 14:24:30 +00:00
|
|
|
span {
|
|
|
|
pointer-events: none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
input {
|
|
|
|
width: calc(100% - 32px);
|
|
|
|
margin: 5px 16px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|