Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
Enable scrollPositionRestoration in router. Code cleanup
Browse files Browse the repository at this point in the history
With the new scrollPositionRestoration option of Angular 6.1 we can
simplify the code controlling the scroll.

Going back to the AppListComponent doesn't work very well, as
scrollPositionRestoration has issues when data is loaded dynamically

More info about this at angular/angular#24547
  • Loading branch information
jgarciao committed Sep 26, 2018
1 parent 0a215c2 commit ce3bc4e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const appRoutes: Routes = [
appRoutes,
{
enableTracing: false, // <-- debugging purposes only
scrollPositionRestoration: 'top',
scrollPositionRestoration: 'enabled',
//anchorScrolling: 'enabled',
scrollOffset: [0, 0] // [x, y]
}
Expand Down
16 changes: 2 additions & 14 deletions src/app/pages/about/about.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Component, OnInit } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { SeoService } from '../../seo.service';

@Component({
Expand All @@ -9,22 +8,11 @@ import { SeoService } from '../../seo.service';
})
export class AboutComponent implements OnInit {

constructor(
private router: Router,
private seoService: SeoService) {

constructor(private seoService: SeoService) {
this.setPageMetadata();

}

ngOnInit() {
this.router.events.subscribe((evt) => {
if (!(evt instanceof NavigationEnd)) {
return;
}
window.scrollTo(0, 0);
});
}
ngOnInit() { }

setPageMetadata() {

Expand Down
18 changes: 8 additions & 10 deletions src/app/pages/app-details/app-details.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Component, OnInit, Input } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
import { ActivatedRoute, ParamMap } from '@angular/router';
import { ViewportScroller } from '@angular/common';
import { Router } from '@angular/router';
import { ActivatedRoute } from '@angular/router';

import { Location } from '@angular/common';
import { SeoService } from '../../seo.service';


import { App } from '../../shared/app.model';
import { Review } from '../../shared/review.model';
import { LinuxStoreApiService } from '../../linux-store-api.service';
Expand All @@ -18,6 +21,7 @@ export class AppDetailsComponent implements OnInit {

@Input() app: App;

scrollPosition: [number, number];
paramAppId: string;

reviews: Review[];
Expand All @@ -30,11 +34,12 @@ export class AppDetailsComponent implements OnInit {
private googleAnalyticsEventsService: GoogleAnalyticsEventsService,
private router: Router,
private route: ActivatedRoute,
private viewportScroller: ViewportScroller,
private location: Location,
private seoService: SeoService) {
}


}

setPageMetadata() {

Expand Down Expand Up @@ -86,13 +91,6 @@ export class AppDetailsComponent implements OnInit {
this.getApp(this.paramAppId);
}
);

this.router.events.subscribe((evt) => {
if (!(evt instanceof NavigationEnd)) {
return;
}
window.scrollTo(0, 0);
});
}

getApp(id: string): void {
Expand Down
17 changes: 7 additions & 10 deletions src/app/pages/app-list/app-list.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { ElementRef, ViewChild } from '@angular/core';
import { Router, ActivatedRoute, ParamMap, NavigationEnd } from '@angular/router';
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
import { ViewChild } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { BreakpointObserver } from '@angular/cdk/layout';
import { SeoService } from '../../seo.service';

import { App } from '../../shared/app.model';
Expand All @@ -18,6 +18,8 @@ export class AppListComponent implements OnInit {

@ViewChild('drawer') drawer;

scrollPosition: [number, number];

apps: App[];
categories: Category[];
selectedCategory: Category;
Expand All @@ -39,6 +41,7 @@ export class AppListComponent implements OnInit {
}

ngOnInit() {

this.getCategories();
this.getFeaturedCollections();

Expand All @@ -52,13 +55,6 @@ export class AppListComponent implements OnInit {
}
}
);

this.router.events.subscribe((evt) => {
if (!(evt instanceof NavigationEnd)) {
return;
}
window.scrollTo(0, 0);
});
}

isSmallScreen(): boolean {
Expand Down Expand Up @@ -118,6 +114,7 @@ export class AppListComponent implements OnInit {
'Developer Tools, Education, Games, Graphics & Photography, Communication & News, Productivity, Science, Settings, Utilities, ...',
this.getFlathubMetaImage());
}

}

showAppsSearchKeyword(searchKeyword: string): void {
Expand Down
9 changes: 1 addition & 8 deletions src/app/pages/home/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
import { Router } from '@angular/router';

import { App } from '../../shared/app.model';
import { Collection } from '../../shared/collection.model';
Expand Down Expand Up @@ -28,13 +28,6 @@ export class HomeComponent implements OnInit {
}

ngOnInit() {
this.router.events.subscribe((evt) => {
if (!(evt instanceof NavigationEnd)) {
return;
}
window.scrollTo(0, 0);
});

this.linuxStoreApiService.getFeaturedCollections()
.subscribe(collections => {
this.featuredCollections = collections;
Expand Down

0 comments on commit ce3bc4e

Please sign in to comment.