visibility.js
11.3 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
* 字段显隐 — sceneType / baseSceneCode / brandId / 出勤 gate
* @see packageDynamicForm/docs/ARCHITECTURE.md
*
* getVisibleList:按 pageContext 与 values 过滤 schema list
* 驻店 F1-1 联动显隐:field.rules.keys + rules.result(field-show-rule.js)
*/
import { filterVisibleRowFields } from './group-visibility.js'
import { valueMapToRowFieldsFromOrigin } from './group-row-adapter.js'
import {
isSubform,
isStaticGroup,
getSubformRowTemplate,
getSubformRowTemplateForAdd,
getStaticGroupChildren,
} from './field-types.js'
import { isFieldVisibleBySpecs } from './visibility/specs/index.js'
import { baseSceneSpec } from './visibility/specs/base-scene.js'
import { isAttendanceOnDuty } from './radio-value.js'
export const ATTENDANCE_KEY = 'F1-1'
export const ON_DUTY = '1'
const DIVIDER_QY_SCENE_CODES = [110101, 110102]
const DIVIDER_YB_SCENE_CODES = [110201, 110202]
/** 判断两个数组是否有交集 */
export function hasIntersection(a = [], b = []) {
return a.some(id => b.includes(id))
}
/** 将 sceneCode 规范化为 number 数组 */
export function normalizeSceneCodeList(codes) {
if (codes == null || codes === '') {
return []
}
const list = Array.isArray(codes) ? codes : [codes]
return list.map(code => Number(code)).filter(code => !Number.isNaN(code))
}
/** 判断 schema 是否包含出勤字段 F1-1 */
export function schemaHasAttendanceField(list = []) {
return flattenFields(list).some(field => field.key === ATTENDANCE_KEY)
}
/** 从 sceneList 提取商户 baseSceneCode 列表 */
export function getMerchantSceneCodes(sceneList = []) {
return normalizeSceneCodeList((sceneList || []).map(item => item?.baseSceneCode))
}
/** 获取当前有效的 sceneCode 列表(优先 sceneList,否则 ctx.baseSceneCode) */
export function getEffectiveSceneCodes(ctx) {
const codes = getMerchantSceneCodes(ctx.sceneList)
if (codes.length) return codes
return normalizeSceneCodeList(ctx.baseSceneCode)
}
/** 判断字段 baseSceneCode 是否与当前商户场景匹配 */
export function matchesBaseSceneCode(field, ctx) {
const fieldCodes = normalizeSceneCodeList(field.baseSceneCode)
if (!fieldCodes.length) return true
const effective = getEffectiveSceneCodes(ctx)
return fieldCodes.some(code => effective.includes(code))
}
/** 判断 divider 是否对当前商户场景可见(QY/YB/qgCode 规则) */
export function isDividerVisible(field, ctx) {
const merchantCodes = getMerchantSceneCodes(ctx.sceneList)
if (field.qgCode?.length) {
return hasIntersection(normalizeSceneCodeList(field.qgCode), merchantCodes)
}
const merchantHasQY = hasIntersection(DIVIDER_QY_SCENE_CODES, merchantCodes)
const merchantHasYB = hasIntersection(DIVIDER_YB_SCENE_CODES, merchantCodes)
const fieldMatchesQY = hasIntersection(field.qyCode || [], merchantCodes)
const fieldMatchesYB = hasIntersection(field.ybCode || [], merchantCodes)
if (field.isQY || field.isYB) {
if (field.isQY) return fieldMatchesQY && !merchantHasYB
if (field.isYB) return fieldMatchesYB && !merchantHasQY
}
return merchantHasQY && merchantHasYB
}
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 : [])
}
/** 详情 jsonContent 回显范围内:跳过 brandId / filterBrandId;baseSceneCode 仍由 baseSceneSpec 校验 */
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
}
/**
* filterBrandId:仅当门店经营品牌全部落在 filter 列表内时隐藏(无「其他品牌」可填)
* @returns {boolean} 是否应隐藏
*/
export function shouldHideByFilterBrandId(field = {}, brandIds = []) {
if (!field.filterBrandId?.length || !brandIds.length) {
return false
}
const hasOtherBrand = brandIds.some(id => !field.filterBrandId.includes(id))
return !hasOtherBrand
}
/** 判断单个字段是否可见(Specification 组合,见 visibility/specs/) */
export function isFieldVisible(field, ctx, values, options = {}) {
return isFieldVisibleBySpecs(field, ctx, values, options)
}
function isRepeaterField(field) {
return isSubform(field)
}
/** 递归展平 schema 为叶子字段列表(跳过 group/subform 容器) */
export function flattenFields(list = []) {
const result = []
for (const field of list) {
if (isRepeaterField(field)) {
result.push(...flattenFields(getSubformRowTemplate(field)))
} else if (isStaticGroup(field)) {
result.push(...flattenFields(getStaticGroupChildren(field)))
} else if (field.type !== 'group' && field.type !== 'subform') {
result.push(field)
}
}
return result
}
function sortBySortNum(list = []) {
return [...list].sort((a, b) => Number(a.sortNum) - Number(b.sortNum))
}
function getVisibilityOptions(list = [], values = {}, ctx = {}) {
return {
schemaList: list,
hasAttendanceField: schemaHasAttendanceField(list),
attendanceOnDuty: isAttendanceOnDuty(list, values),
echoSubformKeys: resolveEchoSubformKeys(ctx, {})
}
}
function filterVisibleChildren(children, ctx, values, visibilityOptions, list) {
return children
.filter(child => isFieldVisible(child, ctx, values, visibilityOptions))
.sort((a, b) => Number(a.sortNum) - Number(b.sortNum))
}
/** 按 pageContext 与 values 过滤 schema list,保留可见 subform/group 结构 */
export function getVisibleList(list = [], ctx, values) {
const visibilityOptions = getVisibilityOptions(list, values, ctx)
const echoSubformKeys = resolveEchoSubformKeys(ctx, {})
const visible = []
for (const field of sortBySortNum(list)) {
if (!isFieldVisible(field, ctx, values, visibilityOptions)) {
continue
}
const forceEchoBlock =
ctx.hasDetailEcho && field.key && echoSubformKeys.has(field.key)
if (isRepeaterField(field)) {
const template = getSubformRowTemplate(field)
const visibleChildren = filterVisibleChildren(
template,
ctx,
values,
{ ...visibilityOptions, echoParentKey: field.key },
list
)
const rowChildren = visibleChildren
if (
rowChildren.length ||
(forceEchoBlock && baseSceneSpec(field, ctx, values, visibilityOptions))
) {
visible.push({
...field,
type: isSubform(field) ? 'subform' : 'subform',
children: [rowChildren]
})
}
} else if (isStaticGroup(field)) {
const groupChildren = getStaticGroupChildren(field)
const visibleChildren = filterVisibleChildren(
groupChildren,
ctx,
values,
{ ...visibilityOptions, echoParentKey: field.key },
list
)
if (visibleChildren.length) {
visible.push({
...field,
children: visibleChildren
})
}
} else if (field.type !== 'group' && field.type !== 'subform') {
visible.push(field)
}
}
return visible
}
/** 将可见 list 拆分为 fixedFields、sectionGroups、groupFields */
export function splitVisibleFields(visibleList = []) {
const fixedFields = []
const sectionGroups = []
const groupFields = []
for (const field of visibleList) {
if (isSubform(field)) {
groupFields.push({ ...field, type: 'subform' })
} else if (isStaticGroup(field)) {
sectionGroups.push(field)
} else {
fixedFields.push(field)
}
}
return { fixedFields, sectionGroups, groupFields, subformFields: groupFields }
}
function isLeafField(field) {
return field.type !== 'group' && field.type !== 'subform' && field.type !== 'divider'
}
/** 顶层固定叶子 + 静态 group 内可见子字段 */
export function getVisibleLeafFields(list = [], ctx, values) {
const visibleList = getVisibleList(list, ctx, values)
const leaves = visibleList.filter(isLeafField)
visibleList
.filter(isStaticGroup)
.forEach(group => {
leaves.push(...getVisibleGroupChildren(group, ctx, values, list).filter(isLeafField))
})
return leaves
}
function hasRowValueMap(row = {}) {
if (!row || typeof row !== 'object') {
return false
}
return Object.keys(row).some(key => {
const v = row[key]
return (
v !== undefined &&
v !== null &&
v !== '' &&
!(Array.isArray(v) && v.length === 0)
)
})
}
/** cache 行快照为空时,用 values 行 valueMap + 列模板合成 FormField[](供校验 childrenShowRules) */
export function resolveEffectiveRowFields(group = {}, rowFields = [], row = {}) {
if (Array.isArray(rowFields) && rowFields.length) {
return rowFields
}
if (hasRowValueMap(row)) {
const columnTemplate = getSubformRowTemplateForAdd(group)
if (columnTemplate.length) {
return valueMapToRowFieldsFromOrigin(columnTemplate, row)
}
}
return Array.isArray(rowFields) ? rowFields : []
}
/** subform 行内可见子字段(模板显隐 + childrenShowRules) */
export function getVisibleGroupChildFields(
group,
ctx,
values,
list = [],
rowFields = null,
row = null
) {
const templateChildren = getVisibleSubformChildren(group, ctx, values, list).filter(isLeafField)
const rowMap = row && typeof row === 'object' ? row : {}
const effectiveRowFields = resolveEffectiveRowFields(group, rowFields || [], rowMap)
if (!effectiveRowFields.length) {
return templateChildren
}
const visibleInRow = filterVisibleRowFields(group, effectiveRowFields, rowMap, ctx)
const visibleKeys = new Set(visibleInRow.map(f => f.key))
return templateChildren.filter(child => visibleKeys.has(child.key))
}
/** 按场景 spec + childrenShowRules 过滤 subform 行 FormField[](保留 cell value/meta) */
export function filterSubformRowFieldsByVisibility(
group,
rowFields = [],
rowMap = {},
ctx = {},
values = {},
list = []
) {
const fullGroup = (list || []).find(item => item.key === group.key) || group
const allowedKeys = new Set(
getVisibleGroupChildFields(fullGroup, ctx, values, list, rowFields, rowMap).map(
field => field.key
)
)
return (rowFields || []).filter(field => allowedKeys.has(field.key))
}
/** subform 行模板内可见子字段(不含 childrenShowRules 行级过滤) */
export function getVisibleSubformChildren(group, ctx, values, list = []) {
if (!group || !isRepeaterField(group)) {
return []
}
const visibilityOptions = {
...getVisibilityOptions(list, values, ctx),
echoParentKey: group?.key
}
return getSubformRowTemplate(group)
.filter(child => isFieldVisible(child, ctx, values, visibilityOptions))
.sort((a, b) => Number(a.sortNum) - Number(b.sortNum))
}
/** 静态 group 可见子字段 */
export function getVisibleGroupChildren(group, ctx, values, list = []) {
if (!group || !isStaticGroup(group)) {
return []
}
const visibilityOptions = {
...getVisibilityOptions(list, values, ctx),
echoParentKey: group?.key
}
return getStaticGroupChildren(group)
.filter(child => isFieldVisible(child, ctx, values, visibilityOptions))
.sort((a, b) => Number(a.sortNum) - Number(b.sortNum))
}