Skip to content

Commit 1a8660b

Browse files
committed
feat(scrollpoint): handle custom scroll points
1 parent 026e75a commit 1a8660b

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

projects/ng-sortgrid/src/lib/helpers/scroll-helper.service.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,31 @@ export class ScrollHelperService {
1313

1414
private window: WindowProxy;
1515
private DEFAULT_SCROLLSPEED = 50;
16+
private SCROLL_BUFFER = 50;
1617

1718
constructor(@Inject(DOCUMENT) private document) {
1819
this.window = document.defaultView;
1920
}
2021

21-
public scrollIfNecessary(element: HTMLElement, scrollPoints: ScrollPoints = {}, scrollSpeed?: number): void {
22-
const bounding = element.getBoundingClientRect();
23-
if (this.isTopScrollNeeded(bounding.top, scrollPoints.top)) {
22+
public scrollIfNecessary(event: any, scrollPoints: ScrollPoints = {}, scrollSpeed?: number): void {
23+
const position = event.pageY - this.window.scrollY;
24+
25+
if (this.isTopScrollNeeded(position, scrollPoints.top)) {
2426
this.window.scrollBy({top: -scrollSpeed || -this.DEFAULT_SCROLLSPEED, behavior: 'smooth'});
2527
return;
2628
}
2729

28-
if (this.isBottomScrollNeeded(bounding.top, scrollPoints.bottom)) {
30+
if (this.isBottomScrollNeeded(position, scrollPoints.bottom)) {
2931
this.window.scrollBy({top: scrollSpeed || this.DEFAULT_SCROLLSPEED, behavior: 'smooth'});
3032
}
3133
}
3234

33-
private isTopScrollNeeded(topBounding: number, scrollPointTop: number): boolean {
34-
return scrollPointTop ? topBounding < scrollPointTop : topBounding < 0;
35+
private isTopScrollNeeded(currentPosition: number, scrollPointTop: number): boolean {
36+
return scrollPointTop ? currentPosition < scrollPointTop + this.SCROLL_BUFFER : currentPosition < 20;
3537
}
3638

37-
private isBottomScrollNeeded(bottomBounding: number, scrollPointBottom: number): boolean {
38-
return scrollPointBottom ? bottomBounding < scrollPointBottom : bottomBounding > this.window.innerHeight;
39+
private isBottomScrollNeeded(currentPosition: number, scrollPointBottom: number): boolean {
40+
return scrollPointBottom ?
41+
currentPosition < scrollPointBottom : currentPosition > this.window.innerHeight - this.SCROLL_BUFFER;
3942
}
4043
}

projects/ng-sortgrid/src/lib/ngsg-item.directive.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
2828
@Input() ngSortGridGroup = 'defaultGroup';
2929
@Input() ngSortGridItems;
3030
@Input() scrollPointTop;
31+
@Input() scrollPointBottom;
3132
@Input() scrollSpeed;
33+
@Input() autoScroll = false;
3234

3335
@Output() sorted = new EventEmitter<any>();
3436

@@ -91,7 +93,12 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
9193

9294
@HostListener('drag', ['$event'])
9395
drag(event): void {
94-
this.scrollHelperService.scrollIfNecessary(event.target, {top: this.scrollPointTop}, this.scrollSpeed);
96+
if (this.autoScroll) {
97+
this.scrollHelperService.scrollIfNecessary(event, {
98+
top: this.scrollPointTop,
99+
bottom: this.scrollPointBottom
100+
}, this.scrollSpeed);
101+
}
95102
}
96103

97104
@HostListener('dragover', ['$event'])

0 commit comments

Comments
 (0)