koel/resources/assets/js/stores/playlist.js

193 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-11-26 03:25:35 +00:00
import { each, find, map, difference, union } from 'lodash'
import NProgress from 'nprogress'
2015-12-13 04:42:28 +00:00
2016-11-26 03:25:35 +00:00
import stub from '../stubs/playlist'
import { http } from '../services'
2016-12-19 05:37:30 +00:00
import { alerts, pluralize } from '../utils'
2016-11-26 03:25:35 +00:00
import { songStore } from '.'
2015-12-13 04:42:28 +00:00
2016-06-25 10:15:57 +00:00
export const playlistStore = {
2016-06-25 16:05:24 +00:00
stub,
state: {
2016-11-26 03:25:35 +00:00
playlists: []
2016-06-25 16:05:24 +00:00
},
2016-11-26 03:25:35 +00:00
init (playlists) {
this.all = playlists
each(this.all, this.objectifySongs)
2016-06-25 16:05:24 +00:00
},
/**
* All playlists of the current user.
*
* @return {Array.<Object>}
*/
2016-11-26 03:25:35 +00:00
get all () {
return this.state.playlists
2016-06-25 16:05:24 +00:00
},
/**
* Set all playlists.
*
* @param {Array.<Object>} value
*/
2016-11-26 03:25:35 +00:00
set all (value) {
this.state.playlists = value
2016-06-25 16:05:24 +00:00
},
2016-07-10 16:56:09 +00:00
/**
* Find a playlist by its ID
*
* @param {Number} id
*
* @return {Object}
*/
2016-11-26 03:25:35 +00:00
byId (id) {
return find(this.all, { id })
2016-07-10 16:56:09 +00:00
},
2016-06-25 16:05:24 +00:00
/**
* Objectify all songs in the playlist.
* (Initially, a playlist only contain the song IDs).
*
* @param {Object} playlist
*/
2016-11-26 03:25:35 +00:00
objectifySongs (playlist) {
playlist.songs = songStore.byIds(playlist.songs)
2016-06-25 16:05:24 +00:00
},
/**
* Get all songs in a playlist.
*
* @param {Object}
*
* return {Array.<Object>}
*/
2016-11-26 03:25:35 +00:00
getSongs (playlist) {
return playlist.songs
2016-06-25 16:05:24 +00:00
},
/**
* Add a playlist/playlists into the store.
*
* @param {Array.<Object>|Object} playlists
*/
2016-11-26 03:25:35 +00:00
add (playlists) {
this.all = union(this.all, [].concat(playlists))
2016-06-25 16:05:24 +00:00
},
/**
* Remove a playlist/playlists from the store.
*
* @param {Array.<Object>|Object} playlist
*/
2016-11-26 03:25:35 +00:00
remove (playlists) {
this.all = difference(this.all, [].concat(playlists))
2016-06-25 16:05:24 +00:00
},
/**
* Create a new playlist, optionally with its songs.
*
* @param {String} name Name of the playlist
* @param {Array.<Object>} songs An array of song objects
*/
2016-11-26 03:25:35 +00:00
store (name, songs = []) {
2016-06-25 16:05:24 +00:00
if (songs.length) {
// Extract the IDs from the song objects.
2016-11-26 03:25:35 +00:00
songs = map(songs, 'id')
2016-06-25 16:05:24 +00:00
}
2016-11-26 03:25:35 +00:00
NProgress.start()
2016-06-25 16:05:24 +00:00
2016-06-27 06:11:35 +00:00
return new Promise((resolve, reject) => {
2017-01-12 16:50:00 +00:00
http.post('playlist', { name, songs }, ({ data: playlist }) => {
2016-11-26 03:25:35 +00:00
playlist.songs = songs
this.objectifySongs(playlist)
this.add(playlist)
2016-12-01 10:54:28 +00:00
alerts.success(`Created playlist &quot;${playlist.name}&quot;.`)
2016-11-26 03:25:35 +00:00
resolve(playlist)
2016-12-20 15:44:47 +00:00
}, error => reject(error))
2016-11-26 03:25:35 +00:00
})
2016-06-25 16:05:24 +00:00
},
/**
* Delete a playlist.
*
* @param {Object} playlist
*/
2016-11-26 03:25:35 +00:00
delete (playlist) {
NProgress.start()
2016-06-25 16:05:24 +00:00
2016-06-27 06:11:35 +00:00
return new Promise((resolve, reject) => {
2017-01-12 16:50:00 +00:00
http.delete(`playlist/${playlist.id}`, {}, ({ data }) => {
2016-11-26 03:25:35 +00:00
this.remove(playlist)
2016-12-01 10:54:28 +00:00
alerts.success(`Deleted playlist &quot;${playlist.name}&quot;.`)
2017-01-12 16:50:00 +00:00
resolve(data)
2016-12-20 15:44:47 +00:00
}, error => reject(error))
2016-11-26 03:25:35 +00:00
})
2016-06-25 16:05:24 +00:00
},
/**
* Add songs into a playlist.
*
* @param {Object} playlist
* @param {Array.<Object>} songs
*/
2016-11-26 03:25:35 +00:00
addSongs (playlist, songs) {
2016-06-27 06:11:35 +00:00
return new Promise((resolve, reject) => {
2016-11-26 03:25:35 +00:00
const count = playlist.songs.length
playlist.songs = union(playlist.songs, songs)
2016-06-27 06:11:35 +00:00
if (count === playlist.songs.length) {
2016-11-26 03:25:35 +00:00
resolve(playlist)
return
2016-06-27 06:11:35 +00:00
}
2016-12-01 10:54:28 +00:00
NProgress.start()
2016-12-20 15:44:47 +00:00
http.put(`playlist/${playlist.id}/sync`, { songs: map(playlist.songs, 'id') }, () => {
alerts.success(`Added ${pluralize(songs.length, 'song')} into &quot;${playlist.name}&quot;.`)
resolve(playlist)
}, error => reject(error))
2016-06-27 06:11:35 +00:00
})
2016-06-25 16:05:24 +00:00
},
/**
* Remove songs from a playlist.
*
* @param {Object} playlist
* @param {Array.<Object>} songs
*/
2016-11-26 03:25:35 +00:00
removeSongs (playlist, songs) {
NProgress.start()
2016-11-26 03:25:35 +00:00
playlist.songs = difference(playlist.songs, songs)
2016-06-25 16:05:24 +00:00
2016-06-27 06:11:35 +00:00
return new Promise((resolve, reject) => {
2016-12-20 15:44:47 +00:00
http.put(`playlist/${playlist.id}/sync`, { songs: map(playlist.songs, 'id') }, () => {
alerts.success(`Removed ${pluralize(songs.length, 'song')} from &quot;${playlist.name}&quot;.`)
resolve(playlist)
}, error => reject(error))
2016-06-27 06:11:35 +00:00
})
2016-06-25 16:05:24 +00:00
},
/**
* Update a playlist (just change its name).
*
* @param {Object} playlist
*/
2016-11-26 03:25:35 +00:00
update (playlist) {
NProgress.start()
2016-06-25 16:05:24 +00:00
2016-06-27 06:11:35 +00:00
return new Promise((resolve, reject) => {
2017-01-12 16:50:00 +00:00
http.put(
`playlist/${playlist.id}`,
{ name: playlist.name },
() => resolve(playlist),
error => reject(error)
)
2016-11-26 03:25:35 +00:00
})
}
}