CzrDialogDrag.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <div
  3. class="__czr-dialog"
  4. v-drag="{ ...layout }"
  5. v-if="state.showDialog"
  6. :style="{ zIndex: state.zIndex }"
  7. @click="() => (state.zIndex = DialogStore.addZIndex())"
  8. >
  9. <div class="el-dialog" style="width: 100%; margin: 0">
  10. <div class="el-dialog__header">
  11. <div class="_czr-dialog-head" v-if="title">
  12. <div class="__cdh-title">{{ title }}</div>
  13. <div class="__cdh-slot">
  14. <slot name="head" />
  15. </div>
  16. <div class="__cdh-close __hover" @click="CDClose()">
  17. <SvgIcon name="czr_close" size="16" color="#303133" />
  18. </div>
  19. </div>
  20. </div>
  21. <div class="el-dialog__body">
  22. <div
  23. class="__czr-dialog-main"
  24. :class="{ isFull: full !== false }"
  25. v-loading="loading"
  26. >
  27. <div class="__czr-dialog-content">
  28. <slot />
  29. </div>
  30. <div
  31. class="__czr-dialog-foot"
  32. :style="`justify-content: ${footAlign};`"
  33. v-if="showSubmit || showClose || $slots.foot"
  34. >
  35. <slot name="foot" :close="CDClose" :submit="CDSubmit" />
  36. <template v-if="footAlign === 'center'">
  37. <div
  38. v-if="showSubmit"
  39. class="__czr-dialog-foot_submit __hover"
  40. @click="CDSubmit"
  41. >
  42. {{ submitText }}
  43. </div>
  44. <div
  45. v-if="showClose"
  46. class="__czr-dialog-foot_cancel __hover"
  47. @click="CDClose()"
  48. >
  49. {{ closeText }}
  50. </div>
  51. </template>
  52. <template v-else>
  53. <div
  54. v-if="showClose"
  55. class="__czr-dialog-foot_cancel __hover"
  56. @click="CDClose()"
  57. >
  58. {{ closeText }}
  59. </div>
  60. <div
  61. v-if="showSubmit"
  62. class="__czr-dialog-foot_submit __hover"
  63. @click="CDSubmit"
  64. >
  65. {{ submitText }}
  66. </div>
  67. </template>
  68. </div>
  69. </div>
  70. </div>
  71. </div>
  72. </div>
  73. </template>
  74. <script setup lang="ts">
  75. import { useDialogStore } from '@/stores'
  76. defineOptions({
  77. name: 'CzrDialogDrag',
  78. })
  79. import { isValue } from '@/utils/czr-util'
  80. import { computed, onMounted, reactive, watch } from 'vue'
  81. import { ElMessage, ElMessageBox } from 'element-plus'
  82. import { v4 } from 'uuid'
  83. const DialogStore = useDialogStore()
  84. const emit = defineEmits(['update:show', 'onClose'])
  85. const props = defineProps({
  86. show: { required: true, type: Boolean },
  87. title: { default: '' },
  88. width: { default: '50%' },
  89. full: { default: false },
  90. submitText: { default: '确定' },
  91. closeText: { default: '取消' },
  92. showClose: { default: true },
  93. showSubmit: { default: true },
  94. footAlign: { default: 'center' },
  95. height: { default: 'auto' },
  96. maxHeight: { default: 'unset' },
  97. minHeight: { default: 'unset' },
  98. closeOnClickModal: { default: false },
  99. closeOnPressEscape: { default: false },
  100. closeConfirm: { default: false },
  101. closeConfirmText: {
  102. default: {
  103. title: null,
  104. message: null,
  105. submit: null,
  106. close: null,
  107. },
  108. },
  109. loading: {
  110. default: false,
  111. type: Boolean,
  112. },
  113. zIndex: {},
  114. layout: { default: {} },
  115. })
  116. const state = reactive({
  117. showDialog: false,
  118. closeConfirmTextTemp: {
  119. title: '提示',
  120. message: '请确认是否关闭?',
  121. submit: '确定',
  122. close: '取消',
  123. },
  124. uuid: v4(),
  125. zIndex: props.zIndex || DialogStore.addZIndex(),
  126. })
  127. watch(
  128. () => props.show,
  129. (n) => {
  130. state.showDialog = n
  131. },
  132. )
  133. const beforeClose = (done) => {
  134. CDClose(done)
  135. }
  136. const closeConfirmTextCpt: any = computed(() => {
  137. return {
  138. title: isValue(props.closeConfirmText.title)
  139. ? props.closeConfirmText.title
  140. : state.closeConfirmTextTemp.title,
  141. message: isValue(props.closeConfirmText.message)
  142. ? props.closeConfirmText.message
  143. : state.closeConfirmTextTemp.message,
  144. submit: isValue(props.closeConfirmText.submit)
  145. ? props.closeConfirmText.submit
  146. : state.closeConfirmTextTemp.submit,
  147. close: isValue(props.closeConfirmText.close)
  148. ? props.closeConfirmText.close
  149. : state.closeConfirmTextTemp.close,
  150. }
  151. })
  152. const CDClose = async (done = () => {}) => {
  153. if (props.closeConfirm !== false) {
  154. await ElMessageBox.confirm(
  155. closeConfirmTextCpt.value.message,
  156. closeConfirmTextCpt.value.title,
  157. {
  158. confirmButtonText: closeConfirmTextCpt.value.submit,
  159. cancelButtonText: closeConfirmTextCpt.value.close,
  160. type: 'warning',
  161. },
  162. )
  163. .then(() => {
  164. emit('update:show', false)
  165. emit('onClose')
  166. done()
  167. })
  168. .catch(() => {})
  169. } else {
  170. emit('update:show', false)
  171. emit('onClose')
  172. done()
  173. }
  174. }
  175. const CDSubmit = () => {
  176. emit('onSubmit')
  177. }
  178. onMounted(() => {
  179. state.showDialog = props.show
  180. })
  181. </script>
  182. <style scoped lang="scss"></style>