lifecycle.js 2.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
import {
  hasOwn
} from 'uni-shared'

import {
  hasLifecycleHook
} from 'uni-helpers/index'

fxy060608's avatar
fxy060608 已提交
9 10 11 12 13 14
import {
  callPageHook
} from 'uni-core/service/plugins/util'

import {
  lifecycleMixin
fxy060608's avatar
init v3  
fxy060608 已提交
15
}
fxy060608's avatar
fxy060608 已提交
16 17
  from 'uni-core/service/plugins/lifecycle'

fxy060608's avatar
fxy060608 已提交
18
import {
Q
qiang 已提交
19 20
  ON_REACH_BOTTOM_DISTANCE,
  TITLEBAR_HEIGHT
fxy060608's avatar
fxy060608 已提交
21 22 23
}
  from '../../constants'

Q
qiang 已提交
24 25
import tabBar from '../tab-bar'

Q
qiang 已提交
26 27 28 29
import {
  getStatusbarHeight
} from '../../api/util'

fxy060608's avatar
fxy060608 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
function parsePageCreateOptions (vm, route) {
  const pagePath = '/' + route
  const routeOptions = __uniRoutes.find(route => route.path === pagePath)

  const windowOptions = Object.assign({}, __uniConfig.window, routeOptions.window)
  const disableScroll = windowOptions.disableScroll === true ? 1 : 0
  const onReachBottomDistance = hasOwn(windowOptions, 'onReachBottomDistance')
    ? parseInt(windowOptions.onReachBottomDistance)
    : ON_REACH_BOTTOM_DISTANCE

  const onPageScroll = hasLifecycleHook(vm.$options, 'onPageScroll') ? 1 : 0
  const onPageReachBottom = hasLifecycleHook(vm.$options, 'onReachBottom') ? 1 : 0

  return {
    disableScroll,
    onPageScroll,
    onPageReachBottom,
47
    onReachBottomDistance,
Q
qiang 已提交
48
    windowTop: windowOptions.titleNView && windowOptions.titleNView.type === 'float' ? (getStatusbarHeight() + TITLEBAR_HEIGHT) : 0,
Q
qiang 已提交
49
    windowBottom: (tabBar.indexOf(route) >= 0 && tabBar.cover) ? tabBar.height : 0
fxy060608's avatar
fxy060608 已提交
50 51 52
  }
}

fxy060608's avatar
fxy060608 已提交
53 54 55 56
export function initLifecycle (Vue) {
  lifecycleMixin(Vue)

  Vue.mixin({
fxy060608's avatar
fxy060608 已提交
57 58 59 60 61 62 63 64 65 66
    beforeCreate () {
      // TODO 临时解决方案,service 层也注入 wxs (适用于工具类)
      const options = this.$options

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

fxy060608's avatar
fxy060608 已提交
68 69 70 71
      if (this.mpType === 'page') {
        this.$scope = this.$options.pageInstance
        this.$scope.$vm = this
        delete this.$options.pageInstance
fxy060608's avatar
fxy060608 已提交
72 73 74 75

        const route = this.$scope.route
        const pageId = this.$scope.$page.id
        // 通知页面已开始创建
76
        this._$vd.sendPageCreate([pageId, route, parsePageCreateOptions(this, route)])
fxy060608's avatar
fxy060608 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
      }
    },
    created () {
      if (this.mpType === 'page') {
        callPageHook(this.$scope, 'onLoad', this.$options.pageQuery)
        callPageHook(this.$scope, 'onShow')
      }
    },
    beforeDestroy () {
      if (this.mpType === 'page') {
        callPageHook(this.$scope, 'onUnload')
      }
    },
    mounted () {
      if (this.mpType === 'page') {
        callPageHook(this.$scope, 'onReady')
      }
    }
  })
Q
qiang 已提交
96
}