group-row-adapter.js
8.17 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/** subform 行数据与 gl_tape 对齐 — @see packageDynamicForm/docs/ARCHITECTURE.md */
import { getSubformRowTemplate, getSubformRowTemplateForAdd } from './field-types.js'
import { coerceSelectValue, resolveSelectOptionValue } from './select-value.js'
import {
extractSelectStorageValueFromRowField,
normalizeOptionRecord
} from './field-submit-value.js'
function cloneJson(data) {
return JSON.parse(JSON.stringify(data))
}
function clearFieldValue(field = {}) {
const next = { ...field }
delete next.value
return next
}
/** 增行:清空 value 与 App 双 key field[field.key] */
export function clearRowFieldForAdd(field = {}) {
const next = clearFieldValue(field)
const key = next.key
if (key != null && Object.prototype.hasOwnProperty.call(next, key)) {
delete next[key]
}
return next
}
/** @returns {Array} subform 行模板(children[0]) */
export function getRowTemplate(group = {}) {
return getSubformRowTemplate(group)
}
/** 深拷贝行模板并清空各字段 value */
export function cloneRowTemplate(group = {}) {
return cloneJson(getRowTemplate(group)).map(clearFieldValue)
}
/** 增行:基于 origin_children 克隆空行(不继承 children[0] 已填值) */
export function cloneRowTemplateForAdd(group = {}) {
const template = cloneJson(getSubformRowTemplateForAdd(group))
const usesOrigin =
Array.isArray(group.origin_children) && group.origin_children.length > 0
if (usesOrigin) {
return template.map(field => {
const next = { ...field }
const key = next.key
if (
key != null &&
next.type === 'select' &&
Object.prototype.hasOwnProperty.call(next, key)
) {
delete next[key]
}
return next
})
}
return template.map(clearRowFieldForAdd)
}
/** 判断 value 是否为 gl_tape 形态 FormField[][] */
export function isRowFieldsList(value) {
if (!Array.isArray(value) || !value.length) {
return false
}
const first = value[0]
return (
Array.isArray(first) &&
first.length > 0 &&
first[0] &&
typeof first[0] === 'object' &&
first[0].key != null &&
first[0].type != null
)
}
function isPlainObject(value) {
return value != null && typeof value === 'object' && !Array.isArray(value)
}
/** optionsKey select 写入 values:有独立展示名时存 { id, name },否则存 id 标量 */
function selectValueMapEntry(field = {}) {
const storageValue = extractSelectStorageValueFromRowField(field)
if (storageValue === undefined || storageValue === null || storageValue === '') {
return undefined
}
if (isPlainObject(storageValue)) {
const id = storageValue.id ?? storageValue.value
const name = storageValue.name ?? storageValue.label ?? ''
if (id != null && id !== '' && name !== '' && String(name) !== String(id)) {
return storageValue
}
if (id != null && id !== '') {
return String(id)
}
}
return storageValue
}
/** FormField[] 转为行 valueMap(跳过空值) */
export function rowFieldsToValueMap(rowFields = []) {
const map = {}
;(rowFields || []).forEach(field => {
if (!field?.key) return
const v = field.value
if (v === undefined || v === null || v === '') {
return
}
if (Array.isArray(v) && v.length === 0) {
return
}
if (field.type === 'select') {
if (field.optionsKey) {
const entry = selectValueMapEntry(field)
if (entry !== undefined) {
map[field.key] = entry
}
return
}
if (typeof v === 'object' && v !== null && !Array.isArray(v)) {
map[field.key] = v
return
}
const meta = field[field.key]
const scalar = resolveSelectOptionValue(
field,
meta != null && meta !== '' ? meta : v
)
if (scalar !== '' && scalar != null) {
map[field.key] = scalar
}
return
}
if (field.type === 'carBrandSelect') {
const meta = field[field.key]
if (meta && typeof meta === 'object') {
map[field.key] = meta
return
}
}
map[field.key] = v
})
return map
}
/** FormField[] → 行 valueMap(fieldDataList 用,optionsKey select 保留完整 object) */
export function rowFieldsToStorageValueMap(rowFields = []) {
const map = {}
;(rowFields || []).forEach(field => {
if (!field?.key) return
if (field.type === 'select') {
const storageValue = extractSelectStorageValueFromRowField(field)
if (storageValue !== undefined && storageValue !== null && storageValue !== '') {
map[field.key] = storageValue
}
return
}
Object.assign(map, rowFieldsToValueMap([field]))
})
return map
}
/** FormField[][] → fieldDataList 用 valueMap 数组 */
export function rowsToStorageValueMaps(rowFieldsList = []) {
return (rowFieldsList || []).map(rowFieldsToStorageValueMap)
}
function isEmptyMapValue(raw) {
return (
raw === undefined ||
raw === null ||
raw === '' ||
(Array.isArray(raw) && raw.length === 0)
)
}
/** 将 valueMap 中单格写入空壳 FormField(select 双 key 与 cache 写入一致) */
export function applyValueMapToRowField(field = {}, raw) {
if (isEmptyMapValue(raw)) {
return clearRowFieldForAdd(field)
}
const next = { ...field }
const key = next.key
if (field.type === 'select' && field.optionsKey) {
if (typeof raw === 'object' && !Array.isArray(raw)) {
const meta = normalizeOptionRecord(raw) || raw
next.value = meta.name ?? meta.label ?? ''
if (key != null) {
next[key] = meta
}
} else {
if (key != null) {
next[key] = raw
}
next.value = String(raw)
}
return next
}
if (field.type === 'select') {
next.value = resolveSelectOptionValue(field, raw)
return next
}
if (field.type === 'carBrandSelect' && typeof raw === 'object' && raw !== null) {
next.value = raw
if (key != null) {
next[key] = raw
}
return next
}
next.value = raw
return next
}
/** 用 origin_children 列模板 + valueMap 还原一行(不继承 children[0] 双 key) */
export function valueMapToRowFieldsFromOrigin(columnTemplate = [], valueMap = {}) {
return (columnTemplate || []).map(field => {
const key = field?.key
const hasOwn =
key != null && Object.prototype.hasOwnProperty.call(valueMap, key)
const raw = hasOwn ? valueMap[key] : field?.value
return applyValueMapToRowField(clearRowFieldForAdd(field), raw)
})
}
/** valueMap 按模板还原为 FormField[](legacy:模板可能含已填行字段) */
export function valueMapToRowFields(template = [], valueMap = {}) {
return cloneJson(template).map(field => {
const next = { ...field }
if (valueMap[field.key] !== undefined && valueMap[field.key] !== null) {
next.value = valueMap[field.key]
} else {
delete next.value
}
return next
})
}
/** valueMap 数组 + origin_children 列模板 → FormField[][] */
export function valueMapsToRowFieldsFromOrigin(columnTemplate = [], valueMaps = []) {
return (valueMaps || []).map(map => valueMapToRowFieldsFromOrigin(columnTemplate, map))
}
/** FormField[][] 批量转为 valueMap 数组 */
export function rowsToValueMaps(rowFieldsList = []) {
return (rowFieldsList || []).map(rowFieldsToValueMap)
}
/** valueMap 数组按模板还原为 FormField[][] */
export function valueMapsToRowFieldsList(template = [], valueMaps = []) {
return (valueMaps || []).map(map => valueMapToRowFields(template, map))
}
/** 深拷贝 FormField[][] */
export function cloneRowFieldsList(rowFieldsList = []) {
return cloneJson(rowFieldsList)
}
/** 从 schema subform 节点解析行列表(支持 FormField[][] 或 valueMap[]) */
export function groupValueToRowFieldsList(group = {}) {
const template = getRowTemplate(group)
const val = group.value
if (isRowFieldsList(val)) {
return cloneJson(val)
}
if (Array.isArray(val) && val.length && val[0] && typeof val[0] === 'object' && !Array.isArray(val[0])) {
return valueMapsToRowFieldsList(template, val)
}
return []
}
/** 从 subform 字段节点提取行 valueMap[](兼容 FormField[][] / valueMap[]) */
export function extractSubformValuesFromField(field = {}) {
const val = field.value
if (isRowFieldsList(val)) {
return rowsToValueMaps(val)
}
if (Array.isArray(val) && val.length && val[0] && typeof val[0] === 'object' && !Array.isArray(val[0])) {
const template = getRowTemplate(field)
if (!template.length) {
return cloneJson(val)
}
}
const rowFieldsList = groupValueToRowFieldsList(field)
if (!rowFieldsList.length) {
return undefined
}
return rowsToValueMaps(rowFieldsList)
}