mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-16 10:01:59 +08:00
feat(angular): push/setRoot/pop
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user