2022-04-15 14:24:30 +00:00
|
|
|
/**
|
|
|
|
* Add necessary functionalities into a view that contains a song-list component.
|
|
|
|
*/
|
2022-04-15 17:00:08 +00:00
|
|
|
import { ComponentInternalInstance, getCurrentInstance, reactive, ref, watchEffect } from 'vue'
|
2022-04-15 14:24:30 +00:00
|
|
|
import isMobile from 'ismobilejs'
|
|
|
|
|
|
|
|
import { playback } from '@/services'
|
|
|
|
import { eventBus } from '@/utils'
|
|
|
|
|
|
|
|
import ControlsToggler from '@/components/ui/screen-controls-toggler.vue'
|
|
|
|
import SongList from '@/components/song/list.vue'
|
|
|
|
import SongListControls from '@/components/song/list-controls.vue'
|
|
|
|
import { songStore } from '@/stores'
|
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
export const useSongList = (controlsConfig: Partial<SongListControlsConfig> = {}) => {
|
2022-04-15 14:24:30 +00:00
|
|
|
const songList = ref(null)
|
2022-04-15 17:00:08 +00:00
|
|
|
const state = reactive<SongListState>({ songs: [] })
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
const meta = reactive<SongListMeta>({
|
2022-04-15 14:24:30 +00:00
|
|
|
songCount: 0,
|
|
|
|
totalLength: '00:00'
|
|
|
|
})
|
|
|
|
|
|
|
|
const selectedSongs = ref<Song[]>([])
|
|
|
|
const showingControls = ref(false)
|
2022-04-15 17:00:08 +00:00
|
|
|
const songListControlConfig = reactive(controlsConfig)
|
2022-04-15 14:24:30 +00:00
|
|
|
const isPhone = isMobile.phone
|
|
|
|
|
|
|
|
watchEffect(() => {
|
2022-04-15 17:00:08 +00:00
|
|
|
if (!state.songs.length) {
|
2022-04-15 14:24:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
meta.songCount = state.songs.length
|
|
|
|
meta.totalLength = songStore.getFormattedLength(state.songs)
|
2022-04-15 14:24:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const getSongsToPlay = (): Song[] => (songList.value as any).getAllSongsWithSort()
|
|
|
|
const playAll = (shuffled: boolean) => playback.queueAndPlay(getSongsToPlay(), shuffled)
|
|
|
|
const playSelected = (shuffled: boolean) => playback.queueAndPlay(selectedSongs.value, shuffled)
|
|
|
|
const toggleControls = () => (showingControls.value = !showingControls.value)
|
|
|
|
|
|
|
|
eventBus.on({
|
2022-04-15 17:00:08 +00:00
|
|
|
SET_SELECTED_SONGS (songs: Song[], target: ComponentInternalInstance) {
|
2022-04-15 14:24:30 +00:00
|
|
|
target === getCurrentInstance() && (selectedSongs.value = songs)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
SongList,
|
|
|
|
SongListControls,
|
|
|
|
ControlsToggler,
|
|
|
|
songList,
|
|
|
|
state,
|
|
|
|
meta,
|
|
|
|
selectedSongs,
|
|
|
|
showingControls,
|
|
|
|
songListControlConfig,
|
|
|
|
isPhone,
|
|
|
|
playAll,
|
|
|
|
playSelected,
|
|
|
|
toggleControls
|
|
|
|
}
|
|
|
|
}
|