<!DOCTYPE html>
<html>

  <head>
    <base href="." />
    <script type="text/javascript" charset="utf-8">
      window.AngularVersionForThisPlunker = 'latest'
    </script>
    <title>angular playground</title>
    <link rel="stylesheet" href="style.css" />
    <script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script>
    <script src="https://unpkg.com/zone.js/dist/zone.js"></script>
    <script src="https://unpkg.com/zone.js/dist/long-stack-trace-zone.js"></script>
    <script src="https://unpkg.com/reflect-metadata@0.1.3/Reflect.js"></script>
    <script src="https://unpkg.com/systemjs@0.19.31/dist/system.js"></script>
    <script src="config.js"></script>
    <script>
    System.import('app')
      .catch(console.error.bind(console));
  </script>
  </head>

  <body>
    <div>This example is a part of the tutorial - <a href="https://angularbytes.witspry.com/2017/09/angular-5-routing-and-navigation.html">	
https://angularbytes.witspry.com/2017/09/angular-5-routing-and-navigation.html</a> </div>
    <div style="margin-bottom:20px;"></div>
    <div style="text-align:center;border-bottom-style:solid;"></div>
    <div style="margin-bottom:20px;"></div>
    <my-app>
    loading...
  </my-app>
  </body>

</html>
/* Styles go here */

### Angular Starter Plunker - Typescript
var angularVersion = '@5.0.0';

System.config({
  //use typescript for compilation
  transpiler: 'typescript',
  //typescript compiler options
  typescriptOptions: {
    emitDecoratorMetadata: true
  },
  paths: {
    'npm:': 'https://unpkg.com/'
  },
  //map tells the System loader where to look for things
  map: {
    
    'app': './src',
    '@angular/core': 'npm:@angular/core'+ angularVersion + '/bundles/core.umd.js',
    '@angular/common': 'npm:@angular/common' + angularVersion + '/bundles/common.umd.js',
    '@angular/common/http': 'npm:@angular/common' + angularVersion + '/bundles/common-http.umd.js',
    '@angular/compiler': 'npm:@angular/compiler' + angularVersion  + '/bundles/compiler.umd.js',
    '@angular/platform-browser': 'npm:@angular/platform-browser' + angularVersion + '/bundles/platform-browser.umd.js',
    '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic' + angularVersion + '/bundles/platform-browser-dynamic.umd.js',
    '@angular/http': 'npm:@angular/http' + angularVersion + '/bundles/http.umd.js',
    '@angular/router': 'npm:@angular/router' + angularVersion +'/bundles/router.umd.js',
    '@angular/forms': 'npm:@angular/forms' + angularVersion + '/bundles/forms.umd.js',
    '@angular/animations': 'npm:@angular/animations' + angularVersion + '/bundles/animations.umd.js',
    '@angular/platform-browser/animations': 'npm:@angular/platform-browser' + angularVersion + '/bundles/platform-browser-animations.umd.js',
    '@angular/animations/browser': 'npm:@angular/animations' + angularVersion + '/bundles/animations-browser.umd.js',
    
    '@angular/core/testing': 'npm:@angular/core' + angularVersion + '/bundles/core-testing.umd.js',
    '@angular/common/testing': 'npm:@angular/common' + angularVersion + '/bundles/common-testing.umd.js',
    '@angular/common/http/testing': 'npm:@angular/common' + angularVersion + '/bundles/common-http-testing.umd.js',
    '@angular/compiler/testing': 'npm:@angular/compiler' + angularVersion + '/bundles/compiler-testing.umd.js',
    '@angular/platform-browser/testing': 'npm:@angular/platform-browser' + angularVersion + '/bundles/platform-browser-testing.umd.js',
    '@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic' + angularVersion + '/bundles/platform-browser-dynamic-testing.umd.js',
    '@angular/http/testing': 'npm:@angular/http' + angularVersion + '/bundles/http-testing.umd.js',
    '@angular/router/testing': 'npm:@angular/router' + angularVersion + '/bundles/router-testing.umd.js',
    'tslib': 'npm:tslib@1.6.1',
    'rxjs': 'npm:rxjs@5.0.0',
    'typescript': 'npm:typescript@2.2.1/lib/typescript.js'
  },
  //packages defines our app package
  packages: {
    app: {
      main: './main.ts',
      defaultExtension: 'ts'
    },
    rxjs: {
      defaultExtension: 'js'
    }
  }
});
//main entry point
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';

