ElementProperties.vue 5.4 KB

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