# All files / src/decorators Event.ts

100% Statements 16/16
66.66% Branches 6/9
60% Functions 3/5
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    1x

     import { Doraemon } from '../instance/init'

    export type IAnyObject = WechatMiniprogram.IAnyObject
    export type Target<
      D extends IAnyObject = IAnyObject
    > = {
      /** 事件组件的 id */
      id: string
      /** 当前组件的类型 */
      tagName?: string
      /** 事件组件上由 `data-` 开头的自定义属性组成的集合 */
      dataset: D
      /** 距离页面顶部的偏移量 */
      offsetTop:E number
      /** 距离页面左边的偏移量 */
      offsetLeft: number
    }

    export interface BaseCustomEvent<
      D extends IAnyObject = IAnyObject,
      M extends IAnyObject = IAnyObject,
      C extends IAnyObject = IAnyObject,
      T extends IAnyObject = C
    > extends WechatMiniprogram.BaseEvent<M, C, T> {
      /** 额外的信息 */
      detail: D
    }

    export interface CustomEvent<
      D extends IAnyObject = IAnyObject,
      M extends IAnyObject = IAnyObject,
      C extends IAnyObject = IAnyObject,
      T extends IAnyObject = C
    > extends BaseCustomEvent<D, M, C, T> {
      /** 触发事件的源组件 */
      target: Target<T> & D
      /** 事件绑定的当前组件 */
      currentTarget: Target<C> & D
      preventDefault(): void
      stopPropagation(): void
    }

    /**
     * decorator of an event function
     *
     * @return MethodDecorator
     */
    export function Event(): MethodDecorator {
      return function (_target: Doraemon, propertyKey: string, descriptor: any) {
        const original = descriptor.value
        descriptor.value = function dispatchEvent(e) {
          const event: CustomEvent = { ...e }
          if (event) {
            event.preventDefault = function () {}
            event.stopPropagation = function () {}
            event.target = event.target || {} as Target
            event.currentTarget = event.currentTarget || event.target || {} as Target
            event.detail = event.detail || {}
            Object.assign(event.target, event.detail)
            Object.assign(event.currentTarget, event.detail)
          }
          return original.call(this, event)
        }
      }
    }