nodes.js 5.6 KB

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