group-rows-cache.js 3.82 KB
/** subform 行 FormField[] 缓存 — @see docs/features/packageDynamicForm/ARCHITECTURE.md */
import {
	cloneRowTemplate,
	cloneRowFieldsList,
	rowFieldsToValueMap,
	rowsToValueMaps,
	valueMapsToRowFieldsList,
	valueMapToRowFields,
	getRowTemplate
} from './group-row-adapter.js'
import { getGroupRows, listGroups } from './group-values.js'

/** 从 values 构建 groupRowsCache(FormField[][])
 *  @returns {object} groupKey → FormField[][] */
export function buildGroupRowsCache(list = [], values = {}) {
	const cache = {}
	listGroups(list).forEach(group => {
		const template = getRowTemplate(group)
		const maps = getGroupRows(values, group.key)
		cache[group.key] = valueMapsToRowFieldsList(template, maps)
	})
	return cache
}

/** 将 cache 中某 subform 的行数据同步回 values
 *  @returns {object} 新的 values 对象 */
export function syncValuesFromGroupCache(values = {}, groupKey, rowFieldsList = []) {
	return {
		...values,
		[groupKey]: rowsToValueMaps(rowFieldsList)
	}
}

/** @returns {Array} 指定行的 FormField[] 深拷贝 */
export function getRowFieldsFromCache(cache = {}, groupKey, rowIndex = 0) {
	const list = cache[groupKey]
	if (!Array.isArray(list) || !list.length) {
		return []
	}
	const index = Math.min(Math.max(0, rowIndex), list.length - 1)
	return cloneRowFieldsList(list[index] || [])
}

/** 向 cache 新增 subform 行(受 maxRows 限制)
 *  @returns {{ cache: object, rowIndex: number }} */
export function addRowToCache(cache = {}, group = {}) {
	const groupKey = group.key
	const maxRows = group.rules?.maxRows ?? 20
	const list = cache[groupKey] ? [...cache[groupKey]] : []
	if (list.length >= maxRows) {
		return { cache: { ...cache, [groupKey]: list }, rowIndex: Math.max(0, list.length - 1) }
	}
	list.push(cloneRowTemplate(group))
	return {
		cache: { ...cache, [groupKey]: list },
		rowIndex: list.length - 1
	}
}

/** 从 cache 删除 subform 指定行
 *  @returns {object} 新的 cache */
export function removeRowFromCache(cache = {}, groupKey, rowIndex) {
	const list = [...(cache[groupKey] || [])]
	list.splice(rowIndex, 1)
	const next = { ...cache }
	if (list.length) {
		next[groupKey] = list
	} else {
		delete next[groupKey]
	}
	return next
}

function seedEmptyRow(rowTemplate = null) {
	if (rowTemplate?.length) {
		return cloneRowFieldsList([rowTemplate])[0] || []
	}
	return []
}

/** 设置 cache 中某行子字段值
 *  @returns {object} 新的 cache */
export function setRowFieldInCache(cache = {}, groupKey, rowIndex, childKey, value, rowTemplate = null) {
	const list = cloneRowFieldsList(cache[groupKey] || [])
	while (list.length <= rowIndex) {
		list.push(seedEmptyRow(rowTemplate))
	}
	let row = [...(list[rowIndex] || [])]
	let idx = row.findIndex(f => f.key === childKey)
	if (idx === -1 && rowTemplate?.length) {
		const existingMap = rowFieldsToValueMap(row)
		list[rowIndex] = valueMapToRowFields(rowTemplate, existingMap)
		row = [...list[rowIndex]]
		idx = row.findIndex(f => f.key === childKey)
	}
	if (idx === -1) {
		return cache
	}
	const field = { ...row[idx] }
	if (
		value === '' ||
		value === null ||
		value === undefined ||
		(Array.isArray(value) && value.length === 0)
	) {
		delete field.value
	} else {
		field.value = value
	}
	row[idx] = field
	list[rowIndex] = row
	return { ...cache, [groupKey]: list }
}

/** 整行深拷贝回写 cache
 *  @returns {object} 新的 cache */
export function replaceRowInCache(cache = {}, groupKey, rowIndex, rowFields = [], rowTemplate = null) {
	const list = cloneRowFieldsList(cache[groupKey] || [])
	while (list.length <= rowIndex) {
		list.push(seedEmptyRow(rowTemplate))
	}
	list[rowIndex] = cloneRowFieldsList(rowFields)
	return { ...cache, [groupKey]: list }
}

/** @returns {Array} 某 subform 全部行的 FormField[][] 深拷贝 */
export function getGroupRowFieldsList(cache = {}, groupKey) {
	return cloneRowFieldsList(cache[groupKey] || [])
}