Skip to content

Commit

Permalink
Fixed #14375 - Add touch device support
Browse files Browse the repository at this point in the history
  • Loading branch information
cetincakiroglu committed Dec 20, 2023
1 parent fa530a1 commit 50b74d0
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions src/app/components/contextmenu/contextmenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,11 @@ export class ContextMenu implements OnInit, AfterContentInit, OnDestroy {
* @group Props
*/
@Input() ariaLabelledBy: string | undefined;
/**
* Press delay in touch devices as miliseconds.
* @group Props
*/
@Input() pressDelay: number | undefined = 500;
/**
* Callback to invoke when overlay menu is shown.
* @group Emits
Expand Down Expand Up @@ -484,6 +489,8 @@ export class ContextMenu implements OnInit, AfterContentInit, OnDestroy {

documentTriggerListener: VoidListener;

touchEndListener: VoidListener;

pageX: number;

pageY: number;
Expand All @@ -510,6 +517,8 @@ export class ContextMenu implements OnInit, AfterContentInit, OnDestroy {

_model: MenuItem[] | undefined;

pressTimer: any;

get visibleItems() {
const processedItem = this.activeItemPath().find((p) => p.key === this.focusedItemInfo().parentKey);

Expand Down Expand Up @@ -554,17 +563,31 @@ export class ContextMenu implements OnInit, AfterContentInit, OnDestroy {
this.bindTriggerEventListener();
}

isMobile() {
return DomHandler.isIOS() || DomHandler.isAndroid();
}

bindTriggerEventListener() {
if (isPlatformBrowser(this.platformId)) {
if (!this.triggerEventListener) {
if (this.global) {
this.triggerEventListener = this.renderer.listen(this.document, this.triggerEvent, (event) => {
this.show(event);
});
} else if (this.target) {
this.triggerEventListener = this.renderer.listen(this.target, this.triggerEvent, (event) => {
this.show(event);
});
if (!this.isMobile()) {
if (this.global) {
this.triggerEventListener = this.renderer.listen(this.document, this.triggerEvent, (event) => {
this.show(event);
});
} else if (this.target) {
this.triggerEventListener = this.renderer.listen(this.target, this.triggerEvent, (event) => {
this.show(event);
});
}
} else {
if (this.global) {
this.triggerEventListener = this.renderer.listen(this.document, 'touchstart', this.onTouchStart.bind(this));
this.touchEndListener = this.renderer.listen(this.document, 'touchend', this.onTouchEnd.bind(this));
} else if (this.target) {
this.triggerEventListener = this.renderer.listen(this.target, 'touchstart', this.onTouchStart.bind(this));
this.touchEndListener = this.renderer.listen(this.target, 'touchend', this.onTouchEnd.bind(this));
}
}
}
}
Expand Down Expand Up @@ -950,6 +973,16 @@ export class ContextMenu implements OnInit, AfterContentInit, OnDestroy {
this.onHide.emit();
}

onTouchStart(event: MouseEvent) {
this.pressTimer = setTimeout(() => {
this.show(event);
}, this.pressDelay);
}

onTouchEnd() {
clearTimeout(this.pressTimer);
}

hide() {
this.visible.set(false);
this.activeItemPath.set([]);
Expand Down Expand Up @@ -1134,6 +1167,11 @@ export class ContextMenu implements OnInit, AfterContentInit, OnDestroy {
this.resizeListener();
this.resizeListener = null;
}

if (this.touchEndListener) {
this.touchEndListener();
this.touchEndListener = null;
}
}

unbindTriggerEventListener() {
Expand Down

0 comments on commit 50b74d0

Please sign in to comment.