1
1
import { Directive , HostListener , Input , Optional , OnChanges } from "@angular/core" ;
2
2
import { NavigationExtras } from "@angular/router" ;
3
- import { ActivatedRoute , Router , UrlTree } from "@angular/router" ;
3
+ import { ActivatedRoute , Router , UrlTree , } from "@angular/router" ;
4
4
import { routerLog } from "../trace" ;
5
5
import { PageRoute } from "./page-router-outlet" ;
6
6
import { RouterExtensions } from "./router-extensions" ;
7
7
import { NavigationOptions } from "./ns-location-strategy" ;
8
8
import { NavigationTransition } from "tns-core-modules/ui/frame" ;
9
9
import { isString } from "tns-core-modules/utils/types" ;
10
10
11
+ // Copied from "@angular/router/src/config"
12
+ export type QueryParamsHandling = 'merge' | 'preserve' | '' ;
13
+
11
14
/**
12
15
* The nsRouterLink directive lets you link to specific parts of your app.
13
16
*
@@ -33,30 +36,27 @@ import { isString } from "tns-core-modules/utils/types";
33
36
* And if the segment begins with `../`, the router will go up one level.
34
37
*/
35
38
@Directive ( { selector : "[nsRouterLink]" } )
36
- export class NSRouterLink implements OnChanges { // tslint:disable-line:directive-class-suffix
37
- private commands : any [ ] = [ ] ;
39
+ export class NSRouterLink { // tslint:disable-line:directive-class-suffix
38
40
@Input ( ) target : string ;
39
41
@Input ( ) queryParams : { [ k : string ] : any } ;
40
42
@Input ( ) fragment : string ;
41
43
44
+ @Input ( ) queryParamsHandling : QueryParamsHandling ;
45
+ @Input ( ) preserveQueryParams : boolean ;
46
+ @Input ( ) preserveFragment : boolean ;
47
+ @Input ( ) skipLocationChange : boolean ;
48
+ @Input ( ) replaceUrl : boolean ;
49
+
42
50
@Input ( ) clearHistory : boolean ;
43
51
@Input ( ) pageTransition : boolean | string | NavigationTransition = true ;
52
+ @Input ( ) pageTransitionDuration ;
44
53
45
- urlTree : UrlTree ;
46
-
47
- private usePageRoute : boolean ;
48
-
49
- private get currentRoute ( ) : ActivatedRoute {
50
- return this . usePageRoute ? this . pageRoute . activatedRoute . getValue ( ) : this . route ;
51
- }
54
+ private commands : any [ ] = [ ] ;
52
55
53
56
constructor (
54
57
private router : Router ,
55
58
private navigator : RouterExtensions ,
56
- private route : ActivatedRoute ,
57
- @Optional ( ) private pageRoute : PageRoute ) {
58
-
59
- this . usePageRoute = ( this . pageRoute && this . route === this . pageRoute . activatedRoute . getValue ( ) ) ;
59
+ private route : ActivatedRoute ) {
60
60
}
61
61
62
62
@Input ( "nsRouterLink" )
@@ -68,12 +68,9 @@ export class NSRouterLink implements OnChanges { // tslint:disable-line:directiv
68
68
}
69
69
}
70
70
71
-
72
71
@HostListener ( "tap" )
73
72
onTap ( ) {
74
- routerLog ( "nsRouterLink.tapped: " + this . commands + " usePageRoute: " +
75
- this . usePageRoute + " clearHistory: " + this . clearHistory + " transition: " +
76
- JSON . stringify ( this . pageTransition ) ) ;
73
+ routerLog ( `nsRouterLink.tapped: ${ this . commands } clear: ${ this . clearHistory } transition: ${ JSON . stringify ( this . pageTransition ) } duration: ${ this . pageTransitionDuration } ` ) ;
77
74
78
75
const extras = this . getExtras ( ) ;
79
76
this . navigator . navigateByUrl ( this . urlTree , extras ) ;
@@ -82,43 +79,62 @@ export class NSRouterLink implements OnChanges { // tslint:disable-line:directiv
82
79
private getExtras ( ) : NavigationExtras & NavigationOptions {
83
80
const transition = this . getTransition ( ) ;
84
81
return {
85
- queryParams : this . queryParams ,
86
- fragment : this . fragment ,
82
+ skipLocationChange : attrBoolValue ( this . skipLocationChange ) ,
83
+ replaceUrl : attrBoolValue ( this . replaceUrl ) ,
84
+
87
85
clearHistory : this . convertClearHistory ( this . clearHistory ) ,
88
86
animated : transition . animated ,
89
87
transition : transition . transition ,
90
- relativeTo : this . currentRoute ,
91
88
} ;
92
89
}
93
90
91
+ get urlTree ( ) : UrlTree {
92
+ const urlTree = this . router . createUrlTree ( this . commands , {
93
+ relativeTo : this . route ,
94
+ queryParams : this . queryParams ,
95
+ fragment : this . fragment ,
96
+ preserveQueryParams : attrBoolValue ( this . preserveQueryParams ) ,
97
+ queryParamsHandling : this . queryParamsHandling ,
98
+ preserveFragment : attrBoolValue ( this . preserveFragment ) ,
99
+ } ) ;
100
+
101
+ routerLog ( `nsRouterLink urlTree created: ${ urlTree } ` )
102
+
103
+ return urlTree ;
104
+ }
105
+
106
+
94
107
private convertClearHistory ( value : boolean | string ) : boolean {
95
108
return value === true || value === "true" ;
96
109
}
97
110
98
111
private getTransition ( ) : { animated : boolean , transition ?: NavigationTransition } {
112
+ let transition : NavigationTransition = { } ;
113
+ let animated : boolean ;
114
+
99
115
if ( typeof this . pageTransition === "boolean" ) {
100
- return { animated : < boolean > this . pageTransition } ;
116
+ animated = this . pageTransition ;
101
117
} else if ( isString ( this . pageTransition ) ) {
102
118
if ( this . pageTransition === "none" || this . pageTransition === "false" ) {
103
- return { animated : false } ;
119
+ animated = false ;
104
120
} else {
105
- return { animated : true , transition : { name : < string > this . pageTransition } } ;
121
+ animated = true ;
122
+ transition . name = < string > this . pageTransition ;
106
123
}
107
124
} else {
108
- return {
109
- animated : true ,
110
- transition : < NavigationTransition > this . pageTransition
111
- } ;
125
+ animated = true ;
126
+ transition = < NavigationTransition > this . pageTransition ;
112
127
}
113
- }
114
128
115
- ngOnChanges ( _ : { } ) : any {
116
- this . updateUrlTree ( ) ;
117
- }
129
+ let duration = + this . pageTransitionDuration ;
130
+ if ( ! isNaN ( duration ) ) {
131
+ transition . duration = duration ;
132
+ }
118
133
119
- private updateUrlTree ( ) : void {
120
- this . urlTree = this . router . createUrlTree (
121
- this . commands ,
122
- { relativeTo : this . currentRoute , queryParams : this . queryParams , fragment : this . fragment } ) ;
134
+ return { animated, transition } ;
123
135
}
124
136
}
137
+
138
+ function attrBoolValue ( s : any ) : boolean {
139
+ return s === '' || ! ! s ;
140
+ }
0 commit comments