koel/resources/assets/js/stores/queueStore.ts

153 lines
3 KiB
TypeScript
Raw Normal View History

import { difference, shuffle, union } from 'lodash'
2022-04-20 09:37:22 +00:00
import { reactive } from 'vue'
import { arrayify } from '@/utils'
2022-04-15 14:24:30 +00:00
interface QueueStoreState {
songs: Song[],
current?: Song
}
2022-04-15 14:24:30 +00:00
export const queueStore = {
state: reactive<QueueStoreState>({
songs: [] as Song[]
2022-04-20 09:37:22 +00:00
}),
2022-04-15 14:24:30 +00:00
init () {
// We don't have anything to do here yet.
// How about another song then?
//
// LITTLE WING
// -- Jimi Hendrix
//
// Well she's walking
// Through the clouds
// With a circus mind
// That's running wild
// Butterflies and zebras and moonbeams and fairy tales
2022-04-15 14:24:30 +00:00
// That's all she ever thinks about
// Riding with the wind
//
// When I'm sad
// She comes to me
// With a thousand smiles
// She gives to me free
// It's alright she said
// It's alright
// Take anything you want from me
// Anything...
},
get all () {
2022-04-15 14:24:30 +00:00
return this.state.songs
},
set all (songs: Song[]) {
this.state.songs = songs
},
get first () {
return this.all[0]
},
get last () {
return this.all[this.all.length - 1]
},
contains (song: Song) {
2022-04-15 14:24:30 +00:00
return this.all.includes(song)
},
/**
* Add song(s) to the end of the current queue.
2022-04-15 14:24:30 +00:00
*/
queue (songs: Song | Song[]) {
2022-04-15 14:24:30 +00:00
this.unqueue(songs)
2022-04-20 09:37:22 +00:00
this.all = union(this.all, arrayify(songs))
2022-04-15 14:24:30 +00:00
},
queueIfNotQueued (song: Song) {
if (!this.contains(song)) {
this.queueAfterCurrent(song)
}
},
queueToTop (songs: Song | Song[]) {
2022-04-20 09:37:22 +00:00
this.all = union(arrayify(songs), this.all)
2022-04-15 14:24:30 +00:00
},
replaceQueueWith (songs: Song | Song[]) {
2022-04-20 09:37:22 +00:00
this.all = arrayify(songs)
2022-04-15 14:24:30 +00:00
},
queueAfterCurrent (songs: Song | Song[]) {
2022-04-20 09:37:22 +00:00
songs = arrayify(songs)
2022-04-15 14:24:30 +00:00
if (!this.current || !this.all.length) {
return this.queue(songs)
}
// First we unqueue the songs to make sure there are no duplicates.
this.unqueue(songs)
const head = this.all.splice(0, this.indexOf(this.current) + 1)
this.all = head.concat(songs, this.all)
},
unqueue (songs: Song | Song[]) {
2022-04-20 09:37:22 +00:00
this.all = difference(this.all, arrayify(songs))
2022-04-15 14:24:30 +00:00
},
/**
* Move some songs to after a target.
*/
move (songs: Song | Song[], target: Song) {
2022-04-15 14:24:30 +00:00
const targetIndex = this.indexOf(target)
2022-04-20 09:37:22 +00:00
const movedSongs = arrayify(songs)
2022-04-15 14:24:30 +00:00
movedSongs.forEach(song => {
this.all.splice(this.indexOf(song), 1)
this.all.splice(targetIndex, 0, song)
})
},
clear () {
2022-04-15 14:24:30 +00:00
this.all = []
},
indexOf (song: Song) {
2022-04-15 14:24:30 +00:00
return this.all.indexOf(song)
},
get next () {
if (!this.current) {
return this.first
}
const index = this.all.map(song => song.id).indexOf(this.current.id) + 1
return index >= this.all.length ? undefined : this.all[index]
},
get previous () {
if (!this.current) {
return this.last
}
const index = this.all.map(song => song.id).indexOf(this.current.id) - 1
return index < 0 ? undefined : this.all[index]
},
get current () {
return this.state.current
},
set current (song) {
this.state.current = song
},
shuffle () {
2022-04-15 14:24:30 +00:00
this.all = shuffle(this.all)
}
}