-
-
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 #15 from kreuzerk/feature/scroll
feat(viewport): add view port service
- Loading branch information
Showing
4 changed files
with
85 additions
and
7 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
projects/ng-sortgrid/src/lib/helpers/view-port.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,32 @@ | ||
import {ViewPortService} from './view-port.service'; | ||
import {beforeEach} from '@angular/core/testing/src/testing_internal'; | ||
|
||
describe('Viewport helper', () => { | ||
|
||
let sut: ViewPortService; | ||
const documentMock = { | ||
defaultView: { | ||
innerHeight: 0, | ||
innerWidth: 0 | ||
}, | ||
}; | ||
|
||
beforeEach(() => sut = new ViewPortService(documentMock)); | ||
|
||
it('should detect when we drag over the top viewport', () => { | ||
const element = { | ||
getBoundingClientRect: () => ({top: -100, left: 0, bottom: 0, right: 0}) | ||
} as any; | ||
const viewPortOverflow = sut.isOutOfViewport(element); | ||
expect(viewPortOverflow.top).toBeTruthy(); | ||
}); | ||
|
||
it('should detect when we drag over the bottom viewport', () => { | ||
spyOn(window, 'innerHeight').and.returnValue(0); | ||
const element = { | ||
getBoundingClientRect: () => ({top: 0, left: 0, bottom: 100, right: 0}) | ||
} as any; | ||
const viewPortOverflow = sut.isOutOfViewport(element); | ||
expect(viewPortOverflow.bottom).toBeTruthy(); | ||
}); | ||
}); |
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,28 @@ | ||
import {Inject, Injectable} from '@angular/core'; | ||
import {DOCUMENT} from '@angular/common'; | ||
|
||
export interface ViewPortOverflow { | ||
top: boolean; | ||
bottom: boolean; | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class ViewPortService { | ||
|
||
private window: WindowProxy; | ||
|
||
constructor(@Inject(DOCUMENT) private document) { | ||
this.window = document.defaultView; | ||
} | ||
|
||
public isOutOfViewport(element: HTMLElement): ViewPortOverflow { | ||
|
||
const bounding = element.getBoundingClientRect(); | ||
return { | ||
top: bounding.top < 0, | ||
bottom: bounding.bottom > (this.window.innerHeight || document.documentElement.clientHeight) | ||
}; | ||
} | ||
} |
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