angular2-data-table
Version:
angular2-data-table is a Angular2 component for presenting large and complex data.
83 lines (65 loc) • 1.9 kB
text/typescript
import {
Directive,
Input,
Output,
EventEmitter,
HostBinding,
HostListener
} from '@angular/core';
export class LongPressDirective {
duration: number = 500;
longPress: EventEmitter<any> = new EventEmitter();
longPressing: EventEmitter<any> = new EventEmitter();
longPressEnd: EventEmitter<any> = new EventEmitter();
pressing: boolean;
isLongPressing: boolean;
timeout: any;
mouseX: number = 0;
mouseY: number = 0;
get press(): boolean { return this.pressing; }
get isLongPress(): boolean { return this.longPressing !== undefined; }
onMouseDown(event: MouseEvent): void {
// don't do right/middle clicks
if(event.which !== 1) return;
this.mouseX = event.clientX;
this.mouseY = event.clientY;
this.pressing = true;
this.isLongPressing = false;
this.timeout = setTimeout(() => {
this.isLongPressing = true;
this.longPress.emit(event);
this.loop(event);
}, this.duration);
this.loop(event);
}
onMouseMove(event: MouseEvent): void {
if(this.pressing && !this.longPressing) {
const xThres = (event.clientX - this.mouseX) > 10;
const yThres = (event.clientY - this.mouseY) > 10;
if(xThres || yThres) {
this.endPress();
}
}
}
loop(event: Event): void {
if(this.longPressing) {
this.timeout = setTimeout(() => {
this.longPressing.emit(event);
this.loop(event);
}, 50);
}
}
endPress(): void {
clearTimeout(this.timeout);
this.isLongPressing = false;
this.pressing = false;
this.longPressEnd.emit(true);
}
onMouseUp(): void { this.endPress(); }
}