ElementProperties.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <div class="panel-tab__content">
  3. <el-table :data="elementPropertyList" max-height="240" fit border>
  4. <el-table-column label="序号" width="50px" type="index" />
  5. <el-table-column label="属性名" prop="name" min-width="100px" show-overflow-tooltip />
  6. <el-table-column label="属性值" prop="value" min-width="100px" show-overflow-tooltip />
  7. <el-table-column label="操作" width="110px">
  8. <template #default="scope">
  9. <el-button link @click="openAttributesForm(scope.row, scope.$index)" size="small">
  10. 编辑
  11. </el-button>
  12. <el-divider direction="vertical" />
  13. <el-button
  14. link
  15. size="small"
  16. style="color: #ff4d4f"
  17. @click="removeAttributes(scope.row, scope.$index)"
  18. >
  19. 移除
  20. </el-button>
  21. </template>
  22. </el-table-column>
  23. </el-table>
  24. <div class="element-drawer__button">
  25. <XButton
  26. type="primary"
  27. preIcon="ep:plus"
  28. title="添加属性"
  29. @click="openAttributesForm(null, -1)"
  30. />
  31. </div>
  32. <el-dialog
  33. v-model="propertyFormModelVisible"
  34. title="属性配置"
  35. width="600px"
  36. append-to-body
  37. destroy-on-close
  38. >
  39. <el-form :model="propertyForm" label-width="80px" ref="attributeFormRef">
  40. <el-form-item label="属性名:" prop="name">
  41. <el-input v-model="propertyForm.name" clearable />
  42. </el-form-item>
  43. <el-form-item label="属性值:" prop="value">
  44. <el-input v-model="propertyForm.value" clearable />
  45. </el-form-item>
  46. </el-form>
  47. <template #footer>
  48. <el-button @click="propertyFormModelVisible = false">取 消</el-button>
  49. <el-button type="primary" @click="saveAttribute">确 定</el-button>
  50. </template>
  51. </el-dialog>
  52. </div>
  53. </template>
  54. <script lang="ts" setup>
  55. import { ElMessageBox } from 'element-plus'
  56. defineOptions({ name: 'ElementProperties' })
  57. const props = defineProps({
  58. id: String,
  59. type: String
  60. })
  61. const prefix = inject('prefix')
  62. // const width = inject('width')
  63. const elementPropertyList = ref<any[]>([])
  64. const propertyForm = ref<any>({})
  65. const editingPropertyIndex = ref(-1)
  66. const propertyFormModelVisible = ref(false)
  67. const bpmnElement = ref()
  68. const otherExtensionList = ref()
  69. const bpmnElementProperties = ref()
  70. const bpmnElementPropertyList = ref()
  71. const attributeFormRef = ref()
  72. const bpmnInstances = () => (window as any)?.bpmnInstances
  73. const resetAttributesList = () => {
  74. bpmnElement.value = bpmnInstances().bpmnElement
  75. otherExtensionList.value = [] // 其他扩展配置
  76. bpmnElementProperties.value =
  77. // bpmnElement.value.businessObject?.extensionElements?.filter((ex) => {
  78. bpmnElement.value.businessObject?.extensionElements?.values?.filter((ex) => {
  79. if (ex.$type !== `${prefix}:Properties`) {
  80. otherExtensionList.value.push(ex)
  81. }
  82. return ex.$type === `${prefix}:Properties`
  83. }) ?? [];
  84. // 保存所有的 扩展属性字段
  85. bpmnElementPropertyList.value = bpmnElementProperties.value.reduce(
  86. (pre, current) => pre.concat(current.values),
  87. []
  88. )
  89. // 复制 显示
  90. elementPropertyList.value = JSON.parse(JSON.stringify(bpmnElementPropertyList.value ?? []))
  91. }
  92. const openAttributesForm = (attr, index) => {
  93. editingPropertyIndex.value = index
  94. propertyForm.value = index === -1 ? {} : JSON.parse(JSON.stringify(attr))
  95. propertyFormModelVisible.value = true
  96. nextTick(() => {
  97. if (attributeFormRef.value) attributeFormRef.value.clearValidate()
  98. })
  99. }
  100. const removeAttributes = (attr, index) => {
  101. console.log(attr, 'attr')
  102. ElMessageBox.confirm('确认移除该属性吗?', '提示', {
  103. confirmButtonText: '确 认',
  104. cancelButtonText: '取 消'
  105. })
  106. .then(() => {
  107. elementPropertyList.value.splice(index, 1)
  108. bpmnElementPropertyList.value.splice(index, 1)
  109. // 新建一个属性字段的保存列表
  110. const propertiesObject = bpmnInstances().moddle.create(`${prefix}:Properties`, {
  111. values: bpmnElementPropertyList.value
  112. })
  113. updateElementExtensions(propertiesObject)
  114. resetAttributesList()
  115. })
  116. .catch(() => console.info('操作取消'))
  117. }
  118. const saveAttribute = () => {
  119. console.log(propertyForm.value, 'propertyForm.value')
  120. const { name, value } = propertyForm.value
  121. if (editingPropertyIndex.value !== -1) {
  122. bpmnInstances().modeling.updateModdleProperties(
  123. toRaw(bpmnElement.value),
  124. toRaw(bpmnElementPropertyList.value)[toRaw(editingPropertyIndex.value)],
  125. {
  126. name,
  127. value
  128. }
  129. )
  130. } else {
  131. // 新建属性字段
  132. const newPropertyObject = bpmnInstances().moddle.create(`${prefix}:Property`, {
  133. name,
  134. value
  135. })
  136. // 新建一个属性字段的保存列表
  137. const propertiesObject = bpmnInstances().moddle.create(`${prefix}:Properties`, {
  138. values: bpmnElementPropertyList.value.concat([newPropertyObject])
  139. })
  140. updateElementExtensions(propertiesObject)
  141. }
  142. propertyFormModelVisible.value = false
  143. resetAttributesList()
  144. }
  145. const updateElementExtensions = (properties) => {
  146. const extensions = bpmnInstances().moddle.create('bpmn:ExtensionElements', {
  147. values: otherExtensionList.value.concat([properties])
  148. })
  149. bpmnInstances().modeling.updateProperties(toRaw(bpmnElement.value), {
  150. extensionElements: extensions
  151. })
  152. }
  153. watch(
  154. () => props.id,
  155. (val) => {
  156. if (val) {
  157. val && val.length && resetAttributesList()
  158. }
  159. },
  160. { immediate: true }
  161. )
  162. </script>