There is no "mousehold
" event, so I wrote one:
import {
Directive, Input, Output, EventEmitter, HostListener
} from '@angular/core';
@Directive({ selector: '[custMousehold]' })
export class MouseholdDirective {
@Input() duration: number = 350; // ms
@Output() mousehold: EventEmitter<any> = new EventEmitter();
public timeout: NodeJS.Timer;
@HostListener('mousedown', ['$event'])
mousedown(event: MouseEvent) {
if (event.which !== 1) return; // left mouse button only
this.timeout = setTimeout(() => {
this.mousehold.emit(event);
}, this.duration);
}
@HostListener('mouseup')
mouseup() { clearTimeout(this.timeout); }
}
This is how I attach it to an element:
<span custMousehold (mousehold)="do something ...
It works great. But I also have a click
event on this same element:
<span custMousehold (mousehold)="do something" (click)="do something else ...
And I want the click
event to only fire if mousehold
did not fire.
I've been trying with preventDefault()
and stopPropagation()
all over the code but they don't make a difference. The click
always fires.