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

68 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-11-26 03:25:35 +00:00
import { extend, has, each } from 'lodash'
2015-12-13 04:42:28 +00:00
2016-11-26 03:25:35 +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,
2016-11-26 03:25:35 +00:00
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,
2016-11-26 03:25:35 +00:00
selectedPreset: -1
2016-06-25 16:05:24 +00:00
},
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.
*/
2016-11-26 03:25:35 +00:00
init (user = null) {
2016-06-25 16:05:24 +00:00
if (!user) {
2016-11-26 03:25:35 +00:00
user = userStore.current
2016-06-25 16:05:24 +00:00
}
2015-12-13 04:42:28 +00:00
2016-11-26 03:25:35 +00:00
this.storeKey = `preferences_${user.id}`
extend(this.state, ls.get(this.storeKey, this.state))
this.setupProxy()
2016-06-25 16:05:24 +00:00
},
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.
*/
2016-11-26 03:25:35 +00:00
setupProxy () {
2016-06-25 16:05:24 +00:00
each(Object.keys(this.state), key => {
Object.defineProperty(this, key, {
get: () => this.state[key],
set: (value) => {
2016-11-26 03:25:35 +00:00
this.state[key] = value
this.save()
2016-06-25 16:05:24 +00:00
},
2016-11-26 03:25:35 +00:00
configurable: true
})
})
2016-06-25 16:05:24 +00:00
},
2015-12-13 04:42:28 +00:00
2016-11-26 03:25:35 +00:00
set (key, val) {
this.state[key] = val
this.save()
2016-06-25 16:05:24 +00:00
},
2015-12-13 04:42:28 +00:00
2016-11-26 03:25:35 +00:00
get (key) {
return has(this.state, key) ? this.state[key] : null
2016-06-25 16:05:24 +00:00
},
2015-12-13 04:42:28 +00:00
2016-11-26 03:25:35 +00:00
save () {
ls.set(this.storeKey, this.state)
}
}