-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from kreuzerk/feature/scrollPoints
Feature/scroll points
- Loading branch information
Showing
7 changed files
with
130 additions
and
77 deletions.
There are no files selected for viewing
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
79 changes: 79 additions & 0 deletions
79
projects/ng-sortgrid/src/lib/helpers/scroll-helper.service.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,79 @@ | ||
import {ScrollHelperService} from './scroll-helper.service'; | ||
import SpyObj = jasmine.SpyObj; | ||
|
||
describe('Scroll helper', () => { | ||
|
||
let sut: ScrollHelperService; | ||
const documentMock = { | ||
defaultView: { | ||
innerHeight: 0, | ||
innerWidth: 0, | ||
scrollBy: () => { | ||
} | ||
} | ||
}; | ||
let scrollSpy: SpyObj<any>; | ||
|
||
beforeEach(() => { | ||
sut = new ScrollHelperService(documentMock); | ||
scrollSpy = spyOn(documentMock.defaultView, 'scrollBy'); | ||
}); | ||
|
||
describe('Top scroll', () => { | ||
|
||
it('should scroll to the top with the default scroll speed when we drag over the top viewport', () => { | ||
const element = { | ||
getBoundingClientRect: () => ({top: -100, left: 0, bottom: 0, right: 0}) | ||
} as any; | ||
sut.scrollIfNecessary(element); | ||
expect(scrollSpy).toHaveBeenCalledWith({top: -50, behavior: 'smooth'}); | ||
}); | ||
|
||
it('should scroll to the top with the default scroll speed when we drag over the top scroll position', () => { | ||
const element = { | ||
getBoundingClientRect: () => ({top: 120, left: 0, bottom: 0, right: 0}) | ||
} as any; | ||
sut.scrollIfNecessary(element, {top: 140}); | ||
expect(scrollSpy).toHaveBeenCalledWith({top: -50, behavior: 'smooth'}); | ||
}); | ||
|
||
it('should scroll to the top with the custom scroll speed when we drag over the top viewport', () => { | ||
const element = { | ||
getBoundingClientRect: () => ({top: -100, left: 0, bottom: 0, right: 0}) | ||
} as any; | ||
const scrollSpeed = 100; | ||
sut.scrollIfNecessary(element, {}, scrollSpeed); | ||
expect(scrollSpy).toHaveBeenCalledWith({top: -scrollSpeed, behavior: 'smooth'}); | ||
}); | ||
|
||
}); | ||
|
||
describe('Bottom scroll', () => { | ||
|
||
it('should scroll to the bottom with the default scroll speed when we drag over the bottom viewport', () => { | ||
const element = { | ||
getBoundingClientRect: () => ({top: 100, left: 0, bottom: 20, right: 0}) | ||
} as any; | ||
sut.scrollIfNecessary(element); | ||
expect(scrollSpy).toHaveBeenCalledWith({top: 50, behavior: 'smooth'}); | ||
}); | ||
|
||
it('should scroll to the bottom with the default scroll speed when we drag over the bottom scroll position', () => { | ||
const element = { | ||
getBoundingClientRect: () => ({top: 120, left: 0, bottom: 200, right: 0}) | ||
} as any; | ||
sut.scrollIfNecessary(element, {bottom: 140}); | ||
expect(scrollSpy).toHaveBeenCalledWith({top: 50, behavior: 'smooth'}); | ||
}); | ||
|
||
it('should scroll to the top with the custom scroll speed when we drag over the top viewport', () => { | ||
const element = { | ||
getBoundingClientRect: () => ({top: 20, left: 0, bottom: 20, right: 0}) | ||
} as any; | ||
const scrollSpeed = 100; | ||
sut.scrollIfNecessary(element, {}, scrollSpeed); | ||
expect(scrollSpy).toHaveBeenCalledWith({top: scrollSpeed, behavior: 'smooth'}); | ||
}); | ||
|
||
}); | ||
}); |
40 changes: 40 additions & 0 deletions
40
projects/ng-sortgrid/src/lib/helpers/scroll-helper.service.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,40 @@ | ||
import {Inject, Injectable} from '@angular/core'; | ||
import {DOCUMENT} from '@angular/common'; | ||
|
||
export interface ScrollPoints { | ||
top?: number; | ||
bottom?: number; | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class ScrollHelperService { | ||
|
||
private window: WindowProxy; | ||
private DEFAULT_SCROLLSPEED = 50; | ||
|
||
constructor(@Inject(DOCUMENT) private document) { | ||
this.window = document.defaultView; | ||
} | ||
|
||
public scrollIfNecessary(element: HTMLElement, scrollPoints: ScrollPoints = {}, scrollSpeed?: number): void { | ||
const bounding = element.getBoundingClientRect(); | ||
if (this.isTopScrollNeeded(bounding.top, scrollPoints.top)) { | ||
this.window.scrollBy({top: -scrollSpeed || -this.DEFAULT_SCROLLSPEED, behavior: 'smooth'}); | ||
return; | ||
} | ||
|
||
if (this.isBottomScrollNeeded(bounding.top, scrollPoints.bottom)) { | ||
this.window.scrollBy({top: scrollSpeed || this.DEFAULT_SCROLLSPEED, behavior: 'smooth'}); | ||
} | ||
} | ||
|
||
private isTopScrollNeeded(topBounding: number, scrollPointTop: number): boolean { | ||
return scrollPointTop ? topBounding < scrollPointTop : topBounding < 0; | ||
} | ||
|
||
private isBottomScrollNeeded(bottomBounding: number, scrollPointBottom: number): boolean { | ||
return scrollPointBottom ? bottomBounding < scrollPointBottom : bottomBounding > this.window.innerHeight; | ||
} | ||
} |
32 changes: 0 additions & 32 deletions
32
projects/ng-sortgrid/src/lib/helpers/view-port.helper.service.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
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