[improvement] optimize popup mixin (#1979)

This commit is contained in:
neverland 2018-10-25 13:10:09 +08:00 committed by GitHub
parent 5564fbf624
commit 88d8eb0897
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 22 deletions

View File

@ -1,13 +1,8 @@
export default { export default {
id: 1,
zIndex: 2000, zIndex: 2000,
stack: [], stack: [],
lockCount: 0, lockCount: 0,
plusKey(key) {
return this[key]++;
},
get top() { get top() {
return this.stack[this.stack.length - 1]; return this.stack[this.stack.length - 1];
} }

View File

@ -61,10 +61,6 @@ export default {
} }
}, },
created() {
this._popupId = 'popup-' + context.plusKey('id');
},
mounted() { mounted() {
if (this.getContainer) { if (this.getContainer) {
this.move(); this.move();
@ -101,7 +97,7 @@ export default {
return; return;
} }
// 如果属性中传入了`zIndex`,则覆盖`context`中对应的`zIndex` // cover default zIndex
if (this.zIndex !== undefined) { if (this.zIndex !== undefined) {
context.zIndex = this.zIndex; context.zIndex = this.zIndex;
} }
@ -112,6 +108,7 @@ export default {
if (this.lockScroll) { if (this.lockScroll) {
on(document, 'touchstart', this.touchStart); on(document, 'touchstart', this.touchStart);
on(document, 'touchmove', this.onTouchMove); on(document, 'touchmove', this.onTouchMove);
if (!context.lockCount) { if (!context.lockCount) {
document.body.classList.add('van-overflow-hidden'); document.body.classList.add('van-overflow-hidden');
} }
@ -128,13 +125,14 @@ export default {
context.lockCount--; context.lockCount--;
off(document, 'touchstart', this.touchStart); off(document, 'touchstart', this.touchStart);
off(document, 'touchmove', this.onTouchMove); off(document, 'touchmove', this.onTouchMove);
if (!context.lockCount) { if (!context.lockCount) {
document.body.classList.remove('van-overflow-hidden'); document.body.classList.remove('van-overflow-hidden');
} }
} }
this.opened = false; this.opened = false;
manager.close(this._popupId); manager.close(this);
this.$emit('input', false); this.$emit('input', false);
}, },
@ -184,16 +182,16 @@ export default {
renderOverlay() { renderOverlay() {
if (this.overlay) { if (this.overlay) {
manager.open(this, { manager.open(this, {
zIndex: context.plusKey('zIndex'), zIndex: context.zIndex++,
className: this.overlayClass, className: this.overlayClass,
customStyle: this.overlayStyle customStyle: this.overlayStyle
}); });
} else { } else {
manager.close(this._popupId); manager.close(this);
} }
this.$nextTick(() => { this.$nextTick(() => {
this.$el.style.zIndex = context.plusKey('zIndex'); this.$el.style.zIndex = context.zIndex++;
}); });
} }
} }

View File

@ -10,23 +10,23 @@ const defaultConfig = {
export default { export default {
open(vm, config) { open(vm, config) {
/* istanbul ignore next */ /* istanbul ignore next */
if (!context.stack.some(item => item.vm._popupId === vm._popupId)) { if (!context.stack.some(item => item.vm === vm)) {
const el = vm.$el; const el = vm.$el;
const targetNode = el && el.parentNode && el.parentNode.nodeType !== 11 ? el.parentNode : document.body; const target = el && el.parentNode ? el.parentNode : document.body;
context.stack.push({ vm, config, targetNode }); context.stack.push({ vm, config, target });
this.update(); this.update();
}; };
}, },
close(id) { close(vm) {
const { stack } = context; const { stack } = context;
if (stack.length) { if (stack.length) {
if (context.top.vm._popupId === id) { if (context.top.vm === vm) {
stack.pop(); stack.pop();
this.update(); this.update();
} else { } else {
context.stack = stack.filter(item => item.vm._popupId !== id); context.stack = stack.filter(item => item.vm !== vm);
} }
} }
}, },
@ -48,9 +48,9 @@ export default {
} }
if (context.top) { if (context.top) {
const { targetNode, config } = context.top; const { target, config } = context.top;
targetNode.appendChild(modal.$el); target.appendChild(modal.$el);
Object.assign(modal, { Object.assign(modal, {
...defaultConfig, ...defaultConfig,
...config, ...config,