nodes.js 5.9 KB

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