platformBrowserDynamic().bootstrapModule(AppModule)
//our root app component
import {Component, NgModule, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {RouterModule} from '@angular/router'
import {Fruits} from './fruits'
import {Vegetables} from './vegetables'
import {Snacks} from './snacks'
import {PageNotFound} from './pagenotfound'
import {RouteGuardService} from './routeguardservice'

@Component({
  selector: 'my-app',
  template: `
    <div><h1>Select Food Category</h1></div>
    <nav><h3>
      <a [routerLink]="['/fruits']" routerLinkActive="active">Fruits</a>
      <a [routerLink]="['/vegetables']" routerLinkActive="active">Vegetables</a>
      <a [routerLink]="['/snacks', '1']" routerLinkActive="active">Normal Snacks</a>
      <a [routerLink]="['/snacks', '2']" routerLinkActive="active">Fat Free Snacks</a>
    </h3></nav>
    <router-outlet></router-outlet>
  `,
})
export class App {
  
  constructor() {
   
  }
}

const appRoutes: Routes = [
  { path: 'fruits', component: Fruits },
  { path: 'vegetables', component: Vegetables },
  { path: 'snacks/:id', canActivate : [RouteGuardService], component: Snacks },
  { path: '',   redirectTo: '/', pathMatch: 'full' },
  { path: '**', component: PageNotFound }
];

@NgModule({
  imports: [ BrowserModule, RouterModule.forRoot(appRoutes) ],
  declarations: [ App, Fruits, Vegetables, Snacks, PageNotFound ],
  providers : [RouteGuardService],
  bootstrap: [ App ]
})
export class AppModule {}
import {Component} from '@angular/core'

@Component({
  template: `
    <h2>Fruits</h2>
    <p>Choose fruits</p>
    <select>
      <option>Apple</option>
      <option>Banana</option>
      <option>Oranges</option>
      <option>Grapes</option>
      <option>Pomgranates</option>
    </select>
    `
})

export class Fruits {}
import {Component} from '@angular/core'

@Component({
  template: `
    <h2>Vegetables</h2>
    <p>Choose vegetables</p>
    <select>
      <option>Tomato</option>
      <option>Spinach</option>
      <option>Carrot</option>
      <option>Olives</option>
      <option>Potatoes</option>
    </select>
    `
})

export class Vegetables {}
import {Component} from '@angular/core'
import {ActivatedRoute} from '@angular/router'
import {Observable} from 'rxjs/Observable'
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/observable/of';

@Component({
  template: `
    <h2>Snacks | {{snackType == 1? 'Normal' : 'Fat free'}}</h2>
    <p>Choose snacks</p>
    <select>
      <option>Potato chips</option>
      <option>Nachos chips</option>
      <option>Biscuits - Sugar</option>
      <option>Biscuits - Salty</option>
      <option>Bread sandwich</option>
    </select>
    `
})

export class Snacks implements OnInit{
  
  snackType:number;
  constructor(private _route : ActivatedRoute){
    
  }
  
  ngOnInit(){
     this._route.paramMap
    .switchMap((params: ParamMap) => {
      return Observable.of(params.get('id'));
    })
    .subscribe(id => {this.snackType = +id;});
    
  }
}
import {Component} from '@angular/core'

@Component({
  template: `
    <h2>Oops - You landed to the page that doesn't exists.</h2>
    `
})

export class PageNotFound {}
import {Injectable} from '@angular/core'
import {CanActivate, ActivatedRouteSnapshot} from '@angular/router'

@Injectable()
export class RouteGuardService implements CanActivate {
  
  canActivate(route: ActivatedRouteSnapshot): boolean {
    let snackTypeId = route.url[1].path;
    if(snackTypeId == 2){
      alert('Not available for guest users!');
      return false;
    }
    return true;
  }
}