lib.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import _ from 'lodash'
  2. import {FunctionRegiste, LibParamType} from "../types"
  3. import {Scope} from "../Scope"
  4. export function getRegList(): FunctionRegiste[] {
  5. let regList = _.get(window, 'yvanLib.regList')
  6. if (!regList) {
  7. regList = []
  8. _.set(window, 'yvanLib.regList', regList)
  9. }
  10. return regList
  11. }
  12. export function getRegParamList(methodName: string) {
  13. let regParamList = _.get(window, 'yvanLib.regParamList')
  14. if (!regParamList) {
  15. regParamList = {}
  16. _.set(window, 'yvanLib.regParamList', regParamList)
  17. }
  18. if (!_.has(regParamList, methodName)) {
  19. regParamList[methodName] = []
  20. }
  21. return regParamList[methodName]
  22. }
  23. /**
  24. * 模拟点击按钮
  25. */
  26. export function raiseClick(buttonHandle) {
  27. if (!buttonHandle) {
  28. return false
  29. }
  30. if (buttonHandle.disabled) {
  31. // 按钮是禁止状态
  32. return false
  33. }
  34. if (buttonHandle.hidden) {
  35. // 按钮是隐藏状态
  36. return false
  37. }
  38. // 按钮点击
  39. buttonHandle.click()
  40. return true
  41. }
  42. /**
  43. * 标注系统全局函数的参数
  44. * @param title 函数名称
  45. * @param type 函数类型
  46. * @param allowEmpty 可否为空
  47. */
  48. export function LibParam(title: string, type: LibParamType, allowEmpty = false) {
  49. return function (target: any, methodName: any, paramsIndex: any) {
  50. const systemFnArgs = getRegParamList(methodName)
  51. systemFnArgs[paramsIndex] = {
  52. type,
  53. title,
  54. name: methodName,
  55. allowEmpty,
  56. }
  57. }
  58. }
  59. /**
  60. * 标注函数变成"系统全局函数"
  61. */
  62. export function Lib(registe: FunctionRegiste) {
  63. return function (target, propertyKey: string, descriptor: PropertyDescriptor) {
  64. const libList = getRegList()
  65. libList.push({
  66. ...registe,
  67. name: propertyKey,
  68. target: target[propertyKey]
  69. })
  70. if (registe.type === 'system') {
  71. _.set(window, 'yvanLib.system.' + propertyKey, target[propertyKey])
  72. } else if (registe.type === 'format') {
  73. _.set(window, 'yvanLib.format.' + propertyKey, target[propertyKey])
  74. }
  75. return target
  76. }
  77. }
  78. /**
  79. * 解析事件
  80. * 'scope.私有方法名'
  81. * 'system.系统函数名'
  82. * 'format.格式化名'
  83. */
  84. export function lookupFn(scope: Scope, event: string, errorWho?: any): Function {
  85. if (window["IS_DESIGN_MODE"]) {
  86. return Ext.emptyFn
  87. }
  88. if (!_.startsWith(event, 'scope.') &&
  89. !_.startsWith(event, 'system.') &&
  90. !_.startsWith(event, 'format.')) {
  91. console.error('无法识别的事件响应类型', event)
  92. throw new TypeError('无法识别的事件响应类型')
  93. }
  94. // 为 eval 准备环境
  95. const {system, format} = window['yvanLib']
  96. const data = scope.viewModel.data
  97. const keys: any[] = []
  98. const values: any[] = []
  99. _.forOwn(data, (value, key) => {
  100. const idxKey = key.indexOf('//')
  101. if (idxKey >= 0) {
  102. keys.push(key.substr(0, idxKey))
  103. } else {
  104. keys.push(key)
  105. }
  106. values.push(value)
  107. })
  108. const func: Function = //eval(event)
  109. Function('scope', 'system', 'format', ...keys, 'return ' + event)
  110. const funcResult = func(scope, system, format, ...values)
  111. if (typeof funcResult !== 'function') {
  112. console.error('无法识别的事件响应类型', event, func, errorWho)
  113. throw new TypeError('无法识别的事件响应类型')
  114. }
  115. return funcResult
  116. }
  117. /**
  118. * 从 ext.element 获取 scope 对象
  119. */
  120. export function lookupScope(extHandle: any): Scope {
  121. if (!extHandle) {
  122. // @ts-ignore
  123. return
  124. }
  125. if (extHandle.isScope) {
  126. return extHandle
  127. }
  128. if (extHandle.column) {
  129. // 列上的组件
  130. return extHandle.column.lookupReferenceHolder().yvanScope
  131. }
  132. if(_.get(extHandle, 'alias[0]') === 'widget.button'){
  133. // 列上的按钮
  134. // return extHandle.lookupViewModel().yvanScope
  135. }
  136. const vm = extHandle.lookupViewModel()
  137. return vm.yvanScope
  138. }