Skip to content

Commit

Permalink
refactor(layout): add ability to use default values in scrollTo met…
Browse files Browse the repository at this point in the history
…hod (#580)
  • Loading branch information
nnixaa authored Jul 25, 2018
1 parent 0ccb4ab commit b36e1fa
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/framework/theme/components/layout/layout.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,11 @@ export class NbLayoutComponent implements AfterViewInit, OnDestroy {
return { x, y };
}

private scroll(x: number, y: number) {
private scroll(x: number = null, y: number = null) {
const { x: currentX, y: currentY } = this.getScrollPosition();
x = x == null ? currentX : x;
y = y == null ? currentY : y;

if (!isPlatformBrowser(this.platformId)) {
return;
}
Expand Down
95 changes: 95 additions & 0 deletions src/framework/theme/services/scroll.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,101 @@ describe('NbScrollService', () => {
});
});

it('should scroll using service (with default x)', (done) => {
componentInstance.useLocalScroll();
componentInstance.setSize('10000px', '10000px');
fixture.detectChanges();
scrollService.scrollTo(null, 10);
fixture.detectChanges();
scrollService.getPosition()
.subscribe((pos: NbScrollPosition) => {
expect(pos.x).toEqual(0);
expect(pos.y).toEqual(10);
done();
});
});

it('should scroll using service (with default y)', (done) => {
componentInstance.useLocalScroll();
componentInstance.setSize('10000px', '10000px');
fixture.detectChanges();
scrollService.scrollTo(10, null);
fixture.detectChanges();
scrollService.getPosition()
.subscribe((pos: NbScrollPosition) => {
expect(pos.x).toEqual(10);
expect(pos.y).toEqual(0);
done();
});
});

it('should scroll using service (with default x)', (done) => {
componentInstance.useLocalScroll();
componentInstance.setSize('10000px', '10000px');
fixture.detectChanges();
scrollService.scrollTo(10, 10);
fixture.detectChanges();

scrollService.scrollTo(null, 20);
fixture.detectChanges();
scrollService.getPosition()
.subscribe((pos: NbScrollPosition) => {
expect(pos.x).toEqual(10);
expect(pos.y).toEqual(20);
done();
});
});

it('should scroll using service (with default y)', (done) => {
componentInstance.useLocalScroll();
componentInstance.setSize('10000px', '10000px');
fixture.detectChanges();
scrollService.scrollTo(10, 10);
fixture.detectChanges();

scrollService.scrollTo(20, null);
fixture.detectChanges();
scrollService.getPosition()
.subscribe((pos: NbScrollPosition) => {
expect(pos.x).toEqual(20);
expect(pos.y).toEqual(10);
done();
});
});

it('should scroll using service back to 0,0', (done) => {
componentInstance.useLocalScroll();
componentInstance.setSize('10000px', '10000px');
fixture.detectChanges();
scrollService.scrollTo(10, 10);
fixture.detectChanges();

scrollService.scrollTo(0, 0);
fixture.detectChanges();
scrollService.getPosition()
.subscribe((pos: NbScrollPosition) => {
expect(pos.x).toEqual(0);
expect(pos.y).toEqual(0);
done();
});
});

it('should scroll using service back (with default x,y)', (done) => {
componentInstance.useLocalScroll();
componentInstance.setSize('10000px', '10000px');
fixture.detectChanges();
scrollService.scrollTo(10, 10);
fixture.detectChanges();

scrollService.scrollTo();
fixture.detectChanges();
scrollService.getPosition()
.subscribe((pos: NbScrollPosition) => {
expect(pos.x).toEqual(10);
expect(pos.y).toEqual(10);
done();
});
});

it('should listen to scroll', (done) => {
scrollService.onScroll()
Expand Down
2 changes: 1 addition & 1 deletion src/framework/theme/services/scroll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class NbLayoutScrollService {
* @param {number} x
* @param {number} y
*/
scrollTo(x: number, y: number) {
scrollTo(x: number = null, y: number = null) {
this.manualScroll$.next({ x, y });
}

Expand Down

0 comments on commit b36e1fa

Please sign in to comment.