group-row-snapshot.js
2.71 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
/** 供小程序组件使用:从 groupRowsCache 深拷贝当前行,避免 wxs 直引导致多行/多 group 串联 */
import { filterVisibleRowFields } from './group-visibility.js'
import { filterSubformRowFieldsByVisibility } from './visibility.js'
import { rowFieldsToValueMap } from './group-row-adapter.js'
function cloneJson(data) {
return JSON.parse(JSON.stringify(data))
}
function resolveRowIndex(activeIndex, rowLength) {
let idx = Number(activeIndex)
if (Number.isNaN(idx) || idx < 0) idx = 0
if (rowLength > 0 && idx >= rowLength) idx = rowLength - 1
return idx
}
export function getRowFieldsSnapshot(cache = {}, groupKey = '', activeIndex = 0) {
const list = cache[groupKey]
if (!Array.isArray(list) || !list.length) {
return []
}
const idx = resolveRowIndex(activeIndex, list.length)
return cloneJson(list[idx] || [])
}
export function getGroupRowTotal(cache = {}, groupKey = '', values = {}) {
const list = cache[groupKey]
if (Array.isArray(list) && list.length) {
return list.length
}
const rows = values[groupKey]
return rows && rows.length ? rows.length : 0
}
export function buildVisibleRowFields(
group = {},
rowFields = [],
activeIndex = 0,
rowTotal = 0,
visibilityContext = null
) {
const groupKey = group.key || ''
const index = resolveRowIndex(activeIndex, rowTotal)
let visible = rowFields
try {
const ctx = visibilityContext?.ctx
const list = visibilityContext?.list
const values = visibilityContext?.values ?? {}
if (ctx && Array.isArray(list)) {
const rowMap = rowFieldsToValueMap(rowFields)
visible = filterSubformRowFieldsByVisibility(
group,
rowFields,
rowMap,
ctx,
values,
list
)
} else if (typeof filterVisibleRowFields === 'function') {
visible = filterVisibleRowFields(group, rowFields)
}
} catch (error) {
console.error('[group-row-snapshot] buildVisibleRowFields', error)
}
const withKeys = (visible || []).map((field, fieldIdx) => ({
...field,
_wxRowKey: `${groupKey}_${index}_${field.key || fieldIdx}`
}))
return cloneJson(withKeys)
}
export function buildGroupRowViewMaps(
groupFields = [],
cache = {},
activeMap = {},
values = {},
visibilityContext = null
) {
const groupRowFieldsMap = {}
const groupRowVisibleMap = {}
const groupRowTotalMap = {}
;(groupFields || []).forEach(group => {
const key = group?.key
if (!key) return
const rowTotal = getGroupRowTotal(cache, key, values)
const rowFields = getRowFieldsSnapshot(cache, key, activeMap[key])
groupRowFieldsMap[key] = rowFields
groupRowTotalMap[key] = rowTotal
groupRowVisibleMap[key] = buildVisibleRowFields(
group,
rowFields,
activeMap[key],
rowTotal,
visibilityContext
)
})
return { groupRowFieldsMap, groupRowVisibleMap, groupRowTotalMap }
}