all files / src/mixins/popup/ index.js

18.18% Statements 12/66
10.26% Branches 4/39
10% Functions 1/10
10.71% Lines 6/56
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159                                                                                                                                                                                                                                                                                                                    
import Vue from 'vue';
import merge from 'src/utils/merge';
import PopupManager from './popup-manager';
 
let idSeed = 1;
 
const getDOM = function(dom) {
  if (dom.nodeType === 3) {
    dom = dom.nextElementSibling || dom.nextSibling;
    getDOM(dom);
  }
  return dom;
};
 
export default {
  props: {
    /**
     * popup当前显示状态
     */
    value: {
      type: Boolean,
      default: false
    },
    /**
     * 是否显示遮罩层
     */
    overlay: {
      type: Boolean,
      default: false
    },
    /**
     * 点击遮罩层是否关闭popup
     */
    closeOnClickOverlay: {
      type: Boolean,
      default: false
    },
    zIndex: [String, Number],
    /**
     * popup滚动时是否body内容也滚动
     * 默认为不滚动
     */
    lockOnScroll: {
      type: Boolean,
      default: true
    }
  },
 
  watch: {
    value(val) {
      if (val) {
        if (this.opening) return;
        this.open();
      } else {
        if (this.closing) return;
        this.close();
      }
    }
  },
 
  beforeMount() {
    this._popupId = 'popup-' + idSeed++;
    PopupManager.register(this._popupId, this);
  },
 
  data() {
    return {
      opening: false,
      opened: false,
      closing: false,
      bodyOverflow: null
    };
  },
 
  methods: {
    /**
     * 显示popup
     */
    open(options) {
      if (this.opened) return;
 
      this.opening = true;
 
      this.$emit('input', true);
 
      const dom = getDOM(this.$el);
      const props = merge({}, this, options);
      const zIndex = props.zIndex;
 
      // 如果属性中传入了`zIndex`,则覆盖`PopupManager`中对应的`zIndex`
      if (zIndex) {
        PopupManager.zIndex = zIndex;
      }
 
      // 如果显示遮罩层
      if (this.overlay) {
        if (this.closing) {
          PopupManager.closeModal(this._popupId);
          this.closing = false;
        }
        PopupManager.openModal(this._popupId, PopupManager.nextZIndex(), dom);
 
        // 如果滚动时需要锁定
        if (this.lockOnScroll) {
          // 将原来的`bodyOverflow`存起来
          if (!this.bodyOverflow) {
            this.bodyOverflow = document.body.style.overflow;
          }
 
          document.body.style.overlay = 'hidden';
        }
      }
 
      dom.style.zIndex = PopupManager.nextZIndex();
      this.opened = true;
      this.opening = false;
    },
 
    /**
     * 关闭popup
     */
    close() {
      if (this.closing) return;
 
      this.closing = true;
 
      this.$emit('input', false);
 
      if (this.lockOnScroll) {
        setTimeout(() => {
          if (this.modal && this.bodyOverflow !== 'hidden') {
            document.body.style.overflow = this.bodyOverflow;
          }
          this.bodyOverflow = null;
        }, 200);
      }
 
      this.opened = false;
      this.doAfterClose();
    },
 
    doAfterClose() {
      this.closing = false;
      PopupManager.closeModal(this._popupId);
    }
  },
 
  beforeDestroy() {
    PopupManager.deregister(this._popupId);
    PopupManager.closeModal(this._popupId);
 
    if (this.modal && this.bodyOverflow !== null && this.bodyOverflow !== 'hidden') {
      document.body.style.overflow = this.bodyOverflow;
    }
    this.bodyOverflow = null;
  }
};