component.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <transition name="dialog-fade">
  3. <div class="el-dialog__wrapper" v-show="visible" @click.self="handleWrapperClick">
  4. <div class="el-dialog" :class="[sizeClass, customClass]" ref="dialog" :style="{ 'margin-bottom': size !== 'full' ? '50px' : '', 'top': size !== 'full' ? dynamicTop + 'px' : '0' }">
  5. <div class="el-dialog__header">
  6. <span class="el-dialog__title">{{title}}</span>
  7. <div class="el-dialog__headerbtn">
  8. <i class="el-dialog__close el-icon el-icon-close" @click='close()'></i>
  9. </div>
  10. </div>
  11. <div class="el-dialog__body"><slot></slot></div>
  12. <div class="el-dialog__footer">
  13. <slot name="footer"></slot>
  14. </div>
  15. </div>
  16. </div>
  17. </transition>
  18. </template>
  19. <script>
  20. import Popup from 'vue-popup';
  21. export default {
  22. name: 'el-dialog',
  23. mixins: [ Popup ],
  24. props: {
  25. title: {
  26. type: String,
  27. default: ''
  28. },
  29. modal: {
  30. type: Boolean,
  31. default: true
  32. },
  33. closeOnClickModal: {
  34. type: Boolean,
  35. default: true
  36. },
  37. closeOnPressEscape: {
  38. type: Boolean,
  39. default: true
  40. },
  41. size: {
  42. type: String,
  43. default: 'small'
  44. },
  45. customClass: {
  46. type: String,
  47. default: ''
  48. }
  49. },
  50. data() {
  51. return {
  52. dynamicTop: 0
  53. };
  54. },
  55. watch: {
  56. computedVisible(val) {
  57. if (val) {
  58. this.$emit('open');
  59. this.$nextTick(() => {
  60. this.$refs.dialog.scrollTop = 0;
  61. });
  62. } else {
  63. this.$emit('close');
  64. }
  65. }
  66. },
  67. computed: {
  68. sizeClass() {
  69. return `el-dialog--${ this.size }`;
  70. }
  71. },
  72. methods: {
  73. handleWrapperClick() {
  74. if (this.closeOnClickModal) {
  75. this.computedVisible = false;
  76. }
  77. },
  78. resetTop() {
  79. this.dynamicTop = Math.floor((window.innerHeight || document.documentElement.clientHeight) * 0.16);
  80. }
  81. },
  82. mounted() {
  83. if (this.visible) {
  84. this.rendered = true;
  85. this.open();
  86. }
  87. window.addEventListener('resize', this.resetTop);
  88. this.resetTop();
  89. },
  90. beforeDestroy() {
  91. window.removeEventListener('resize', this.resetTop);
  92. }
  93. };
  94. </script>