-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #510 from appuio/back
Navigate back via history
- Loading branch information
Showing
14 changed files
with
86 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Directive, HostListener, Input } from '@angular/core'; | ||
import { NavigationService } from './navigation.service'; | ||
import { ActivatedRoute, Router } from '@angular/router'; | ||
|
||
/** | ||
* This directive adds a `click` event listener to navigate back in history. | ||
* It accepts an input that is used as the default path in case there is no history (e.g. opened link in a new tab). | ||
*/ | ||
@Directive({ | ||
selector: '[appBackLink]', | ||
}) | ||
export class BackLinkDirective { | ||
constructor(private navigation: NavigationService, private router: Router, private activatedRoute: ActivatedRoute) {} | ||
|
||
@Input() | ||
appBackLink?: string; | ||
|
||
@HostListener('click') | ||
onClick(): void { | ||
const route = this.navigation.previousLocation(this.appBackLink); | ||
void this.router.navigate([route], { relativeTo: this.activatedRoute }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { NavigationEnd, Router } from '@angular/router'; | ||
|
||
/** | ||
* Inspired by https://nils-mehlhorn.de/posts/angular-navigate-back-previous-page/ | ||
* Modified to be used with a Router working with default and relative paths in case the history is empty. | ||
*/ | ||
@Injectable({ providedIn: 'root' }) | ||
export class NavigationService { | ||
private history: string[] = []; | ||
|
||
constructor(private router: Router) { | ||
this.router.events.subscribe((event) => { | ||
if (event instanceof NavigationEnd) { | ||
this.history.push(event.urlAfterRedirects); | ||
this.history.splice(0, this.history.length - 5); // only keep the latest few, no need for more. | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Gets the previous URI location in the history. | ||
* @param defaultPath if the history is empty, return this path as fallback value | ||
* @returns the URI, or '/' if no default was given. | ||
*/ | ||
previousLocation(defaultPath?: string): string { | ||
void this.history.pop(); // remove "current" location | ||
if (this.history.length > 0) { | ||
const previousLocation = this.history.pop(); | ||
return previousLocation ?? defaultPath ?? '/'; | ||
} else { | ||
return defaultPath ?? '/'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters