relation.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. let id = 0
  2. const { connectNodes } = require('./utils')
  3. let astCache = {}
  4. function createAstData() {
  5. const RelationAst = {
  6. $refNodes: {},
  7. $nodes: {},
  8. $page: null,
  9. current: null,
  10. createArray: [],
  11. destoryArray: [],
  12. mountedHandles: [],
  13. componentNodes: {},
  14. }
  15. return RelationAst
  16. }
  17. function CreateNode(ctx) {
  18. this.$self = ctx
  19. ctx.$node = this
  20. this.$id = id++
  21. this.$children = []
  22. }
  23. CreateNode.prototype = {
  24. getRootNode() {
  25. const ctx = this.$self
  26. const cacheId = ctx.$page ? ctx.$page.$id : ctx.$id
  27. return astCache[cacheId]
  28. },
  29. setParent(parent) {
  30. this.$parent = parent
  31. parent.appendChild(this)
  32. },
  33. appendChildren() {
  34. this.$children
  35. .forEach((child) => {
  36. this.appendChild(child)
  37. })
  38. },
  39. destory() {
  40. const index = this.$relationNode.$index
  41. this.$parent.$children.splice(index, 1)
  42. },
  43. appendChild(child) {
  44. this.$children.push(child)
  45. child.$parent = this
  46. },
  47. removeChild(child) {
  48. this.$children = this.$children
  49. .filter((el) => {
  50. return el.$id !== child.$id
  51. })
  52. },
  53. }
  54. module.exports = function(node, cb = () => {}, relationNode, bool = false, _bool = false) {
  55. let RelationAst = {}
  56. const cacheId = this.$page ? this.$page.$id : this.$id
  57. if (_bool) {
  58. return astCache[cacheId]
  59. }
  60. if (bool || !astCache[cacheId]) {
  61. astCache[cacheId] = createAstData()
  62. return astCache[cacheId]
  63. }
  64. let _relationData = {}
  65. function initData(isComponent = false) {
  66. let _ctx = this
  67. _relationData = createAstData()
  68. if (isComponent) {
  69. _ctx = this.$page
  70. }
  71. _ctx.$antmove = _ctx.$antmove || {}
  72. _ctx.$antmove.relationData = _relationData
  73. _ctx.$antmove.astCache = astCache
  74. }
  75. if (!this.$page) {
  76. initData.call(this)
  77. } else {
  78. if (!this.$page.$antmove
  79. || !this.$page.$antmove.relationData) {
  80. initData.call(this, true)
  81. }
  82. _relationData = this.$page.$antmove.relationData
  83. astCache = this.$page.$antmove.astCache
  84. }
  85. RelationAst = astCache[cacheId]
  86. const wrapNode = new CreateNode(node)
  87. const route = relationNode.$route
  88. RelationAst.$page = wrapNode
  89. /**
  90. * component
  91. */
  92. wrapNode.$relationNode = relationNode
  93. RelationAst.$nodes[node.$id] = wrapNode
  94. RelationAst.$refNodes[route] = RelationAst.$refNodes[route] || {}
  95. const componentNodes = RelationAst.$refNodes[route]
  96. RelationAst.$refNodes[route][relationNode.$id] = RelationAst.$refNodes[route][relationNode.$id] || []
  97. componentNodes[relationNode.$id].push(wrapNode)
  98. if (RelationAst.isPageReady) {
  99. setTimeout(() => {
  100. connectNodes(wrapNode, RelationAst)
  101. RelationAst.mountedHandles
  102. .forEach((fn) => {
  103. if (wrapNode.$parent) {
  104. fn()
  105. } else {
  106. setTimeout(() => {
  107. fn()
  108. }, 0)
  109. }
  110. })
  111. RelationAst.mountedHandles = []
  112. }, 0)
  113. }
  114. cb && cb(RelationAst)
  115. return RelationAst
  116. }