nodes.js 5.0 KB

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