Open arcio opened 6 years ago
This is due to the way navigation is handled in Angular. Despite a drag, Angular still thinks that it is a mouse click hence triggering the event.
One of the ways to work around this situation is to use a timer on the mousedown event and assume that any (mousedown -> mouseup) that happens in a specified interval is a click:
// fires on mousedown, "(mousedown)=trackMouse()"
trackMouse() {
this.mouseDown = true;
window.setTimeout(() => {
this.mouseDown = false;
}, 100);
}
// fires on click, "(click)=navigateIfClick(item)"
navigateIfClick(item) {
if (this.mouseDown) {
this.router.navigate(['/item/', item.id]);
}
}
A better solution could be tracking the position of the mousedown and mouseup event and assume it is a click if both events' positions are close.
trackMouseDown(event: MouseEvent) {
this.x1 = event.x;
this.y1 = event.y;
}
trackMouseUp(event: MouseEvent, item: Item) {
let a = this.x1 - event.x;
let b = this.y1 - event.y;
let c = Math.sqrt(a * a + b * b);
if (c < 10) {
switch (event.which) {
case 1:
// left click
this.router.navigate(['/items/', item.id]);
break;
case 2:
// middle button
window.open(window.location.hostname + `/items/${item.id}`, '_blank');
break;
case 3:
// right click
break;
default:
this.router.navigate(['/items/', item.id]);
break;
}
}
}
Thanks! This worked like a charm, with one exception. I had to use an event listener as the function isn't always passed through from the component.
document.getElementById("ID_NAME").addEventListener("mousedown", function(e) {self.FUNCTION_NAME(e);});
If I drag the carousel left <> right, when I mouse up, it fires a (click) event to the object in the array.
feature-story itself has a click handler (click)="GoToStory(storyID)"
Is there a simple way to disable this? It doesn't appear to be happening on mobile, only with the mouse/trackpad.