import { _t } from '@/libs/Language/Language'
|
import { injectStore } from '../../core/store'
|
import { WidgetNameType } from './WidgetTypeByEnum'
|
|
const { flowMap } = injectStore()
|
|
export class CreateFormItem {
|
category: string = ''
|
label: string = ''
|
prop: string = ''
|
el: any
|
rules: any
|
readonly: boolean = false
|
tip: string = ''
|
icon: string = ''
|
options: any
|
defaultValue: any
|
clearable: boolean = true
|
controlsPosition: string = ''
|
step: number = 1
|
min: number = 0
|
propertyType: string = ''
|
elementAttributes: any
|
elementType: any
|
type: string = ''
|
placeholder: string = ''
|
valueOnClear: number | any
|
nullable: boolean = false
|
pattern: string = ''
|
rows: string | number = 10
|
constructor(item: any, currentNode: string) {
|
this.init(item, currentNode)
|
}
|
|
init(item: any, currentNode: string) {
|
const el =
|
WidgetNameType[item.propertyType]?.el ||
|
WidgetNameType[item.propertyType]?.type
|
this.category = item.category
|
this.label = item.name
|
this.prop = item.propertyKey
|
this.el = el
|
this.readonly = item.readonly
|
this.tip = item.description
|
this.icon = 'wen'
|
this.options = item.options
|
this.defaultValue = item.propertyValue
|
this.clearable = item.nullable ? true : false
|
this.controlsPosition = 'right'
|
this.valueOnClear = item.nullable ? undefined : item.propertyValue
|
this.step = 1
|
this.nullable = item.nullable
|
this.min = WidgetNameType[item.propertyType]?.type?.min ?? undefined
|
this.propertyType = item.propertyType
|
this.elementAttributes = item?.elementAttributes
|
this.elementType = item.elementType
|
this.placeholder = item.placeholder
|
this.type = WidgetNameType[item.propertyType]?.el
|
? WidgetNameType[item.propertyType]?.type
|
: 'select'
|
this.rules = this.getRules(item.propertyKey, currentNode)
|
this.rows = item.rows || WidgetNameType[item.propertyType]?.rows
|
|
const requiredRule = {
|
required: true,
|
message: this.options.length ? _t('请选择') : _t('请输入') + item.name,
|
trigger: 'change',
|
}
|
if (item.required && !this.rules) {
|
this.rules = [requiredRule]
|
}
|
if (item.pattern) {
|
this.rules.push({
|
pattern: new RegExp(item.pattern),
|
message: item.ruleMessage || _t('格式错误'),
|
trigger: 'change',
|
})
|
}
|
}
|
|
getRules(key: string, currentNode: string) {
|
// 根据实际情况实现获取规则的逻辑
|
const ruleMap: Record<string, any> = {
|
Name: [
|
{
|
required: true,
|
message: _t('请输入步骤名称'),
|
trigger: 'change',
|
},
|
|
{
|
validator: (rule: any, value: string, callback: any) => {
|
let count = 0
|
let isSameValue = false
|
flowMap.forEach((item: any) => {
|
if (item?.properties?.name === value) {
|
if (currentNode !== item.id) {
|
count++
|
if (count >= 1) {
|
return (isSameValue = true)
|
}
|
}
|
}
|
})
|
|
if (isSameValue) {
|
callback(_t('节点名称重复,请检查后重试'))
|
} else {
|
callback()
|
}
|
},
|
trigger: 'blur',
|
},
|
],
|
}
|
return ruleMap[key]
|
}
|
}
|