tabbar.js
2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
104
105
106
107
108
109
110
111
const defaultTabbarItemMap = {
home: {
key: 'home',
title: '首页',
class: 'fa-home',
visible: true,
page_url: '/pages/index/index',
webview_url: '',
webview_title: '首页',
},
message: {
key: 'message',
title: '资讯',
class: 'fa-newspaper-o',
visible: true,
page_url: '/pages/message/index',
webview_url: 'https://oa-dev.onwall.cn/f/futian_home/?f=f&p=news_list',
webview_title: '资讯',
},
application: {
key: 'application',
title: '应用',
class: 'fa-th-large',
visible: true,
page_url: '/pages/application/index',
webview_url: 'https://oa-dev.onwall.cn/f/futian_home/?f=f&p=futian_list',
webview_title: '应用',
},
mine: {
key: 'mine',
title: '我的',
class: 'fa-user',
visible: true,
page_url: '/pages/mine/index',
webview_url: 'https://oa-dev.onwall.cn/f/futian_home/?f=f&p=futian_list',
webview_title: '我的',
},
}
export const TABBAR_ORDER = ['home', 'message', 'application', 'mine']
const normalizeVisibleValue = (rawValue, fallbackValue = true) => {
if (typeof rawValue === 'boolean') {
return rawValue
}
if (rawValue === 1 || rawValue === '1') {
return true
}
if (rawValue === 0 || rawValue === '0') {
return false
}
return fallbackValue
}
export const getDefaultTabbarItem = (key) => {
const normalizedKey = String(key || '').trim()
const fallbackItem = defaultTabbarItemMap[normalizedKey]
if (!fallbackItem) {
return null
}
return {
...fallbackItem,
}
}
export const getDefaultTabbarItems = () => (
TABBAR_ORDER
.map((key) => getDefaultTabbarItem(key))
.filter(Boolean)
)
export const normalizeTabbarItem = (rawItem = {}) => {
const normalizedKey = String(rawItem.key || rawItem.name || '').trim()
const fallbackItem = getDefaultTabbarItem(normalizedKey)
if (!fallbackItem) {
return null
}
return {
...fallbackItem,
...rawItem,
key: fallbackItem.key,
title: String(rawItem.title || fallbackItem.title),
class: String(rawItem.class || rawItem.icon_class || rawItem.icon || fallbackItem.class || ''),
page_url: String(rawItem.page_url || fallbackItem.page_url || ''),
webview_url: String(rawItem.webview_url || rawItem.link_url || rawItem.url || fallbackItem.webview_url || ''),
webview_title: String(rawItem.webview_title || rawItem.link_title || rawItem.title || fallbackItem.webview_title || ''),
visible: normalizeVisibleValue(
rawItem.visible ?? rawItem.is_show ?? rawItem.show,
fallbackItem.visible,
),
}
}
export const normalizeTabbarItems = (rawItems = []) => {
if (!Array.isArray(rawItems) || !rawItems.length) {
return getDefaultTabbarItems()
}
const normalizedItems = rawItems
.map((item) => normalizeTabbarItem(item))
.filter(Boolean)
return normalizedItems.length ? normalizedItems : getDefaultTabbarItems()
}