123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- import Vue from 'vue'
- import api from '@/api'
- import {
- DATA_LEVELS,
- ID_VARIANTS,
- VARIANT_IDS,
- 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.type === 'Creation') {
- node.variant = [{ id: 0 }]
- }
- if ('creations' in node) {
- if (node.creations) {
- if (!Array.isArray(node.children)) {
- node.children = []
- }
- node.creations.forEach(item => {
- item.variant = [{ id: 0 }]
- node.children.push(item)
- })
- }
- delete node.creations
- }
- 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)
- }
- },
- 'ADD_HISTORY_ENTRIES' (state, ids) {
- for (const id of ids) {
- if (!state.history.includes(id)) {
- state.history.push(id)
- }
- }
- },
- '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, { variantId: VARIANT_IDS[variant] }).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,
- history: state => state.history.map(id => state.nodes[id])
- }
- }
|