index.js 1.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import {
  isPage
fxy060608's avatar
fxy060608 已提交
3
} from 'uni-helpers/index'
fxy060608's avatar
fxy060608 已提交
4 5 6 7 8 9

import {
  initEvents,
  processEvent
} from './events'

fxy060608's avatar
fxy060608 已提交
10 11
import initBehaviors from './behaviors'

fxy060608's avatar
fxy060608 已提交
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
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()

    Vue.prototype.$handleEvent = function ($event) {
      if ($event instanceof Event) { // 未处理的 event 对象 需要对 target 校正及包装
        // 查找 uniTarget
        let target = $event.target
        const $el = this.$el
        for (; target && target !== $el; target = target.parentNode) {
          if (target.tagName && target.tagName.indexOf('UNI-') === 0) {
            break
          }
        }
        $event = processEvent.call(this, $event.type, $event, {}, target || $event.target, $event.currentTarget)
fxy060608's avatar
fxy060608 已提交
39
      }
fxy060608's avatar
fxy060608 已提交
40 41 42 43 44 45
      return $event
    }

    Vue.mixin({
      beforeCreate () {
        const options = this.$options
fxy060608's avatar
fxy060608 已提交
46 47 48 49 50 51 52

        const wxs = options.wxs
        if (wxs) {
          Object.keys(wxs).forEach(module => {
            this[module] = wxs[module]
          })
        }
fxy060608's avatar
fxy060608 已提交
53 54 55 56 57

        if (options.behaviors && options.behaviors.length) {
          initBehaviors(options, this)
        }

fxy060608's avatar
fxy060608 已提交
58 59 60 61 62 63 64 65 66
        if (isPage(this)) {
          options.mounted = options.mounted ? [].concat(pageMounted, options.mounted) : [pageMounted]
        }
      }
    })
    // TODO 跨平台时,View 层需要注入$page属性
  }

}