nodes.js 5.8 KB

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