2017-07-27 20:19:52 +00:00
|
|
|
import Vue from 'vue';
|
2017-11-10 23:39:51 +00:00
|
|
|
import utils from '../services/utils';
|
2017-08-25 10:37:46 +00:00
|
|
|
|
|
|
|
export default (empty, simpleHash = false) => {
|
2018-03-12 00:45:54 +00:00
|
|
|
// Use Date.now() as a simple hash function, which is ok for not-synced types
|
2018-04-27 14:37:05 +00:00
|
|
|
const hashFunc = simpleHash ? Date.now : item => utils.getItemHash(item);
|
2017-07-27 20:19:52 +00:00
|
|
|
|
2017-08-03 17:08:12 +00:00
|
|
|
return {
|
|
|
|
namespaced: true,
|
|
|
|
state: {
|
2018-06-21 19:16:33 +00:00
|
|
|
itemsById: {},
|
2017-07-27 20:19:52 +00:00
|
|
|
},
|
2017-08-03 17:08:12 +00:00
|
|
|
getters: {
|
2018-06-21 19:16:33 +00:00
|
|
|
items: ({ itemsById }) => Object.values(itemsById),
|
2017-07-27 20:19:52 +00:00
|
|
|
},
|
2017-08-03 17:08:12 +00:00
|
|
|
mutations: {
|
2018-03-12 00:45:54 +00:00
|
|
|
setItem(state, value) {
|
|
|
|
const item = Object.assign(empty(value.id), value);
|
2018-04-27 14:37:05 +00:00
|
|
|
if (!item.hash || !simpleHash) {
|
2018-03-12 00:45:54 +00:00
|
|
|
item.hash = hashFunc(item);
|
|
|
|
}
|
2018-06-21 19:16:33 +00:00
|
|
|
Vue.set(state.itemsById, item.id, item);
|
2018-03-12 00:45:54 +00:00
|
|
|
},
|
|
|
|
patchItem(state, patch) {
|
2018-06-21 19:16:33 +00:00
|
|
|
const item = state.itemsById[patch.id];
|
2018-03-12 00:45:54 +00:00
|
|
|
if (item) {
|
|
|
|
Object.assign(item, patch);
|
|
|
|
item.hash = hashFunc(item);
|
2018-06-21 19:16:33 +00:00
|
|
|
Vue.set(state.itemsById, item.id, item);
|
2018-03-12 00:45:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2017-08-03 17:08:12 +00:00
|
|
|
deleteItem(state, id) {
|
2018-06-21 19:16:33 +00:00
|
|
|
Vue.delete(state.itemsById, id);
|
2017-08-03 17:08:12 +00:00
|
|
|
},
|
2017-07-27 20:19:52 +00:00
|
|
|
},
|
2017-08-03 17:08:12 +00:00
|
|
|
actions: {},
|
|
|
|
};
|
|
|
|
};
|