uvue.uts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // @ts-nocheck
  2. // #ifdef APP
  3. type EchartsEventHandler = (event: UTSJSONObject)=>void
  4. // type EchartsTempResolve = (obj : UTSJSONObject) => void
  5. // type EchartsTempOptions = UTSJSONObject
  6. export class Echarts {
  7. options: UTSJSONObject = {} as UTSJSONObject
  8. context: UniWebViewElement
  9. eventMap: Map<string, EchartsEventHandler> = new Map()
  10. private temp: UTSJSONObject[] = []
  11. constructor(context: UniWebViewElement){
  12. this.context = context
  13. this.init()
  14. }
  15. init(){
  16. this.context.evalJS(`init(null, null, ${JSON.stringify({})})`)
  17. this.context.addEventListener('message', (e : UniWebViewMessageEvent) => {
  18. // event.stopPropagation()
  19. // event.preventDefault()
  20. const detail = e.detail.data[0]
  21. const file = detail.getString('file')
  22. const data = detail.get('data')
  23. const key = detail.getString('event')
  24. const options = typeof data == 'object' ? (data as UTSJSONObject).getJSON('options'): null
  25. const event = typeof data == 'object' ? (data as UTSJSONObject).getString('event'): null
  26. if (key == 'log' && data != null) {
  27. console.log(data)
  28. }
  29. if (event != null && options != null) {
  30. this.dispatchAction(event.replace(/"/g,''), options)
  31. }
  32. if(file != null){
  33. while (this.temp.length > 0) {
  34. const opt = this.temp.pop()
  35. const success = opt?.get('success')
  36. if(typeof success == 'function'){
  37. success as (res: UTSJSONObject) => void
  38. success({tempFilePath: file})
  39. }
  40. }
  41. }
  42. })
  43. }
  44. setOption(option: UTSJSONObject){
  45. this.options = option;
  46. this.context.evalJS(`setOption(${JSON.stringify([option])})`)
  47. }
  48. setOption(option: UTSJSONObject, notMerge: boolean = false, lazyUpdate: boolean = false){
  49. this.options = option;
  50. this.context.evalJS(`setOption(${JSON.stringify([option, notMerge, lazyUpdate])})`)
  51. }
  52. setOption(option: UTSJSONObject, notMerge: UTSJSONObject){
  53. this.options = option;
  54. this.context.evalJS(`setOption(${JSON.stringify([option, notMerge])})`)
  55. }
  56. getOption(): UTSJSONObject {
  57. return this.options
  58. }
  59. showLoading(){
  60. this.context.evalJS(`showLoading(${JSON.stringify([] as any[])})`);
  61. }
  62. showLoading(type: string, opts: UTSJSONObject){
  63. this.context.evalJS(`showLoading(${JSON.stringify([type, opts])})`);
  64. }
  65. hideLoading(){
  66. this.context.evalJS(`hideLoading()`);
  67. }
  68. clear(){
  69. this.context.evalJS(`clear()`);
  70. }
  71. dispose(){
  72. this.context.evalJS(`dispose()`);
  73. }
  74. resize(size:UTSJSONObject){
  75. setTimeout(()=>{
  76. this.context.evalJS(`resize(${JSON.stringify(size)})`);
  77. },0)
  78. }
  79. resize(){
  80. setTimeout(()=>{
  81. this.context.evalJS(`resize()`);
  82. },10)
  83. }
  84. on(type:string, query: any, callback: EchartsEventHandler) {
  85. const key = `${type}${JSON.stringify(query)}`
  86. if(typeof callback == 'function'){
  87. this.eventMap.set(key, callback)
  88. }
  89. this.context.evalJS(`on(${JSON.stringify([type, query])})`);
  90. console.warn('uvue 暂不支持事件')
  91. }
  92. on(type:string, callback: EchartsEventHandler) {
  93. const key = `${type}`
  94. if(typeof callback == 'function'){
  95. this.eventMap.set(key, callback)
  96. }
  97. this.context.evalJS(`on(${JSON.stringify([type])})`);
  98. console.warn('uvue 暂不支持事件')
  99. }
  100. dispatchAction(type:string, options: UTSJSONObject){
  101. const handler = this.eventMap.get(type)
  102. if(handler!=null){
  103. handler(options)
  104. }
  105. }
  106. canvasToTempFilePath(opt: UTSJSONObject){
  107. // this.context.evalJS(`on(${JSON.stringify(opt)})`);
  108. this.context.evalJS(`canvasToTempFilePath(${JSON.stringify(opt)})`);
  109. this.temp.push(opt)
  110. }
  111. }
  112. // #endif
  113. // #ifndef APP
  114. export class Echarts {
  115. constructor() {}
  116. setOption(option: UTSJSONObject): void
  117. isDisposed(): boolean;
  118. clear(): void;
  119. resize(size:UTSJSONObject): void;
  120. resize(): void;
  121. canvasToTempFilePath(opt : UTSJSONObject): void;
  122. dispose(): void;
  123. showLoading(cfg?: UTSJSONObject): void;
  124. showLoading(name?: string, cfg?: UTSJSONObject): void;
  125. hideLoading(): void;
  126. getZr(): any
  127. }
  128. // #endif