nodes.js 5.6 KB

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