Refactored ScrollHandler

pull/525/head
Cagatay Civici 2020-09-28 13:24:10 +03:00
parent fd188a46bb
commit a80bdc9498
4 changed files with 17 additions and 34 deletions

View File

@ -1,7 +1,6 @@
import Vue from 'vue'; import Vue from 'vue';
export declare class Menu extends Vue { export declare class Menu extends Vue {
id?: string;
popup?: boolean; popup?: boolean;
model?: any[]; model?: any[];
appendTo?: string; appendTo?: string;

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" :id="containerId" :class="containerClass" v-if="popup ? overlayVisible : true"> <div :ref="containerRef" :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">
@ -20,13 +20,11 @@
<script> <script>
import ConnectedOverlayScrollHandler from '../utils/ConnectedOverlayScrollHandler'; 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
@ -63,8 +61,11 @@ export default {
this.restoreAppend(); this.restoreAppend();
this.unbindResizeListener(); this.unbindResizeListener();
this.unbindOutsideClickListener(); this.unbindOutsideClickListener();
this.unbindScrollListener();
this.scrollHandler = null; if (this.scrollHandler) {
this.scrollHandler.destroy();
this.scrollHandler = null;
}
this.target = null; this.target = null;
}, },
methods: { methods: {
@ -137,7 +138,7 @@ export default {
}, },
bindScrollListener() { bindScrollListener() {
if (!this.scrollHandler) { if (!this.scrollHandler) {
this.scrollHandler = new ConnectedOverlayScrollHandler(this.target, this.containerId, () => { this.scrollHandler = new ConnectedOverlayScrollHandler(this.target, () => {
if (this.overlayVisible) { if (this.overlayVisible) {
this.hide(); this.hide();
} }
@ -203,9 +204,6 @@ 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

View File

@ -1,39 +1,31 @@
import DomHandler from './DomHandler'; import DomHandler from './DomHandler';
import UniqueComponentId from './UniqueComponentId';
export default class ConnectedOverlayScrollHandler { export default class ConnectedOverlayScrollHandler {
constructor(element, elementId = UniqueComponentId(), listener = () => {}) { constructor(element, listener = () => {}) {
this.element = element; this.element = element;
this.elementId = elementId;
this.listener = listener; this.listener = listener;
} }
bindScrollListener() { bindScrollListener() {
this.scrollableParents = DomHandler.getScrollableParents(this.element); this.scrollableParents = DomHandler.getScrollableParents(this.element);
this.scrollListeners = {};
for (let i = 0; i < this.scrollableParents.length; i++) { for (let i = 0; i < this.scrollableParents.length; i++) {
let parent = this.scrollableParents[i]; this.scrollableParents[i].addEventListener('scroll', this.listener);
let namespace = `${this.elementId}_${i}`;
if (!this.scrollListeners[namespace]) {
this.scrollListeners[namespace] = this.listener;
parent.addEventListener('scroll', this.scrollListeners[namespace]);
}
} }
} }
unbindScrollListener() { unbindScrollListener() {
if (this.scrollableParents) { if (this.scrollableParents) {
for (let i = 0; i < this.scrollableParents.length; i++) { for (let i = 0; i < this.scrollableParents.length; i++) {
let parent = this.scrollableParents[i]; this.scrollableParents[i].removeEventListener('scroll', this.listener);
let namespace = `${this.elementId}_${i}`;
if (this.scrollListeners[namespace]) {
parent.removeEventListener('scroll', this.scrollListeners[namespace]);
this.scrollListeners[namespace] = null;
}
} }
} }
} }
destroy() {
this.unbindScrollListener();
this.element = null;
this.listener = null;
this.scrollableParents = null;
}
} }

View File

@ -107,12 +107,6 @@ toggle(event) {
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr>
<td>id</td>
<td>string</td>
<td>null</td>
<td>Unique identifier of the element.</td>
</tr>
<tr> <tr>
<td>model</td> <td>model</td>
<td>array</td> <td>array</td>