navigationBar.ts 3.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import {
2
  defineAsyncApi,
fxy060608's avatar
fxy060608 已提交
3 4 5 6 7 8 9 10 11 12 13
  API_SET_NAVIGATION_BAR_COLOR,
  API_SET_NAVIGATION_BAR_TITLE,
  API_SHOW_NAVIGATION_BAR_LOADING,
  API_HIDE_NAVIGATION_BAR_LOADING,
  API_TYPE_SET_NAVIGATION_BAR_COLOR,
  API_TYPE_SET_NAVIGATION_BAR_TITLE,
  API_TYPE_SHOW_NAVIGATION_BAR_LOADING,
  API_TYPE_HIDE_NAVIGATION_BAR_LOADING,
  SetNavigationBarColorOptions,
  SetNavigationBarColorProtocol,
  SetNavigationBarTitleProtocol,
14 15
} from '@dcloudio/uni-api'
import { getCurrentPageMeta } from '@dcloudio/uni-core'
Q
qiang 已提交
16
import { updateDocumentTitle } from '../../../helpers/useDocumentTitle'
fxy060608's avatar
fxy060608 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

function setNavigationBar(
  pageMeta: UniApp.PageRouteMeta | undefined,
  type: string,
  args: Record<string, any>,
  resolve: () => void,
  reject: (err: string) => void
) {
  if (!pageMeta) {
    return reject('page not found')
  }
  const { navigationBar } = pageMeta

  switch (type) {
    case API_SET_NAVIGATION_BAR_COLOR:
      const { frontColor, backgroundColor, animation } = args
      const { duration, timingFunc } = animation
      if (frontColor) {
fxy060608's avatar
fxy060608 已提交
35 36
        navigationBar.titleColor =
          frontColor === '#000000' ? '#000000' : '#ffffff'
fxy060608's avatar
fxy060608 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
      }
      if (backgroundColor) {
        navigationBar.backgroundColor = backgroundColor
      }
      navigationBar.duration = duration + 'ms'
      navigationBar.timingFunc = timingFunc
      // TODO
      // UniServiceJSBridge.emit('onNavigationBarChange', {
      //   textColor: frontColor === '#000000' ? '#000' : '#fff',
      //   backgroundColor: navigationBar.backgroundColor,
      // })
      break
    case API_SHOW_NAVIGATION_BAR_LOADING:
      navigationBar.loading = true
      break
    case API_HIDE_NAVIGATION_BAR_LOADING:
      navigationBar.loading = false
      break
    case API_SET_NAVIGATION_BAR_TITLE:
      const { title } = args
      navigationBar.titleText = title
Q
qiang 已提交
58 59 60 61
      if (__NODE_JS__) {
        // watch 无效,主动更新 title
        updateDocumentTitle(args.title)
      }
fxy060608's avatar
fxy060608 已提交
62 63 64 65 66 67 68 69 70 71 72 73
      // TODO isCurrentPage逻辑主要是navigationBar组件使用
      // if (isCurrentPage(page)) {
      //   // 仅当前页面
      //   document.title = title
      // }
      // TODO
      // UniServiceJSBridge.emit('onNavigationBarChange', {
      //   titleText: title,
      // })

      break
  }
74
  resolve()
fxy060608's avatar
fxy060608 已提交
75 76
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
export const setNavigationBarColor =
  defineAsyncApi<API_TYPE_SET_NAVIGATION_BAR_COLOR>(
    API_SET_NAVIGATION_BAR_COLOR,
    (args, { resolve, reject }) => {
      setNavigationBar(
        getCurrentPageMeta(),
        API_SET_NAVIGATION_BAR_COLOR,
        args,
        resolve,
        reject
      )
    },
    SetNavigationBarColorProtocol,
    SetNavigationBarColorOptions
  )
fxy060608's avatar
fxy060608 已提交
92

93 94 95 96 97 98 99
export const showNavigationBarLoading =
  defineAsyncApi<API_TYPE_SHOW_NAVIGATION_BAR_LOADING>(
    API_SHOW_NAVIGATION_BAR_LOADING,
    (args, { resolve, reject }) => {
      setNavigationBar(
        getCurrentPageMeta(),
        API_SHOW_NAVIGATION_BAR_LOADING,
fxy060608's avatar
fxy060608 已提交
100
        args || {},
101 102 103 104 105
        resolve,
        reject
      )
    }
  )
fxy060608's avatar
fxy060608 已提交
106

107 108 109 110 111 112 113
export const hideNavigationBarLoading =
  defineAsyncApi<API_TYPE_HIDE_NAVIGATION_BAR_LOADING>(
    API_HIDE_NAVIGATION_BAR_LOADING,
    (args, { resolve, reject }) => {
      setNavigationBar(
        getCurrentPageMeta(),
        API_HIDE_NAVIGATION_BAR_LOADING,
fxy060608's avatar
fxy060608 已提交
114
        args || {},
115 116 117 118 119
        resolve,
        reject
      )
    }
  )
fxy060608's avatar
fxy060608 已提交
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134
export const setNavigationBarTitle =
  defineAsyncApi<API_TYPE_SET_NAVIGATION_BAR_TITLE>(
    API_SET_NAVIGATION_BAR_TITLE,
    (args, { resolve, reject }) => {
      setNavigationBar(
        getCurrentPageMeta(),
        API_SET_NAVIGATION_BAR_TITLE,
        args,
        resolve,
        reject
      )
    },
    SetNavigationBarTitleProtocol
  )