1
1
import { Location } from '@angular/common' ;
2
2
import { Injectable , Optional } from '@angular/core' ;
3
3
import { NavigationExtras , NavigationStart , Router , UrlTree } from '@angular/router' ;
4
- import { RouterDirection } from '@ionic/core' ;
4
+ import { NavDirection , RouterDirection } from '@ionic/core' ;
5
5
6
6
import { Platform } from './platform' ;
7
7
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 { }
9
14
10
15
@Injectable ( )
11
16
export class NavController {
12
17
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 ;
15
20
private guessDirection : RouterDirection = 'forward' ;
16
- private guessAnimation = false ;
21
+ private guessAnimation ?: NavDirection ;
17
22
private lastNavId = - 1 ;
18
23
19
24
constructor (
@@ -26,8 +31,8 @@ export class NavController {
26
31
router . events . subscribe ( ev => {
27
32
if ( ev instanceof NavigationStart ) {
28
33
const id = ( ev . restoredState ) ? ev . restoredState . navigationId : ev . id ;
29
- this . guessAnimation = ! ev . restoredState ;
30
34
this . guessDirection = id < this . lastNavId ? 'back' : 'forward' ;
35
+ this . guessAnimation = ! ev . restoredState ? this . guessDirection : undefined ;
31
36
this . lastNavId = this . guessDirection === 'forward' ? ev . id : id ;
32
37
}
33
38
} ) ;
@@ -37,66 +42,77 @@ export class NavController {
37
42
platform . backButton . subscribeWithPriority ( 0 , ( ) => this . goBack ( ) ) ;
38
43
}
39
44
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 ) ;
42
47
if ( Array . isArray ( url ) ) {
43
- return this . router ! . navigate ( url , extras ) ;
48
+ return this . router ! . navigate ( url , options ) ;
44
49
} else {
45
- return this . router ! . navigateByUrl ( url , extras ) ;
50
+ return this . router ! . navigateByUrl ( url , options ) ;
46
51
}
47
52
}
48
53
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 ) ;
51
56
// extras = { replaceUrl: true, ...extras };
52
57
if ( Array . isArray ( url ) ) {
53
- return this . router ! . navigate ( url , extras ) ;
58
+ return this . router ! . navigate ( url , options ) ;
54
59
} else {
55
- return this . router ! . navigateByUrl ( url , extras ) ;
60
+ return this . router ! . navigateByUrl ( url , options ) ;
56
61
}
57
62
}
58
63
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 ) ;
61
66
if ( Array . isArray ( url ) ) {
62
- return this . router ! . navigate ( url , extras ) ;
67
+ return this . router ! . navigate ( url , options ) ;
63
68
} else {
64
- return this . router ! . navigateByUrl ( url , extras ) ;
69
+ return this . router ! . navigateByUrl ( url , options ) ;
65
70
}
66
71
}
67
72
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 ( ) ;
71
76
}
72
77
73
- setDirection ( direction : NavDirection , animated ?: boolean ) {
78
+ setDirection ( direction : RouterDirection , animated ?: boolean , animationDirection ?: 'forward' | 'back' ) {
74
79
this . direction = direction ;
75
- this . animated = ( animated === undefined )
76
- ? direction !== 'root'
77
- : animated ;
80
+ this . animated = getAnimation ( direction , animated , animationDirection ) ;
78
81
}
79
82
80
83
consumeTransition ( ) {
81
84
let direction : RouterDirection = 'root' ;
82
- let animated = false ;
85
+ let animation : NavDirection | undefined ;
83
86
84
87
if ( this . direction === 'auto' ) {
85
88
direction = this . guessDirection ;
86
- animated = this . guessAnimation ;
89
+ animation = this . guessAnimation ;
87
90
} else {
88
- animated = this . animated ;
91
+ animation = this . animated ;
89
92
direction = this . direction ;
90
93
}
91
94
this . direction = DEFAULT_DIRECTION ;
92
95
this . animated = DEFAULT_ANIMATED ;
93
96
94
97
return {
95
98
direction,
96
- animated
99
+ animation
97
100
} ;
98
101
}
99
102
}
100
103
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
+
101
117
const DEFAULT_DIRECTION = 'auto' ;
102
- const DEFAULT_ANIMATED = false ;
118
+ const DEFAULT_ANIMATED = undefined ;
0 commit comments