index.vue 8.16 KB
<template>
  <div class="gl-dialog-wrap " :class="mode" :id="idDom">
    <el-dialog v-bind="$attrs" v-on="$listeners">
      <template #title>
        <!-- 标题插槽 -->
        <slot name="title" v-if="!bigTitle"></slot>
        <template v-if="bigTitle">
          <template v-if="mode === 'big'">
            <span class="el-dialog__title" v-if="!$slots.custombigtitle">
              {{ bigTitle }}
            </span>
            <span class="el-dialog__title" v-if="$slots.custombigtitle">
              <!-- 大弹窗自定义插槽 -->
              <slot name="custombigtitle"></slot>
            </span>
            <button type="button" aria-label="Close" class="el-dialog__headerbtn">
              <!-- <i v-if="mode === 'big'" class="el-icon-full-screen full-screen" @click="fullScreenFn"></i> -->
              <!-- <i v-if="mode === 'big'" :class="isFull ? 's-full-screen' : 'full-screen'" @click="fullScreenFn"></i> -->
              <!-- <i class="el-icon-close "></i> -->
            </button>
          </template>
        </template>
      </template>

      <template #default>
        <!-- 默认插槽 -->
        <slot name="default" v-if="!content"></slot>
        <template v-if="content">
          <div v-if="mode === 'small'" class="small-content">
            <i :class="iconType"></i>
            <div class="content">{{ content }}</div>
          </div>
          <div v-else class="content">{{ content }}</div>
        </template>
      </template>
      <template #footer v-if="hasFooter">
        <template v-if="!$slots.footer">
          <span class="dialog-footer">
            <el-button type="primary" @click="ok">{{ okName }}</el-button>
            <el-button plain class="btn-three" @click="cancel">{{ cancelName }}</el-button>
          </span>
        </template>
        <!-- footer插槽 -->
        <slot name="footer" v-if="$slots.footer"></slot>
      </template>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'GLDialog',
  inheritAttrs: false,
  // 传入width会覆盖默认的宽度
  props: {
    // 弹框模式
    mode: {
      type: String,
      //  `big` / `mid` / `small`
      default: 'mid'
    },
    // 是否有按钮
    hasFooter: {
      type: Boolean,
      default: true
    },
    // 取消按钮名称
    cancelText: {
      type: String,
      default: ''
    },
    // 确定按钮名称
    okText: {
      type: String,
      default: ''
    },
    // 大标题(不与title(低)共存)
    bigTitle: {
      type: String,
      default: ''
    },
    // 内容(small生效)
    content: {
      type: String,
      default: ''
    },
    // 消息类型(图标,small生效)
    type: {
      type: String,
      // `success` / `info` / `warning` / `error`
      default: ''
    },
    // 是否添加到body
    appendToBody: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isFull: false,
      idDom: Math.random().toString(36).slice(-8)
    }
  },
  mounted() {
    document.addEventListener('keyup', (e) => {
      this.keyupHook(e)
    })
    if (this.appendToBody) {
      document.body.appendChild(this.$el)
    }
  },
  computed: {
    okName: function () {
      let resName
      switch (this.mode) {
        case 'small':
          resName = this.okText ? this.okText : '是'
          break
        default:
          resName = this.okText ? this.okText : '确定'
          break
      }
      return resName
    },
    cancelName: function () {
      let resName
      switch (this.mode) {
        case 'small':
          resName = this.cancelText ? this.cancelText : '否'
          break
        default:
          resName = this.cancelText ? this.cancelText : '取消'
          break
      }
      return resName
    },
    iconType: function () {
      let type
      switch (this.type) {
        case 'success':
          type = 'el-icon-success'
          break
        case 'info':
          type = 'el-icon-info'
          break
        case 'warning':
          type = 'el-icon-warning'
          break
        case 'error':
          type = 'el-icon-error'
          break
      }
      return type
    }
  },
  methods: {
    ok() {
      // 这是确认事件
      this.$emit('onOk')
    },
    cancel() {
      // 这是取消事件
      this.$emit('onCancel')
    },
    escFn() {
      setTimeout(() => {
        this.isFull = false
        this.exitFullscreenHook()
      }, 300)
    },
    fullScreenFn() {
      if (this.isFull) {
        this.exitFullscreenHook()
        this.isFull = false
      } else {
        this.fullScreenHook()
        this.isFull = true
      }
    },
    fullScreenHook() {
      const rootdom = document.getElementById(this.idDom)
      const dialog = rootdom.getElementsByClassName('el-dialog')[0]
      const dialogBody = rootdom.getElementsByClassName('el-dialog__body')[0]
      dialog.style.margin = 0
      dialog.style.width = window.innerWidth + 'px'
      dialog.style.height = window.innerHeight + 'px'
      // dialogBody.style.maxHeight = window.innerHeight -60 -40 -64 + 'px'
      dialogBody.style.minHeight = window.innerHeight - 60 - 40 - 64 + 'px'
    },
    exitFullscreenHook() {
      const rootdom = document.getElementById(this.idDom)
      const dialog = rootdom.getElementsByClassName('el-dialog')[0]
      const dialogBody = rootdom.getElementsByClassName('el-dialog__body')[0]
      if (dialogBody) {
        // dialogBody.style.maxHeight = '586px'
        dialogBody.style.minHeight = '156px'
      }
      dialog.style.margin = '15vh auto 50px'
      dialog.style.width = 1080 + 'px'
      dialog.style.height = 'auto'
    },
    keyupHook(e) {
      if (this.isFull) {
        if (e.key === 'Escape') {
          this.escFn()
        }
      }
    }
  },
  beforeDestroy() {
    document.removeEventListener('keyup', (e) => {
      this.keyupHook(e)
    })
    if (this.appendToBody && this.$el && this.$el.parentNode) {
      this.$el.parentNode.removeChild(this.$el)
    }
  }
}
</script>

