tab-bar.js 4.3 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
const TABBAR_HEIGHT = 50
11
const isIOS = plus.os.name === 'iOS'
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
  }
}
/**
 * 动态设置 tabBar 某一项的内容
 */
52
function setTabBarItem (index, text, iconPath, selectedIconPath, visible, iconfont) {
53 54 55 56 57 58
  const item = {
    index
  }
  if (text !== undefined) {
    item.text = text
  }
Q
qiang 已提交
59 60
  if (iconPath) {
    item.iconPath = getRealPath(iconPath)
fxy060608's avatar
fxy060608 已提交
61
  }
Q
qiang 已提交
62 63
  if (selectedIconPath) {
    item.selectedIconPath = getRealPath(selectedIconPath)
64 65 66
  }
  if (iconfont !== undefined) {
    item.iconfont = iconfont
fxy060608's avatar
fxy060608 已提交
67
  }
68 69 70 71 72 73 74 75 76 77 78
  if (visible !== undefined) {
    item.visible = config.list[index].visible = visible
    delete item.index

    const tabbarItems = config.list.map(item => ({ visible: item.visible }))
    tabbarItems[index] = item

    tabBar && tabBar.setTabBarItems({ list: tabbarItems })
  } else {
    tabBar && tabBar.setTabBarItem(item)
  }
fxy060608's avatar
fxy060608 已提交
79 80 81 82 83 84
}
/**
 * 动态设置 tabBar 的整体样式
 * @param {Object} style 样式
 */
function setTabBarStyle (style) {
Q
qiang 已提交
85
  tabBar && tabBar.setTabBarStyle(style)
fxy060608's avatar
fxy060608 已提交
86 87 88
}
/**
 * 隐藏 tabBar
Q
qiang 已提交
89
 * @param {boolean} animation 是否需要动画效果
fxy060608's avatar
fxy060608 已提交
90 91 92
 */
function hideTabBar (animation) {
  visible = false
Q
qiang 已提交
93 94 95
  tabBar && tabBar.hideTabBar({
    animation
  })
fxy060608's avatar
fxy060608 已提交
96 97 98
}
/**
 * 显示 tabBar
Q
qiang 已提交
99
 * @param {boolean} animation 是否需要动画效果
fxy060608's avatar
fxy060608 已提交
100 101 102
 */
function showTabBar (animation) {
  visible = true
Q
qiang 已提交
103 104 105
  tabBar && tabBar.showTabBar({
    animation
  })
106 107
}

fxy060608's avatar
fxy060608 已提交
108
const maskClickCallback = []
fxy060608's avatar
fxy060608 已提交
109 110

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