koel/resources/assets/js/stores/shared.js

74 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-12-20 12:17:35 +00:00
import { assign } from 'lodash';
import isMobile from 'ismobilejs';
2015-12-13 04:42:28 +00:00
2016-06-25 10:15:57 +00:00
import { http } from '../services';
import { userStore, preferenceStore, artistStore, songStore, playlistStore, queueStore, settingStore } from '.';
export const sharedStore = {
2016-06-25 16:05:24 +00:00
state: {
songs: [],
albums: [],
artists: [],
favorites: [],
queued: [],
interactions: [],
users: [],
settings: [],
currentUser: null,
playlists: [],
useLastfm: false,
allowDownload: false,
currentVersion: '',
latestVersion: '',
cdnUrl: '',
},
2015-12-13 04:42:28 +00:00
2016-06-27 06:11:35 +00:00
init() {
2016-06-25 16:05:24 +00:00
this.reset();
2016-06-27 06:11:35 +00:00
return new Promise((resolve, reject) => {
http.get('data', r => {
const data = r.data;
2016-02-29 16:50:25 +00:00
2016-06-27 06:11:35 +00:00
// Don't allow downloading on mobile devices
data.allowDownload = data.allowDownload && !isMobile.any;
2016-06-27 06:11:35 +00:00
assign(this.state, data);
2015-12-20 12:17:35 +00:00
2016-06-27 06:11:35 +00:00
// If this is a new user, initialize his preferences to be an empty object.
if (!this.state.currentUser.preferences) {
this.state.currentUser.preferences = {};
}
2015-12-13 04:42:28 +00:00
2016-06-27 06:11:35 +00:00
userStore.init(this.state.users, this.state.currentUser);
preferenceStore.init(this.state.preferences);
artistStore.init(this.state.artists); // This will init album and song stores as well.
songStore.initInteractions(this.state.interactions);
playlistStore.init(this.state.playlists);
queueStore.init();
settingStore.init(this.state.settings);
2015-12-22 17:46:54 +00:00
2016-06-27 06:11:35 +00:00
resolve(r)
}, r => reject(r));
});
2016-06-25 16:05:24 +00:00
},
2015-12-22 17:46:54 +00:00
2016-06-25 16:05:24 +00:00
reset() {
this.state.songs = [];
this.state.albums = [];
this.state.artists = [];
this.state.favorites = [];
this.state.queued = [];
this.state.interactions = [];
this.state.users = [];
this.state.settings = [];
this.state.currentUser = null;
this.state.playlists = [];
this.state.useLastfm = false;
this.state.allowDownload = false;
this.state.currentVersion = '';
this.state.latestVersion = '';
this.state.cdnUrl = '';
},
2015-12-13 04:42:28 +00:00
};