on.js 2.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7
import {
  callAppHook,
  callPageHook
} from '../plugins/util'

import {
  setPullDownRefreshPageId
fxy060608's avatar
fxy060608 已提交
8
} from 'uni-platform/service/api/ui/pull-down-refresh'
9 10

import onWebInvokeAppService from 'uni-platform/service/on-web-invoke-app-service'
fxy060608's avatar
fxy060608 已提交
11

fxy060608's avatar
fxy060608 已提交
12 13 14
export default function initOn (on, {
  getApp,
  getCurrentPages
fxy060608's avatar
fxy060608 已提交
15
}) {
fxy060608's avatar
fxy060608 已提交
16 17
  function onError (err) {
    callAppHook(getApp(), 'onError', err)
fxy060608's avatar
fxy060608 已提交
18 19
  }

fxy060608's avatar
fxy060608 已提交
20 21
  function onPageNotFound (page) {
    callAppHook(getApp(), 'onPageNotFound', page)
fxy060608's avatar
fxy060608 已提交
22 23
  }

fxy060608's avatar
fxy060608 已提交
24 25
  function onResize (args, pageId) {
    const page = getCurrentPages().find(page => page.$page.id === pageId)
fxy060608's avatar
fxy060608 已提交
26
    page && callPageHook(page, 'onResize', args)
fxy060608's avatar
fxy060608 已提交
27 28
  }

fxy060608's avatar
fxy060608 已提交
29 30 31 32 33 34
  function onPullDownRefresh (args, pageId) {
    const page = getCurrentPages().find(page => page.$page.id === pageId)
    if (page) {
      setPullDownRefreshPageId(pageId)
      callPageHook(page, 'onPullDownRefresh')
    }
fxy060608's avatar
fxy060608 已提交
35
  }
fxy060608's avatar
fxy060608 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

  function callCurrentPageHook (hook, args) {
    const pages = getCurrentPages()
    if (pages.length) {
      callPageHook(pages[pages.length - 1], hook, args)
    }
  }

  function createCallCurrentPageHook (hook) {
    return function (args) {
      callCurrentPageHook(hook, args)
    }
  }

  function onAppEnterBackground () {
    callAppHook(getApp(), 'onHide')
    callCurrentPageHook('onHide')
  }

55
  function onAppEnterForeground (enterOptions) {
D
DCloud_LXH 已提交
56
    callAppHook(getApp(), 'onShow', enterOptions)
57 58 59 60
    const pages = getCurrentPages()
    if (pages.length === 0) {
      return
    }
fxy060608's avatar
fxy060608 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    callCurrentPageHook('onShow')
  }

  const routeHooks = {
    navigateTo () {
      callCurrentPageHook('onHide')
    },
    navigateBack () {
      callCurrentPageHook('onShow')
    }
  }

  function onAppRoute ({
    type
  }) {
    const routeHook = routeHooks[type]
    routeHook && routeHook()
fxy060608's avatar
fxy060608 已提交
78 79
  }

fxy060608's avatar
fxy060608 已提交
80 81 82
  on('onError', onError)
  on('onPageNotFound', onPageNotFound)

fxy060608's avatar
fxy060608 已提交
83 84 85 86
  if (__PLATFORM__ !== 'h5') { // 后续有时间,h5 平台也要迁移到 onAppRoute
    on('onAppRoute', onAppRoute)
  }

fxy060608's avatar
fxy060608 已提交
87 88 89
  on('onAppEnterBackground', onAppEnterBackground)
  on('onAppEnterForeground', onAppEnterForeground)

fxy060608's avatar
fxy060608 已提交
90
  on('onResize', onResize)
fxy060608's avatar
fxy060608 已提交
91 92 93 94
  on('onPullDownRefresh', onPullDownRefresh)

  on('onTabItemTap', createCallCurrentPageHook('onTabItemTap'))
  on('onNavigationBarButtonTap', createCallCurrentPageHook('onNavigationBarButtonTap'))
fxy060608's avatar
fxy060608 已提交
95

Q
qiang 已提交
96 97
  on('onNavigationBarSearchInputChanged', createCallCurrentPageHook('onNavigationBarSearchInputChanged'))
  on('onNavigationBarSearchInputConfirmed', createCallCurrentPageHook('onNavigationBarSearchInputConfirmed'))
98 99 100
  on('onNavigationBarSearchInputClicked', createCallCurrentPageHook('onNavigationBarSearchInputClicked'))
  on('onNavigationBarSearchInputFocusChanged', createCallCurrentPageHook('onNavigationBarSearchInputFocusChanged'))

fxy060608's avatar
fxy060608 已提交
101
  on('onWebInvokeAppService', onWebInvokeAppService)
102
}