koel/resources/assets/js/stores/preference.js
2017-10-03 00:01:37 +01:00

65 lines
1.3 KiB
JavaScript

import { extend, has, each } from 'lodash'
import { userStore } from '.'
import { ls } from '@/services'
export const preferenceStore = {
storeKey: '',
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]
},
artistsViewMode: null,
albumsViewMode: null,
selectedPreset: -1,
transcodeOnMobile: false
},
/**
* Init the store.
*
* @param {Object} user The user whose preferences we are managing.
*/
init (user = null) {
if (!user) {
user = userStore.current
}
this.storeKey = `preferences_${user.id}`
extend(this.state, ls.get(this.storeKey, this.state))
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.get(key),
set: value => this.set(key, value),
configurable: true
})
})
},
set (key, val) {
this.state[key] = val
this.save()
},
get (key) {
return has(this.state, key) ? this.state[key] : null
},
save () {
ls.set(this.storeKey, this.state)
}
}