Refactor #500 - For Menu

pull/525/head
mertsincan 2020-09-27 19:48:57 +03:00
parent b5522c4d20
commit 300e27800f
1 changed files with 31 additions and 2 deletions

View File

@ -1,6 +1,6 @@
<template> <template>
<transition name="p-connected-overlay" @enter="onEnter" @leave="onLeave"> <transition name="p-connected-overlay" @enter="onEnter" @leave="onLeave">
<div :ref="containerRef" :class="containerClass" v-if="popup ? overlayVisible : true"> <div :ref="containerRef" :id="containerId" :class="containerClass" v-if="popup ? overlayVisible : true">
<ul class="p-menu-list p-reset" role="menu"> <ul class="p-menu-list p-reset" role="menu">
<template v-for="(item, i) of model"> <template v-for="(item, i) of model">
<template v-if="item.items && visible(item) && !item.separator"> <template v-if="item.items && visible(item) && !item.separator">
@ -19,11 +19,14 @@
</template> </template>
<script> <script>
import ConnectedOverlayScrollHandler from '../utils/ConnectedOverlayScrollHandler';
import UniqueComponentId from '../utils/UniqueComponentId';
import DomHandler from '../utils/DomHandler'; import DomHandler from '../utils/DomHandler';
import Menuitem from './Menuitem'; import Menuitem from './Menuitem';
export default { export default {
props: { props: {
id: null,
popup: { popup: {
type: Boolean, type: Boolean,
default: false default: false
@ -52,6 +55,7 @@ export default {
}, },
target: null, target: null,
outsideClickListener: null, outsideClickListener: null,
scrollHandler: null,
resizeListener: null, resizeListener: null,
relativeAlign: false, relativeAlign: false,
container: null, container: null,
@ -59,6 +63,8 @@ export default {
this.restoreAppend(); this.restoreAppend();
this.unbindResizeListener(); this.unbindResizeListener();
this.unbindOutsideClickListener(); this.unbindOutsideClickListener();
this.unbindScrollListener();
this.scrollHandler = null;
this.target = null; this.target = null;
}, },
methods: { methods: {
@ -87,7 +93,7 @@ export default {
}, },
hide() { hide() {
this.overlayVisible = false; this.overlayVisible = false;
this.target = false; this.target = null;
this.relativeAlign = false; this.relativeAlign = false;
}, },
onEnter() { onEnter() {
@ -95,6 +101,7 @@ export default {
this.alignOverlay(); this.alignOverlay();
this.bindOutsideClickListener(); this.bindOutsideClickListener();
this.bindResizeListener(); this.bindResizeListener();
this.bindScrollListener();
if (this.autoZIndex) { if (this.autoZIndex) {
this.container.style.zIndex = String(this.baseZIndex + DomHandler.generateZIndex()); this.container.style.zIndex = String(this.baseZIndex + DomHandler.generateZIndex());
@ -103,6 +110,7 @@ export default {
onLeave() { onLeave() {
this.unbindOutsideClickListener(); this.unbindOutsideClickListener();
this.unbindResizeListener(); this.unbindResizeListener();
this.unbindScrollListener();
}, },
alignOverlay() { alignOverlay() {
if (this.relativeAlign) if (this.relativeAlign)
@ -127,6 +135,22 @@ export default {
this.outsideClickListener = null; this.outsideClickListener = null;
} }
}, },
bindScrollListener() {
if (!this.scrollHandler) {
this.scrollHandler = new ConnectedOverlayScrollHandler(this.target, this.containerId, () => {
if (this.overlayVisible) {
this.hide();
}
});
}
this.scrollHandler.bindScrollListener();
},
unbindScrollListener() {
if (this.scrollHandler) {
this.scrollHandler.unbindScrollListener();
}
},
bindResizeListener() { bindResizeListener() {
if (!this.resizeListener) { if (!this.resizeListener) {
this.resizeListener = () => { this.resizeListener = () => {
@ -166,6 +190,8 @@ export default {
this.restoreAppend(); this.restoreAppend();
this.unbindResizeListener(); this.unbindResizeListener();
this.unbindOutsideClickListener(); this.unbindOutsideClickListener();
this.unbindScrollListener();
this.scrollHandler = null;
this.target = null; this.target = null;
this.container = null; this.container = null;
}, },
@ -177,6 +203,9 @@ export default {
} }
}, },
computed: { computed: {
containerId() {
return this.id || UniqueComponentId();
},
containerClass() { containerClass() {
return ['p-menu p-component', { return ['p-menu p-component', {
'p-menu-overlay': this.popup 'p-menu-overlay': this.popup