Skip to content

Commit

Permalink
feat(lib): implemented ngxLongPress2's directive
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyNahas committed Sep 25, 2020
1 parent f468d83 commit 7b5e5ad
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 79 deletions.
25 changes: 0 additions & 25 deletions projects/ngx-long-press2/src/lib/ngx-long-press2.component.spec.ts

This file was deleted.

20 changes: 0 additions & 20 deletions projects/ngx-long-press2/src/lib/ngx-long-press2.component.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NgxLongPress2Directive } from './ngx-long-press2.directive';

describe('NgxLongPress2Directive', () => {
it('should create an instance', () => {
const directive = new NgxLongPress2Directive();
expect(directive).toBeTruthy();
});
});
80 changes: 80 additions & 0 deletions projects/ngx-long-press2/src/lib/ngx-long-press2.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Directive, EventEmitter, HostBinding, HostListener, Input, Output } from '@angular/core';

@Directive({
// tslint:disable-next-line:directive-selector
selector: '[ngxLongPress2]'
})
export class NgxLongPress2Directive {

pressing: boolean;
longPressing: boolean;
timeout: any;
interval: any;

delay = 50;
lapsedTime = 0; // used for onLongPressing event

@Input()
minTime = 500; // used for onLongPress event

@Input()
maxTime = 2000; // used for onReleasePressing event

// tslint:disable-next-line:no-output-on-prefix
@Output()
onLongPress = new EventEmitter();

// tslint:disable-next-line:no-output-on-prefix
@Output()
onLongPressing: EventEmitter<number> = new EventEmitter();

// tslint:disable-next-line:no-output-on-prefix
@Output()
onReleasePressing: EventEmitter<void> = new EventEmitter();

@HostBinding('class.press')
get press(): boolean {
return this.pressing;
}

@HostBinding('class.longpress')
get longPress(): boolean {
return this.longPressing;
}

@HostListener('touchstart', ['$event'])
@HostListener('mousedown', ['$event'])
onMouseDown(event: any): void {
this.pressing = true;
this.longPressing = false;
this.timeout = setTimeout(() => {
this.longPressing = true;
this.onLongPress.emit(event);
this.interval = setInterval(() => {
this.onLongPressing.emit(this.lapsedTime);
if (this.lapsedTime < this.maxTime) {
this.lapsedTime += this.delay;
} else {
this.end();
}
}, this.delay);
}, this.minTime);
}

@HostListener('touchend')
@HostListener('mouseup')
@HostListener('mouseleave')
endPress(): void {
this.end();
}

end(): void {
clearTimeout(this.timeout);
clearInterval(this.interval);
this.longPressing = false;
this.pressing = false;
this.lapsedTime = 0;
this.onReleasePressing.next();
}

}
12 changes: 5 additions & 7 deletions projects/ngx-long-press2/src/lib/ngx-long-press2.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { NgModule } from '@angular/core';
import { NgxLongPress2Component } from './ngx-long-press2.component';

import { NgxLongPress2Directive } from './ngx-long-press2.directive';


@NgModule({
declarations: [NgxLongPress2Component],
imports: [
],
exports: [NgxLongPress2Component]
declarations: [NgxLongPress2Directive],
exports: [NgxLongPress2Directive]
})
export class NgxLongPress2Module { }
export class NgxLongPress2Module {
}
16 changes: 0 additions & 16 deletions projects/ngx-long-press2/src/lib/ngx-long-press2.service.spec.ts

This file was deleted.

9 changes: 0 additions & 9 deletions projects/ngx-long-press2/src/lib/ngx-long-press2.service.ts

This file was deleted.

3 changes: 1 addition & 2 deletions projects/ngx-long-press2/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
* Public API Surface of ngx-long-press2
*/

export * from './lib/ngx-long-press2.service';
export * from './lib/ngx-long-press2.component';
export * from './lib/ngx-long-press2.directive';
export * from './lib/ngx-long-press2.module';

0 comments on commit 7b5e5ad

Please sign in to comment.