tab-bar.js 2.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12
import {
  setProperties
} from 'uni-shared'

const setTabBarItemProps = ['text', 'iconPath', 'selectedIconPath']

const setTabBarStyleProps = ['color', 'selectedColor', 'backgroundColor', 'borderStyle']

const setTabBarBadgeProps = ['badge', 'redDot']

function setTabBar (type, args = {}) {
  const app = getApp()
13

fxy060608's avatar
fxy060608 已提交
14
  if (app) {
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    let isTabBar = false
    const pages = getCurrentPages()
    if (pages.length) {
      if (pages[pages.length - 1].$page.meta.isTabBar) {
        isTabBar = true
      }
    } else if (app.$children[0].hasTabBar) {
      isTabBar = true
    }
    if (!isTabBar) {
      return {
        errMsg: `${type}:fail not TabBar page`
      }
    }

fxy060608's avatar
fxy060608 已提交
30 31 32 33
    const {
      index
    } = args
    const tabBar = app.$children[0].tabBar
34 35 36 37 38
    if (index >= __uniConfig.tabBar.list.length) {
      return {
        errMsg: `${type}:fail tabbar item not found`
      }
    }
fxy060608's avatar
fxy060608 已提交
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    switch (type) {
      case 'showTabBar':
        app.$children[0].hideTabBar = false
        break
      case 'hideTabBar':
        app.$children[0].hideTabBar = true
        break
      case 'setTabBarItem':
        setProperties(tabBar.list[index], setTabBarItemProps, args)
        break
      case 'setTabBarStyle':
        setProperties(tabBar, setTabBarStyleProps, args)
        break
      case 'showTabBarRedDot':
        setProperties(tabBar.list[index], setTabBarBadgeProps, {
          badge: '',
          redDot: true
        })
        break
      case 'setTabBarBadge':
        setProperties(tabBar.list[index], setTabBarBadgeProps, {
          badge: args.text,
          redDot: true
        })
        break
      case 'hideTabBarRedDot':
      case 'removeTabBarBadge':
        setProperties(tabBar.list[index], setTabBarBadgeProps, {
          badge: '',
          redDot: false
        })
        break
    }
  }
  return {}
}
export function setTabBarItem (args) {
  return setTabBar('setTabBarItem', args)
}

export function setTabBarStyle (args) {
  return setTabBar('setTabBarStyle', args)
}

export function hideTabBar (args) {
  return setTabBar('hideTabBar', args)
}

export function showTabBar (args) {
  return setTabBar('showTabBar', args)
}
export function hideTabBarRedDot (args) {
  return setTabBar('hideTabBarRedDot', args)
}

export function showTabBarRedDot (args) {
  return setTabBar('showTabBarRedDot', args)
}

export function removeTabBarBadge (args) {
  return setTabBar('removeTabBarBadge', args)
}

export function setTabBarBadge (args) {
  return setTabBar('setTabBarBadge', args)
104
}