unis-oa/frontend/src/utils/fieldCapabilities.ts

144 lines
7.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 「这一格能挂什么」的唯一一处口径:自动带出、提交时阈值判断、额度控制。
*
* 以前这三样各自在界面上写死一份类型白名单(能带出的 7 种、能判阈值的 7 种、能控额度的 7 种),
* 三张名单还互不相干:日期格能判阈值却不能带出,人员格能选却不能问一句"有没有填",
* 34 种字段类型里二十多种三个面板都看不见——不是功能没有,是界面没给入口。
* 这里把它们收成一份,按**这一格装得下什么**来分:
*
* - 装得下一个数或一句话的:三样全能挂
* - 装得下一串/一个对象的(多选、附件、部门、日期区间):能问"有没有填",不能比大小
* - 纯排版(标题、分隔线、说明文字):没有值,什么都挂不上
*
* 判定仍然只在服务端做一次:这里决定的是"设计器摆不摆这一栏、摆哪几个比较符",
* 提交时后端照旧按同一份配置再判一遍(``service/threshold.py``、``service/quota.py``)。
*/
import type { FormFieldDef, LinkageOp, ThresholdConfig } from '@/types'
export type FieldCapability = 'fetch' | 'threshold' | 'quota'
/** 今天摆在面板里的全部比较符(比"属于/包含"那几个已经不给了)。 */
const OFFERED_OPS: LinkageOp[] = ['gt', 'ge', 'lt', 'le', 'eq', 'ne', 'empty', 'notEmpty']
/** 装得下一个数:既能比大小,也能直接当额度里那笔钱。 */
export const NUMBER_TYPES: string[] = ['money', 'number', 'formula', 'rate', 'year']
/** 比的是先后,比较符和数值同一档。 */
export const DATE_TYPES: string[] = ['date', 'datetime', 'time', 'month']
/** 装得下一句文本或一个选项值。 */
export const TEXT_TYPES: string[] = [
'text', 'textarea', 'richtext', 'mobile', 'email', 'idcard', 'url',
'select', 'picker', 'radio', 'auto', 'hidden'
]
/** 只有是与不是。 */
export const FLAG_TYPES: string[] = ['switch']
/** 装的是一串值或一个对象:能问"填了没有",比大小说不清。 */
export const BAG_TYPES: string[] = [
'daterange', 'timerange', 'week', 'months', 'checkbox', 'dept', 'cascader', 'user', 'relation', 'address', 'location',
'attachment', 'image', 'signature', 'sign', 'file'
]
/** 明细表:一叠行,额度逐行控,阈值只能问"有没有填过行"。 */
export const TABLE_TYPES: string[] = ['table']
/** 纯排版:没有值。 */
// embed 也归在这一组:它不产生值,所以挂不上取数、阈值、额度,条件里也不该出现。
// 但它绝不是"无所谓"——它上面挂着一条能免登进别人系统的短码,可见性由环节字段权限管,
// 那道闸在 utils/formRuntime 的 effectivePerm 与后端 formruntime.effective_perm 里。
// tabs 也归在这一组:它只切页签,自己不产生值,挂不上取数、阈值、额度,条件里也不出现。
// 它管的是"哪几格在这一屏",真正有没有值、收不收得下,还是各自那一格说了算。
export const LAYOUT_TYPES: string[] = ['title', 'divider', 'description', 'group', 'spacer', 'embed', 'tabs']
const SCALAR_TYPES = [...NUMBER_TYPES, ...DATE_TYPES, ...TEXT_TYPES]
/** 附件那几类连一笔额度都挂不住,口径与后端 ``quota.UNCONTROLABLE_TYPES`` 一致。 */
const UNQUOTABLE_TYPES = ['attachment', 'image', 'signature', 'sign', 'file', 'location', 'timerange', 'week', 'months']
/** 这一格能不能挂某一样。名单之外的一律当"装不下"处理,新加类型时先归组再用。 */
export function canCarry(type: string, cap: FieldCapability): boolean {
if (!type || LAYOUT_TYPES.includes(type)) return false
if (cap === 'fetch') return SCALAR_TYPES.includes(type)
if (cap === 'quota') return !UNQUOTABLE_TYPES.includes(type)
return true
}
/** 这一格能挂哪些面板:设计器与字段卡都读这一份,不再各自数名单。 */
export function capabilitiesOf(type: string): FieldCapability[] {
return (['fetch', 'threshold', 'quota'] as FieldCapability[]).filter((cap) => canCarry(type, cap))
}
/** 阈值能用的比较符:装不下一句数的格子只给"有没有填"这一档。 */
export function thresholdOps(type: string): LinkageOp[] {
if (NUMBER_TYPES.includes(type) || DATE_TYPES.includes(type) || TEXT_TYPES.includes(type)) {
return OFFERED_OPS
}
if (FLAG_TYPES.includes(type)) return ['eq', 'ne', 'empty', 'notEmpty']
return ['empty', 'notEmpty']
}
/** 新配一条阈值时默认给哪个比较符:数值/日期比大小,其余先问有没有填。 */
export function defaultThresholdOp(type: string): LinkageOp {
return NUMBER_TYPES.includes(type) || DATE_TYPES.includes(type) || TEXT_TYPES.includes(type) ? 'gt' : 'notEmpty'
}
/** 明细表点了列却找不到那一列:类型说不清,比较符也就无从评判(缺哪一列由面板点名)。 */
const MISSING_TYPE = '__missing__'
/**
* 这条阈值**实际要比的东西**是什么类型。
*
* 明细表不再等于"一整串行":点了 `column` 之后,比的是那一列的值,
* 列是金额就能比大小、能挑字段当右值,比较符跟着列走。
*/
export function thresholdSubjectType(field: FormFieldDef, cfg?: ThresholdConfig): string {
if (field.type !== 'table') return String(field.type ?? '')
const column = String(cfg?.column ?? '').trim()
if (!column) return 'table'
const hit = ((field.columns ?? []) as FormFieldDef[]).find((item) => String(item.key ?? '') === column)
return hit ? String(hit.type ?? '') : MISSING_TYPE
}
/** 这一条阈值能用哪些比较符:口径同后端 ``threshold.ops_for_field``。 */
export function thresholdOpsFor(field: FormFieldDef, cfg?: ThresholdConfig): LinkageOp[] {
const kind = thresholdSubjectType(field, cfg)
return kind === MISSING_TYPE ? [] : thresholdOps(kind)
}
/** 新配一条阈值默认给哪个比较符(明细表点了金额列就默认比大小)。 */
export function defaultThresholdOpFor(field: FormFieldDef, cfg?: ThresholdConfig): LinkageOp {
const kind = thresholdSubjectType(field, cfg)
return kind === MISSING_TYPE ? 'notEmpty' : defaultThresholdOp(kind)
}
/** 这条阈值比的是不是一句数(面板上的措辞与「比什么」那一栏跟着它走)。 */
export function holdsNumberFor(field: FormFieldDef, cfg?: ThresholdConfig): boolean {
const kind = thresholdSubjectType(field, cfg)
return kind !== MISSING_TYPE && holdsNumber(kind)
}
/** 这条阈值的比较符与要比的东西搭不搭(口径同后端 ``problems``)。 */
export function thresholdTypeProblemFor(field: FormFieldDef, cfg?: ThresholdConfig): string {
const op = String(cfg?.op ?? '')
if (!op) return ''
const kind = thresholdSubjectType(field, cfg)
if (kind === MISSING_TYPE) return ''
if (thresholdOps(kind).includes(op as LinkageOp)) return ''
return thresholdTypeProblem(kind, op)
}
/** 这一格能不能自己装下阈值要比的那个数(面板上的措辞跟着变)。 */
export function holdsNumber(type: string): boolean {
return NUMBER_TYPES.includes(type) || DATE_TYPES.includes(type) || TEXT_TYPES.includes(type)
}
/**
* 阈值配置与字段类型搭不搭。回空串表示没问题。
*
* 只对新放开的那些类型说话(多选、附件、开关、明细表……以前根本挂不上阈值),
* 老配置能配出来的组合一概不报,免得一夜之间满地都是历史毛病。
*/
export function thresholdTypeProblem(type: string, op: string): string {
if (!op) return ''
if (thresholdOps(type).includes(op as LinkageOp)) return ''
return BAG_TYPES.includes(type) || TABLE_TYPES.includes(type)
? '这一格装的是一串值,阈值只能问它填了没有(为空 / 不为空)'
: '这一格比不了大小,换一个比较符'
}