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

import {
  setPullDownRefreshPageId
} from '../api/page-event'

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

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

fxy060608's avatar
fxy060608 已提交
22 23 24 25 26 27
  function onPullDownRefresh (args, pageId) {
    const page = getCurrentPages().find(page => page.$page.id === pageId)
    if (page) {
      setPullDownRefreshPageId(pageId)
      callPageHook(page, 'onPullDownRefresh')
    }
fxy060608's avatar
fxy060608 已提交
28
  }
fxy060608's avatar
fxy060608 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

  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')
  }

  function onAppEnterForeground () {
    callAppHook(getApp(), 'onShow')
    callCurrentPageHook('onShow')
  }

  function onWebInvokeAppService ({
    name,
    arg
  }, pageId) {
    if (name === 'postMessage') {
      // TODO 小程序后退、组件销毁、分享时通知
    } else {
      uni[name](arg)
    }
  }

  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 90 91 92 93
  on('onAppEnterBackground', onAppEnterBackground)
  on('onAppEnterForeground', onAppEnterForeground)

  on('onPullDownRefresh', onPullDownRefresh)

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

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

fxy060608's avatar
fxy060608 已提交
99
  on('onWebInvokeAppService', onWebInvokeAppService)
fxy060608's avatar
fxy060608 已提交
100
}