group-visibility.js
1.43 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
/** 行内子字段显隐 childrenShowRules(对齐 gl_tape EditForm.itemShow) */
const { evaluateShowRule } = require('./group-show-rule-eval.js')
function getRowFieldValue(rowFields = [], key = '', rowMap = null) {
const field = (rowFields || []).find(item => item.key === key)
if (field) {
if (field.value != null && field.value !== '') {
return field.value
}
const metaKey = field.key
if (metaKey != null && field[metaKey] != null && field[metaKey] !== '') {
const meta = field[metaKey]
if (typeof meta === 'object' && !Array.isArray(meta)) {
return meta.name ?? meta.label ?? meta.id ?? meta
}
return meta
}
}
if (rowMap && rowMap[key] != null && rowMap[key] !== '') {
return rowMap[key]
}
return null
}
function isGroupChildVisibleInRow(group = {}, child = {}, rowFields = [], rowMap = null) {
const rules = group.childrenShowRules
if (!rules || !child?.key) {
return true
}
const rule = rules[child.key]
if (!rule) {
return true
}
const keys = rule.keys || []
const resultExpr = rule.result
if (!keys.length || !resultExpr) {
return true
}
return evaluateShowRule(keys, resultExpr, rowFields, (rf, k) =>
getRowFieldValue(rf, k, rowMap)
)
}
function filterVisibleRowFields(group = {}, rowFields = [], rowMap = null) {
return (rowFields || []).filter(field =>
isGroupChildVisibleInRow(group, field, rowFields, rowMap)
)
}
module.exports = {
isGroupChildVisibleInRow,
filterVisibleRowFields
}