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) => {
|
|
|
|
// Use Date.now as a simple hash function, which is ok for not-synced types
|
|
|
|
const hashFunc = simpleHash ? Date.now : item => utils.hash(utils.serializeObject({
|
|
|
|
...item,
|
|
|
|
hash: undefined,
|
|
|
|
}));
|
2017-07-27 20:19:52 +00:00
|
|
|
|
2017-08-03 17:08:12 +00:00
|
|
|
function setItem(state, value) {
|
|
|
|
const item = Object.assign(empty(value.id), value);
|
2017-08-25 10:37:46 +00:00
|
|
|
if (!item.hash) {
|
|
|
|
item.hash = hashFunc(item);
|
2017-08-03 17:08:12 +00:00
|
|
|
}
|
|
|
|
Vue.set(state.itemMap, item.id, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
function patchItem(state, patch) {
|
|
|
|
const item = state.itemMap[patch.id];
|
|
|
|
if (item) {
|
|
|
|
Object.assign(item, patch);
|
2017-08-25 10:37:46 +00:00
|
|
|
item.hash = hashFunc(item);
|
2017-07-27 20:19:52 +00:00
|
|
|
Vue.set(state.itemMap, item.id, item);
|
2017-08-03 17:08:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
namespaced: true,
|
|
|
|
state: {
|
|
|
|
itemMap: {},
|
2017-07-27 20:19:52 +00:00
|
|
|
},
|
2017-08-03 17:08:12 +00:00
|
|
|
getters: {
|
|
|
|
items: state => Object.keys(state.itemMap).map(key => state.itemMap[key]),
|
2017-07-27 20:19:52 +00:00
|
|
|
},
|
2017-08-03 17:08:12 +00:00
|
|
|
mutations: {
|
|
|
|
setItem,
|
|
|
|
patchItem,
|
|
|
|
deleteItem(state, id) {
|
|
|
|
Vue.delete(state.itemMap, id);
|
|
|
|
},
|
2017-07-27 20:19:52 +00:00
|
|
|
},
|
2017-08-03 17:08:12 +00:00
|
|
|
actions: {},
|
|
|
|
};
|
|
|
|
};
|