|
| 1 | + |
| 2 | +// Import the core angular services. |
| 3 | +import { ActivatedRouteSnapshot } from "@angular/router"; |
| 4 | +import { CanActivate } from "@angular/router"; |
| 5 | +import { Injectable } from "@angular/core"; |
| 6 | +import { PRIMARY_OUTLET } from "@angular/router"; |
| 7 | +import { Router } from "@angular/router"; |
| 8 | +import { RouterStateSnapshot } from "@angular/router"; |
| 9 | +import { UrlTree } from "@angular/router"; |
| 10 | + |
| 11 | +// ----------------------------------------------------------------------------------- // |
| 12 | +// ----------------------------------------------------------------------------------- // |
| 13 | + |
| 14 | +@Injectable({ |
| 15 | + providedIn: "root" |
| 16 | +}) |
| 17 | +export class DoNotShowSecondaryOnRefreshGuard implements CanActivate { |
| 18 | + |
| 19 | + private router: Router; |
| 20 | + |
| 21 | + // I initialize the secondary-view route guard. |
| 22 | + constructor( router: Router ) { |
| 23 | + |
| 24 | + this.router = router; |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + // --- |
| 29 | + // PUBLIC METHODS. |
| 30 | + // --- |
| 31 | + |
| 32 | + // I determine if the requested route can be activated (ie, navigated to). |
| 33 | + public canActivate( |
| 34 | + activatedRouteSnapshot: ActivatedRouteSnapshot, |
| 35 | + routerStateSnapshot: RouterStateSnapshot |
| 36 | + ) : boolean { |
| 37 | + |
| 38 | + console.log( "[GUARD] Page Refresh:", this.isPageRefresh() ); |
| 39 | + |
| 40 | + // We don't want to render this secondary view on page-refresh. As such, if this |
| 41 | + // is a page-refresh, we'll navigate to the same URL less the secondary outlet. |
| 42 | + if ( this.isPageRefresh() ) { |
| 43 | + |
| 44 | + console.warn( "[GUARD] Secondary view not allowed on refresh." ); |
| 45 | + |
| 46 | + this.router.navigateByUrl( this.getUrlWithoutSecondary( routerStateSnapshot ) ); |
| 47 | + return( false ); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + return( true ); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + // --- |
| 56 | + // PRIVATE METHODS. |
| 57 | + // --- |
| 58 | + |
| 59 | + // I return the requested URL (as defined in the snapshot), less any the "secondary" |
| 60 | + // named-outlet segments. |
| 61 | + private getUrlWithoutSecondary( routerStateSnapshot: RouterStateSnapshot ) : UrlTree { |
| 62 | + |
| 63 | + var urlTree = this.router.parseUrl( routerStateSnapshot.url ); |
| 64 | + var segment = urlTree.root; |
| 65 | + |
| 66 | + // Since the "secondary" outlet is known to be directly off the primary view |
| 67 | + // (ie, not nested within another named-outlet), we're going to walk down the |
| 68 | + // tree of primary outlets and delete any "secondary" children. This should |
| 69 | + // leave us with a UrlTree that contains everything that the original URL had, |
| 70 | + // less the "secondary" named-outlet. |
| 71 | + while ( segment && segment.children ) { |
| 72 | + |
| 73 | + delete( segment.children.secondary ); |
| 74 | + |
| 75 | + segment = segment.children[ PRIMARY_OUTLET ]; |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + return( urlTree ); |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + // I determine if the current route-request is part of a page refresh. |
| 85 | + private isPageRefresh() : boolean { |
| 86 | + |
| 87 | + // If the router has yet to establish a single navigation, it means that this |
| 88 | + // navigation is the first attempt to reconcile the application state with the |
| 89 | + // URL state. Meaning, this is a page refresh. |
| 90 | + return( ! this.router.navigated ); |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments