Commit f54c67c2 authored by Vitali Stupin's avatar Vitali Stupin
Browse files

Better scroll restoration

parent ce3aed3f
......@@ -84,7 +84,6 @@ export class MethodsService {
return filtered.slice(this.offset, this.limit);
}
// TODO: move that to Class constructor?
private setFullNames() {
for (let i in this.subsystems) {
this.subsystems[i].fullSubsystemName = this.subsystems[i].xRoadInstance
......@@ -126,7 +125,7 @@ export class MethodsService {
return this.instance
}
emitWarning(msg: string) {
private emitWarning(msg: string) {
this.warnings.emit(msg);
}
......
......@@ -2,7 +2,7 @@ import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subsystem } from '../subsystem';
import { MethodsService } from '../methods.service';
import { ActivatedRoute, Router, Scroll } from '@angular/router';
import { Subscription } from 'rxjs';
import { Subscription, BehaviorSubject } from 'rxjs';
import { ViewportScroller } from '@angular/common';
import { filter } from 'rxjs/operators';
......@@ -14,11 +14,12 @@ import { filter } from 'rxjs/operators';
export class SubsystemListComponent implements OnInit, OnDestroy {
subsystems: Subsystem[]
message: string = ''
scrollPosition: [number, number] = [0, 0]
scrollSubject: BehaviorSubject<any> = new BehaviorSubject(null);
routerScrollSubscription: Subscription
routeSubscription: Subscription
updatedSubscription: Subscription
warningsSubscription: Subscription
scrollSubjectSubscription: Subscription
constructor(
private methodsService: MethodsService,
......@@ -29,11 +30,9 @@ export class SubsystemListComponent implements OnInit, OnDestroy {
// Geting previous scroll position
this.routerScrollSubscription = this.router.events.pipe(
filter(e => e instanceof Scroll)
).subscribe(e => {
).subscribe(async(e) => {
if ((e as Scroll).position) {
this.scrollPosition = (e as Scroll).position;
} else {
this.scrollPosition = [0, 0];
this.scrollSubject.next((e as Scroll).position)
}
});
}
......@@ -73,11 +72,11 @@ export class SubsystemListComponent implements OnInit, OnDestroy {
ngAfterViewInit() {
// Restoring scroll position
this.viewportScroller.scrollToPosition(this.scrollPosition);
// TODO: what if this.scrollPosition is not ready yet?
/*this.routerScrollSubscription.add(() => {
this.viewportScroller.scrollToPosition(this.scrollPosition);
})*/
this.scrollSubjectSubscription = this.scrollSubject.subscribe( position => {
if(position) {
this.viewportScroller.scrollToPosition(position);
}
})
}
ngOnDestroy() {
......@@ -85,6 +84,7 @@ export class SubsystemListComponent implements OnInit, OnDestroy {
this.routeSubscription.unsubscribe()
this.updatedSubscription.unsubscribe()
this.warningsSubscription.unsubscribe()
this.scrollSubjectSubscription.unsubscribe()
}
getInstance(): string {
......
......@@ -2,9 +2,9 @@ import { Component, OnInit, OnDestroy } from '@angular/core';
import { MethodsService } from '../methods.service';
import { Subsystem } from '../subsystem';
import { ActivatedRoute, Router, Scroll } from '@angular/router';
import { Subscription } from 'rxjs';
import { Subscription, BehaviorSubject } from 'rxjs';
import { ViewportScroller } from '@angular/common';
import { filter, take } from 'rxjs/operators';
import { filter } from 'rxjs/operators';
@Component({
selector: 'app-subsystem',
......@@ -15,11 +15,12 @@ export class SubsystemComponent implements OnInit, OnDestroy {
subsystem: Subsystem
subsystemId: string
message: string = ''
scrollPosition: [number, number] = [0, 0]
scrollSubject: BehaviorSubject<any> = new BehaviorSubject(null);
routerScrollSubscription: Subscription
routeSubscription: Subscription
updatedSubscription: Subscription
warningsSubscription: Subscription
scrollSubjectSubscription: Subscription
constructor(
private methodsService: MethodsService,
......@@ -29,13 +30,10 @@ export class SubsystemComponent implements OnInit, OnDestroy {
) {
// Geting previous scroll position
this.routerScrollSubscription = this.router.events.pipe(
filter(e => e instanceof Scroll),
take(1)
).subscribe(e => {
filter(e => e instanceof Scroll)
).subscribe(async(e) => {
if ((e as Scroll).position) {
this.scrollPosition = (e as Scroll).position;
} else {
this.scrollPosition = [0, 0];
this.scrollSubject.next((e as Scroll).position)
}
});
}
......@@ -86,11 +84,11 @@ export class SubsystemComponent implements OnInit, OnDestroy {
ngAfterViewInit() {
// Restoring scroll position
this.viewportScroller.scrollToPosition(this.scrollPosition);
// TODO: what if this.scrollPosition is not ready yet?
/*this.routerScrollSubscription.add(() => {
this.viewportScroller.scrollToPosition(this.scrollPosition);
})*/
this.scrollSubjectSubscription = this.scrollSubject.subscribe( position => {
if(position) {
this.viewportScroller.scrollToPosition(position);
}
})
}
ngOnDestroy() {
......@@ -98,6 +96,7 @@ export class SubsystemComponent implements OnInit, OnDestroy {
this.routeSubscription.unsubscribe()
this.updatedSubscription.unsubscribe()
this.warningsSubscription.unsubscribe()
this.scrollSubjectSubscription.unsubscribe()
}
getInstance(): string {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment