koel/resources/assets/js/stores/album.js

121 lines
2.5 KiB
JavaScript
Raw Normal View History

/*eslint camelcase: ["error", {properties: "never"}]*/
2016-11-26 03:25:35 +00:00
import Vue from 'vue'
2016-12-17 08:10:08 +00:00
import { reduce, each, union, difference, take, filter, orderBy } from 'lodash'
2015-12-13 04:42:28 +00:00
2016-11-26 03:25:35 +00:00
import stub from '../stubs/album'
2017-04-24 06:38:25 +00:00
import { artistStore } from '.'
2015-12-13 04:42:28 +00:00
2016-06-25 10:15:57 +00:00
export const albumStore = {
2016-06-25 16:05:24 +00:00
stub,
2016-12-17 07:31:08 +00:00
cache: [],
2016-06-25 16:05:24 +00:00
state: {
2016-11-26 03:25:35 +00:00
albums: [stub]
2016-06-25 16:05:24 +00:00
},
/**
* Init the store.
*
2017-04-23 16:01:02 +00:00
* @param {Array.<Object>} albums The array of album objects
2016-06-25 16:05:24 +00:00
*/
2017-04-23 16:01:02 +00:00
init (albums) {
2016-06-25 16:05:24 +00:00
// Traverse through the artists array and add their albums into our master album list.
2017-04-23 16:01:02 +00:00
this.all = albums
each(this.all, album => this.setupAlbum(album))
2016-06-25 16:05:24 +00:00
},
2017-04-23 16:01:02 +00:00
setupAlbum (album) {
const artist = artistStore.byId(album.artist_id)
artist.albums = union(artist.albums, [album])
2016-11-26 03:25:35 +00:00
Vue.set(album, 'artist', artist)
Vue.set(album, 'info', null)
2017-04-23 16:01:02 +00:00
Vue.set(album, 'songs', [])
Vue.set(album, 'playCount', 0)
this.cache[album.id] = album
2016-06-25 16:05:24 +00:00
2016-11-26 03:25:35 +00:00
return album
2016-06-25 16:05:24 +00:00
},
/**
* Get all albums in the store.
*
* @return {Array.<Object>}
*/
2016-11-26 03:25:35 +00:00
get all () {
return this.state.albums
2016-06-25 16:05:24 +00:00
},
/**
* Set all albums.
*
* @param {Array.<Object>} value
*/
2016-11-26 03:25:35 +00:00
set all (value) {
this.state.albums = value
2016-06-25 16:05:24 +00:00
},
2016-11-26 03:25:35 +00:00
byId (id) {
2016-12-17 07:31:08 +00:00
return this.cache[id]
2016-06-25 16:05:24 +00:00
},
/**
* Add new album/albums into the current collection.
*
* @param {Array.<Object>|Object} albums
*/
2016-11-26 03:25:35 +00:00
add (albums) {
albums = [].concat(albums)
2017-01-17 07:02:19 +00:00
each(albums, album => {
this.setupAlbum(album, album.artist)
album.playCount = reduce(album.songs, (count, song) => count + song.playCount, 0)
2016-11-26 03:25:35 +00:00
})
2016-06-25 16:05:24 +00:00
2016-11-26 03:25:35 +00:00
this.all = union(this.all, albums)
2016-06-25 16:05:24 +00:00
},
2017-04-24 06:38:25 +00:00
purify () {
this.compact()
2016-06-25 16:05:24 +00:00
},
/**
2017-04-24 06:38:25 +00:00
* Remove empty albums from the store.
2016-06-25 16:05:24 +00:00
*/
2017-04-24 06:38:25 +00:00
compact () {
const emptyAlbums = filter(this.all, album => album.songs.length === 0)
if (!emptyAlbums.length) {
return
}
this.all = difference(this.all, emptyAlbums)
each(emptyAlbums, album => delete this.cache[album.id])
2016-06-25 16:05:24 +00:00
},
/**
* Get top n most-played albums.
*
* @param {Number} n
*
* @return {Array.<Object>}
*/
2016-11-26 03:25:35 +00:00
getMostPlayed (n = 6) {
2016-06-25 16:05:24 +00:00
// Only non-unknown albums with actually play count are applicable.
2017-01-17 07:02:19 +00:00
const applicable = filter(this.all, album => album.playCount && album.id !== 1)
2016-11-26 03:25:35 +00:00
return take(orderBy(applicable, 'playCount', 'desc'), n)
2016-06-25 16:05:24 +00:00
},
/**
* Get n most recently added albums.
*
* @param {Number} n
*
* @return {Array.<Object>}
*/
2016-11-26 03:25:35 +00:00
getRecentlyAdded (n = 6) {
const applicable = filter(this.all, album => album.id !== 1)
return take(orderBy(applicable, 'created_at', 'desc'), n)
}
}