<style lang="scss">
.gl-dialog-wrap {
  &.big {
    .el-dialog {
      width: 1080px;
    }

    .el-dialog__body {
      min-height: 156px;
      max-height: 586px;
    }
  }

  &.mid {
    .el-dialog {
      width: 720px;
    }

    .el-dialog__body {
      min-height: 156px;
      max-height: 586px;
    }
  }

  &.small {
    .el-dialog {
      width: 576px;
    }

    .el-dialog__body {
      height: 152px;
    }

    .el-dialog__footer {
      text-align: center;
    }

    .small-content {
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      .el-icon-success {
        font-size: 48px;
        color: #67c23a;
      }

      .el-icon-info {
        font-size: 48px;
        color: #909399;
      }

      .el-icon-warning {
        font-size: 48px;
        color: #e6a23c;
      }

      .el-icon-error {
        font-size: 48px;
        color: #f56c6c;
      }

      .content {
        margin-top: 16px;
      }
    }
  }

  .el-dialog__header {
    padding: 16px 20px;

    .el-dialog__headerbtn {
      top: 18px;
      font-size: 24px;
      font-weight: 600;
      line-height: 24px;
      color: #000;
      // .full-screen {
      //   display: block;
      //   margin-right: 34px;
      //   width: 24px;
      //   height: 24px;
      //   background-image: url('../../../assets/full-screen.png');
      //   background-size: 100% 100%;
      //   // &:hover {
      //   //   color: $color-primary;
      //   // }
      // }
      // .s-full-screen {
      //   display: block;
      //   margin-right: 34px;
      //   width: 24px;
      //   height: 24px;
      //   background-image: url('../../../assets/s-full-screen.png');
      //   background-size: 100% 100%;
      // }
    }
  }

  .el-dialog__body {
    padding: 20px;
    overflow: auto;
  }

  .el-dialog__footer {
    padding: 15px 20px 16px;
  }

  .el-table .el-table__row td {
    height: 40px;
  }
}
</style>