nodes.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import Vue from 'vue'
  2. import api from '@/api'
  3. import {
  4. DATA_LEVELS,
  5. ID_VARIANTS,
  6. VARIANT_IDS,
  7. RELATIONS // FIXME REMOVE WHEN DB MAJ
  8. } from '@/store/utils'
  9. import {
  10. AllNodesOfVariant
  11. } from '@/api/queries'
  12. export default {
  13. state: {
  14. nodes: {},
  15. history: [],
  16. optionsVisible: true,
  17. // IDS
  18. ids: {
  19. // depart: undefined,
  20. // kit: undefined,
  21. // creation: undefined
  22. }
  23. },
  24. mutations: {
  25. 'SET_ALL_NODES_IDS' (state, [variant, nodes]) {
  26. Vue.set(state.ids, variant, nodes.map(({ id }) => id))
  27. },
  28. 'ADD_NODES' (state, [nodes, dataLevel]) {
  29. function addNode (node, level) {
  30. const stateNode = node.id in state.nodes ? state.nodes[node.id] : undefined
  31. if (node.type === 'Creation') {
  32. node.variant = [{ id: 0 }]
  33. }
  34. if ('creations' in node) {
  35. if (node.creations) {
  36. if (!Array.isArray(node.children)) {
  37. node.children = []
  38. }
  39. node.creations.forEach(item => {
  40. item.variant = [{ id: 0 }]
  41. node.children.push(item)
  42. })
  43. }
  44. delete node.creations
  45. }
  46. if (node.variant === null || Array.isArray(node.variant)) {
  47. node.variant = node.variant !== null ? ID_VARIANTS[node.variant[0].id] : 'dark'
  48. }
  49. if (node.type) {
  50. node.type = node.type === 'Textref' ? 'ref' : 'prod'
  51. }
  52. for (const relation of RELATIONS) {
  53. if (relation in node && node[relation]) {
  54. node[relation] = node[relation].map(subNode => {
  55. if (!(subNode.id in state.nodes)) {
  56. addNode(subNode, -1)
  57. }
  58. return state.nodes[subNode.id]
  59. })
  60. }
  61. if (node[relation] === null) {
  62. node[relation] = []
  63. }
  64. }
  65. if (node.notes) {
  66. node.notes.forEach(note => {
  67. if (note.links && note.links.length) {
  68. note.links = note.links.map(link => {
  69. if (!(link.id in state.nodes) || state.nodes[link.id].dataLevel < 1) {
  70. addNode(link, -1)
  71. }
  72. return state.nodes[link.id]
  73. })
  74. }
  75. })
  76. }
  77. if (!stateNode || stateNode.dataLevel < level) {
  78. node.dataLevel = level
  79. }
  80. if (stateNode === undefined) {
  81. Vue.set(state.nodes, node.id, node)
  82. } else {
  83. for (const key in node) {
  84. Vue.set(state.nodes[node.id], key, node[key])
  85. }
  86. }
  87. }
  88. for (const node of nodes) {
  89. addNode(node, dataLevel)
  90. }
  91. },
  92. 'ADD_HISTORY_ENTRIES' (state, ids) {
  93. for (const id of ids) {
  94. if (!state.history.includes(id)) {
  95. state.history.push(id)
  96. }
  97. }
  98. },
  99. 'UPDATE_OPTIONS_VISIBILITY' (state, visible) {
  100. state.optionsVisible = visible
  101. }
  102. },
  103. actions: {
  104. async 'GET_ALL_NODES_IDS' ({ state, commit }, variant) {
  105. if (state.ids[variant] === undefined) {
  106. // FIXME UPDATE WHEN QUERY OK
  107. // await api.query(AllNodesOfVariant, { variantId: VARIANT_IDS[variant] }).then(data => {
  108. await api.query(AllNodesOfVariant, { variantId: VARIANT_IDS[variant] }).then(data => {
  109. commit('SET_ALL_NODES_IDS', [variant, data.nodes])
  110. })
  111. }
  112. return state.ids[variant]
  113. },
  114. async 'GET_NODES' ({ state, commit, getters }, { ids, dataLevel = 'partial' }) {
  115. const lvl = DATA_LEVELS[dataLevel]
  116. const nodesIdsToQuery = []
  117. let lowestLvl = lvl
  118. for (const id of ids) {
  119. const nodeLvl = id in state.nodes ? state.nodes[id].dataLevel : -1
  120. if (nodeLvl < lvl) {
  121. nodesIdsToQuery.push(id)
  122. }
  123. if (nodeLvl < lowestLvl) {
  124. lowestLvl = nodeLvl
  125. }
  126. }
  127. if (nodesIdsToQuery.length) {
  128. const levelsToQuery = Array(lvl - lowestLvl).fill(lowestLvl + 1).map((v, i) => v + i)
  129. await api.queryNodes(nodesIdsToQuery, levelsToQuery).then(data => {
  130. commit('ADD_NODES', [data.nodes, lvl])
  131. })
  132. }
  133. return getters.nodes(ids)
  134. },
  135. async 'GET_NODE' ({ state, commit, getters }, { id, dataLevel = 'full' }) {
  136. const lvl = DATA_LEVELS[dataLevel]
  137. const nodeLvl = id in state.nodes ? state.nodes[id].dataLevel : -1
  138. if (nodeLvl < lvl) {
  139. const levelsToQuery = Array(lvl - nodeLvl).fill(nodeLvl + 1).map((v, i) => v + i)
  140. await api.queryNode(id, levelsToQuery).then(data => {
  141. commit('ADD_NODES', [[data.node], lvl])
  142. })
  143. }
  144. return getters.node(id)
  145. }
  146. },
  147. getters: {
  148. allNodes: state => state.nodes,
  149. // Args getters
  150. nodes: state => ids => ids.map(id => state.nodes[id]),
  151. node: state => id => state.nodes[id],
  152. optionsVisible: state => state.optionsVisible,
  153. history: state => state.history.map(id => state.nodes[id])
  154. }
  155. }