lifecycle.js 2.1 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 19 20 21 22
import {
  ON_REACH_BOTTOM_DISTANCE
}
  from '../../constants'

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

fxy060608's avatar
fxy060608 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
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,
42 43
    onReachBottomDistance,
    windowTop: 0, // TODO
Q
qiang 已提交
44
    windowBottom: (tabBar.indexOf(route) >= 0 && tabBar.cover) ? tabBar.height : 0
fxy060608's avatar
fxy060608 已提交
45 46 47
  }
}

fxy060608's avatar
fxy060608 已提交
48 49 50 51 52 53 54 55 56
export function initLifecycle (Vue) {
  lifecycleMixin(Vue)

  Vue.mixin({
    beforeCreate () {
      if (this.mpType === 'page') {
        this.$scope = this.$options.pageInstance
        this.$scope.$vm = this
        delete this.$options.pageInstance
fxy060608's avatar
fxy060608 已提交
57 58 59 60

        const route = this.$scope.route
        const pageId = this.$scope.$page.id
        // 通知页面已开始创建
61
        this._$vd.sendPageCreate([pageId, route, parsePageCreateOptions(this, route)])
fxy060608's avatar
fxy060608 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
      }
    },
    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 已提交
81
}