Skip to content

Commit

Permalink
feat(angular): push/setRoot/pop
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Apr 10, 2018
1 parent 81dc67d commit 4d23cba
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
4 changes: 2 additions & 2 deletions angular/src/directives/navigation/go-back.ts
Original file line number Diff line number Diff line change
@@ -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]',
Expand All @@ -12,6 +12,6 @@ export class GoBack {

@HostListener('click')
onClick() {
this.navCtrl.setGoback();
this.navCtrl.setIntent(NavIntent.Back);
}
}
4 changes: 2 additions & 2 deletions angular/src/directives/navigation/ion-back-button.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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();
}
Expand Down
48 changes: 40 additions & 8 deletions angular/src/providers/nav-controller.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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;
}

0 comments on commit 4d23cba

Please sign in to comment.