group-visibility.js 1.43 KB
/** 行内子字段显隐 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
}