index.js
3.42 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
import { IMG_URL } from '~/utils/constantData.js'
Component({
externalClasses: ['custom-class'],
properties: {
items: {
type: Array,
value: []
},
threshold: {
type: Number,
value: 24
},
clampLines: {
type: Number,
value: 1
}
},
data: {
IMG_URL,
displayItems: [],
showPopup: false,
popupTitle: '',
popupText: ''
},
observers: {
'items, threshold, clampLines': function (items, threshold, clampLines) {
console.log(items)
const display = (items || []).map(it => {
const val = (it && it.value !== undefined && it.value !== null) ? String(it.value) : ''
// 初始不按字符数判断,渲染后根据容器宽度再计算
return { ...it, isLong: false, clampLines: it.clampLines || clampLines }
})
this.setData({ displayItems: display }, () => {
wx.nextTick(() => this._calcOverflowByWidth())
})
}
},
methods: {
rpx2px(rpx) {
const sw = this._screenWidth || (this._screenWidth = wx.getSystemInfoSync().windowWidth)
return (rpx * sw) / 750
},
_calcTextWidth(text, fontPx) {
if (!text) return 0
let width = 0
for (let i = 0; i < text.length; i++) {
const ch = text[i]
if (ch === '/' || ch === '-' || ch === '·') {
width += fontPx * 0.35
} else if (ch === '_' || ch === ':' || ch === ';' || ch === '、' || ch === ',' || ch === '。') {
width += fontPx * 0.5
} else if (/\s/.test(ch)) {
width += fontPx * 0.32
} else if (/\d/.test(ch)) {
width += fontPx * 0.62
} else if (/[a-z]/.test(ch)) {
width += fontPx * 0.62
} else if (/[A-Z]/.test(ch)) {
width += fontPx * 0.74
} else if (/[\u4e00-\u9fa5]/.test(ch)) {
width += fontPx * 0.98
} else {
width += fontPx * 0.55
}
}
return width
},
_calcOverflowByWidth() {
const query = wx.createSelectorQuery().in(this)
query.selectAll('.value-text').boundingClientRect()
query.exec(res => {
const rects = (res && res[0]) || []
const { displayItems, clampLines, threshold } = this.data
if (!displayItems || !displayItems.length) return
const updated = displayItems.map((it, i) => {
const rect = rects[i] || {}
const widthPx = rect.width || 0
const fontRpx = it.highlight ? 36 : 24
const fontPx = this.rpx2px(fontRpx)
const lines = it.clampLines || clampLines || 1
const fallback = it.threshold || threshold
const val = (it.value !== undefined && it.value !== null) ? String(it.value) : ''
if (!widthPx || !fontPx) {
return { ...it, isLong: val.length > fallback }
}
const textWidth = this._calcTextWidth(val, fontPx)
const isLong = textWidth > (widthPx * lines)
return { ...it, isLong }
})
this.setData({ displayItems: updated })
})
},
onMoreTap(e) {
let { index, type } = e.currentTarget.dataset
if (!type) return
const item = this.data.displayItems[index] || {}
this.setData({
showPopup: true,
popupTitle: item.label || '',
popupText: item.value || ''
})
},
onPopupConfirm() {
this.setData({ showPopup: false })
},
onPopupClose() {
this.setData({ showPopup: false })
}
}
})