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';
|
|
|
|
|
|
|
|
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-02-08 12:21:24 +00:00
|
|
|
Vue.set(album, 'playCount', 0);
|
|
|
|
Vue.set(album, 'artist', artist);
|
2016-02-08 13:14:51 +00:00
|
|
|
Vue.set(album, 'info', null);
|
2015-12-13 04:42:28 +00:00
|
|
|
this.getLength(album);
|
|
|
|
});
|
|
|
|
|
|
|
|
return albums.concat(artist.albums);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// Then we init the song store.
|
|
|
|
songStore.init(this.state.albums);
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get top n most-played albums.
|
|
|
|
*
|
|
|
|
* @param {Number} n
|
|
|
|
*
|
|
|
|
* @return {Array.<Object>}
|
|
|
|
*/
|
|
|
|
getMostPlayed(n = 6) {
|
|
|
|
var albums = _.take(_.sortByOrder(this.state.albums, 'playCount', 'desc'), n);
|
|
|
|
|
|
|
|
// Remove those with playCount=0
|
|
|
|
_.remove(albums, album => !album.playCount);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-02-08 12:21:24 +00:00
|
|
|
return albums;
|
2015-12-13 04:42:28 +00:00
|
|
|
},
|
|
|
|
};
|