index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { createStore } from 'vuex'
  2. export default createStore({
  3. state: {
  4. panoData: null,
  5. haveShownStartUp: process.env.VUE_APP_CLI_MODE === 'dev' ? true : false,
  6. canStart: false,
  7. },
  8. getters: {
  9. catalogTopology: (state) => {
  10. if (!state.panoData || !state.panoData.catalogRoot || !state.panoData.catalogs) {
  11. return
  12. }
  13. //四层:root,level1(一级分类),level2(二级分类或直属于一级分类的场景), level3(场景)
  14. let root = utils.deepClone(state.panoData.catalogRoot)
  15. // 对于每个一级分类
  16. for (const itemLevel1 of root) {
  17. // 指定每个一级分类的下级
  18. itemLevel1.childrenTemp = []
  19. for (const itemLevel2Id of itemLevel1.children) {
  20. for (const catalogsItem of state.panoData.catalogs) {
  21. if (itemLevel2Id === catalogsItem.id) {
  22. const itemLevel2 = utils.deepClone(catalogsItem)
  23. itemLevel2.parentId = itemLevel1.id // 看起来,vuex getter中的数据如果存在循环引用,在通过mutation转而再存储到vuex中时,会导致调用栈溢出,原因难道是vuex mutation在深拷贝时没有考虑循环引用的情况?所以这里不进行循环引用,只记录parent的id。
  24. itemLevel2.children = []
  25. itemLevel1.childrenTemp.push(itemLevel2)
  26. // 对于每个三级元素
  27. for (const sceneItem of state.panoData.scenes) {
  28. // 如果属于上述二级分类
  29. if (itemLevel2.id === sceneItem.category /* 注意拼写!!! */) {
  30. const itemLevel3 = utils.deepClone(sceneItem)
  31. itemLevel3.parentId = itemLevel2.id // 看起来,vuex getter中的数据如果存在循环引用,在通过mutation转而再存储到vuex中时,会导致调用栈溢出,原因难道是vuex mutation在深拷贝时没有考虑循环引用的情况?所以这里不进行循环引用,只记录parent的id。
  32. itemLevel2.children.push(itemLevel3)
  33. }
  34. }
  35. }
  36. }
  37. }
  38. itemLevel1.children = itemLevel1.childrenTemp
  39. delete itemLevel1.childrenTemp
  40. }
  41. return root
  42. },
  43. },
  44. mutations: {
  45. setPanoData(state, value) {
  46. state.panoData = value
  47. },
  48. declareCanStart(state) {
  49. state.canStart = true
  50. },
  51. setHaveShownStartUp(state, value) {
  52. state.haveShownStartUp = value
  53. },
  54. },
  55. actions: {
  56. },
  57. modules: {
  58. }
  59. })