2016-02-09 04:57:08 +00:00
|
|
|
import Vue from 'vue';
|
2015-12-13 04:42:28 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
import utils from '../services/utils';
|
|
|
|
import stub from '../stubs/album';
|
|
|
|
import songStore from './song';
|
2016-03-05 09:01:12 +00:00
|
|
|
import artistStore from './artist';
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
stub,
|
2016-02-08 12:21:24 +00:00
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
state: {
|
2016-02-08 12:21:24 +00:00
|
|
|
albums: [stub],
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init the store.
|
2016-02-08 12:21:24 +00:00
|
|
|
*
|
2016-01-17 14:26:24 +00:00
|
|
|
* @param {Array.<Object>} artists The array of artists to extract album data from.
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
init(artists) {
|
|
|
|
// Traverse through the artists array and add their albums into our master album list.
|
|
|
|
this.state.albums = _.reduce(artists, (albums, artist) => {
|
|
|
|
// While we're doing so, for each album, we get its length
|
|
|
|
// and keep a back reference to the artist too.
|
|
|
|
_.each(artist.albums, album => {
|
2016-03-05 09:01:12 +00:00
|
|
|
this.setupAlbum(album, artist);
|
2015-12-13 04:42:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return albums.concat(artist.albums);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// Then we init the song store.
|
|
|
|
songStore.init(this.state.albums);
|
|
|
|
},
|
|
|
|
|
2016-03-05 09:01:12 +00:00
|
|
|
setupAlbum(album, artist) {
|
|
|
|
Vue.set(album, 'playCount', 0);
|
|
|
|
Vue.set(album, 'artist', artist);
|
|
|
|
Vue.set(album, 'info', null);
|
|
|
|
this.getLength(album);
|
|
|
|
|
|
|
|
return album;
|
|
|
|
},
|
|
|
|
|
2016-01-17 14:26:24 +00:00
|
|
|
/**
|
|
|
|
* Get all albums in the store.
|
2016-02-08 12:21:24 +00:00
|
|
|
*
|
2016-01-17 14:26:24 +00:00
|
|
|
* @return {Array.<Object>}
|
|
|
|
*/
|
2015-12-13 04:42:28 +00:00
|
|
|
all() {
|
|
|
|
return this.state.albums;
|
|
|
|
},
|
|
|
|
|
2016-03-05 09:01:12 +00:00
|
|
|
byId(id) {
|
|
|
|
return _.find(this.all(), 'id', id);
|
|
|
|
},
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
|
|
|
* Get the total length of an album by summing up its songs' duration.
|
|
|
|
* The length will also be converted into a H:i:s format and stored as fmtLength.
|
2016-02-08 12:21:24 +00:00
|
|
|
*
|
2016-01-07 09:03:38 +00:00
|
|
|
* @param {Object} album
|
2016-02-08 12:21:24 +00:00
|
|
|
*
|
2016-01-17 14:26:24 +00:00
|
|
|
* @return {String} The H:i:s format of the album length.
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
getLength(album) {
|
2016-02-08 12:21:24 +00:00
|
|
|
Vue.set(album, 'length', _.reduce(album.songs, (length, song) => length + song.length, 0));
|
|
|
|
Vue.set(album, 'fmtLength', utils.secondsToHis(album.length));
|
|
|
|
|
|
|
|
return album.fmtLength;
|
|
|
|
},
|
|
|
|
|
2016-03-05 09:01:12 +00:00
|
|
|
/**
|
|
|
|
* Appends a new album into the current collection.
|
|
|
|
*
|
|
|
|
* @param {Object} album
|
|
|
|
*/
|
|
|
|
append(album) {
|
|
|
|
this.state.albums.push(this.setupAlbum(album, album.artist));
|
|
|
|
album.playCount = _.reduce(album.songs, (count, song) => count + song.playCount, 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add song(s) into an album.
|
|
|
|
*
|
|
|
|
* @param {Object} album
|
|
|
|
* @param {Array.<Object>|Object} song
|
|
|
|
*/
|
|
|
|
addSongsIntoAlbum(album, songs) {
|
|
|
|
songs = [].concat(songs);
|
|
|
|
|
|
|
|
album.songs = _.union(album.songs ? album.songs : [], songs);
|
|
|
|
|
|
|
|
songs.forEach(song => {
|
|
|
|
song.album_id = album.id;
|
|
|
|
song.album = album;
|
|
|
|
});
|
|
|
|
|
|
|
|
album.playCount = _.reduce(album.songs, (count, song) => count + song.playCount, 0);
|
|
|
|
this.getLength(album);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove song(s) from an album.
|
|
|
|
*
|
|
|
|
* @param {Object} album
|
|
|
|
* @param {Array.<Object>|Object} songs
|
|
|
|
*/
|
|
|
|
removeSongsFromAlbum(album, songs) {
|
|
|
|
album.songs = _.difference(album.songs, [].concat(songs));
|
|
|
|
album.playCount = _.reduce(album.songs, (count, song) => count + song.playCount, 0);
|
|
|
|
this.getLength(album);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if an album is empty.
|
|
|
|
*
|
|
|
|
* @param {Object} album
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
isAlbumEmpty(album) {
|
|
|
|
return !album.songs.length;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove album(s) from the store.
|
|
|
|
*
|
|
|
|
* @param {Array.<Object>|Object} albums
|
|
|
|
*/
|
|
|
|
remove(albums) {
|
|
|
|
albums = [].concat(albums);
|
|
|
|
this.state.albums = _.difference(this.state.albums, albums);
|
|
|
|
|
|
|
|
// Remove from the artist as well
|
|
|
|
albums.forEach(album => {
|
|
|
|
artistStore.removeAlbumsFromArtist(album.artist, album);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-02-08 12:21:24 +00:00
|
|
|
/**
|
|
|
|
* Get top n most-played albums.
|
|
|
|
*
|
|
|
|
* @param {Number} n
|
|
|
|
*
|
|
|
|
* @return {Array.<Object>}
|
|
|
|
*/
|
|
|
|
getMostPlayed(n = 6) {
|
2016-03-06 09:06:28 +00:00
|
|
|
// Only non-unknown albums with actually play count are applicable.
|
|
|
|
var applicable = _.filter(this.state.albums, album => {
|
|
|
|
return album.playCount && album.id !== 1;
|
|
|
|
});
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-03-06 09:06:28 +00:00
|
|
|
return _.take(_.sortByOrder(applicable, 'playCount', 'desc'), n);
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
|
|
|
};
|