-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib): implemented ngxLongPress2's directive
- Loading branch information
1 parent
f468d83
commit 7b5e5ad
Showing
8 changed files
with
94 additions
and
79 deletions.
There are no files selected for viewing
25 changes: 0 additions & 25 deletions
25
projects/ngx-long-press2/src/lib/ngx-long-press2.component.spec.ts
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
projects/ngx-long-press2/src/lib/ngx-long-press2.component.ts
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
projects/ngx-long-press2/src/lib/ngx-long-press2.directive.spec.ts
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
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
80
projects/ngx-long-press2/src/lib/ngx-long-press2.directive.ts
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
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(); | ||
} | ||
|
||
} |
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
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
16
projects/ngx-long-press2/src/lib/ngx-long-press2.service.spec.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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