NavigationBar.js
2.59 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
// components/navigationBar/navigationBar.js
Component({
/**
* 组件的属性列表
*/
properties: {
navStyle: String,
custom: Boolean,
showBack: Boolean,
pageTitle: {
type: String,
value: '',
observer(n) {
if (n) {
this.title = n
this.setPageTitle()
}
}
}
},
title: '',
/**
* 组件的初始数据
*/
data: {},
/**
* 组件的方法列表
*/
methods: {
setPageTitle() {
let thisPage = wx.p.router.getPageInstance()
if (thisPage) thisPage.navigationBarTitleText = this.title
},
init() {
/**
* 获取自定义导航栏信息。
* 基础库 2.20.1+ 起 getSystemInfo 已废弃,按字段拆分到新接口:
* statusBarHeight / screenWidth → wx.getWindowInfo()
* fontSizeSetting → wx.getAppBaseInfo()
* 新接口均为同步返回,去掉原来的 .then 异步时序。
*/
const windowInfo = wx.getWindowInfo() || {}
const appBaseInfo = wx.getAppBaseInfo() || {}
let {
fontSizeSetting,
statusBarHeight,
screenWidth,
} = {
...windowInfo,
fontSizeSetting: appBaseInfo.fontSizeSetting,
}
const {
height,
right,
width,
top,
} = wx.getMenuButtonBoundingClientRect()
const currentPages = getCurrentPages()
const { showBack } = this.data
if (showBack && currentPages.length > 1) this.setData({
SHOWBACK: true
})
let navigationBarHeight = height + ((top - statusBarHeight) * 2)
let navigationBarPadding = screenWidth - right
let navigationBarWidth = screenWidth - (navigationBarPadding * 2 + width * 2)
this.setData({
statusBarHeight,
navigationBarHeight,
navigationBarStyle: `font-size:${fontSizeSetting}px;`,
navigationBarTextWidth: `width:${navigationBarWidth}px;padding:0 ${navigationBarPadding}px;`
})
this.emit({
navigationBarHeight,
statusBarHeight
})
},
emit({
navigationBarHeight,
statusBarHeight
}) {
let navHeight = navigationBarHeight + statusBarHeight
const thisPage = wx.p.router.getPageInstance()
this.triggerEvent('navbarReady', {
height: navHeight,
statusBarHeight
})
this.setData({
showNavBar: true
})
thisPage.setData({
navHeight,
statusBarHeight
})
},
back() {
wx.p.router.back()
}
},
lifetimes: {
attached() {
this.init()
this.setPageTitle()
}
}
})