Stackedit/src/store/locationTemplate.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-04-27 14:37:05 +00:00
import moduleTemplate from './moduleTemplate';
import providerRegistry from '../services/providers/common/providerRegistry';
const addToGroup = (groups, item) => {
const list = groups[item.fileId] || [];
list.push(item);
groups[item.fileId] = list;
};
export default (empty) => {
const module = moduleTemplate(empty);
module.getters = {
...module.getters,
2018-05-13 13:27:33 +00:00
groupedByFileId: (state, { items }) => {
2018-04-27 14:37:05 +00:00
const groups = {};
2018-05-13 13:27:33 +00:00
items.forEach(item => addToGroup(groups, item));
2018-04-27 14:37:05 +00:00
return groups;
},
2018-05-13 13:27:33 +00:00
filteredGroupedByFileId: (state, { items }) => {
2018-04-27 14:37:05 +00:00
const groups = {};
2018-05-13 13:27:33 +00:00
items.filter((item) => {
2018-04-27 14:37:05 +00:00
// Filter items that we can't use
const provider = providerRegistry.providers[item.providerId];
return provider && provider.getToken(item);
}).forEach(item => addToGroup(groups, item));
return groups;
},
2018-05-13 13:27:33 +00:00
current: (state, { filteredGroupedByFileId }, rootState, rootGetters) => {
const locations = filteredGroupedByFileId[rootGetters['file/current'].id] || [];
2018-04-27 14:37:05 +00:00
return locations.map((location) => {
const provider = providerRegistry.providers[location.providerId];
return {
...location,
description: provider.getDescription(location),
url: provider.getUrl(location),
};
});
},
};
return module;
};