SHARE
TWEET

Untitled

a guest Jan 12th, 2017 168 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {
  2.     RouteReuseStrategy, DetachedRouteHandle, ActivatedRouteSnapshot, UrlSegment,
  3.     ActivatedRoute, PRIMARY_OUTLET
  4. } from '@angular/router';
  5.  
  6. export class CustomReuseStrategy implements RouteReuseStrategy {
  7.  
  8.     handlers: {[key: string]: DetachedRouteHandle} = {};
  9.  
  10.     shouldDetach(route: ActivatedRouteSnapshot): boolean {
  11.  
  12.         return !!this.getBreadcrumbs(route.root).join(".");
  13.     }
  14.  
  15.     store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
  16.  
  17.         let url = this.getBreadcrumbs(route.root).join(".");
  18.  
  19.         this.handlers[url] = handle;
  20.     }
  21.  
  22.     shouldAttach(route: ActivatedRouteSnapshot): boolean {
  23.  
  24.         let url = this.getBreadcrumbs(route.root).join(".");
  25.  
  26.         return !!this.handlers[url];
  27.     }
  28.  
  29.     retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
  30.  
  31.         let url = this.getBreadcrumbs(route.root).join(".");
  32.  
  33.         if (!url) return null;
  34.  
  35.         return this.handlers[url];
  36.     }
  37.  
  38.     shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
  39.         console.debug('CustomReuseStrategy:shouldReuseRoute', future, curr);
  40.  
  41.         return future.routeConfig === curr.routeConfig;
  42.     }
  43.  
  44.     private getBreadcrumbs(route: ActivatedRouteSnapshot, url: string = "", breadcrumbs: string[] = []): string[] {
  45.  
  46.         //get the child routes
  47.         let children: ActivatedRouteSnapshot[] = route.children;
  48.  
  49.         //return if there are no more children
  50.         if (children.length === 0) {
  51.             return breadcrumbs;
  52.         }
  53.  
  54.         //iterate over each children
  55.         for (let child of children) {
  56.             //verify primary route
  57.             if (child.outlet !== PRIMARY_OUTLET) {
  58.                 continue;
  59.             }
  60.  
  61.             //get the route's URL segment
  62.             let routeURL: string = child.url.map(segment => segment.path).join("/");
  63.  
  64.             //append route URL to URL
  65.             url += `/${routeURL}`;
  66.  
  67.             breadcrumbs.push(url);
  68.  
  69.             //recursive
  70.             return this.getBreadcrumbs(child, url, breadcrumbs);
  71.         }
  72.  
  73.         return breadcrumbs;
  74.     }
  75. }
RAW Paste Data
Pastebin PRO Summer Special!
Get 60% OFF on Pastebin PRO accounts!
Top