Skip to content

Commit 320eb03

Browse files
authored
feat(angular): expose animationDirection (#16802)
* feat(angular): expose animationDirection * update
1 parent dfbb5b6 commit 320eb03

File tree

4 files changed

+57
-42
lines changed

4 files changed

+57
-42
lines changed

angular/src/directives/navigation/ion-tabs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ export class IonTabs {
6464
? href
6565
: this.outlet.getLastUrl(tab) || href;
6666

67-
return this.navCtrl.navigateBack(url, true);
67+
return this.navCtrl.navigateBack(url);
6868
}
6969
}

angular/src/directives/navigation/router-link-delegate.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { LocationStrategy } from '@angular/common';
22
import { Directive, ElementRef, HostListener, Input, Optional } from '@angular/core';
33
import { Router, RouterLink } from '@angular/router';
4+
import { RouterDirection } from '@ionic/core';
45
import { Subscription } from 'rxjs';
56

6-
import { NavController, NavDirection } from '../../providers/nav-controller';
7+
import { NavController } from '../../providers/nav-controller';
78

89
@Directive({
910
selector: '[routerLink]',
@@ -12,7 +13,7 @@ export class RouterLinkDelegate {
1213

1314
private subscription?: Subscription;
1415

15-
@Input() routerDirection: NavDirection = 'forward';
16+
@Input() routerDirection: RouterDirection = 'forward';
1617

1718
constructor(
1819
private locationStrategy: LocationStrategy,

angular/src/directives/navigation/stack-controller.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ComponentRef, NgZone } from '@angular/core';
22
import { ActivatedRoute, Router } from '@angular/router';
33
import { RouterDirection } from '@ionic/core';
44

5-
import { NavController, NavDirection } from '../../providers/nav-controller';
5+
import { NavController } from '../../providers/nav-controller';
66

77
import { RouteView, computeStackId, destroyView, getUrl, insertView, isTabSwitch, toSegments } from './stack-utils';
88

@@ -43,14 +43,14 @@ export class StackController {
4343
}
4444

4545
async setActive(enteringView: RouteView) {
46-
let { direction, animated } = this.navCtrl.consumeTransition();
46+
let { direction, animation } = this.navCtrl.consumeTransition();
4747
const leavingView = this.activeView;
4848
if (isTabSwitch(enteringView, leavingView)) {
4949
direction = 'back';
50-
animated = false;
50+
animation = undefined;
5151
}
5252
this.insertView(enteringView, direction);
53-
await this.transition(enteringView, leavingView, direction, animated, this.canGoBack(1), false);
53+
await this.transition(enteringView, leavingView, animation, this.canGoBack(1), false);
5454
this.cleanup();
5555
}
5656

@@ -73,7 +73,6 @@ export class StackController {
7373
views[views.length - 1], // leaving view
7474
'back',
7575
true,
76-
true,
7776
true
7877
);
7978
}
@@ -130,8 +129,7 @@ export class StackController {
130129
private async transition(
131130
enteringView: RouteView | undefined,
132131
leavingView: RouteView | undefined,
133-
direction: NavDirection,
134-
animated: boolean,
132+
direction: 'forward' | 'back' | undefined,
135133
showGoBack: boolean,
136134
progressAnimation: boolean
137135
) {
@@ -159,9 +157,9 @@ export class StackController {
159157

160158
await containerEl.componentOnReady();
161159
this.runningTransition = containerEl.commit(enteringEl, leavingEl, {
162-
duration: !animated ? 0 : undefined,
163-
direction: direction === 'forward' ? 'forward' : 'back', // TODO: refactor
164160
deepWait: true,
161+
duration: direction === undefined ? 0 : undefined,
162+
direction,
165163
showGoBack,
166164
progressAnimation
167165
});
Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import { Location } from '@angular/common';
22
import { Injectable, Optional } from '@angular/core';
33
import { NavigationExtras, NavigationStart, Router, UrlTree } from '@angular/router';
4-
import { RouterDirection } from '@ionic/core';
4+
import { NavDirection, RouterDirection } from '@ionic/core';
55

66
import { Platform } from './platform';
77

8-
export type NavDirection = 'forward' | 'back' | 'root' | 'auto';
8+
export interface AnimationOptions {
9+
animated?: boolean;
10+
animationDirection?: 'forward' | 'back';
11+
}
12+
13+
export interface NavigationOptions extends NavigationExtras, AnimationOptions {}
914

1015
@Injectable()
1116
export class NavController {
1217

13-
private direction: NavDirection = DEFAULT_DIRECTION;
14-
private animated = DEFAULT_ANIMATED;
18+
private direction: 'forward' | 'back' | 'root' | 'auto' = DEFAULT_DIRECTION;
19+
private animated?: NavDirection = DEFAULT_ANIMATED;
1520
private guessDirection: RouterDirection = 'forward';
16-
private guessAnimation = false;
21+
private guessAnimation?: NavDirection;
1722
private lastNavId = -1;
1823

1924
constructor(
@@ -26,8 +31,8 @@ export class NavController {
2631
router.events.subscribe(ev => {
2732
if (ev instanceof NavigationStart) {
2833
const id = (ev.restoredState) ? ev.restoredState.navigationId : ev.id;
29-
this.guessAnimation = !ev.restoredState;
3034
this.guessDirection = id < this.lastNavId ? 'back' : 'forward';
35+
this.guessAnimation = !ev.restoredState ? this.guessDirection : undefined;
3136
this.lastNavId = this.guessDirection === 'forward' ? ev.id : id;
3237
}
3338
});
@@ -37,66 +42,77 @@ export class NavController {
3742
platform.backButton.subscribeWithPriority(0, () => this.goBack());
3843
}
3944

40-
navigateForward(url: string | UrlTree | any[], animated?: boolean, extras?: NavigationExtras) {
41-
this.setDirection('forward', animated);
45+
navigateForward(url: string | UrlTree | any[], options: NavigationOptions = {}) {
46+
this.setDirection('forward', options.animated, options.animationDirection);
4247
if (Array.isArray(url)) {
43-
return this.router!.navigate(url, extras);
48+
return this.router!.navigate(url, options);
4449
} else {
45-
return this.router!.navigateByUrl(url, extras);
50+
return this.router!.navigateByUrl(url, options);
4651
}
4752
}
4853

49-
navigateBack(url: string | UrlTree | any[], animated?: boolean, extras?: NavigationExtras) {
50-
this.setDirection('back', animated);
54+
navigateBack(url: string | UrlTree | any[], options: NavigationOptions = {}) {
55+
this.setDirection('back', options.animated, options.animationDirection);
5156
// extras = { replaceUrl: true, ...extras };
5257
if (Array.isArray(url)) {
53-
return this.router!.navigate(url, extras);
58+
return this.router!.navigate(url, options);
5459
} else {
55-
return this.router!.navigateByUrl(url, extras);
60+
return this.router!.navigateByUrl(url, options);
5661
}
5762
}
5863

59-
navigateRoot(url: string | UrlTree | any[], animated?: boolean, extras?: NavigationExtras) {
60-
this.setDirection('root', animated);
64+
navigateRoot(url: string | UrlTree | any[], options: NavigationOptions = {}) {
65+
this.setDirection('root', options.animated, options.animationDirection);
6166
if (Array.isArray(url)) {
62-
return this.router!.navigate(url, extras);
67+
return this.router!.navigate(url, options);
6368
} else {
64-
return this.router!.navigateByUrl(url, extras);
69+
return this.router!.navigateByUrl(url, options);
6570
}
6671
}
6772

68-
goBack(animated?: boolean) {
69-
this.setDirection('back', animated);
70-
return this.location.back();
73+
goBack(options: AnimationOptions = { animated: true, animationDirection: 'back' }) {
74+
this.setDirection('back', options.animated, options.animationDirection);
75+
return this.location.back();
7176
}
7277

73-
setDirection(direction: NavDirection, animated?: boolean) {
78+
setDirection(direction: RouterDirection, animated?: boolean, animationDirection?: 'forward' | 'back') {
7479
this.direction = direction;
75-
this.animated = (animated === undefined)
76-
? direction !== 'root'
77-
: animated;
80+
this.animated = getAnimation(direction, animated, animationDirection);
7881
}
7982

8083
consumeTransition() {
8184
let direction: RouterDirection = 'root';
82-
let animated = false;
85+
let animation: NavDirection | undefined;
8386

8487
if (this.direction === 'auto') {
8588
direction = this.guessDirection;
86-
animated = this.guessAnimation;
89+
animation = this.guessAnimation;
8790
} else {
88-
animated = this.animated;
91+
animation = this.animated;
8992
direction = this.direction;
9093
}
9194
this.direction = DEFAULT_DIRECTION;
9295
this.animated = DEFAULT_ANIMATED;
9396

9497
return {
9598
direction,
96-
animated
99+
animation
97100
};
98101
}
99102
}
100103

104+
function getAnimation(direction: RouterDirection, animated: boolean | undefined, animationDirection: 'forward' | 'back' | undefined): NavDirection | undefined {
105+
if (animated === false) {
106+
return undefined;
107+
}
108+
if (animationDirection !== undefined) {
109+
return animationDirection;
110+
}
111+
if (direction === 'forward' || direction === 'back') {
112+
return direction;
113+
}
114+
return undefined;
115+
}
116+
101117
const DEFAULT_DIRECTION = 'auto';
102-
const DEFAULT_ANIMATED = false;
118+
const DEFAULT_ANIMATED = undefined;

0 commit comments

Comments
 (0)