feat(angular): push/setRoot/pop

This commit is contained in:
Manu Mtz.-Almeida
2018-04-10 23:51:36 +02:00
parent 81dc67dda4
commit 4d23cba8a0
3 changed files with 44 additions and 12 deletions

View File

@ -1,5 +1,5 @@
import { Directive, HostListener } from '@angular/core'; import { Directive, HostListener } from '@angular/core';
import { NavController } from '../../providers/nav-controller'; import { NavController, NavIntent } from '../../providers/nav-controller';
@Directive({ @Directive({
selector: '[goBack]', selector: '[goBack]',
@ -12,6 +12,6 @@ export class GoBack {
@HostListener('click') @HostListener('click')
onClick() { onClick() {
this.navCtrl.setGoback(); this.navCtrl.setIntent(NavIntent.Back);
} }
} }

View File

@ -1,6 +1,6 @@
import { Directive, ElementRef, HostListener, Input, Optional } from '@angular/core'; import { Directive, ElementRef, HostListener, Input, Optional } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { NavController } from '../../providers/nav-controller'; import { NavController, NavIntent } from '../../providers/nav-controller';
import { IonRouterOutlet } from './ion-router-outlet'; import { IonRouterOutlet } from './ion-router-outlet';
@Directive({ @Directive({
@ -29,7 +29,7 @@ export class IonBackButton {
this.routerOutlet.pop(); this.routerOutlet.pop();
ev.preventDefault(); ev.preventDefault();
} else if (this.router && this.defaultHref != null) { } else if (this.router && this.defaultHref != null) {
this.navCtrl.setGoback(); this.navCtrl.setIntent(NavIntent.Back);
this.router.navigateByUrl(this.defaultHref); this.router.navigateByUrl(this.defaultHref);
ev.preventDefault(); ev.preventDefault();
} }

View File

@ -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() @Injectable()
export class NavController { export class NavController {
private direction = 0; private direction = 0;
private goBack = false; private intent: NavIntent = NavIntent.Auto;
private stack: string[] = []; private stack: string[] = [];
setGoback() { constructor(
this.goBack = true; @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() { consumeDirection() {
@ -23,12 +50,17 @@ export class NavController {
} }
} }
const direction = this.goBack const direction = directionForIntent(this.intent, this.direction);
? -1
: this.direction;
this.goBack = false; this.intent = NavIntent.Auto;
this.direction = 0; this.direction = 0;
return direction; return direction;
} }
} }
function directionForIntent(intent: NavIntent, nav: number): number {
if (intent === NavIntent.Auto) {
return nav;
}
return intent === NavIntent.Back ? -1 : 1;
}