on.js 2.9 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 56
  function onAppEnterForeground () {
    const pages = getCurrentPages()
57 58 59
    if (pages.length === 0) {
      return
    }
60 61 62 63 64 65 66
    const page = pages[pages.length - 1]
    const args = {
      path: page.route,
      query: page.options
    }

    callAppHook(getApp(), 'onShow', args)
fxy060608's avatar
fxy060608 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    callCurrentPageHook('onShow')
  }

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

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

fxy060608's avatar
fxy060608 已提交
86 87 88
  on('onError', onError)
  on('onPageNotFound', onPageNotFound)

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

fxy060608's avatar
fxy060608 已提交
93 94 95
  on('onAppEnterBackground', onAppEnterBackground)
  on('onAppEnterForeground', onAppEnterForeground)

fxy060608's avatar
fxy060608 已提交
96
  on('onResize', onResize)
fxy060608's avatar
fxy060608 已提交
97 98 99 100
  on('onPullDownRefresh', onPullDownRefresh)

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

Q
qiang 已提交
102 103
  on('onNavigationBarSearchInputChanged', createCallCurrentPageHook('onNavigationBarSearchInputChanged'))
  on('onNavigationBarSearchInputConfirmed', createCallCurrentPageHook('onNavigationBarSearchInputConfirmed'))
104 105 106
  on('onNavigationBarSearchInputClicked', createCallCurrentPageHook('onNavigationBarSearchInputClicked'))
  on('onNavigationBarSearchInputFocusChanged', createCallCurrentPageHook('onNavigationBarSearchInputFocusChanged'))

fxy060608's avatar
fxy060608 已提交
107
  on('onWebInvokeAppService', onWebInvokeAppService)
108
}