koel/resources/assets/js/stores/artist.js

161 lines
3.8 KiB
JavaScript
Raw Normal View History

2016-02-09 04:57:08 +00:00
import Vue from 'vue';
2015-12-13 04:42:28 +00:00
import _ from 'lodash';
import config from '../config';
2016-01-15 07:27:25 +00:00
import stub from '../stubs/artist';
2015-12-13 04:42:28 +00:00
import albumStore from './album';
export default {
2016-01-15 07:27:25 +00:00
stub,
2015-12-13 04:42:28 +00:00
state: {
artists: [],
},
/**
* Init the store.
*
2016-01-17 14:26:24 +00:00
* @param {Array.<Object>} artists The array of artists we got from the server.
2015-12-13 04:42:28 +00:00
*/
2015-12-22 17:46:54 +00:00
init(artists) {
this.state.artists = artists;
2015-12-13 04:42:28 +00:00
// Traverse through artists array to get the cover and number of songs for each.
_.each(this.state.artists, artist => {
2016-03-05 09:01:12 +00:00
this.setupArtist(artist);
2015-12-13 04:42:28 +00:00
});
2015-12-22 17:46:54 +00:00
albumStore.init(this.state.artists);
2015-12-13 04:42:28 +00:00
},
2016-03-05 09:01:12 +00:00
setupArtist(artist) {
this.getImage(artist);
Vue.set(artist, 'playCount', 0);
2016-03-13 17:00:32 +00:00
Vue.set(artist, 'songCount', _.reduce(artist.albums, (count, album) => count + album.songs.length, 0));
2016-03-05 09:01:12 +00:00
Vue.set(artist, 'info', null);
return artist;
},
2016-03-18 04:45:12 +00:00
get all() {
2015-12-13 04:42:28 +00:00
return this.state.artists;
},
2016-03-05 09:01:12 +00:00
byId(id) {
2016-03-18 04:45:12 +00:00
return _.find(this.all, 'id', id);
2016-03-05 09:01:12 +00:00
},
/**
* Appends a new artist into the current collection.
*
* @param {Object} artist
*/
append(artist) {
this.state.artists.push(this.setupArtist(artist));
},
addAlbumsIntoArtist(artist, albums) {
albums = [].concat(albums);
artist.albums = _.union(artist.albums ? artist.albums : [], albums);
albums.forEach(album => {
album.artist_id = artist.id;
album.artist = artist;
});
2016-03-13 17:00:32 +00:00
artist.playCount = _.reduce(artist.albums, (count, album) => count + album.playCount, 0);
2016-03-05 09:01:12 +00:00
},
/**
* Remove album(s) from an artist.
*
* @param {Object} artist
* @param {Array.<Object>|Object} albums
*/
removeAlbumsFromArtist(artist, albums) {
artist.albums = _.difference(artist.albums, [].concat(albums));
2016-03-13 17:00:32 +00:00
artist.playCount = _.reduce(artist.albums, (count, album) => count + album.playCount, 0);
2016-03-05 09:01:12 +00:00
},
/**
* Checks if an artist is empty.
*
* @param {Object} artist
*
* @return {boolean}
*/
isArtistEmpty(artist) {
return !artist.albums.length;
},
/**
* Remove artist(s) from the store.
*
* @param {Array.<Object>|Object} artists
*/
remove(artists) {
this.state.artists = _.difference(this.state.artists, [].concat(artists));
},
2015-12-13 04:42:28 +00:00
/**
* Get all songs performed by an artist.
*
2015-12-22 09:53:03 +00:00
* @param {Object} artist
2015-12-13 04:42:28 +00:00
*
2016-01-17 14:26:24 +00:00
* @return {Array.<Object>}
2015-12-13 04:42:28 +00:00
*/
getSongsByArtist(artist) {
if (!artist.songs) {
2015-12-14 13:13:12 +00:00
artist.songs = _.reduce(artist.albums, (songs, album) => songs.concat(album.songs), []);
2015-12-13 04:42:28 +00:00
}
return artist.songs;
},
/**
2015-12-22 09:53:03 +00:00
* Get the artist's image.
2015-12-13 04:42:28 +00:00
*
2015-12-22 09:53:03 +00:00
* @param {Object} artist
2015-12-13 04:42:28 +00:00
*
2016-01-17 14:26:24 +00:00
* @return {String}
2015-12-13 04:42:28 +00:00
*/
2015-12-22 09:53:03 +00:00
getImage(artist) {
// If the artist already has a proper image, just return it.
if (artist.image) {
return artist.image;
}
// Otherwise, we try to get an image from one of their albums.
artist.image = config.unknownCover;
2015-12-13 04:42:28 +00:00
artist.albums.every(album => {
// If there's a "real" cover, use it.
2016-03-13 17:00:32 +00:00
if (album.image !== config.unknownCover) {
2015-12-22 09:53:03 +00:00
artist.image = album.cover;
2015-12-13 04:42:28 +00:00
// I want to break free.
return false;
}
});
2015-12-22 09:53:03 +00:00
return artist.image;
2015-12-13 04:42:28 +00:00
},
/**
* Get top n most-played artists.
*
* @param {Number} n
*
* @return {Array.<Object>}
*/
getMostPlayed(n = 6) {
// Only non-unknown artists with actually play count are applicable.
2016-03-28 13:38:14 +00:00
const applicable = _.filter(this.state.artists, artist => {
return artist.playCount && artist.id !== 1;
});
return _.take(_.sortByOrder(applicable, 'playCount', 'desc'), n);
},
2015-12-13 04:42:28 +00:00
};