element dialog源碼


<template>
  <transition
    name="dialog-fade"
    @after-enter="afterEnter"
    @after-leave="afterLeave">
    <div class="el-dialog__wrapper" v-show="visible" @click.self="handleWrapperClick">
      <div
        role="dialog"
        aria-modal="true"
        :aria-label="title || 'dialog'"
        class="el-dialog"
        :class="[{ 'is-fullscreen': fullscreen, 'el-dialog--center': center }, customClass]"
        ref="dialog"
        :style="style">
        <div class="el-dialog__header">
          <slot name="title">
            <span class="el-dialog__title">{{ title }}</span>
          </slot>
          <button
            type="button"
            class="el-dialog__headerbtn"
            aria-label="Close"
            v-if="showClose"
            @click="handleClose">
            <i class="el-dialog__close el-icon el-icon-close"></i>
          </button>
        </div>
        <div class="el-dialog__body" v-if="rendered"><slot></slot></div>
        <div class="el-dialog__footer" v-if="$slots.footer">
          <slot name="footer"></slot>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
  import Popup from 'element-ui/src/utils/popup';
  import Migrating from 'element-ui/src/mixins/migrating';
  import emitter from 'element-ui/src/mixins/emitter';

  export default {
    name: 'ElDialog',

    mixins: [Popup, emitter, Migrating],

    props: {
      // Dialog 的標題,也可通過具名 slot (見下表)傳入
      title: {
        type: String,
        default: ''
      },
      // modal    是否需要遮罩層
      modal: {
        type: Boolean,
        default: true
      },
      // 遮罩層是否插入至 body 元素上,若為 false,則遮罩層會插入至 Dialog 的父元素上
      modalAppendToBody: {
        type: Boolean,
        default: true
      },
      // Dialog 自身是否插入至 body 元素上。嵌套的 Dialog 必須指定該屬性並賦值為 true
      appendToBody: {
        type: Boolean,
        default: false
      },
      // lock-scroll    是否在 Dialog 出現時將 body 滾動鎖定
      lockScroll: {
        type: Boolean,
        default: true
      },
      // close-on-click-modal    是否可以通過點擊 modal 關閉 Dialog
      closeOnClickModal: {
        type: Boolean,
        default: true
      },
      // close-on-press-escape    是否可以通過按下 ESC 關閉 Dialog    
      closeOnPressEscape: {
        type: Boolean,
        default: true
      },
      // show-close    是否顯示關閉按鈕
      showClose: {
        type: Boolean,
        default: true
      },
      // width    Dialog 的寬度
      width: String,
      // fullscreen    是否為全屏 Dialog 
      fullscreen: Boolean,
      // custom-class    Dialog 的自定義類名
      customClass: {
        type: String,
        default: ''
      },
      // top    Dialog CSS 中的 margin-top 值
      top: {
        type: String,
        default: '15vh'
      },
      // before-close    關閉前的回調,會暫停 Dialog 的關閉
      beforeClose: Function,
      // center    是否對頭部和底部采用居中布局
      center: {
        type: Boolean,
        default: false
      }
    },

    data() {
      return {
        closed: false
      };
    },

    watch: {
      // 是否顯示 Dialog,支持 .sync 修飾符
      visible(val) {
        if (val) {
          this.closed = false;
          this.$emit('open');
          this.$el.addEventListener('scroll', this.updatePopper);
          this.$nextTick(() => {
            this.$refs.dialog.scrollTop = 0;
          });
          if (this.appendToBody) {
            document.body.appendChild(this.$el);
          }
        } else {
          this.$el.removeEventListener('scroll', this.updatePopper);
          if (!this.closed) this.$emit('close');
        }
      }
    },

    computed: {
      style() {
        let style = {};
        if (!this.fullscreen) {
          style.marginTop = this.top;
          if (this.width) {
            style.width = this.width;
          }
        }
        return style;
      }
    },

    methods: {
      // 暫時沒發現這個什么用
      getMigratingConfig() {
        return {
          props: {
            'size': 'size is removed.'
          }
        };
      },
      // 點擊dialog自身
      handleWrapperClick() {
        if (!this.closeOnClickModal) return;
        this.handleClose();
      },
      // 關閉dialog
      handleClose() {
        // 如果有beforeClose,執行beforeClose
        if (typeof this.beforeClose === 'function') {
          this.beforeClose(this.hide);
        } else {
          // 否則隱藏
          this.hide();
        }
      },
      // 隱藏dialog
      hide(cancel) {
        if (cancel !== false) {
          this.$emit('update:visible', false);
          this.$emit('close');
          this.closed = true;
        }
      },
      updatePopper() {
        this.broadcast('ElSelectDropdown', 'updatePopper');
        this.broadcast('ElDropdownMenu', 'updatePopper');
      },
      // Dialog 打開動畫結束時的回調
      afterEnter() {
        this.$emit('opened');
      },
      // Dialog 關閉動畫結束時的回調
      afterLeave() {
        this.$emit('closed');
      }
    },

    mounted() {
      if (this.visible) {
        this.rendered = true;
        this.open();
        if (this.appendToBody) {
          document.body.appendChild(this.$el);
        }
      }
    },
    // 銷毀前移除dom
    destroyed() {
      // if appendToBody is true, remove DOM node after destroy
      if (this.appendToBody && this.$el && this.$el.parentNode) {
        this.$el.parentNode.removeChild(this.$el);
      }
    }
  };
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM