diff --git a/src/framework/theme/components/layout/layout.component.ts b/src/framework/theme/components/layout/layout.component.ts index 3b103c77df..197fe4768c 100644 --- a/src/framework/theme/components/layout/layout.component.ts +++ b/src/framework/theme/components/layout/layout.component.ts @@ -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; } diff --git a/src/framework/theme/services/scroll.service.spec.ts b/src/framework/theme/services/scroll.service.spec.ts index 3f420b0150..17003dac74 100644 --- a/src/framework/theme/services/scroll.service.spec.ts +++ b/src/framework/theme/services/scroll.service.spec.ts @@ -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() diff --git a/src/framework/theme/services/scroll.service.ts b/src/framework/theme/services/scroll.service.ts index 0eb6c339a8..84c9c4d36e 100644 --- a/src/framework/theme/services/scroll.service.ts +++ b/src/framework/theme/services/scroll.service.ts @@ -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 }); }