page.js 4.0 KB

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