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

68 lines
1.5 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 = {
2015-12-13 04:42:28 +00:00
storeKey: '',
state: {
volume: 7,
notify: true,
repeatMode: 'NO_REPEAT',
showExtraPanel: true,
confirmClosing: false,
2016-01-19 11:00:23 +00:00
equalizer: {
preamp: 0,
gains: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
},
artistsViewMode: null,
albumsViewMode: null,
selectedPreset: -1,
2015-12-13 04:42:28 +00:00
},
/**
2015-12-22 17:46:54 +00:00
* Init the store.
2016-03-13 17:00:32 +00:00
*
2015-12-22 17:46:54 +00:00
* @param {Object} user The user whose preferences we are managing.
2015-12-13 04:42:28 +00:00
*/
init(user = null) {
if (!user) {
2016-03-18 04:45:12 +00:00
user = userStore.current;
2015-12-13 04:42:28 +00:00
}
this.storeKey = `preferences_${user.id}`;
extend(this.state, ls.get(this.storeKey, this.state));
2016-04-05 09:19:20 +00:00
this.setupProxy();
},
/**
* 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
},
set(key, val) {
this.state[key] = val;
this.save();
},
get(key) {
return has(this.state, key) ? this.state[key] : null;
2015-12-13 04:42:28 +00:00
},
save() {
ls.set(this.storeKey, this.state);
},
};