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

171 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-06-25 16:05:24 +00:00
import { each, map, difference, union, without } from 'lodash';
2016-03-11 09:55:04 +00:00
import NProgress from 'nprogress';
2015-12-13 04:42:28 +00:00
import stub from '../stubs/playlist';
2016-06-25 10:15:57 +00:00
import { http } from '../services';
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: {
playlists: [],
},
init(playlists) {
this.all = playlists;
each(this.all, this.objectifySongs);
},
/**
* All playlists of the current user.
*
* @return {Array.<Object>}
*/
get all() {
return this.state.playlists;
},
/**
* Set all playlists.
*
* @param {Array.<Object>} value
*/
set all(value) {
this.state.playlists = value;
},
/**
* Objectify all songs in the playlist.
* (Initially, a playlist only contain the song IDs).
*
* @param {Object} playlist
*/
objectifySongs(playlist) {
playlist.songs = songStore.byIds(playlist.songs);
},
/**
* Get all songs in a playlist.
*
* @param {Object}
*
* return {Array.<Object>}
*/
getSongs(playlist) {
return playlist.songs;
},
/**
* Add a playlist/playlists into the store.
*
* @param {Array.<Object>|Object} playlists
*/
add(playlists) {
this.all = union(this.all, [].concat(playlists));
},
/**
* Remove a playlist/playlists from the store.
*
* @param {Array.<Object>|Object} playlist
*/
remove(playlists) {
this.all = difference(this.all, [].concat(playlists));
},
/**
* 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-06-27 06:11:35 +00:00
store(name, songs = []) {
2016-06-25 16:05:24 +00:00
if (songs.length) {
// Extract the IDs from the song objects.
songs = map(songs, 'id');
}
NProgress.start();
2016-06-27 06:11:35 +00:00
return new Promise((resolve, reject) => {
http.post('playlist', { name, songs }, r => {
const playlist = r.data;
playlist.songs = songs;
this.objectifySongs(playlist);
this.add(playlist);
resolve(playlist);
}, r => reject(r));
2016-06-25 16:05:24 +00:00
});
},
/**
* Delete a playlist.
*
* @param {Object} playlist
*/
2016-06-27 06:11:35 +00:00
delete(playlist) {
2016-06-25 16:05:24 +00:00
NProgress.start();
2016-06-27 06:11:35 +00:00
return new Promise((resolve, reject) => {
http.delete(`playlist/${playlist.id}`, {}, r => {
this.remove(playlist);
resolve(r);
}, r => reject(r));
2016-06-25 16:05:24 +00:00
});
},
/**
* Add songs into a playlist.
*
* @param {Object} playlist
* @param {Array.<Object>} songs
*/
2016-06-27 06:11:35 +00:00
addSongs(playlist, songs) {
return new Promise((resolve, reject) => {
const count = playlist.songs.length;
playlist.songs = union(playlist.songs, songs);
if (count === playlist.songs.length) {
resolve(playlist);
return;
}
http.put(`playlist/${playlist.id}/sync`, { songs: map(playlist.songs, 'id') },
r => resolve(playlist),
r => reject(r)
);
})
2016-06-25 16:05:24 +00:00
},
/**
* Remove songs from a playlist.
*
* @param {Object} playlist
* @param {Array.<Object>} songs
*/
2016-06-27 06:11:35 +00:00
removeSongs(playlist, songs) {
2016-06-25 16:05:24 +00:00
playlist.songs = difference(playlist.songs, songs);
2016-06-27 06:11:35 +00:00
return new Promise((resolve, reject) => {
http.put(`playlist/${playlist.id}/sync`, { songs: map(playlist.songs, 'id') },
r => resolve(playlist),
r => reject(r)
);
})
2016-06-25 16:05:24 +00:00
},
/**
* Update a playlist (just change its name).
*
* @param {Object} playlist
*/
2016-06-27 06:11:35 +00:00
update(playlist) {
2016-06-25 16:05:24 +00:00
NProgress.start();
2016-06-27 06:11:35 +00:00
return new Promise((resolve, reject) => {
http.put(`playlist/${playlist.id}`, { name: playlist.name }, r => resolve(playlist), r => reject(r));
});
2016-06-25 16:05:24 +00:00
},
2015-12-13 04:42:28 +00:00
};