tab-bar.js 3.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import {
Q
qiang 已提交
2
  getRealPath
fxy060608's avatar
fxy060608 已提交
3 4
} from '../api/util'

Q
qiang 已提交
5
import {
6
  publish,
Q
qiang 已提交
7 8
  requireNativePlugin
} from '../bridge'
fxy060608's avatar
fxy060608 已提交
9

10 11
const TABBAR_HEIGHT = 56

fxy060608's avatar
fxy060608 已提交
12 13 14 15 16 17 18
let config

/**
 * tabbar显示状态
 */
let visible = true

Q
qiang 已提交
19
let tabBar
fxy060608's avatar
fxy060608 已提交
20 21 22 23 24 25 26 27

/**
 * 设置角标
 * @param {string} type
 * @param {number} index
 * @param {string} text
 */
function setTabBarBadge (type, index, text) {
Q
qiang 已提交
28 29 30
  if (!tabBar) {
    return
  }
fxy060608's avatar
fxy060608 已提交
31
  if (type === 'none') {
Q
qiang 已提交
32 33 34 35 36 37
    tabBar.hideTabBarRedDot({
      index
    })
    tabBar.removeTabBarBadge({
      index
    })
fxy060608's avatar
fxy060608 已提交
38
  } else if (type === 'text') {
Q
qiang 已提交
39 40 41 42
    tabBar.setTabBarBadge({
      index,
      text
    })
fxy060608's avatar
fxy060608 已提交
43
  } else if (type === 'redDot') {
Q
qiang 已提交
44 45 46
    tabBar.showTabBarRedDot({
      index
    })
fxy060608's avatar
fxy060608 已提交
47 48 49 50 51 52
  }
}
/**
 * 动态设置 tabBar 某一项的内容
 */
function setTabBarItem (index, text, iconPath, selectedIconPath) {
Q
qiang 已提交
53 54 55
  const item = {}
  if (iconPath) {
    item.iconPath = getRealPath(iconPath)
fxy060608's avatar
fxy060608 已提交
56
  }
Q
qiang 已提交
57 58
  if (selectedIconPath) {
    item.selectedIconPath = getRealPath(selectedIconPath)
fxy060608's avatar
fxy060608 已提交
59
  }
Q
qiang 已提交
60 61 62 63
  tabBar && tabBar.setTabBarItem(Object.assign({
    index,
    text
  }, item))
fxy060608's avatar
fxy060608 已提交
64 65 66 67 68 69
}
/**
 * 动态设置 tabBar 的整体样式
 * @param {Object} style 样式
 */
function setTabBarStyle (style) {
Q
qiang 已提交
70
  tabBar && tabBar.setTabBarStyle(style)
fxy060608's avatar
fxy060608 已提交
71 72 73 74 75 76 77
}
/**
 * 隐藏 tabBar
 * @param {boolean} animation 是否需要动画效果 暂未支持
 */
function hideTabBar (animation) {
  visible = false
Q
qiang 已提交
78 79 80
  tabBar && tabBar.hideTabBar({
    animation
  })
fxy060608's avatar
fxy060608 已提交
81 82 83 84 85 86 87
}
/**
 * 显示 tabBar
 * @param {boolean} animation 是否需要动画效果 暂未支持
 */
function showTabBar (animation) {
  visible = true
Q
qiang 已提交
88 89 90
  tabBar && tabBar.showTabBar({
    animation
  })
fxy060608's avatar
fxy060608 已提交
91 92 93 94 95 96 97
}

export default {
  init (options, clickCallback) {
    if (options && options.list.length) {
      config = options
    }
Q
qiang 已提交
98 99 100 101 102 103 104 105
    try {
      tabBar = requireNativePlugin('uni-tabview')
    } catch (error) {
      console.log(`uni.requireNativePlugin("uni-tabview") error ${error}`)
    }
    tabBar && tabBar.onClick(({ index }) => {
      clickCallback(config.list[index], index, true)
    })
106 107 108
    tabBar && tabBar.onMidButtonClick(() => {
      publish('onTabBarMidButtonTap', {})
    })
fxy060608's avatar
fxy060608 已提交
109 110
  },
  switchTab (page) {
Q
qiang 已提交
111
    const itemLength = config.list.length
fxy060608's avatar
fxy060608 已提交
112 113
    if (itemLength) {
      for (let i = 0; i < itemLength; i++) {
fxy060608's avatar
fxy060608 已提交
114 115 116 117
        if (
          config.list[i].pagePath === page ||
          config.list[i].pagePath === `${page}.html`
        ) {
Q
qiang 已提交
118 119 120
          tabBar && tabBar.switchSelect({
            index: i
          })
fxy060608's avatar
fxy060608 已提交
121 122 123 124 125 126 127 128 129 130 131
          return true
        }
      }
    }
    return false
  },
  setTabBarBadge,
  setTabBarItem,
  setTabBarStyle,
  hideTabBar,
  showTabBar,
Q
qiang 已提交
132 133 134 135 136
  append (webview) {
    tabBar && tabBar.append({
      id: webview.id
    }, ({ code }) => {
      if (code !== 0) {
137
        // console.log('tab append error')
Q
qiang 已提交
138 139
        setTimeout(() => {
          this.append(webview)
Q
qiang 已提交
140
        }, 20)
Q
qiang 已提交
141 142 143
      }
    })
  },
fxy060608's avatar
fxy060608 已提交
144 145
  get visible () {
    return visible
146 147 148
  },
  get height () {
    return config && config.height ? parseFloat(config.height) : TABBAR_HEIGHT
fxy060608's avatar
fxy060608 已提交
149
  }
Q
qiang 已提交
150
}