tab-bar.js 4.4 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
fxy060608's avatar
fxy060608 已提交
11 12 13 14 15 16 17
let config

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

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

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

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

export default {
110
  id: '0',
fxy060608's avatar
fxy060608 已提交
111 112 113 114
  init (options, clickCallback) {
    if (options && options.list.length) {
      config = options
    }
Q
qiang 已提交
115 116 117 118
    try {
      tabBar = requireNativePlugin('uni-tabview')
    } catch (error) {
      console.log(`uni.requireNativePlugin("uni-tabview") error ${error}`)
119 120 121 122 123
    }
    tabBar.onMaskClick(() => {
      maskClickCallback.forEach((callback) => {
        callback()
      })
124
    })
Q
qiang 已提交
125
    tabBar && tabBar.onClick(({ index }) => {
126
      clickCallback(config.list[index], index)
Q
qiang 已提交
127
    })
128 129 130
    tabBar && tabBar.onMidButtonClick(() => {
      publish('onTabBarMidButtonTap', {})
    })
fxy060608's avatar
fxy060608 已提交
131
  },
Q
qiang 已提交
132
  indexOf (page) {
Q
qiang 已提交
133
    const config = this.config
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
      }
    })
  },
Q
qiang 已提交
174 175 176
  get config () {
    return config || __uniConfig.tabBar
  },
fxy060608's avatar
fxy060608 已提交
177 178
  get visible () {
    return visible
179 180
  },
  get height () {
Q
qiang 已提交
181
    const config = this.config
182
    return (config && config.height ? parseFloat(config.height) : TABBAR_HEIGHT) + plus.navigator.getSafeAreaInsets().deviceBottom
Q
qiang 已提交
183 184 185
  },
  // tabBar是否遮挡内容区域
  get cover () {
Q
qiang 已提交
186
    const config = this.config
Q
qiang 已提交
187
    const array = ['extralight', 'light', 'dark']
Q
qiang 已提交
188
    return config && array.indexOf(config.blurEffect) >= 0
189 190 191 192 193 194
  },
  setStyle ({ mask }) {
    tabBar.setMask({
      color: mask
    })
  },
195
  addEventListener (name, callback) {
196 197
    maskClickCallback.push(callback)
  },
198
  removeEventListener (name, callback) {
fxy060608's avatar
fxy060608 已提交
199
    const callbackIndex = maskClickCallback.indexOf(callback)
200
    maskClickCallback.splice(callbackIndex, 1)
fxy060608's avatar
fxy060608 已提交
201
  }
Q
qiang 已提交
202
}