'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import {
  isPage
} from 'uni-helpers/index'
 
import {
  initEvents,
  processEvent
} from './events'
 
import initBehaviors from './behaviors'
 
import {
  createComponentDescriptor
} from './wxs/component-descriptor'
 
function pageMounted () {
  // 通知 Service,View 层已 ready
  UniViewJSBridge.publishHandler('onPageReady', {}, this.$page.id)
}
/**
 * View 层 Vue 插件
 * 1.init events
 * 2.$trigger
 * 3.$handleProxy
 */
export default {
  install (Vue, {
    routes
  } = {}) {
    initEvents()
 
    const findUniTarget = function ($event, $el) {
      let target = $event.target
      for (; target && target !== $el; target = target.parentNode) {
        if (target.tagName && target.tagName.indexOf('UNI-') === 0) {
          break
        }
      }
      return target
    }
 
    Vue.prototype.$handleEvent = function ($event) {
      if ($event instanceof Event) { // 未处理的 event 对象 需要对 target 校正及包装
        // 查找 uniTarget
        const target = findUniTarget($event, this.$el)
        $event = processEvent.call(this, $event.type, $event, {}, target || $event.target, $event.currentTarget)
      }
      return $event
    }
 
    Vue.prototype.$getComponentDescriptor = function (vm, isOwnerInstance) {
      return createComponentDescriptor(vm || this, isOwnerInstance)
    }
 
    Object.defineProperty(Vue.prototype, '$ownerInstance', {
      get () {
        return this.$getComponentDescriptor(this)
      }
    })
 
    Vue.prototype.$handleWxsEvent = function ($event) {
      if ($event instanceof Event) { // 未处理的 event 对象 需要对 target 校正及包装
        const currentTarget = $event.currentTarget
        // vue component / web component
        const component = currentTarget && (currentTarget.__vue__ || currentTarget)
        const instance = currentTarget && component.$getComponentDescriptor && component.$getComponentDescriptor(component, false)
        const $origEvent = $event
        $event = processEvent.call(this, $origEvent.type, $origEvent, {}, findUniTarget($origEvent, this.$el) || $origEvent.target,
          $origEvent.currentTarget)
        $event.instance = instance
        $event.preventDefault = function () {
          return $origEvent.preventDefault()
        }
        $event.stopPropagation = function () {
          return $origEvent.stopPropagation()
        }
      }
      return $event
    }
 
    Vue.mixin({
      beforeCreate () {
        const options = this.$options
 
        const wxs = options.wxs
        if (wxs) {
          Object.keys(wxs).forEach(module => {
            this[module] = wxs[module]
          })
        }
 
        if (options.behaviors && options.behaviors.length) {
          initBehaviors(options, this)
        }
 
        if (__PLATFORM__ === 'h5' && isPage(this)) {
          options.mounted = options.mounted ? [].concat(pageMounted, options.mounted) : [pageMounted]
        }
      }
    })
  }
 
}