diff --git a/angular/src/directives/navigation/go-back.ts b/angular/src/directives/navigation/go-back.ts index 649b88fa75..c675d07dec 100644 --- a/angular/src/directives/navigation/go-back.ts +++ b/angular/src/directives/navigation/go-back.ts @@ -1,5 +1,5 @@ import { Directive, HostListener } from '@angular/core'; -import { NavController } from '../../providers/nav-controller'; +import { NavController, NavIntent } from '../../providers/nav-controller'; @Directive({ selector: '[goBack]', @@ -12,6 +12,6 @@ export class GoBack { @HostListener('click') onClick() { - this.navCtrl.setGoback(); + this.navCtrl.setIntent(NavIntent.Back); } } diff --git a/angular/src/directives/navigation/ion-back-button.ts b/angular/src/directives/navigation/ion-back-button.ts index 53e80d60ad..51ca6e3164 100644 --- a/angular/src/directives/navigation/ion-back-button.ts +++ b/angular/src/directives/navigation/ion-back-button.ts @@ -1,6 +1,6 @@ import { Directive, ElementRef, HostListener, Input, Optional } from '@angular/core'; import { Router } from '@angular/router'; -import { NavController } from '../../providers/nav-controller'; +import { NavController, NavIntent } from '../../providers/nav-controller'; import { IonRouterOutlet } from './ion-router-outlet'; @Directive({ @@ -29,7 +29,7 @@ export class IonBackButton { this.routerOutlet.pop(); ev.preventDefault(); } else if (this.router && this.defaultHref != null) { - this.navCtrl.setGoback(); + this.navCtrl.setIntent(NavIntent.Back); this.router.navigateByUrl(this.defaultHref); ev.preventDefault(); } diff --git a/angular/src/providers/nav-controller.ts b/angular/src/providers/nav-controller.ts index a8d56e2486..6613bdd4b3 100644 --- a/angular/src/providers/nav-controller.ts +++ b/angular/src/providers/nav-controller.ts @@ -1,14 +1,41 @@ -import { Injectable } from '@angular/core'; +import { Injectable, Optional } from '@angular/core'; +import { NavigationExtras, Router, UrlTree } from '@angular/router'; + +export const enum NavIntent { + Auto, + Forward, + Back, + Root +} @Injectable() export class NavController { private direction = 0; - private goBack = false; + private intent: NavIntent = NavIntent.Auto; private stack: string[] = []; - setGoback() { - this.goBack = true; + constructor( + @Optional() private router?: Router + ) {} + + goForward(url: string | UrlTree, extras?: NavigationExtras) { + this.intent = NavIntent.Forward; + return this.router.navigateByUrl(url, extras); + } + + goBack(url: string | UrlTree, extras?: NavigationExtras) { + this.intent = NavIntent.Back; + return this.router.navigateByUrl(url, extras); + } + + goRoot(url: string | UrlTree, extras?: NavigationExtras) { + this.intent = NavIntent.Root; + return this.router.navigateByUrl(url, extras); + } + + setIntent(intent: NavIntent) { + this.intent = intent; } consumeDirection() { @@ -23,12 +50,17 @@ export class NavController { } } - const direction = this.goBack - ? -1 - : this.direction; + const direction = directionForIntent(this.intent, this.direction); - this.goBack = false; + this.intent = NavIntent.Auto; this.direction = 0; return direction; } } + +function directionForIntent(intent: NavIntent, nav: number): number { + if (intent === NavIntent.Auto) { + return nav; + } + return intent === NavIntent.Back ? -1 : 1; +}