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

78 lines
2.3 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
2015-12-22 17:46:54 +00:00
import http from '../services/http';
import userStore from './user';
import preferenceStore from './preference';
import artistStore from './artist';
import songStore from './song';
import playlistStore from './playlist';
import queueStore from './queue';
import settingStore from './setting';
2015-12-13 04:42:28 +00:00
export default {
state: {
songs: [],
albums: [],
artists: [],
favorites: [],
queued: [],
interactions: [],
users: [],
settings: [],
currentUser: null,
playlists: [],
2015-12-20 12:17:35 +00:00
useLastfm: false,
allowDownload: false,
2015-12-27 14:06:10 +00:00
currentVersion: '',
latestVersion: '',
2016-01-25 10:55:00 +00:00
cdnUrl: '',
2015-12-13 04:42:28 +00:00
},
2015-12-29 01:35:22 +00:00
init(successCb = null, errorCb = null) {
2015-12-30 04:14:47 +00:00
this.reset();
2016-02-29 16:50:25 +00:00
http.get('data', response => {
2016-03-28 13:38:14 +00:00
const data = response.data;
2016-02-29 16:50:25 +00:00
// Don't allow downloading on mobile devices
data.allowDownload = data.allowDownload && !isMobile.any;
2015-12-30 04:14:47 +00:00
assign(this.state, data);
2015-12-20 12:17: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
2015-12-22 17:46:54 +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);
2016-05-03 04:08:24 +00:00
successCb && successCb();
2016-02-29 16:50:25 +00:00
}, errorCb);
2015-12-30 04:14:47 +00:00
},
2015-12-22 17:46:54 +00:00
2015-12-30 04:14:47 +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;
2015-12-30 04:14:47 +00:00
this.state.currentVersion = '';
this.state.latestVersion = '';
2016-01-25 10:55:00 +00:00
this.state.cdnUrl = '';
2015-12-13 04:42:28 +00:00
},
};