event.js 1.2 KB
Newer Older
fxy060608's avatar
init v3  
fxy060608 已提交
1
import {
fxy060608's avatar
fxy060608 已提交
2 3
  vd
} from './data'
fxy060608's avatar
init v3  
fxy060608 已提交
4 5

export function initEvent (Vue) {
fxy060608's avatar
fxy060608 已提交
6 7 8 9 10
  // 部分组件内部使用了$page
  Object.defineProperty(Vue.prototype, '$page', {
    get () {
      return getCurrentPages()[0].$page
    }
fxy060608's avatar
fxy060608 已提交
11 12 13
  })

  Vue.prototype.$handleVModelEvent = function (nid, value) {
fxy060608's avatar
fxy060608 已提交
14
    vd.sendUIEvent(this._$id, nid, {
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19 20
      type: 'input',
      target: {
        value
      }
    })
  }
fxy060608's avatar
fxy060608 已提交
21

fxy060608's avatar
init v3  
fxy060608 已提交
22
  Vue.prototype.$handleViewEvent = function ($vueEvent, options) {
fxy060608's avatar
fxy060608 已提交
23
    // const isCustomEvent = $vueEvent._processed // 自定义事件已提前处理过
fxy060608's avatar
fxy060608 已提交
24
    const $event = this.$handleEvent($vueEvent)
fxy060608's avatar
init v3  
fxy060608 已提交
25
    const cid = this._$id
fxy060608's avatar
fxy060608 已提交
26
    // 当自定义组件根节点触发事件时,nid 始终为 0
fxy060608's avatar
fxy060608 已提交
27
    const nid = $vueEvent.currentTarget === this.$el ? 0 : $event.options.nid
fxy060608's avatar
fxy060608 已提交
28
    if (typeof nid === 'undefined') {
fxy060608's avatar
init v3  
fxy060608 已提交
29 30
      return console.error(`[${cid}] nid not found`)
    }
fxy060608's avatar
fxy060608 已提交
31 32

    // 移除无用属性
fxy060608's avatar
init v3  
fxy060608 已提交
33 34 35 36
    delete $event._processed
    delete $event.mp
    delete $event.preventDefault
    delete $event.stopPropagation
fxy060608's avatar
fxy060608 已提交
37
    delete $event.options
fxy060608's avatar
fxy060608 已提交
38 39
    // 实时发送,延迟的话,会导致 touch 类事件被合并,影响实际业务逻辑,比如 touchstart 中修改变量为 true,touchend 修改为 false
    vd.sendUIEvent(cid, nid, $event)
fxy060608's avatar
init v3  
fxy060608 已提交
40 41
  }
}