lib.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import _ from 'lodash'
  2. import {FunctionRegiste} from "../types";
  3. import {Scope} from "../Scope";
  4. export const LibList: FunctionRegiste[] = []
  5. export function Lib(registe: FunctionRegiste) {
  6. return function (target, propertyKey: string, descriptor: PropertyDescriptor) {
  7. LibList.push({
  8. ...registe,
  9. name: propertyKey,
  10. target: target[propertyKey]
  11. })
  12. if (registe.type === 'system') {
  13. _.set(window, 'yvanLib.system.' + propertyKey, target[propertyKey])
  14. } else if (registe.type === 'format') {
  15. _.set(window, 'yvanLib.format.' + propertyKey, target[propertyKey])
  16. }
  17. return target
  18. }
  19. }
  20. /**
  21. * 解析事件
  22. * 'scope.私有方法名'
  23. * 'system.系统函数名'
  24. * 'format.格式化名'
  25. */
  26. export function lookupFn(scope: Scope, event: string): Function {
  27. if (window["IS_DESIGN_MODE"]) {
  28. return Ext.emptyFn
  29. }
  30. // 为 eval 准备环境
  31. const {system, format} = window['yvanLib']
  32. const func: Function = eval(event)
  33. if (typeof func !== 'function') {
  34. console.error('无法识别的事件响应类型', event, func)
  35. throw new TypeError('无法识别的事件响应类型')
  36. }
  37. return func
  38. }
  39. /**
  40. * 从 ext.element 获取 scope 对象
  41. */
  42. export function lookupScope(extHandle: any): Scope {
  43. const vm = extHandle.lookupViewModel()
  44. return vm.yvanScope
  45. }