-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(draggable-scroll-container): Added input activeLongPressDrag (#79)
Start drag after a long touch to be able to scroll the container without dragging any item
Closes #78
- Loading branch information
1 parent
5bd76ce
commit f98f586
Showing
3 changed files
with
217 additions
and
10 deletions.
There are no files selected for viewing
69 changes: 66 additions & 3 deletions
69
projects/angular-draggable-droppable/src/lib/draggable-scroll-container.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 |
---|---|---|
@@ -1,8 +1,71 @@ | ||
import { Directive, ElementRef } from '@angular/core'; | ||
import { | ||
Directive, | ||
ElementRef, | ||
Input, | ||
NgZone, | ||
OnInit, | ||
Renderer2 | ||
} from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: '[mwlDraggableScrollContainer]' | ||
}) | ||
export class DraggableScrollContainerDirective { | ||
constructor(public elementRef: ElementRef<HTMLElement>) {} | ||
export class DraggableScrollContainerDirective implements OnInit { | ||
/** | ||
* Trigger the DragStart after a long touch in scrollable container when true | ||
*/ | ||
@Input() | ||
activeLongPressDrag: boolean = false; | ||
|
||
/** | ||
* Configuration of a long touch | ||
* Duration in ms of a long touch before activating DragStart | ||
* Delta of the | ||
*/ | ||
@Input() | ||
longPressConfig = { duration: 300, delta: 30 }; | ||
|
||
private cancelledScroll = false; | ||
|
||
constructor( | ||
public elementRef: ElementRef<HTMLElement>, | ||
private renderer: Renderer2, | ||
private zone: NgZone | ||
) {} | ||
|
||
ngOnInit() { | ||
this.zone.runOutsideAngular(() => { | ||
this.renderer.listen( | ||
this.elementRef.nativeElement, | ||
'touchmove', | ||
(event: TouchEvent) => { | ||
if (this.cancelledScroll && event.cancelable) { | ||
event.preventDefault(); | ||
} | ||
} | ||
); | ||
}); | ||
} | ||
|
||
disableScroll(): void { | ||
this.cancelledScroll = true; | ||
this.renderer.setStyle(this.elementRef.nativeElement, 'overflow', 'hidden'); | ||
} | ||
|
||
enableScroll(): void { | ||
this.cancelledScroll = false; | ||
this.renderer.setStyle(this.elementRef.nativeElement, 'overflow', 'auto'); | ||
} | ||
|
||
hasScrollbar(): boolean { | ||
const containerHasHorizontalScroll = | ||
this.elementRef.nativeElement.scrollWidth - | ||
this.elementRef.nativeElement.clientWidth > | ||
0; | ||
const containerHasVerticalScroll = | ||
this.elementRef.nativeElement.scrollHeight - | ||
this.elementRef.nativeElement.clientHeight > | ||
0; | ||
return containerHasHorizontalScroll || containerHasVerticalScroll; | ||
} | ||
} |
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
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