relation.js 3.5 KB

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