page.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. "use strict";
  2. var config = require('../../api/config');
  3. var Relations = require('../../api/relations');
  4. var _require = require('../utils'),
  5. watchShakes = _require.watchShakes,
  6. getUrl = _require.getUrl;
  7. var createNode = require('./relation');
  8. var processRelationHandle = require('./processRelation');
  9. var _require2 = require('./utils'),
  10. connectNodes = _require2.connectNodes,
  11. antmoveAction = _require2.antmoveAction;
  12. var SelectComponent = require('./selectComponent');
  13. module.exports = {
  14. processTransformationPage: function processTransformationPage(_opts, options) {
  15. _opts = Object.assign(_opts, options);
  16. _opts.onLoad = function (res) {
  17. this.selectComponentApp = new SelectComponent(this);
  18. this.selectComponentApp.connect(); // 初始化节点树
  19. createNode.call(this, null, null, null, true);
  20. processRelations(this, Relations);
  21. if (typeof options.data === 'function') {
  22. options.data = options.data();
  23. }
  24. getUrl();
  25. this.createSelectorQuery = function () {
  26. return my.createSelectorQuery();
  27. };
  28. if (options.onLoad) {
  29. options.onLoad.call(this, res);
  30. }
  31. };
  32. _opts.onReady = function (param) {
  33. var ast = null;
  34. if (this.$node) {
  35. ast = this.$node.getRootNode();
  36. }
  37. ast && processRelationNodes(ast);
  38. if (options.onReady) {
  39. options.onReady.call(this, param);
  40. }
  41. if (ast) {
  42. ast.isPageReady = true;
  43. }
  44. };
  45. _opts.onShow = function (param) {
  46. if (config.env === 'development' && config.useRuntimeLog) {
  47. watchShakes();
  48. }
  49. if (options.onShow) {
  50. options.onShow.call(this, param);
  51. }
  52. };
  53. if (options.onResize) {
  54. _opts.events = options.events || {};
  55. _opts.events.onResize = function (e) {
  56. var _e = e,
  57. size = _e.size;
  58. var windowHeight = size.windowHeight,
  59. windowWidth = size.windowWidth;
  60. var deviceOrientation = 'landscape';
  61. var resizeObj = {};
  62. if (windowHeight > windowWidth) {
  63. deviceOrientation = 'portrait';
  64. }
  65. var _my$getSystemInfoSync = my.getSystemInfoSync(),
  66. screenWidth = _my$getSystemInfoSync.screenWidth,
  67. screenHeight = _my$getSystemInfoSync.screenHeight;
  68. size.screenWidth = screenWidth;
  69. size.screenHeight = screenHeight;
  70. resizeObj = {
  71. deviceOrientation: deviceOrientation,
  72. size: size
  73. };
  74. /**
  75. * 组件所在的页面尺寸变化时执行
  76. */
  77. if (this.$node && Array.isArray(this.$node.$children)) {
  78. this.$node.$children.forEach(function (c) {
  79. if (c.$self.antmovePageLifetimes) {
  80. c.$self.antmovePageLifetimes(e = resizeObj);
  81. }
  82. });
  83. }
  84. options.onResize(e = resizeObj);
  85. };
  86. }
  87. _opts.antmoveAction = antmoveAction;
  88. }
  89. };
  90. function processRelationNodes() {
  91. var ast = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  92. var $nodes = ast.$nodes;
  93. /**
  94. * componentNodes onPageReady
  95. */
  96. Object.keys($nodes).forEach(function (item) {
  97. var node = $nodes[item];
  98. connectNodes(node, ast);
  99. if (node.$self && typeof node.$self.onPageReady === 'function') {
  100. node.$self.onPageReady();
  101. }
  102. });
  103. ast.mountedHandles.forEach(function (fn) {
  104. fn();
  105. });
  106. ast.mountedHandles = [];
  107. }
  108. function processRelations(ctx) {
  109. var relationInfo = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  110. var route = ctx.route;
  111. route = route.replace(/\/node_modules\/[a-z-]+\/[a-z-]+/, '');
  112. if (route[0] !== '/') {
  113. route = "/".concat(route);
  114. }
  115. var info = relationInfo[route] || relationInfo[route.substring(1)];
  116. if (info) {
  117. processRelationHandle(info, function (node) {
  118. var id = node.$id;
  119. if (id === 'saveChildRef0') {
  120. ctx[id] = function () {};
  121. node.$index = 0;
  122. node.$route = route;
  123. createNode.call(ctx, ctx, null, node);
  124. return false;
  125. }
  126. ctx[id] = function (ref) {
  127. if (!ref) {
  128. return false;
  129. }
  130. ctx.$antmove = ctx.$antmove || {};
  131. if (ctx.$antmove[id] === undefined) {
  132. ctx.$antmove[id] = 0;
  133. } else {
  134. ctx.$antmove[id] += 1;
  135. }
  136. ctx.selectComponentApp.preProcesscomponents(ref);
  137. node.$index = ctx.$antmove[id];
  138. node.$route = route;
  139. createNode.call(ctx, ref, null, node);
  140. };
  141. });
  142. } else {
  143. console.warn('Missing nodes relation of ', route);
  144. }
  145. }