Last active
August 28, 2016 18:28
-
-
Save b12f/cbcdeda645f13f26ee504e4be5a1e26f to your computer and use it in GitHub Desktop.
Angular 2 directive that emits a longpress event after a touch event that lasts 400ms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//<ion-item (click)="open(item)" long-press (longpress)="select(item)"></ion-item> | |
import {Directive, HostListener, Output, EventEmitter} from '@angular/core'; | |
@Directive({ | |
selector: '[long-press]' | |
}) | |
export class LongPressDirective { | |
private touchTimeout: any; | |
@Output() longpress = new EventEmitter(); | |
private rootPage: any; | |
constructor() {} | |
@HostListener('touchstart') touchstart():void { | |
this.touchTimeout = setTimeout(() => { | |
this.longpress.emit({}); | |
}, 400); | |
} | |
@HostListener('touchend') touchend():void { | |
this.touchEnd(); | |
} | |
@HostListener('touchcancel') touchcancel():void { | |
this.touchEnd(); | |
} | |
private touchEnd():void { | |
clearTimeout(this.touchTimeout); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment