on.js 2.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import {
  callAppHook,
  callPageHook
} from '../plugins/util'

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

function onError (err) {
  callAppHook(getApp(), 'onError', err)
}

function onPageNotFound (page) {
  callAppHook(getApp(), 'onPageNotFound', page)
}

function onPullDownRefresh (args, pageId) {
  const page = getCurrentPages().find(page => page.$page.id === pageId)
  if (page) {
    setPullDownRefreshPageId(pageId)
    callPageHook(page, 'onPullDownRefresh')
  }
}

fxy060608's avatar
fxy060608 已提交
26
function callCurrentPageHook (hook, args) {
fxy060608's avatar
fxy060608 已提交
27 28
  const pages = getCurrentPages()
  if (pages.length) {
fxy060608's avatar
fxy060608 已提交
29
    callPageHook(pages[pages.length - 1], hook, args)
fxy060608's avatar
fxy060608 已提交
30 31 32 33
  }
}

function createCallCurrentPageHook (hook) {
fxy060608's avatar
fxy060608 已提交
34 35
  return function (args) {
    callCurrentPageHook(hook, args)
fxy060608's avatar
fxy060608 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
  }
}

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

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

fxy060608's avatar
fxy060608 已提交
49 50 51 52 53 54 55 56 57 58 59
function onWebInvokeAppService ({
  name,
  arg
}, pageId) {
  if (name === 'postMessage') {
    // TODO 小程序后退、组件销毁、分享时通知
  } else {
    uni[name](arg)
  }
}

fxy060608's avatar
fxy060608 已提交
60 61 62 63 64 65 66 67 68 69 70
export default function initOn (on) {
  on('onError', onError)
  on('onPageNotFound', onPageNotFound)

  on('onAppEnterBackground', onAppEnterBackground)
  on('onAppEnterForeground', onAppEnterForeground)

  on('onPullDownRefresh', onPullDownRefresh)

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

Q
qiang 已提交
72 73 74 75
  on('onNavigationBarSearchInputChanged', createCallCurrentPageHook('onNavigationBarSearchInputChanged'))
  on('onNavigationBarSearchInputConfirmed', createCallCurrentPageHook('onNavigationBarSearchInputConfirmed'))
  on('onNavigationBarSearchInputClicked', createCallCurrentPageHook('onNavigationBarSearchInputClicked'))

fxy060608's avatar
fxy060608 已提交
76
  on('onWebInvokeAppService', onWebInvokeAppService)
Q
qiang 已提交
77
}