tab-bar.js 4.0 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

Q
qiang 已提交
10
import safeAreaInsets from './safe-area-insets'
11

Q
qiang 已提交
12 13
const TABBAR_HEIGHT = 50
const isIOS = plus.os.name === 'iOS'
fxy060608's avatar
fxy060608 已提交
14 15 16 17 18 19 20
let config

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

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

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

let maskClickCallback = []
fxy060608's avatar
fxy060608 已提交
98 99

export default {
100
  id: '0',
fxy060608's avatar
fxy060608 已提交
101 102 103 104
  init (options, clickCallback) {
    if (options && options.list.length) {
      config = options
    }
Q
qiang 已提交
105 106 107 108
    try {
      tabBar = requireNativePlugin('uni-tabview')
    } catch (error) {
      console.log(`uni.requireNativePlugin("uni-tabview") error ${error}`)
109 110 111 112 113 114
    }
    tabBar.onMaskClick(() => {
      maskClickCallback.forEach((callback) => {
        callback()
      })
    })
Q
qiang 已提交
115
    tabBar && tabBar.onClick(({ index }) => {
116
      clickCallback(config.list[index], index)
Q
qiang 已提交
117
    })
118 119 120
    tabBar && tabBar.onMidButtonClick(() => {
      publish('onTabBarMidButtonTap', {})
    })
fxy060608's avatar
fxy060608 已提交
121
  },
Q
qiang 已提交
122
  indexOf (page) {
Q
qiang 已提交
123
    const itemLength = config && config.list && config.list.length
fxy060608's avatar
fxy060608 已提交
124 125
    if (itemLength) {
      for (let i = 0; i < itemLength; i++) {
fxy060608's avatar
fxy060608 已提交
126 127 128 129
        if (
          config.list[i].pagePath === page ||
          config.list[i].pagePath === `${page}.html`
        ) {
Q
qiang 已提交
130
          return i
fxy060608's avatar
fxy060608 已提交
131 132 133
        }
      }
    }
Q
qiang 已提交
134 135 136 137 138 139 140 141 142 143
    return -1
  },
  switchTab (page) {
    const index = this.indexOf(page)
    if (index >= 0) {
      tabBar && tabBar.switchSelect({
        index
      })
      return true
    }
fxy060608's avatar
fxy060608 已提交
144 145 146 147 148 149 150
    return false
  },
  setTabBarBadge,
  setTabBarItem,
  setTabBarStyle,
  hideTabBar,
  showTabBar,
Q
qiang 已提交
151 152 153 154 155
  append (webview) {
    tabBar && tabBar.append({
      id: webview.id
    }, ({ code }) => {
      if (code !== 0) {
156
        // console.log('tab append error')
Q
qiang 已提交
157 158
        setTimeout(() => {
          this.append(webview)
Q
qiang 已提交
159
        }, 20)
Q
qiang 已提交
160 161 162
      }
    })
  },
fxy060608's avatar
fxy060608 已提交
163 164
  get visible () {
    return visible
165 166
  },
  get height () {
Q
qiang 已提交
167 168 169 170 171 172 173
    return (config && config.height ? parseFloat(config.height) : TABBAR_HEIGHT) + safeAreaInsets.bottom
  },
  // tabBar是否遮挡内容区域
  get cover () {
    const array = ['extralight', 'light', 'dark']
    // 设置背景颜色会失效
    return isIOS && array.indexOf(config.blurEffect) >= 0 && !config.backgroundColor
174 175 176 177 178 179
  },
  setStyle ({ mask }) {
    tabBar.setMask({
      color: mask
    })
  },
180 181 182 183 184 185
  addEventListener (name, callback) {
    maskClickCallback.push(callback)
  },
  removeEventListener (name, callback) {
    let callbackIndex = maskClickCallback.indexOf(callback)
    maskClickCallback.splice(callbackIndex, 1)
fxy060608's avatar
fxy060608 已提交
186
  }
Q
qiang 已提交
187
}