group-rows-cache.js
3.82 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/** 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] || [])
}