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

187 lines
5.7 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
<div ref="el" class="song-list-controls" data-testid="song-list-controls">
<div class="wrapper">
<BtnGroup uppercased>
<template v-if="mergedConfig.play">
<template v-if="altPressed">
<Btn
v-if="selectedSongs.length < 2 && songs.length"
class="btn-play-all"
orange
title="Play all songs"
@click.prevent="playAll"
>
<icon :icon="faPlay" fixed-width/>
All
</Btn>
<Btn
v-if="selectedSongs.length > 1"
class="btn-play-selected"
orange
title="Play selected songs"
@click.prevent="playSelected"
>
<icon :icon="faPlay" fixed-width/>
Selected
</Btn>
</template>
<template v-else>
<Btn
v-if="selectedSongs.length < 2 && songs.length"
class="btn-shuffle-all"
data-testid="btn-shuffle-all"
orange
title="Shuffle all songs"
@click.prevent="shuffle"
>
<icon :icon="faRandom" fixed-width/>
All
</Btn>
<Btn
v-if="selectedSongs.length > 1"
class="btn-shuffle-selected"
data-testid="btn-shuffle-selected"
orange
title="Shuffle selected songs"
@click.prevent="shuffleSelected"
>
<icon :icon="faRandom" fixed-width/>
Selected
</Btn>
</template>
2022-04-15 14:24:30 +00:00
</template>
<Btn
v-if="selectedSongs.length"
:title="`${showingAddToMenu ? 'Cancel' : 'Add selected songs to…'}`"
class="btn-add-to"
data-testid="add-to-btn"
green
@click.prevent.stop="toggleAddToMenu"
>
{{ showingAddToMenu ? 'Cancel' : 'Add To…' }}
</Btn>
<Btn v-if="showClearQueueButton" red title="Clear current queue" @click.prevent="clearQueue">Clear</Btn>
</BtnGroup>
<BtnGroup>
<Btn v-if="config.refresh" v-koel-tooltip green title="Refresh" @click.prevent="refresh">
<icon :icon="faRotateRight" fixed-width/>
</Btn>
<Btn
v-if="showDeletePlaylistButton"
v-koel-tooltip
class="del btn-delete-playlist"
red
title="Delete this playlist"
@click.prevent="deletePlaylist"
>
<icon :icon="faTrashCan"/>
</Btn>
</BtnGroup>
</div>
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
<AddToMenu
2022-04-21 16:06:45 +00:00
v-koel-clickaway="closeAddToMenu"
2022-04-15 14:24:30 +00:00
:config="mergedConfig.addTo"
:showing="showingAddToMenu"
2022-04-21 16:06:45 +00:00
:songs="selectedSongs"
@closing="closeAddToMenu"
2022-04-15 14:24:30 +00:00
/>
</div>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
import { faPlay, faRandom, faRotateRight, faTrashCan } from '@fortawesome/free-solid-svg-icons'
2022-07-20 08:00:02 +00:00
import { computed, nextTick, onMounted, onUnmounted, ref, toRefs } from 'vue'
2022-06-10 10:47:46 +00:00
import { SelectedSongsKey, SongsKey } from '@/symbols'
2022-07-20 08:00:02 +00:00
import { requireInjection } from '@/utils'
2022-04-15 14:24:30 +00:00
import AddToMenu from '@/components/song/AddToMenu.vue'
import Btn from '@/components/ui/Btn.vue'
import BtnGroup from '@/components/ui/BtnGroup.vue'
2022-04-21 16:06:45 +00:00
2022-06-10 10:47:46 +00:00
const props = withDefaults(defineProps<{ config?: Partial<SongListControlsConfig> }>(), { config: () => ({}) })
const { config } = toRefs(props)
2022-04-15 14:24:30 +00:00
2022-07-20 08:00:02 +00:00
const [songs] = requireInjection(SongsKey)
const [selectedSongs] = requireInjection(SelectedSongsKey)
2022-04-15 14:24:30 +00:00
2022-04-21 16:06:45 +00:00
const el = ref<HTMLElement>()
2022-04-15 17:00:08 +00:00
const showingAddToMenu = ref(false)
const altPressed = ref(false)
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
const mergedConfig = computed((): SongListControlsConfig => Object.assign({
play: true,
addTo: {
queue: true,
favorites: true,
playlists: true,
newPlaylist: true
2022-04-15 14:24:30 +00:00
},
2022-04-15 17:00:08 +00:00
clearQueue: false,
deletePlaylist: false,
refresh: false
2022-04-21 16:06:45 +00:00
}, config.value)
2022-04-15 17:00:08 +00:00
)
const showClearQueueButton = computed(() => mergedConfig.value.clearQueue)
const showDeletePlaylistButton = computed(() => mergedConfig.value.deletePlaylist)
const emit = defineEmits(['playAll', 'playSelected', 'clearQueue', 'deletePlaylist', 'refresh'])
2022-04-15 17:00:08 +00:00
const shuffle = () => emit('playAll', true)
const shuffleSelected = () => emit('playSelected', true)
const playAll = () => emit('playAll', false)
const playSelected = () => emit('playSelected', false)
const clearQueue = () => emit('clearQueue')
const deletePlaylist = () => emit('deletePlaylist')
const refresh = () => emit('refresh')
2022-04-15 17:00:08 +00:00
const closeAddToMenu = () => (showingAddToMenu.value = false)
const registerKeydown = (event: KeyboardEvent) => event.key === 'Alt' && (altPressed.value = true)
const registerKeyup = (event: KeyboardEvent) => event.key === 'Alt' && (altPressed.value = false)
2022-04-15 17:00:08 +00:00
const toggleAddToMenu = async () => {
showingAddToMenu.value = !showingAddToMenu.value
if (!showingAddToMenu.value) {
return
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
await nextTick()
2022-04-15 14:24:30 +00:00
2022-04-21 16:06:45 +00:00
const btnAddTo = el.value?.querySelector<HTMLButtonElement>('.btn-add-to')!
2022-04-15 17:00:08 +00:00
const { left: btnLeft, bottom: btnBottom, width: btnWidth } = btnAddTo.getBoundingClientRect()
2022-04-21 16:06:45 +00:00
const contextMenu = el.value?.querySelector<HTMLElement>('.add-to')!
2022-04-15 17:00:08 +00:00
const menuWidth = contextMenu.getBoundingClientRect().width
contextMenu.style.top = `${btnBottom + 10}px`
contextMenu.style.left = `${btnLeft + btnWidth / 2 - menuWidth / 2}px`
}
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
onMounted(() => {
window.addEventListener('keydown', registerKeydown)
window.addEventListener('keyup', registerKeyup)
})
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
onUnmounted(() => {
window.removeEventListener('keydown', registerKeydown)
window.removeEventListener('keyup', registerKeyup)
2022-04-15 14:24:30 +00:00
})
</script>
<style lang="scss" scoped>
.song-list-controls {
position: relative;
.wrapper {
display: flex;
gap: .5rem;
}
2022-04-15 14:24:30 +00:00
}
</style>