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

96 lines
2.2 KiB
JavaScript
Raw Normal View History

2015-12-13 04:42:28 +00:00
import _ from 'lodash';
import http from '../services/http';
import stub from '../stubs/playlist';
import songStore from './song';
export default {
stub,
state: {
playlists: [],
},
init(playlists) {
this.state.playlists = playlists;
2015-12-13 04:42:28 +00:00
_.each(this.state.playlists, this.getSongs);
},
all() {
return this.state.playlists;
},
/**
* Get all songs in a playlist.
*
2015-12-22 17:46:54 +00:00
* return {Array}
2015-12-13 04:42:28 +00:00
*/
getSongs(playlist) {
2015-12-14 13:13:12 +00:00
return (playlist.songs = songStore.byIds(playlist.songs));
2015-12-13 04:42:28 +00:00
},
2016-01-03 10:24:12 +00:00
/**
* Create a new playlist, optionally with its songs.
*
* @param {string} name Name of the playlist
* @param {Array} songs An array of song objects
* @param {Function} cb
*/
2015-12-13 04:42:28 +00:00
store(name, songs, cb = null) {
2016-01-03 10:24:12 +00:00
if (songs.length) {
// Extract the IDs from the song objects.
songs = _.pluck(songs, 'id');
}
2015-12-29 01:35:22 +00:00
http.post('playlist', { name, songs }, response => {
var playlist = response.data;
2015-12-14 04:29:16 +00:00
playlist.songs = songs;
2015-12-13 04:42:28 +00:00
this.getSongs(playlist);
this.state.playlists.push(playlist);
if (cb) {
cb();
}
});
},
delete(playlist, cb = null) {
http.delete(`playlist/${playlist.id}`, {}, () => {
this.state.playlists = _.without(this.state.playlists, playlist);
if (cb) {
cb();
}
});
},
addSongs(playlist, songs, cb = null) {
playlist.songs = _.union(playlist.songs, songs);
http.put(`playlist/${playlist.id}/sync`, { songs: _.pluck(playlist.songs, 'id') }, () => {
if (cb) {
cb();
}
});
},
removeSongs(playlist, songs, cb = null) {
playlist.songs = _.difference(playlist.songs, songs);
http.put(`playlist/${playlist.id}/sync`, { songs: _.pluck(playlist.songs, 'id') }, () => {
if (cb) {
cb();
}
});
},
update(playlist, cb = null) {
http.put(`playlist/${playlist.id}`, { name: playlist.name }, () => {
if (cb) {
cb();
}
});
},
};