Page.uts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // @ts-ignore
  2. import type { Callback } from '../index.uts'
  3. // @ts-ignore
  4. import { pageGetData, pageSetData, getPageVm, getValidComponentsOrNodes } from './util.uts'
  5. export type GetDataParams = {
  6. pageId: string
  7. path?: string | null
  8. }
  9. export const getData = (params: GetDataParams, callback: Callback): void => {
  10. const page = getPageVm(params.pageId)
  11. if (page == null) {
  12. callback(null, { errMsg: 'Page.getData:fail, Page not found.' })
  13. return
  14. }
  15. const data = pageGetData(page)
  16. callback({ data }, null)
  17. }
  18. export type SetDataParams = {
  19. pageId: string
  20. data: Map<string, any | null>
  21. }
  22. export const setData = (params: SetDataParams, callback: Callback): void => {
  23. const pageId = params.pageId
  24. const page = getPageVm(pageId)
  25. if (page != null) {
  26. pageSetData(page, params.data)
  27. callback({ result: { errMsg: 'Page.setData: ok.' } }, null)
  28. } else {
  29. callback(null, { errMsg: `Page.setData:fail, Page:${pageId} is not found.` })
  30. }
  31. }
  32. export type CallMethodParams = {
  33. pageId: string
  34. method: string
  35. args: any[]
  36. }
  37. export const callMethod = (params: CallMethodParams, callback: Callback): void => {
  38. const page = getPageVm(params.pageId)
  39. if (page == null) {
  40. callback(null, { errMsg: `Page[${params.pageId}] not exists` })
  41. // @ts-ignore
  42. } else if (findVueMethod(page.$.type.type, params.method, page) == null) {
  43. callback(null, { errMsg: `Page.${params.method} not exists` })
  44. } else {
  45. const result = params.args.length > 0 ? page.$callMethod(params.method, params.args[0]) : page.$callMethod(params.method)
  46. // @ts-ignore
  47. if (result instanceof Promise<unknown>) {
  48. (result as Promise<any>).then((res: any) => {
  49. callback({ result: res }, null)
  50. }).catch((err) => {
  51. const errMsg = err instanceof Error ? err.message : err
  52. callback({ result: errMsg }, null)
  53. })
  54. } else {
  55. callback({ result }, null)
  56. }
  57. }
  58. }
  59. export type GetElementParams = {
  60. pageId: string
  61. selector: string
  62. }
  63. export const getElement = (params: GetElementParams, callback: Callback): void => {
  64. const page = getPageVm(params.pageId)
  65. if (page == null) {
  66. callback(null, { errMsg: `Page[${params.pageId}] not exists` })
  67. } else {
  68. let selector = params.selector
  69. if (selector.startsWith('uni-')) {
  70. selector = selector.replace('uni-', '')
  71. }
  72. // @ts-ignore
  73. const list: UTSJSONObject[] = []
  74. getValidComponentsOrNodes(page.$.subTree, selector, list)
  75. if (list.length > 0) {
  76. callback(list[0], null)
  77. } else {
  78. callback(null, { errMsg: `Element[${params.selector}] not exists` })
  79. }
  80. }
  81. }
  82. export const getElements = (params: GetElementParams, callback: Callback): void => {
  83. const page = getPageVm(params.pageId)
  84. if (page == null) {
  85. callback(null, { errMsg: `Page[${params.pageId}] not exists` })
  86. } else {
  87. let selector = params.selector
  88. if (selector.startsWith('uni-')) {
  89. selector = selector.replace('uni-', '')
  90. }
  91. const elements = page.$querySelectorAll(selector)
  92. // @ts-ignore
  93. const result = [] as UTSJSONObject[]
  94. elements?.forEach(element => {
  95. result.push({
  96. elementId: element.getNodeId(),
  97. tagName: element.tagName
  98. })
  99. })
  100. callback({ elements: result }, null)
  101. }
  102. }
  103. export type GetWindowPropertiesParams = {
  104. pageId: string,
  105. names: string[]
  106. }
  107. export const getWindowProperties = (params: GetWindowPropertiesParams, callback: Callback): void => {
  108. const page = getPageVm(params.pageId)
  109. if (page == null) {
  110. callback(null, { errMsg: 'Page.getWindowProperties:fail, Page not found.' })
  111. return
  112. }
  113. const document = page.$appPage!.document
  114. const rootNode = document.childNodes[0]
  115. const properties = params.names.map((name): any | null => {
  116. switch (name) {
  117. case 'document.documentElement.scrollWidth':
  118. return rootNode.scrollWidth
  119. case 'document.documentElement.scrollHeight':
  120. return rootNode.scrollHeight
  121. case 'document.documentElement.scrollTop':
  122. return rootNode.scrollTop
  123. }
  124. return null
  125. })
  126. callback({ properties }, null)
  127. }