all files / src/mixins/popup/ popup-manager.js

10.29% Statements 7/68
0% Branches 0/43
0% Functions 0/11
9.38% Lines 6/64
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                                                                                                                                                                                                                                                                  
import { addClass, removeClass } from 'src/utils/dom';
 
let hasModal = false;
 
const getModal = function() {
  let modalDom = PopupManager.modalDom;
  if (modalDom) {
    hasModal = true;
  } else {
    hasModal = false;
    modalDom = document.createElement('div');
    PopupManager.modalDom = modalDom;
 
    modalDom.addEventListener('touchmove', function(event) {
      event.preventDefault();
      event.stopPropagation();
    });
 
    modalDom.addEventListener('click', function() {
      PopupManager.handleOverlayClick && PopupManager.handleOverlayClick();
    });
  }
 
  return modalDom;
};
 
const instances = {};
 
const PopupManager = {
  zIndex: 2000,
 
  modalStack: [],
 
  nextZIndex() {
    return this.zIndex++;
  },
 
  getInstance(id) {
    return instances[id];
  },
 
  register(id, instance) {
    if (id && instance) {
      instances[id] = instance;
    }
  },
 
  deregister(id) {
    if (id) {
      instances[id] = null;
      delete instances[id];
    }
  },
 
  /**
   * 遮罩层点击回调,`closeOnClickOverlay`为`true`时会关闭当前`popup`
   */
  handleOverlayClick() {
    const topModal = PopupManager.modalStack[PopupManager.modalStack.length - 1];
    if (!topModal) return;
 
    const instance = PopupManager.getInstance(topModal.id);
    if (instance && instance.closeOnClickOverlay) {
      instance.close();
    }
  },
 
  openModal(id, zIndex, dom) {
    if (!id || zIndex === undefined) return;
 
    const modalStack = this.modalStack;
 
    for (let i = 0, len = modalStack.length; i < len; i++) {
      const item = modalStack[i];
      if (item.id === id) {
        return;
      }
    }
 
    const modalDom = getModal();
 
    addClass(modalDom, 'zan-modal');
 
    if (dom && dom.parentNode && dom.parentNode.nodeType !== 11) {
      dom.parentNode.appendChild(modalDom);
    } else {
      document.body.appendChild(modalDom);
    }
 
    if (zIndex) {
      modalDom.style.zIndex = zIndex;
    }
    modalDom.style.display = '';
 
    this.modalStack.push({ id: id, zIndex: zIndex });
  },
 
  closeModal(id) {
    const modalStack = this.modalStack;
    const modalDom = getModal();
 
    if (modalStack.length > 0) {
      const topItem = modalStack[modalStack.length - 1];
      if (topItem.id === id) {
        modalStack.pop();
        if (modalStack.length > 0) {
          modalDom.style.zIndex = modalStack[modalStack.length - 1].zIndex;
        }
      } else {
        for (let i = modalStack.length - 1; i >= 0; i--) {
          if (modalStack[i].id === id) {
            modalStack.splice(i, 1);
            break;
          }
        }
      }
    }
 
    if (modalStack.length === 0) {
      setTimeout(() => {
        if (modalStack.length === 0) {
          if (modalDom.parentNode) modalDom.parentNode.removeChild(modalDom);
 
          modalDom.style.display = 'none';
          this.modalDom = null;
        }
      }, 200);
    }
  }
};
 
export default PopupManager;