Skip to content

Commit

Permalink
convert directive to service
Browse files Browse the repository at this point in the history
  • Loading branch information
rrchai committed Oct 23, 2023
1 parent c3f394c commit 846c977
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 55 deletions.
10 changes: 8 additions & 2 deletions apps/openchallenges/app/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { RouterOutlet } from '@angular/router';
import { HomeDataService } from '@sagebionetworks/openchallenges/home';
import { GoogleTagManagerComponent } from './google-tag-manager/google-tag-manager.component';
import { ConfigService } from '@sagebionetworks/openchallenges/config';
import { NgIf } from '@angular/common';
import { NgIf, ViewportScroller } from '@angular/common';
import { CustomScrollRestorationService } from './custom-scroll-restoration.service';

@Component({
selector: 'openchallenges-root',
Expand All @@ -36,7 +37,9 @@ export class AppComponent implements OnInit, OnDestroy {
constructor(
private pageTitleService: PageTitleService,
private homeDataService: HomeDataService,
private configService: ConfigService
private configService: ConfigService,
private viewportScroller: ViewportScroller,
private customScrollRestorationService: CustomScrollRestorationService
) {
this.useGoogleTagManager =
this.configService.config.googleTagManagerId.length > 0;
Expand All @@ -54,6 +57,9 @@ export class AppComponent implements OnInit, OnDestroy {
this.pageTitleService.setTitle('OpenChallenges');

this.homeDataService.setChallengesPerYear();
// overwrite the app's scroll restoration
this.viewportScroller.setHistoryScrollRestoration('manual');
this.customScrollRestorationService.scrollPositionRestoration();
}

ngOnDestroy(): void {
Expand Down
4 changes: 2 additions & 2 deletions apps/openchallenges/app/src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ export const appConfig: ApplicationConfig = {
withEnabledBlockingInitialNavigation(),
withInMemoryScrolling({
// anchorScrolling: 'enabled',
scrollPositionRestoration: 'enabled',
// scrollPositionRestoration: 'enabled',
}),
withRouterConfig({
onSameUrlNavigation: 'reload',
onSameUrlNavigation: 'ignore',
})
),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
import { ViewportScroller } from '@angular/common';
import { AfterViewInit, Directive, OnInit } from '@angular/core';
import { Injectable } from '@angular/core';
import { Event, Router, RoutesRecognized, Scroll } from '@angular/router';
import { combineLatest, filter, pairwise } from 'rxjs';

/**
* A directive for restoring scroll positions based on the method of navigation.
* A service for restoring scroll positions based on the method of navigation.
* It helps preserve the user's scroll position during various types of navigation actions.
* - Backward navigation: Scrolls to the previous position.
* - Anchor navigation: Scrolls to a specified anchor.
* - Param navigation: Maintains the current scroll position,
* - Forward navigation/Same url navigation: Scrolls to the top of the page.
*/

@Directive({
selector: '[sageCustomScrollRestore]',
standalone: true,
@Injectable({
providedIn: 'root',
})
export class CustomScrollRestoreDirective implements OnInit, AfterViewInit {
export class CustomScrollRestorationService {
private scrollX = 0;
private scrollY = 0;
private previousUrl!: string;

constructor(
private router: Router,
private viewportScroller: ViewportScroller
) {}

ngOnInit(): void {
scrollPositionRestoration(): void {
// Capture the current scroll position
window.addEventListener('scroll', () => {
this.scrollX = window.scrollX;
this.scrollY = window.scrollY;
});
}

ngAfterViewInit(): void {
const routesRecognized$ = this.router.events.pipe(
filter(
(e: Event): e is RoutesRecognized => e instanceof RoutesRecognized
Expand All @@ -52,7 +48,7 @@ export class CustomScrollRestoreDirective implements OnInit, AfterViewInit {
const previousUrl = routeEvents[0].urlAfterRedirects;
const currentUrl = routeEvents[1].urlAfterRedirects;
const isSamePageUrl = previousUrl === currentUrl;
console.log(1);

if (scrollEvent.position) {
// backward navigation
this.viewportScroller.scrollToPosition(scrollEvent.position);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<main sageCustomScrollRestore class="base mat-typography">
<main class="base mat-typography">
<section id="search-top">
<div id="title" class="row">
<div class="col col-10">
Expand Down Expand Up @@ -35,18 +35,18 @@ <h2>Challenges</h2>
<section id="bottom" class="content">
<div class="facets">
<p-panel
header="{{ incentivesFilter.label }}"
[toggleable]="true"
[collapsed]="incentivesFilter.collapsed"
>
<openchallenges-checkbox-filter
[values]="incentivesFilter.values"
[selectedValues]="selectedIncentives"
(selectionChange)="onIncentivesChange($event)"
inputId="{{ incentivesFilter.query }}"
/>
</p-panel>
<p-divider></p-divider>
header="{{ incentivesFilter.label }}"
[toggleable]="true"
[collapsed]="incentivesFilter.collapsed"
>
<openchallenges-checkbox-filter
[values]="incentivesFilter.values"
[selectedValues]="selectedIncentives"
(selectionChange)="onIncentivesChange($event)"
inputId="{{ incentivesFilter.query }}"
/>
</p-panel>
<p-divider></p-divider>
<p-panel
header="{{ statusFilter.label }}"
[toggleable]="true"
Expand All @@ -61,18 +61,18 @@ <h2>Challenges</h2>
</p-panel>
<p-divider></p-divider>
<p-panel
header="{{ categoriesFilter.label }}"
[toggleable]="true"
[collapsed]="categoriesFilter.collapsed"
>
<openchallenges-checkbox-filter
[values]="categoriesFilter.values"
[selectedValues]="selectedCategories"
(selectionChange)="onCategoriesChange($event)"
inputId="{{ categoriesFilter.query }}"
/>
</p-panel>
<p-divider></p-divider>
header="{{ categoriesFilter.label }}"
[toggleable]="true"
[collapsed]="categoriesFilter.collapsed"
>
<openchallenges-checkbox-filter
[values]="categoriesFilter.values"
[selectedValues]="selectedCategories"
(selectionChange)="onCategoriesChange($event)"
inputId="{{ categoriesFilter.query }}"
/>
</p-panel>
<p-divider></p-divider>
<!-- <p-panel
header="{{ inputDataTypesFilter.label }}"
[toggleable]="true"
Expand Down Expand Up @@ -168,7 +168,10 @@ <h2>Challenges</h2>
<div class="main col">
<h3>Results ({{ searchResultsCount }})</h3>
<div class="card-group">
<openchallenges-challenge-card *ngFor="let challenge of challenges" [challenge]="challenge"/>
<openchallenges-challenge-card
*ngFor="let challenge of challenges"
[challenge]="challenge"
/>
</div>
<openchallenges-paginator
[pageNumber]="selectedPageNumber || defaultPageNumber"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<main sageCustomScrollRestore class="base mat-typography">
<main class="base mat-typography">
<ng-container *ngIf="challenge$ | async as challenge">
<section id="profile-top" class="row">
<openchallenges-avatar class="profile-pic" [avatar]="challengeAvatar" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<main sageCustomScrollRestore class="base mat-typography">
<main class="base mat-typography">
<ng-container *ngIf="organization$ | async as org">
<section id="profile-top" class="row">
<openchallenges-avatar
Expand All @@ -18,7 +18,7 @@ <h2>
<section id="profile-stats" class="row">
<div class="col fill-empty"></div>
<div class="col">
<openchallenges-org-profile-stats [loggedIn]="loggedIn"/>
<openchallenges-org-profile-stats [loggedIn]="loggedIn" />
</div>
</section>
<section id="profile-bottom" class="content">
Expand Down Expand Up @@ -60,7 +60,10 @@ <h2>
*ngSwitchCase="tabs['challenges']"
[organization]="org"
/>
<openchallenges-org-profile-members *ngSwitchCase="tabs['members']" [organization]="org"/>
<openchallenges-org-profile-members
*ngSwitchCase="tabs['members']"
[organization]="org"
/>
</ng-container>
</div>
</section>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<main sageCustomScrollRestore class="base mat-typography">
<main class="base mat-typography">
<section id="search-top">
<div id="title" class="row">
<div class="col col-10">
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion libs/shared/typescript/util/src/lib/directives/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './app-shell-no-render.directive';
export * from './app-shell-render.directive';
export * from './custom-scroll-restore.directive';

0 comments on commit 846c977

Please sign in to comment.