CusDialog.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <el-dialog
  3. :style="`
  4. --cus-dialog_height: ${height};
  5. --cus-dialog_max-height: ${maxHeight};
  6. --cus-dialog_min-height: ${minHeight};
  7. background-color: transparent;
  8. `"
  9. :modal-class="`
  10. ${uuid}
  11. __cus-dialog ${maxHeight === 'unset' ? '' : '__cus-dialog-auto-height'} ${hiddenStyle ? '__cus-dialog-hidden-style' : ''}
  12. `"
  13. v-bind="$attrs"
  14. v-model="showDialog"
  15. :title="title"
  16. :width="width"
  17. align-center
  18. :before-close="beforeClose"
  19. :show-close="false"
  20. :append-to-body="$util.isValue($attrs.appendToBody) ? $attrs.appendToBody : true"
  21. :close-on-click-modal="closeOnClickModal"
  22. :close-on-press-escape="closeOnPressEscape"
  23. >
  24. <template #header>
  25. <div class="_cus-dialog-head" v-if="title">
  26. <div class="__cdh-title">{{title}}</div>
  27. <div class="__cdh-slot">
  28. <slot name="head"/>
  29. </div>
  30. <div class="__cdh-close __hover" @click="CDClose()">
  31. <SvgIcon name="close_3" size="16" color="#ffffff"/>
  32. </div>
  33. </div>
  34. </template>
  35. <div class="__cus-dialog-main" :class="{isFull: full !== false}" v-loading="loading">
  36. <div class="__cus-dialog-content">
  37. <slot/>
  38. </div>
  39. <div class="__cus-dialog-foot" :style="`justify-content: ${footAlign};padding: ${footPadding};`" v-if="showSubmit || showClose || $slots.foot">
  40. <slot name="foot" :close="CDClose" :submit="CDSubmit"/>
  41. <template v-if="footAlign === 'center'">
  42. <div v-if="showSubmit" class="__cus-dialog-foot_submit __hover" @click="CDSubmit">{{submitText}}</div>
  43. <div v-if="showClose" class="__cus-dialog-foot_cancel __hover" @click="CDClose()">{{closeText}}</div>
  44. </template>
  45. <template v-else>
  46. <div v-if="showClose" class="__cus-dialog-foot_cancel __hover" @click="CDClose()">{{closeText}}</div>
  47. <div v-if="showSubmit" class="__cus-dialog-foot_submit __hover" @click="CDSubmit">{{submitText}}</div>
  48. </template>
  49. </div>
  50. </div>
  51. </el-dialog>
  52. </template>
  53. <script lang="ts">
  54. import {
  55. defineComponent,
  56. computed,
  57. onMounted,
  58. ref,
  59. reactive,
  60. watch,
  61. getCurrentInstance,
  62. ComponentInternalInstance,
  63. toRefs,
  64. nextTick
  65. } from 'vue'
  66. import {ElMessage, ElMessageBox} from "element-plus";
  67. import { v4 } from "uuid";
  68. import {useDialogLevelStore} from "@/stores/dialog-level";
  69. export default defineComponent({
  70. name: 'CusDialog',
  71. components: {},
  72. props: {
  73. show: {required: true, type: Boolean},
  74. title: {default: ''},
  75. width: {default: '50%'},
  76. full: {default: false},
  77. submitText: {default: '确定'},
  78. closeText: {default: '取消'},
  79. showClose: {default: true},
  80. showSubmit: {default: true},
  81. footAlign: {default: 'center'},
  82. footPadding: {default: '16px 26px'},
  83. height: {default: '60%'},
  84. maxHeight: {default: 'unset'},
  85. minHeight: {default: 'unset'},
  86. closeOnClickModal: {default: false},
  87. closeOnPressEscape: {default: false},
  88. closeConfirm: {default: false},
  89. closeConfirmText: {default: {
  90. title: null,
  91. message: null,
  92. submit: null,
  93. close: null,
  94. }},
  95. loading: {
  96. default: false,
  97. type: Boolean
  98. },
  99. hiddenStyle: {
  100. default: false
  101. }
  102. },
  103. setup(props, {emit}) {
  104. const that = (getCurrentInstance() as ComponentInternalInstance).appContext.config.globalProperties
  105. const DialogLevelStore = useDialogLevelStore()
  106. const state = reactive({
  107. showDialog: false,
  108. closeConfirmTextTemp: {
  109. title: '提示',
  110. message: '请确认是否关闭?',
  111. submit: '确定',
  112. close: '取消',
  113. },
  114. uuid: v4()
  115. })
  116. watch(() => props.show, (n) => {
  117. state.showDialog = n
  118. if (n) {
  119. DialogLevelStore.add(state.uuid)
  120. } else {
  121. DialogLevelStore.del(state.uuid)
  122. }
  123. }, {immediate: true})
  124. const beforeClose = (done) => {
  125. CDClose(done)
  126. }
  127. const closeConfirmTextCpt: any = computed(() => {
  128. return {
  129. title: that.$util.isValue(props.closeConfirmText.title) ? props.closeConfirmText.title : state.closeConfirmTextTemp.title,
  130. message: that.$util.isValue(props.closeConfirmText.message) ? props.closeConfirmText.message : state.closeConfirmTextTemp.message,
  131. submit: that.$util.isValue(props.closeConfirmText.submit) ? props.closeConfirmText.submit : state.closeConfirmTextTemp.submit,
  132. close: that.$util.isValue(props.closeConfirmText.close) ? props.closeConfirmText.close : state.closeConfirmTextTemp.close,
  133. }
  134. })
  135. const CDClose = async (done = () => {}) => {
  136. if (props.closeConfirm !== false) {
  137. await ElMessageBox.confirm(closeConfirmTextCpt.value.message, closeConfirmTextCpt.value.title, {
  138. confirmButtonText: closeConfirmTextCpt.value.submit,
  139. cancelButtonText: closeConfirmTextCpt.value.close,
  140. type: "warning",
  141. }).then(() => {
  142. emit('update:show', false)
  143. emit('onClose')
  144. done()
  145. }).catch(() => {})
  146. } else {
  147. emit('update:show', false)
  148. emit('onClose')
  149. done()
  150. }
  151. }
  152. const CDSubmit = () => {
  153. emit('onSubmit')
  154. }
  155. onMounted(() => {
  156. state.showDialog = props.show
  157. })
  158. return {
  159. ...toRefs(state),
  160. beforeClose,
  161. CDClose,
  162. CDSubmit,
  163. }
  164. },
  165. })
  166. </script>
  167. <style scoped lang="scss">
  168. </style>