ElementProperties.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. console.log(window, 'windowwindowwindowwindowwindowwindowwindow')
  75. bpmnElement.value = bpmnInstances().bpmnElement
  76. otherExtensionList.value = [] // 其他扩展配置
  77. bpmnElementProperties.value =
  78. // bpmnElement.value.businessObject?.extensionElements?.filter((ex) => {
  79. bpmnElement.value.businessObject?.extensionElements?.values.filter((ex) => {
  80. if (ex.$type !== `${prefix}:Properties`) {
  81. otherExtensionList.value.push(ex)
  82. }
  83. return ex.$type === `${prefix}:Properties`
  84. }) ?? []
  85. // 保存所有的 扩展属性字段
  86. bpmnElementPropertyList.value = bpmnElementProperties.value.reduce(
  87. (pre, current) => pre.concat(current.values),
  88. []
  89. )
  90. // 复制 显示
  91. elementPropertyList.value = JSON.parse(JSON.stringify(bpmnElementPropertyList.value ?? []))
  92. }
  93. const openAttributesForm = (attr, index) => {
  94. editingPropertyIndex.value = index
  95. propertyForm.value = index === -1 ? {} : JSON.parse(JSON.stringify(attr))
  96. propertyFormModelVisible.value = true
  97. nextTick(() => {
  98. if (attributeFormRef.value) attributeFormRef.value.clearValidate()
  99. })
  100. }
  101. const removeAttributes = (attr, index) => {
  102. console.log(attr, 'attr')
  103. ElMessageBox.confirm('确认移除该属性吗?', '提示', {
  104. confirmButtonText: '确 认',
  105. cancelButtonText: '取 消'
  106. })
  107. .then(() => {
  108. elementPropertyList.value.splice(index, 1)
  109. bpmnElementPropertyList.value.splice(index, 1)
  110. // 新建一个属性字段的保存列表
  111. const propertiesObject = bpmnInstances().moddle.create(`${prefix}:Properties`, {
  112. values: bpmnElementPropertyList.value
  113. })
  114. updateElementExtensions(propertiesObject)
  115. resetAttributesList()
  116. })
  117. .catch(() => console.info('操作取消'))
  118. }
  119. const saveAttribute = () => {
  120. console.log(propertyForm.value, 'propertyForm.value')
  121. const { name, value } = propertyForm.value
  122. if (editingPropertyIndex.value !== -1) {
  123. bpmnInstances().modeling.updateModdleProperties(
  124. toRaw(bpmnElement.value),
  125. toRaw(bpmnElementPropertyList.value)[toRaw(editingPropertyIndex.value)],
  126. {
  127. name,
  128. value
  129. }
  130. )
  131. } else {
  132. // 新建属性字段
  133. const newPropertyObject = bpmnInstances().moddle.create(`${prefix}:Property`, {
  134. name,
  135. value
  136. })
  137. // 新建一个属性字段的保存列表
  138. const propertiesObject = bpmnInstances().moddle.create(`${prefix}:Properties`, {
  139. values: bpmnElementPropertyList.value.concat([newPropertyObject])
  140. })
  141. updateElementExtensions(propertiesObject)
  142. }
  143. propertyFormModelVisible.value = false
  144. resetAttributesList()
  145. }
  146. const updateElementExtensions = (properties) => {
  147. const extensions = bpmnInstances().moddle.create('bpmn:ExtensionElements', {
  148. values: otherExtensionList.value.concat([properties])
  149. })
  150. bpmnInstances().modeling.updateProperties(toRaw(bpmnElement.value), {
  151. extensionElements: extensions
  152. })
  153. }
  154. watch(
  155. () => props.id,
  156. (val) => {
  157. if (val) {
  158. val && val.length && resetAttributesList()
  159. }
  160. },
  161. { immediate: true }
  162. )
  163. </script>