nodes.js 4.7 KB

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