ElementForm.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <template>
  2. <div class="panel-tab__content">
  3. <el-form label-width="80px">
  4. <el-form-item label="表单标识">
  5. <el-input v-model="formKey" clearable @change="updateElementFormKey" />
  6. </el-form-item>
  7. <el-form-item label="业务标识">
  8. <el-select v-model="businessKey" @change="updateElementBusinessKey">
  9. <el-option v-for="i in fieldList" :key="i.id" :value="i.id" :label="i.label" />
  10. <el-option label="无" value="" />
  11. </el-select>
  12. </el-form-item>
  13. </el-form>
  14. <!--字段列表-->
  15. <div class="element-property list-property">
  16. <el-divider><Icon icon="ep:coin" /> 表单字段</el-divider>
  17. <el-table :data="fieldList" max-height="240" border fit>
  18. <el-table-column label="序号" type="index" width="50px" />
  19. <el-table-column label="字段名称" prop="label" min-width="80px" show-overflow-tooltip />
  20. <el-table-column
  21. label="字段类型"
  22. prop="type"
  23. min-width="80px"
  24. :formatter="(row) => fieldType[row.type] || row.type"
  25. show-overflow-tooltip
  26. />
  27. <el-table-column
  28. label="默认值"
  29. prop="defaultValue"
  30. min-width="80px"
  31. show-overflow-tooltip
  32. />
  33. <el-table-column label="操作" width="90px">
  34. <template #default="scope">
  35. <el-button type="text" @click="openFieldForm(scope, scope.$index)">编辑</el-button>
  36. <el-divider direction="vertical" />
  37. <el-button type="text" style="color: #ff4d4f" @click="removeField(scope, scope.$index)"
  38. >移除</el-button
  39. >
  40. </template>
  41. </el-table-column>
  42. </el-table>
  43. </div>
  44. <div class="element-drawer__button">
  45. <XButton type="primary" proIcon="ep:plus" title="添加字段" @click="openFieldForm(null, -1)" />
  46. </div>
  47. <!--字段配置侧边栏-->
  48. <el-drawer
  49. v-model="fieldModelVisible"
  50. title="字段配置"
  51. :size="`${width}px`"
  52. append-to-body
  53. destroy-on-close
  54. >
  55. <el-form :model="formFieldForm" label-width="90px">
  56. <el-form-item label="字段ID">
  57. <el-input v-model="formFieldForm.id" clearable />
  58. </el-form-item>
  59. <el-form-item label="类型">
  60. <el-select
  61. v-model="formFieldForm.typeType"
  62. placeholder="请选择字段类型"
  63. clearable
  64. @change="changeFieldTypeType"
  65. >
  66. <el-option v-for="(value, key) of fieldType" :label="value" :value="key" :key="key" />
  67. </el-select>
  68. </el-form-item>
  69. <el-form-item label="类型名称" v-if="formFieldForm.typeType === 'custom'">
  70. <el-input v-model="formFieldForm.type" clearable />
  71. </el-form-item>
  72. <el-form-item label="名称">
  73. <el-input v-model="formFieldForm.label" clearable />
  74. </el-form-item>
  75. <el-form-item label="时间格式" v-if="formFieldForm.typeType === 'date'">
  76. <el-input v-model="formFieldForm.datePattern" clearable />
  77. </el-form-item>
  78. <el-form-item label="默认值">
  79. <el-input v-model="formFieldForm.defaultValue" clearable />
  80. </el-form-item>
  81. </el-form>
  82. <!-- 枚举值设置 -->
  83. <template v-if="formFieldForm.type === 'enum'">
  84. <el-divider key="enum-divider" />
  85. <p class="listener-filed__title" key="enum-title">
  86. <span><Icon icon="ep:menu" />枚举值列表:</span>
  87. <el-button type="primary" @click="openFieldOptionForm(null, -1, 'enum')"
  88. >添加枚举值</el-button
  89. >
  90. </p>
  91. <el-table :data="fieldEnumList" key="enum-table" max-height="240" border fit>
  92. <el-table-column label="序号" width="50px" type="index" />
  93. <el-table-column label="枚举值编号" prop="id" min-width="100px" show-overflow-tooltip />
  94. <el-table-column label="枚举值名称" prop="name" min-width="100px" show-overflow-tooltip />
  95. <el-table-column label="操作" width="90px">
  96. <template #default="scope">
  97. <el-button type="text" @click="openFieldOptionForm(scope, scope.$index, 'enum')"
  98. >编辑</el-button
  99. >
  100. <el-divider direction="vertical" />
  101. <el-button
  102. type="text"
  103. style="color: #ff4d4f"
  104. @click="removeFieldOptionItem(scope, scope.$index, 'enum')"
  105. >移除</el-button
  106. >
  107. </template>
  108. </el-table-column>
  109. </el-table>
  110. </template>
  111. <!-- 校验规则 -->
  112. <el-divider key="validation-divider" />
  113. <p class="listener-filed__title" key="validation-title">
  114. <span><Icon icon="ep:menu" />约束条件列表:</span>
  115. <el-button type="primary" @click="openFieldOptionForm(null, -1, 'constraint')"
  116. >添加约束</el-button
  117. >
  118. </p>
  119. <el-table :data="fieldConstraintsList" key="validation-table" max-height="240" border fit>
  120. <el-table-column label="序号" width="50px" type="index" />
  121. <el-table-column label="约束名称" prop="name" min-width="100px" show-overflow-tooltip />
  122. <el-table-column label="约束配置" prop="config" min-width="100px" show-overflow-tooltip />
  123. <el-table-column label="操作" width="90px">
  124. <template #default="scope">
  125. <el-button type="text" @click="openFieldOptionForm(scope, scope.$index, 'constraint')"
  126. >编辑</el-button
  127. >
  128. <el-divider direction="vertical" />
  129. <el-button
  130. type="text"
  131. style="color: #ff4d4f"
  132. @click="removeFieldOptionItem(scope, scope.$index, 'constraint')"
  133. >移除</el-button
  134. >
  135. </template>
  136. </el-table-column>
  137. </el-table>
  138. <!-- 表单属性 -->
  139. <el-divider key="property-divider" />
  140. <p class="listener-filed__title" key="property-title">
  141. <span><Icon icon="ep:menu" />字段属性列表:</span>
  142. <el-button type="primary" @click="openFieldOptionForm(null, -1, 'property')"
  143. >添加属性</el-button
  144. >
  145. </p>
  146. <el-table :data="fieldPropertiesList" key="property-table" max-height="240" border fit>
  147. <el-table-column label="序号" width="50px" type="index" />
  148. <el-table-column label="属性编号" prop="id" min-width="100px" show-overflow-tooltip />
  149. <el-table-column label="属性值" prop="value" min-width="100px" show-overflow-tooltip />
  150. <el-table-column label="操作" width="90px">
  151. <template #default="scope">
  152. <el-button type="text" @click="openFieldOptionForm(scope, scope.$index, 'property')"
  153. >编辑</el-button
  154. >
  155. <el-divider direction="vertical" />
  156. <el-button
  157. type="text"
  158. style="color: #ff4d4f"
  159. @click="removeFieldOptionItem(scope, scope.$index, 'property')"
  160. >移除</el-button
  161. >
  162. </template>
  163. </el-table-column>
  164. </el-table>
  165. <!-- 底部按钮 -->
  166. <div class="element-drawer__button">
  167. <el-button>取 消</el-button>
  168. <el-button type="primary" @click="saveField">保 存</el-button>
  169. </div>
  170. </el-drawer>
  171. <el-dialog
  172. v-model="fieldOptionModelVisible"
  173. :title="optionModelTitle"
  174. width="600px"
  175. append-to-body
  176. destroy-on-close
  177. >
  178. <el-form :model="fieldOptionForm" label-width="96px">
  179. <el-form-item label="编号/ID" v-if="fieldOptionType !== 'constraint'" key="option-id">
  180. <el-input v-model="fieldOptionForm.id" clearable />
  181. </el-form-item>
  182. <el-form-item label="名称" v-if="fieldOptionType !== 'property'" key="option-name">
  183. <el-input v-model="fieldOptionForm.name" clearable />
  184. </el-form-item>
  185. <el-form-item label="配置" v-if="fieldOptionType === 'constraint'" key="option-config">
  186. <el-input v-model="fieldOptionForm.config" clearable />
  187. </el-form-item>
  188. <el-form-item label="值" v-if="fieldOptionType === 'property'" key="option-value">
  189. <el-input v-model="fieldOptionForm.value" clearable />
  190. </el-form-item>
  191. </el-form>
  192. <template #footer>
  193. <el-button @click="fieldOptionModelVisible = false">取 消</el-button>
  194. <el-button type="primary" @click="saveFieldOption">确 定</el-button>
  195. </template>
  196. </el-dialog>
  197. </div>
  198. </template>
  199. <script setup lang="ts" name="ElementForm">
  200. const props = defineProps({
  201. id: String,
  202. type: String
  203. })
  204. const prefix = inject('prefix')
  205. const width = inject('width')
  206. const formKey = ref('')
  207. const businessKey = ref('')
  208. const optionModelTitle = ref('')
  209. const fieldList = ref<any[]>([])
  210. const formFieldForm = ref<any>({})
  211. const fieldType = ref({
  212. long: '长整型',
  213. string: '字符串',
  214. boolean: '布尔类',
  215. date: '日期类',
  216. enum: '枚举类',
  217. custom: '自定义类型'
  218. })
  219. const formFieldIndex = ref(-1) // 编辑中的字段, -1 为新增
  220. const formFieldOptionIndex = ref(-1) // 编辑中的字段配置项, -1 为新增
  221. const fieldModelVisible = ref(false)
  222. const fieldOptionModelVisible = ref(false)
  223. const fieldOptionForm = ref<any>({}) // 当前激活的字段配置项数据
  224. const fieldOptionType = ref('') // 当前激活的字段配置项弹窗 类型
  225. const fieldEnumList = ref<any[]>([]) // 枚举值列表
  226. const fieldConstraintsList = ref<any[]>([]) // 约束条件列表
  227. const fieldPropertiesList = ref<any[]>([]) // 绑定属性列表
  228. const bpmnELement = ref()
  229. const elExtensionElements = ref()
  230. const formData = ref()
  231. const otherExtensions = ref()
  232. const bpmnInstances = () => (window as any)?.bpmnInstances
  233. const resetFormList = () => {
  234. bpmnELement.value = bpmnInstances().bpmnElement
  235. formKey.value = bpmnELement.value.businessObject.formKey
  236. // 获取元素扩展属性 或者 创建扩展属性
  237. elExtensionElements.value =
  238. bpmnELement.value.businessObject.get('extensionElements') ||
  239. bpmnInstances().moddle.create('bpmn:ExtensionElements', { values: [] })
  240. // 获取元素表单配置 或者 创建新的表单配置
  241. formData.value =
  242. elExtensionElements.value.values.filter((ex) => ex.$type === `${prefix}:FormData`)?.[0] ||
  243. bpmnInstances().moddle.create(`${prefix}:FormData`, { fields: [] })
  244. // 业务标识 businessKey, 绑定在 formData 中
  245. businessKey.value = formData.value.businessKey
  246. // 保留剩余扩展元素,便于后面更新该元素对应属性
  247. otherExtensions.value = elExtensionElements.value.values.filter(
  248. (ex) => ex.$type !== `${prefix}:FormData`
  249. )
  250. // 复制原始值,填充表格
  251. fieldList.value = JSON.parse(JSON.stringify(formData.value.fields || []))
  252. // 更新元素扩展属性,避免后续报错
  253. updateElementExtensions()
  254. }
  255. const updateElementFormKey = () => {
  256. bpmnInstances().modeling.updateProperties(toRaw(bpmnELement.value), {
  257. formKey: formKey.value
  258. })
  259. }
  260. const updateElementBusinessKey = () => {
  261. bpmnInstances().modeling.updateModdleProperties(toRaw(bpmnELement.value), formData.value, {
  262. businessKey: businessKey.value
  263. })
  264. }
  265. // 根据类型调整字段type
  266. const changeFieldTypeType = (type) => {
  267. // this.$set(this.formFieldForm, "type", type === "custom" ? "" : type);
  268. formFieldForm.value['type'] = type === 'custom' ? '' : type
  269. }
  270. // 打开字段详情侧边栏
  271. const openFieldForm = (field, index) => {
  272. formFieldIndex.value = index
  273. if (index !== -1) {
  274. const FieldObject = formData.value.fields[index]
  275. formFieldForm.value = JSON.parse(JSON.stringify(field))
  276. // 设置自定义类型
  277. // this.$set(this.formFieldForm, "typeType", !this.fieldType[field.type] ? "custom" : field.type);
  278. formFieldForm.value['typeType'] = !fieldType.value[field.type] ? 'custom' : field.type
  279. // 初始化枚举值列表
  280. field.type === 'enum' &&
  281. (fieldEnumList.value = JSON.parse(JSON.stringify(FieldObject?.values || [])))
  282. // 初始化约束条件列表
  283. fieldConstraintsList.value = JSON.parse(
  284. JSON.stringify(FieldObject?.validation?.constraints || [])
  285. )
  286. // 初始化自定义属性列表
  287. fieldPropertiesList.value = JSON.parse(JSON.stringify(FieldObject?.properties?.values || []))
  288. } else {
  289. formFieldForm.value = {}
  290. // 初始化枚举值列表
  291. fieldEnumList.value = []
  292. // 初始化约束条件列表
  293. fieldConstraintsList.value = []
  294. // 初始化自定义属性列表
  295. fieldPropertiesList.value = []
  296. }
  297. fieldModelVisible.value = true
  298. }
  299. // 打开字段 某个 配置项 弹窗
  300. const openFieldOptionForm = (option, index, type) => {
  301. fieldOptionModelVisible.value = true
  302. fieldOptionType.value = type
  303. formFieldOptionIndex.value = index
  304. if (type === 'property') {
  305. fieldOptionForm.value = option ? JSON.parse(JSON.stringify(option)) : {}
  306. return (optionModelTitle.value = '属性配置')
  307. }
  308. if (type === 'enum') {
  309. fieldOptionForm.value = option ? JSON.parse(JSON.stringify(option)) : {}
  310. return (optionModelTitle.value = '枚举值配置')
  311. }
  312. fieldOptionForm.value = option ? JSON.parse(JSON.stringify(option)) : {}
  313. return (optionModelTitle.value = '约束条件配置')
  314. }
  315. // 保存字段 某个 配置项
  316. const saveFieldOption = () => {
  317. if (formFieldOptionIndex.value === -1) {
  318. if (fieldOptionType.value === 'property') {
  319. fieldPropertiesList.value.push(fieldOptionForm.value)
  320. }
  321. if (fieldOptionType.value === 'constraint') {
  322. fieldConstraintsList.value.push(fieldOptionForm.value)
  323. }
  324. if (fieldOptionType.value === 'enum') {
  325. fieldEnumList.value.push(fieldOptionForm.value)
  326. }
  327. } else {
  328. fieldOptionType.value === 'property' &&
  329. fieldPropertiesList.value.splice(formFieldOptionIndex.value, 1, fieldOptionForm.value)
  330. fieldOptionType.value === 'constraint' &&
  331. fieldConstraintsList.value.splice(formFieldOptionIndex.value, 1, fieldOptionForm.value)
  332. fieldOptionType.value === 'enum' &&
  333. fieldEnumList.value.splice(formFieldOptionIndex.value, 1, fieldOptionForm.value)
  334. }
  335. fieldOptionModelVisible.value = false
  336. fieldOptionForm.value = {}
  337. }
  338. // 保存字段配置
  339. const saveField = () => {
  340. const { id, type, label, defaultValue, datePattern } = formFieldForm.value
  341. const Field = bpmnInstances().moddle.create(`${prefix}:FormField`, { id, type, label })
  342. defaultValue && (Field.defaultValue = defaultValue)
  343. datePattern && (Field.datePattern = datePattern)
  344. // 构建属性
  345. if (fieldPropertiesList.value && fieldPropertiesList.value.length) {
  346. const fieldPropertyList = fieldPropertiesList.value.map((fp) => {
  347. return bpmnInstances().moddle.create(`${prefix}:Property`, {
  348. id: fp.id,
  349. value: fp.value
  350. })
  351. })
  352. Field.properties = bpmnInstances().moddle.create(`${prefix}:Properties`, {
  353. values: fieldPropertyList
  354. })
  355. }
  356. // 构建校验规则
  357. if (fieldConstraintsList.value && fieldConstraintsList.value.length) {
  358. const fieldConstraintList = fieldConstraintsList.value.map((fc) => {
  359. return bpmnInstances().moddle.create(`${prefix}:Constraint`, {
  360. name: fc.name,
  361. config: fc.config
  362. })
  363. })
  364. Field.validation = bpmnInstances().moddle.create(`${prefix}:Validation`, {
  365. constraints: fieldConstraintList
  366. })
  367. }
  368. // 构建枚举值
  369. if (fieldEnumList.value && fieldEnumList.value.length) {
  370. Field.values = fieldEnumList.value.map((fe) => {
  371. return bpmnInstances().moddle.create(`${prefix}:Value`, { name: fe.name, id: fe.id })
  372. })
  373. }
  374. // 更新数组 与 表单配置实例
  375. if (formFieldIndex.value === -1) {
  376. fieldList.value.push(formFieldForm.value)
  377. formData.value.fields.push(Field)
  378. } else {
  379. fieldList.value.splice(formFieldIndex.value, 1, formFieldForm.value)
  380. formData.value.fields.splice(formFieldIndex.value, 1, Field)
  381. }
  382. updateElementExtensions()
  383. fieldModelVisible.value = false
  384. }
  385. // 移除某个 字段的 配置项
  386. const removeFieldOptionItem = (option, index, type) => {
  387. console.log(option, 'option')
  388. if (type === 'property') {
  389. fieldPropertiesList.value.splice(index, 1)
  390. return
  391. }
  392. if (type === 'enum') {
  393. fieldEnumList.value.splice(index, 1)
  394. return
  395. }
  396. fieldConstraintsList.value.splice(index, 1)
  397. }
  398. // 移除 字段
  399. const removeField = (field, index) => {
  400. console.log(field, 'field')
  401. fieldList.value.splice(index, 1)
  402. formData.value.fields.splice(index, 1)
  403. updateElementExtensions()
  404. }
  405. const updateElementExtensions = () => {
  406. // 更新回扩展元素
  407. const newElExtensionElements = bpmnInstances().moddle.create(`bpmn:ExtensionElements`, {
  408. values: otherExtensions.value.concat(formData.value)
  409. })
  410. // 更新到元素上
  411. bpmnInstances().modeling.updateProperties(toRaw(bpmnELement.value), {
  412. extensionElements: newElExtensionElements
  413. })
  414. }
  415. watch(
  416. () => props.id,
  417. (val) => {
  418. val &&
  419. val.length &&
  420. nextTick(() => {
  421. resetFormList()
  422. })
  423. },
  424. { immediate: true }
  425. )
  426. </script>