nodes.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. if (node.tags) {
  62. node.tags = node.tags.map(tag => {
  63. return { ...tag, name: tag.name[0].toUpperCase() + tag.name.slice(1) }
  64. })
  65. }
  66. for (const relation of RELATIONS) {
  67. if (relation in node && node[relation]) {
  68. node[relation] = node[relation].map(subNode => {
  69. if (!(subNode.id in state.nodes)) {
  70. addNode(subNode, -1)
  71. }
  72. return state.nodes[subNode.id]
  73. })
  74. }
  75. if (node[relation] === null) {
  76. node[relation] = []
  77. }
  78. }
  79. if (node.notes) {
  80. node.notes.forEach(note => {
  81. if (note.links && note.links.length) {
  82. note.links = note.links.map(link => {
  83. if (!(link.id in state.nodes) || state.nodes[link.id].dataLevel < 1) {
  84. addNode(link, -1)
  85. }
  86. return state.nodes[link.id]
  87. })
  88. }
  89. })
  90. }
  91. if (!stateNode || stateNode.dataLevel < level) {
  92. node.dataLevel = level
  93. }
  94. if (stateNode === undefined) {
  95. Vue.set(state.nodes, node.id, node)
  96. } else {
  97. for (const key in node) {
  98. Vue.set(state.nodes[node.id], key, node[key])
  99. }
  100. }
  101. }
  102. for (const node of nodes) {
  103. addNode(node, dataLevel)
  104. }
  105. },
  106. 'ADD_HISTORY_ENTRIES' (state, ids) {
  107. for (const id of ids) {
  108. if (!state.history.includes(id)) {
  109. state.history.push(id)
  110. }
  111. }
  112. },
  113. 'UPDATE_OPTIONS_VISIBILITY' (state, visible) {
  114. state.optionsVisible = visible
  115. }
  116. },
  117. actions: {
  118. async 'GET_ALL_NODES_IDS' ({ state, commit }, { variant, dataLevel = 'initial' }) {
  119. if (state.ids[variant] === undefined) {
  120. const levels = reduceQueryLevels(DATA_LEVELS[dataLevel])
  121. await api.queryAllNodes(VARIANT_IDS[variant], levels).then(({ nodes }) => {
  122. commit('SET_ALL_NODES_IDS', [variant, nodes])
  123. commit('ADD_NODES', [nodes, DATA_LEVELS[dataLevel]])
  124. })
  125. }
  126. return state.ids[variant]
  127. },
  128. async 'GET_NODES' ({ state, commit, getters }, { ids, dataLevel = 'partial' }) {
  129. const { idsToQuery, levelsToQuery } = reduceQueryIds(ids, dataLevel, state.nodes)
  130. if (idsToQuery) {
  131. await api.queryNodes(idsToQuery, levelsToQuery).then(data => {
  132. commit('ADD_NODES', [data.nodes, DATA_LEVELS[dataLevel]])
  133. })
  134. }
  135. return getters.nodes(ids)
  136. },
  137. async 'GET_NODE' ({ state, commit, getters }, { id, dataLevel = 'full' }) {
  138. const lvl = DATA_LEVELS[dataLevel]
  139. const nodeLvl = id in state.nodes ? state.nodes[id].dataLevel : -1
  140. if (nodeLvl < lvl) {
  141. const levelsToQuery = Array(lvl - nodeLvl).fill(nodeLvl + 1).map((v, i) => v + i)
  142. await api.queryNode(id, levelsToQuery).then(data => {
  143. commit('ADD_NODES', [[data.node], lvl])
  144. })
  145. }
  146. return getters.node(id)
  147. }
  148. },
  149. getters: {
  150. allNodes: state => state.nodes,
  151. // Args getters
  152. nodes: state => ids => ids.map(id => state.nodes[id]),
  153. node: state => id => state.nodes[id],
  154. optionsVisible: state => state.optionsVisible,
  155. history: state => state.history.map(id => state.nodes[id])
  156. }
  157. }