detail-echo.js 2.35 KB
/** 详情 jsonContent 回显范围:跳过模版 brand/filterBrandId;baseSceneCode 由 specs/index 层校验 */
import { isSubform, isStaticGroup } from '../../field-types.js'
import { ATTENDANCE_KEY, isDividerVisible } from '../../visibility.js'

/** 字段是否受 F1-1 出勤 rules.result 联动(与 field-show-rule.js 一致) */
function isAttendanceGatedField(field = {}) {
	const rules = field.rules || {}
	const keys = rules.keys || []
	return keys.includes(ATTENDANCE_KEY) && !!rules.result
}

function resolveEchoSubformKeys(ctx = {}, options = {}) {
	if (options.echoSubformKeys instanceof Set && options.echoSubformKeys.size) {
		return options.echoSubformKeys
	}
	const fromCtx = ctx.echoSubformKeys
	if (fromCtx instanceof Set) return fromCtx
	return new Set(Array.isArray(fromCtx) ? fromCtx : [])
}

function resolveEchoTopLevelKeys(ctx = {}) {
	const fromCtx = ctx.echoTopLevelKeys
	if (fromCtx instanceof Set) return fromCtx
	return new Set(Array.isArray(fromCtx) ? fromCtx : [])
}

function isInDetailEchoScope(field = {}, ctx = {}, options = {}) {
	if (!ctx.hasDetailEcho) return false
	const echoSubformKeys = resolveEchoSubformKeys(ctx, options)
	if (
		field.key &&
		echoSubformKeys.has(field.key) &&
		(isSubform(field) || isStaticGroup(field))
	) {
		return true
	}
	if (options.echoParentKey && echoSubformKeys.has(options.echoParentKey)) {
		return true
	}
	const echoTopLevelKeys = resolveEchoTopLevelKeys(ctx)
	if (field.key && echoTopLevelKeys.has(field.key)) {
		return true
	}
	return false
}

/**
 * 详情回显 spec:在 echo 范围内则可见(divider 仍走场景规则)
 * @returns {boolean|null} true/false 表 decisive;null 表示不在 echo 模式、交给后续 spec
 */
export function detailEchoSpec(field, ctx, _values, options = {}) {
	if (!ctx.hasDetailEcho) return null
	if (isInDetailEchoScope(field, ctx, options)) {
		if (field.type === 'divider') {
			return isDividerVisible(field, ctx)
		}
		// 子表单内的“其他品牌”类字段必须继续服从当前主营品牌,避免历史回显扩大字段范围。
		if (options.echoParentKey && field.filterBrandId?.length) {
			return null
		}
		if (
			options.hasAttendanceField &&
			!options.attendanceOnDuty &&
			isAttendanceGatedField(field)
		) {
			return null
		}
		return true
	}
	if (options.hasAttendanceField && options.attendanceOnDuty) {
		return null
	}
	return false
}