150 lines
4.1 KiB
JavaScript
150 lines
4.1 KiB
JavaScript
import Vue from 'vue'
|
|
|
|
import api from '@/api'
|
|
import {
|
|
DATA_LEVELS,
|
|
ID_VARIANTS,
|
|
RELATIONS // FIXME REMOVE WHEN DB MAJ
|
|
} from '@/store/utils'
|
|
import {
|
|
AllNodesOfVariant
|
|
} from '@/api/queries'
|
|
|
|
|
|
export default {
|
|
state: {
|
|
nodes: {},
|
|
history: [],
|
|
optionsVisible: true,
|
|
// IDS
|
|
ids: {
|
|
// depart: undefined,
|
|
// kit: undefined,
|
|
// creation: undefined
|
|
}
|
|
},
|
|
|
|
mutations: {
|
|
'SET_ALL_NODES_IDS' (state, [variant, nodes]) {
|
|
Vue.set(state.ids, variant, nodes.map(({ id }) => id))
|
|
},
|
|
|
|
'ADD_NODES' (state, [nodes, dataLevel]) {
|
|
function addNode (node, level) {
|
|
const stateNode = node.id in state.nodes ? state.nodes[node.id] : undefined
|
|
|
|
if (node.variant === null || Array.isArray(node.variant)) {
|
|
node.variant = node.variant !== null ? ID_VARIANTS[node.variant[0].id] : 'dark'
|
|
}
|
|
if (node.type) {
|
|
node.type = node.type === 'Textref' ? 'ref' : 'prod'
|
|
}
|
|
|
|
for (const relation of RELATIONS) {
|
|
if (relation in node && node[relation]) {
|
|
node[relation] = node[relation].map(subNode => {
|
|
if (!(subNode.id in state.nodes)) {
|
|
addNode(subNode, -1)
|
|
}
|
|
return state.nodes[subNode.id]
|
|
})
|
|
}
|
|
if (node[relation] === null) {
|
|
node[relation] = []
|
|
}
|
|
}
|
|
|
|
if (node.notes) {
|
|
node.notes.forEach(note => {
|
|
if (note.links && note.links.length) {
|
|
note.links = note.links.map(link => {
|
|
if (!(link.id in state.nodes) || state.nodes[link.id].dataLevel < 1) {
|
|
addNode(link, -1)
|
|
}
|
|
return state.nodes[link.id]
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
if (!stateNode || stateNode.dataLevel < level) {
|
|
node.dataLevel = level
|
|
}
|
|
if (stateNode === undefined) {
|
|
Vue.set(state.nodes, node.id, node)
|
|
} else {
|
|
for (const key in node) {
|
|
Vue.set(state.nodes[node.id], key, node[key])
|
|
}
|
|
}
|
|
}
|
|
|
|
for (const node of nodes) {
|
|
addNode(node, dataLevel)
|
|
}
|
|
},
|
|
|
|
'UPDATE_OPTIONS_VISIBILITY' (state, visible) {
|
|
state.optionsVisible = visible
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
async 'GET_ALL_NODES_IDS' ({ state, commit }, variant) {
|
|
if (state.ids[variant] === undefined) {
|
|
// FIXME UPDATE WHEN QUERY OK
|
|
// await api.query(AllNodesOfVariant, { variantId: VARIANT_IDS[variant] }).then(data => {
|
|
await api.query(AllNodesOfVariant).then(data => {
|
|
commit('SET_ALL_NODES_IDS', [variant, data.nodes])
|
|
})
|
|
}
|
|
return state.ids[variant]
|
|
},
|
|
|
|
async 'GET_NODES' ({ state, commit, getters }, { ids, dataLevel = 'partial' }) {
|
|
const lvl = DATA_LEVELS[dataLevel]
|
|
const nodesIdsToQuery = []
|
|
let lowestLvl = lvl
|
|
for (const id of ids) {
|
|
const nodeLvl = id in state.nodes ? state.nodes[id].dataLevel : -1
|
|
if (nodeLvl < lvl) {
|
|
nodesIdsToQuery.push(id)
|
|
}
|
|
if (nodeLvl < lowestLvl) {
|
|
lowestLvl = nodeLvl
|
|
}
|
|
}
|
|
|
|
if (nodesIdsToQuery.length) {
|
|
const levelsToQuery = Array(lvl - lowestLvl).fill(lowestLvl + 1).map((v, i) => v + i)
|
|
await api.queryNodes(nodesIdsToQuery, levelsToQuery).then(data => {
|
|
commit('ADD_NODES', [data.nodes, lvl])
|
|
})
|
|
}
|
|
|
|
return getters.nodes(ids)
|
|
},
|
|
|
|
async 'GET_NODE' ({ state, commit, getters }, { id, dataLevel = 'full' }) {
|
|
const lvl = DATA_LEVELS[dataLevel]
|
|
const nodeLvl = id in state.nodes ? state.nodes[id].dataLevel : -1
|
|
if (nodeLvl < lvl) {
|
|
const levelsToQuery = Array(lvl - nodeLvl).fill(nodeLvl + 1).map((v, i) => v + i)
|
|
await api.queryNode(id, levelsToQuery).then(data => {
|
|
commit('ADD_NODES', [[data.node], lvl])
|
|
})
|
|
}
|
|
|
|
return getters.node(id)
|
|
}
|
|
},
|
|
|
|
getters: {
|
|
allNodes: state => state.nodes,
|
|
// Args getters
|
|
nodes: state => ids => ids.map(id => state.nodes[id]),
|
|
node: state => id => state.nodes[id],
|
|
optionsVisible: state => state.optionsVisible
|
|
}
|
|
}
|