[bugfix] Popup modal display error

This commit is contained in:
陈嘉涵 2017-10-16 16:24:10 +08:00
parent ed26ed2284
commit ade8a97d6d
7 changed files with 100 additions and 236 deletions

View File

@ -1,9 +1,9 @@
import PopupManager from './popup-manager'; import manager from './popup-manager';
import PopupContext from './popup-context'; import context from './popup-context';
export default { export default {
props: { props: {
// popup当前显示状态 // popup 当前显示状态
value: { value: {
type: Boolean, type: Boolean,
default: false default: false
@ -13,16 +13,13 @@ export default {
type: Boolean, type: Boolean,
default: false default: false
}, },
/** // 点击遮罩层是否关闭 popup
* 点击遮罩层是否关闭popup
*/
closeOnClickOverlay: { closeOnClickOverlay: {
type: Boolean, type: Boolean,
default: false default: false
}, },
zIndex: [String, Number], zIndex: [String, Number],
// popup滚动时是否body内容也滚动 // popup 滚动时是否禁用 body 滚动
// 默认为不滚动
lockOnScroll: { lockOnScroll: {
type: Boolean, type: Boolean,
default: true default: true
@ -36,27 +33,18 @@ export default {
watch: { watch: {
value(val) { value(val) {
if (val) { this[val ? 'open' : 'close']();
if (this.opening) return;
this.open();
} else {
if (this.closing) return;
this.close();
}
} }
}, },
beforeMount() { beforeMount() {
this._popupId = 'popup-' + PopupContext.plusKeyByOne('idSeed'); this._popupId = 'popup-' + context.plusKeyByOne('idSeed');
PopupManager.register(this._popupId, this); context.instances[this._popupId] = this;
}, },
data() { data() {
return { return {
opening: false,
opened: false, opened: false,
closing: false,
bodyOverflow: null,
pos: { pos: {
x: 0, x: 0,
y: 0 y: 0
@ -71,6 +59,7 @@ export default {
y: e.touches[0].clientY y: e.touches[0].clientY
}; };
}, },
watchTouchMove(e) { watchTouchMove(e) {
const pos = this.pos; const pos = this.pos;
const dx = e.touches[0].clientX - pos.x; const dx = e.touches[0].clientX - pos.x;
@ -95,47 +84,28 @@ export default {
e.stopPropagation(); e.stopPropagation();
} }
}, },
/**
* 显示popup
*/
open() {
/* istanbul ignore if */
if (this.$isServer) return;
if (this.opened) return;
this.opening = true; open() {
if (this.opened || this.$isServer) {
return;
}
this.$emit('input', true); this.$emit('input', true);
const zIndex = this.zIndex; // 如果属性中传入了`zIndex`,则覆盖`context`中对应的`zIndex`
if (this.zIndex !== undefined) {
// 如果属性中传入了`zIndex`,则覆盖`popupContext`中对应的`zIndex` context.zIndex = this.zIndex;
if (zIndex) {
PopupContext.setContext('zIndex', zIndex);
} }
// 如果显示遮罩层
if (this.overlay) { if (this.overlay) {
if (this.closing) { manager.openModal(this._popupId, context.plusKeyByOne('zIndex'), this.$el);
PopupManager.closeModal(this._popupId);
this.closing = false;
}
PopupManager.openModal(this._popupId, PopupManager.nextZIndex(), this.$el);
// 如果滚动时需要锁定
if (this.lockOnScroll) { if (this.lockOnScroll) {
// 将原来的`bodyOverflow`存起来 document.body.classList.add('van-overflow-hidden');
if (!this.bodyOverflow) {
this.bodyOverflow = document.body.style.overflow;
}
document.body.style.overflow = 'hidden';
} }
} }
this.$el.style.zIndex = PopupManager.nextZIndex(); this.$el.style.zIndex = context.plusKeyByOne('zIndex');
this.opened = true; this.opened = true;
this.opening = false;
if (this.preventScroll) { if (this.preventScroll) {
document.addEventListener('touchstart', this.recordPosition, false); document.addEventListener('touchstart', this.recordPosition, false);
@ -143,23 +113,15 @@ export default {
} }
}, },
/**
* 关闭popup
*/
close() { close() {
if (this.closing) return; if (!this.opened || this.$isServer) {
return;
this.closing = true; }
this.$emit('input', false); this.$emit('input', false);
if (this.lockOnScroll) { if (this.lockOnScroll) {
setTimeout(() => { document.body.classList.remove('van-overflow-hidden');
if (this.overlay && this.bodyOverflow !== 'hidden') {
document.body.style.overflow = this.bodyOverflow;
}
this.bodyOverflow = null;
}, 200);
} }
this.opened = false; this.opened = false;
@ -167,8 +129,7 @@ export default {
}, },
doAfterClose() { doAfterClose() {
this.closing = false; manager.closeModal(this._popupId);
PopupManager.closeModal(this._popupId);
if (this.preventScroll) { if (this.preventScroll) {
document.removeEventListener('touchstart', this.recordPosition, false); document.removeEventListener('touchstart', this.recordPosition, false);
@ -178,12 +139,10 @@ export default {
}, },
beforeDestroy() { beforeDestroy() {
PopupManager.deregister(this._popupId); context.instances[this._popupId] = null;
PopupManager.closeModal(this._popupId); manager.closeModal(this._popupId);
if (this.lockOnScroll) {
if (this.overlay && this.bodyOverflow !== null && this.bodyOverflow !== 'hidden') { document.body.classList.remove('van-overflow-hidden');
document.body.style.overflow = this.bodyOverflow;
} }
this.bodyOverflow = null;
} }
}; };

View File

@ -1,35 +1,15 @@
import Vue from 'vue'; const PopupContext = {
const _global = Vue.prototype.$isServer ? global : window;
const DEFAULT_CONTEXT = {
idSeed: 1, idSeed: 1,
zIndex: 2000, zIndex: 2000,
hasModal: false,
instances: {}, instances: {},
modalStack: [] modalStack: [],
};
if (!_global.popupContext) {
_global.popupContext = {
...DEFAULT_CONTEXT
};
}
const PopupContext = {
getContext(key) {
return _global.popupContext[key];
},
setContext(key, value) {
_global.popupContext[key] = value;
},
plusKeyByOne(key) { plusKeyByOne(key) {
const oldVal = +_global.popupContext[key]; return this[key]++;
_global.popupContext[key] = oldVal + 1; },
return oldVal; get topModal() {
return this.modalStack[this.modalStack.length - 1];
} }
}; };

View File

@ -1,133 +1,68 @@
import Vue from 'vue'; import context from './popup-context';
import PopupContext from './popup-context';
const getModal = function() {
if (Vue.prototype.$isServer) return;
let modalDom = PopupContext.getContext('modalDom');
if (modalDom) {
PopupContext.setContext('hasModal', true);
} else {
PopupContext.setContext('hasModal', false);
modalDom = document.createElement('div');
PopupContext.setContext('modalDom', modalDom);
modalDom.addEventListener('touchmove', function(event) {
event.preventDefault();
event.stopPropagation();
});
modalDom.addEventListener('click', function() {
PopupManager.handleOverlayClick && PopupManager.handleOverlayClick();
});
}
return modalDom;
};
const PopupManager = { const PopupManager = {
nextZIndex() { getModal() {
return PopupContext.plusKeyByOne('zIndex'); let { modal } = context;
},
getInstance(id) { if (!modal) {
return PopupContext.getContext('instances')[id]; modal = document.createElement('div');
}, modal.classList.add('van-modal');
modal.addEventListener('touchmove', event => {
event.preventDefault();
event.stopPropagation();
});
modal.addEventListener('click', () => {
PopupManager.handleOverlayClick();
});
register(id, instance) { context.modal = modal;
if (id && instance) {
const instances = PopupContext.getContext('instances');
instances[id] = instance;
} }
return modal;
}, },
deregister(id) { // close popup when click modal && closeOnClickOverlay is true
if (id) {
const instances = PopupContext.getContext('instances');
instances[id] = null;
delete instances[id];
}
},
/**
* 遮罩层点击回调`closeOnClickOverlay``true`时会关闭当前`popup`
*/
handleOverlayClick() { handleOverlayClick() {
const modalStack = PopupContext.getContext('modalStack'); const { topModal } = context;
const topModal = modalStack[modalStack.length - 1]; if (topModal) {
if (!topModal) return; const instance = context.instances[topModal.id];
if (instance && instance.closeOnClickOverlay) {
const instance = PopupManager.getInstance(topModal.id); instance.close();
if (instance && instance.closeOnClickOverlay) { }
instance.close();
} }
}, },
openModal(id, zIndex, dom) { openModal(id, zIndex, dom) {
if (!id || zIndex === undefined) return; const { modalStack } = context;
const exist = modalStack.some(item => item.id === id);
const modalStack = PopupContext.getContext('modalStack'); if (!exist) {
const modal = this.getModal();
modal.style.zIndex = zIndex;
for (let i = 0, len = modalStack.length; i < len; i++) { const parentNode = dom && dom.parentNode && dom.parentNode.nodeType !== 11 ? dom.parentNode : document.body;
const item = modalStack[i]; parentNode.appendChild(modal);
if (item.id === id) { modalStack.push({ id, zIndex, parentNode });
return; };
}
}
const modalDom = getModal();
modalDom.classList.add('van-modal');
let domParentNode;
if (dom && dom.parentNode && dom.parentNode.nodeType !== 11) {
domParentNode = dom.parentNode;
} else {
domParentNode = document.body;
}
domParentNode.appendChild(modalDom);
if (zIndex) {
modalDom.style.zIndex = zIndex;
}
modalDom.style.display = '';
modalStack.push({ id: id, zIndex: zIndex, parentNode: domParentNode });
}, },
closeModal(id) { closeModal(id) {
const modalStack = PopupContext.getContext('modalStack'); const { modalStack } = context;
const modalDom = getModal();
if (modalStack.length > 0) { if (modalStack.length) {
const topItem = modalStack[modalStack.length - 1]; if (context.topModal.id === id) {
if (topItem.id === id) { const modal = this.getModal();
modalStack.pop(); modalStack.pop();
if (modalStack.length > 0) { modal.parentNode.removeChild(modal);
modalDom.style.zIndex = modalStack[modalStack.length - 1].zIndex; if (modalStack.length) {
modalDom.parentNode.removeChild(modalDom); const { topModal } = context;
const currModalParent = modalStack[0].parentNode; modal.style.zIndex = topModal.zIndex;
currModalParent && currModalParent.appendChild(modalDom); topModal.parentNode.appendChild(modal);
} }
} else { } else {
for (let i = modalStack.length - 1; i >= 0; i--) { context.modalStack = modalStack.filter(item => item.id !== id);
if (modalStack[i].id === id) {
modalStack.splice(i, 1);
break;
}
}
} }
} }
if (modalStack.length === 0) {
setTimeout(() => {
if (modalDom.parentNode) modalDom.parentNode.removeChild(modalDom);
modalDom.style.display = 'none';
this.modalDom = null;
}, 200);
}
} }
}; };

View File

@ -1,6 +1,6 @@
<template> <template>
<transition :name="currentTransition"> <transition :name="currentTransition">
<div v-show="currentValue" class="van-popup" :class="[position ? 'van-popup--' + position : '']"> <div v-show="value" class="van-popup" :class="[position ? 'van-popup--' + position : '']">
<slot></slot> <slot></slot>
</div> </div>
</transition> </transition>
@ -8,29 +8,23 @@
<script> <script>
import Popup from '../mixins/popup'; import Popup from '../mixins/popup';
export default { export default {
name: 'van-popup', name: 'van-popup',
mixins: [Popup], mixins: [Popup],
props: { props: {
transition: String,
overlay: { overlay: {
default: true default: true
}, },
lockOnScroll: { lockOnScroll: {
default: false default: false
}, },
closeOnClickOverlay: { closeOnClickOverlay: {
default: true default: true
}, },
transition: {
type: String,
default: 'popup-slide'
},
position: { position: {
type: String, type: String,
default: '' default: ''
@ -38,31 +32,15 @@ export default {
}, },
data() { data() {
const transition = this.transition || (this.position === '' ? 'popup-fade' : `popup-slide-${this.position}`);
return { return {
currentValue: false, currentValue: false,
currentTransition: this.transition currentTransition: transition
}; };
}, },
watch: {
currentValue(val) {
this.$emit('input', val);
},
value(val) {
this.currentValue = val;
}
},
beforeMount() {
if (this.transition !== 'popup-fade') {
this.currentTransition = `popup-slide-${this.position}`;
}
},
mounted() { mounted() {
if (this.value) { if (this.value) {
this.currentValue = true;
this.open(); this.open();
} }
} }

View File

@ -10,6 +10,15 @@
} }
} }
@keyframes van-fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.van-slide-bottom { .van-slide-bottom {
&-enter-active { &-enter-active {
animation: van-slide-bottom-enter .3s both ease; animation: van-slide-bottom-enter .3s both ease;

View File

@ -8,7 +8,11 @@
top: 0; top: 0;
left: 0; left: 0;
background-color: rgba(0, 0, 0, 0.7); background-color: rgba(0, 0, 0, 0.7);
transition: .5s ease-out; animation: van-fade-in .3s both;
}
&-overflow-hidden {
overflow: hidden !important;
} }
&-popup { &-popup {

View File

@ -27,12 +27,11 @@ describe('Popup', () => {
}); });
const eventStub = sinon.stub(wrapper.vm, '$emit'); const eventStub = sinon.stub(wrapper.vm, '$emit');
expect(wrapper.data().currentValue).to.be.false; expect(wrapper.element.style.display).to.equal('none');
wrapper.vm.value = true; wrapper.vm.value = true;
wrapper.update();
wrapper.vm.$nextTick(() => { wrapper.vm.$nextTick(() => {
expect(wrapper.data().currentValue).to.be.true; expect(wrapper.element.style.display).to.equal('');
expect(eventStub.calledWith('input')); expect(eventStub.calledWith('input'));
done(); done();
}); });
@ -45,7 +44,7 @@ describe('Popup', () => {
} }
}); });
expect(wrapper.data().currentValue).to.be.true; expect(wrapper.element.style.display).to.equal('');
}); });
it('create a popup-fade transition popup', () => { it('create a popup-fade transition popup', () => {
@ -70,7 +69,7 @@ describe('Popup', () => {
expect(wrapper.hasClass('van-popup')).to.be.true; expect(wrapper.hasClass('van-popup')).to.be.true;
setTimeout(() => { setTimeout(() => {
expect(wrapper.data().currentValue).to.be.true; expect(wrapper.element.style.display).to.equal('');
wrapper.vm.value = false; wrapper.vm.value = false;
triggerTouch(document, 'touchstart', 0, 0); triggerTouch(document, 'touchstart', 0, 0);
triggerTouch(document, 'touchmove', 0, 10); triggerTouch(document, 'touchmove', 0, 10);
@ -78,7 +77,7 @@ describe('Popup', () => {
triggerTouch(document, 'touchmove', 0, -30); triggerTouch(document, 'touchmove', 0, -30);
setTimeout(() => { setTimeout(() => {
expect(wrapper.data().currentValue).to.be.false; expect(wrapper.element.style.display).to.equal('none');
done(); done();
}, 300); }, 300);
}, 300); }, 300);