schema-normalizer.js
3.31 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
import { flattenFields } from './visibility.js'
import {
isSubform,
isStaticGroup,
isLegacyNestedSubformShape,
getSubformRowTemplate,
getStaticGroupChildren
} from './field-types.js'
function cloneJson(data) {
return JSON.parse(JSON.stringify(data))
}
function mergeFieldRules(field = {}, parentRules = {}) {
const mergedRules = {
...(field.rules || {}),
...parentRules
}
if (field.unit != null && mergedRules.unit == null) {
mergedRules.unit = field.unit
}
return {
...field,
rules: mergedRules
}
}
function coerceFieldType(field = {}) {
if (isLegacyNestedSubformShape(field)) {
return { ...field, type: 'subform' }
}
return field
}
function normalizeSubformChildren(field = {}) {
if (!isSubform(field)) {
return field
}
const template = getSubformRowTemplate(field)
if (!template.length) {
return field
}
const childrenRules = field.childrenRules || {}
const nextChildren = template.map(child =>
mergeFieldRules(child, childrenRules[child.key] || {})
)
return {
...field,
children: [nextChildren]
}
}
function normalizeStaticGroupChildren(field = {}) {
if (!isStaticGroup(field)) {
return field
}
const children = getStaticGroupChildren(field)
if (!children.length) {
return field
}
const childrenRules = field.childrenRules || {}
const nextChildren = children.map(child =>
mergeFieldRules(child, childrenRules[child.key] || {})
)
return {
...field,
children: nextChildren
}
}
export function resolveOptionsFromKey(optionsKey = '', optionsMap = {}) {
if (!optionsKey) {
return []
}
return optionsMap[optionsKey] || []
}
/** multipleSelect + optionsKey 由组件按需走接口;select 等仍可从 map 预填 */
export function shouldPrefillOptionsFromKey(field = {}) {
if (!field.optionsKey) {
return false
}
if (field.type === 'multipleSelect') {
return false
}
return !field.options || !field.options.length
}
function applyOptionsFromKey(field = {}, optionsMap = {}) {
if (!shouldPrefillOptionsFromKey(field)) {
return field
}
return {
...field,
options: resolveOptionsFromKey(field.optionsKey, optionsMap)
}
}
function applyOptionsToChildren(children = [], childrenRules = {}, optionsMap = {}) {
return children.map(child => {
let nextChild = mergeFieldRules(child, childrenRules[child.key] || {})
nextChild = applyOptionsFromKey(nextChild, optionsMap)
return nextChild
})
}
export function normalizeSchemaRoot(schemaRoot = {}, optionsMap = {}) {
const nextSchema = cloneJson(schemaRoot)
nextSchema.list = (nextSchema.list || []).map(rawField => {
let field = coerceFieldType(rawField)
field = applyOptionsFromKey(field, optionsMap)
if (isSubform(field)) {
field = normalizeSubformChildren(field)
field = {
...field,
children: [
applyOptionsToChildren(
getSubformRowTemplate(field),
field.childrenRules || {},
optionsMap
)
]
}
} else if (isStaticGroup(field)) {
field = normalizeStaticGroupChildren(field)
field = {
...field,
children: applyOptionsToChildren(
field.children || [],
field.childrenRules || {},
optionsMap
)
}
}
return mergeFieldRules(field)
})
return nextSchema
}
export function getSchemaFieldLabels(schemaRoot = {}) {
const labels = {}
flattenFields(schemaRoot.list || []).forEach(field => {
labels[field.key] = field.label
})
return labels
}