koel/resources/assets/js/stores/preference.js

68 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-04-05 09:19:20 +00:00
import { extend, has, each } from 'lodash';
2015-12-13 04:42:28 +00:00
2016-06-25 10:15:57 +00:00
import { userStore } from '.';
import { ls } from '../services';
2015-12-13 04:42:28 +00:00
2016-06-25 10:15:57 +00:00
export const preferenceStore = {
2016-06-25 16:05:24 +00:00
storeKey: '',
2015-12-13 04:42:28 +00:00
2016-06-25 16:05:24 +00:00
state: {
volume: 7,
notify: true,
repeatMode: 'NO_REPEAT',
showExtraPanel: true,
confirmClosing: false,
equalizer: {
preamp: 0,
gains: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
2015-12-13 04:42:28 +00:00
},
2016-06-25 16:05:24 +00:00
artistsViewMode: null,
albumsViewMode: null,
selectedPreset: -1,
},
2015-12-13 04:42:28 +00:00
2016-06-25 16:05:24 +00:00
/**
* Init the store.
*
* @param {Object} user The user whose preferences we are managing.
*/
init(user = null) {
if (!user) {
user = userStore.current;
}
2015-12-13 04:42:28 +00:00
2016-06-25 16:05:24 +00:00
this.storeKey = `preferences_${user.id}`;
extend(this.state, ls.get(this.storeKey, this.state));
this.setupProxy();
},
2016-04-05 09:19:20 +00:00
2016-06-25 16:05:24 +00:00
/**
* Proxy the state properties, so that each can be directly accessed using the key.
*/
setupProxy() {
each(Object.keys(this.state), key => {
Object.defineProperty(this, key, {
get: () => this.state[key],
set: (value) => {
this.state[key] = value;
this.save();
},
configurable: true,
});
});
},
2015-12-13 04:42:28 +00:00
2016-06-25 16:05:24 +00:00
set(key, val) {
this.state[key] = val;
this.save();
},
2015-12-13 04:42:28 +00:00
2016-06-25 16:05:24 +00:00
get(key) {
return has(this.state, key) ? this.state[key] : null;
},
2015-12-13 04:42:28 +00:00
2016-06-25 16:05:24 +00:00
save() {
ls.set(this.storeKey, this.state);
},
2015-12-13 04:42:28 +00:00
};