@@ -34,85 +34,89 @@ import { isString } from "tns-core-modules/utils/types";
34
34
*/
35
35
@Directive ( { selector : "[nsRouterLink]" } )
36
36
export class NSRouterLink implements OnChanges { // tslint:disable-line:directive-class-suffix
37
- private commands : any [ ] = [ ] ;
38
- @Input ( ) target : string ;
39
- @Input ( ) queryParams : { [ k : string ] : any } ;
40
- @Input ( ) fragment : string ;
37
+ private commands : any [ ] = [ ] ;
38
+ @Input ( ) target : string ;
39
+ @Input ( ) queryParams : { [ k : string ] : any } ;
40
+ @Input ( ) fragment : string ;
41
41
42
- @Input ( ) clearHistory : boolean ;
43
- @Input ( ) pageTransition : boolean | string | NavigationTransition = true ;
42
+ @Input ( ) clearHistory : boolean ;
43
+ @Input ( ) pageTransition : boolean | string | NavigationTransition = true ;
44
44
45
- urlTree : UrlTree ;
45
+ urlTree : UrlTree ;
46
46
47
- private usePageRoute : boolean ;
47
+ private usePageRoute : boolean ;
48
48
49
- private get currentRoute ( ) : ActivatedRoute {
50
- return this . usePageRoute ? this . pageRoute . activatedRoute . getValue ( ) : this . route ;
51
- }
52
-
53
- constructor (
54
- private router : Router ,
55
- private navigator : RouterExtensions ,
56
- private route : ActivatedRoute ,
57
- @Optional ( ) private pageRoute : PageRoute ) {
58
-
59
- this . usePageRoute = ( this . pageRoute && this . route === this . pageRoute . activatedRoute . getValue ( ) ) ;
60
- }
61
-
62
- @Input ( "nsRouterLink" )
63
- set params ( data : any [ ] | string ) {
64
- if ( Array . isArray ( data ) ) {
65
- this . commands = data ;
66
- } else {
67
- this . commands = [ data ] ;
49
+ private get currentRoute ( ) : ActivatedRoute {
50
+ return this . usePageRoute ? this . pageRoute . activatedRoute . getValue ( ) : this . route ;
68
51
}
69
- }
70
-
71
-
72
- @HostListener ( "tap" )
73
- onTap ( ) {
74
- routerLog ( "nsRouterLink.tapped: " + this . commands + " usePageRoute: " +
75
- this . usePageRoute + " clearHistory: " + this . clearHistory + " transition: " +
76
- JSON . stringify ( this . pageTransition ) ) ;
77
-
78
- const transition = this . getTransition ( ) ;
79
-
80
- let extras : NavigationExtras & NavigationOptions = {
81
- relativeTo : this . currentRoute ,
82
- queryParams : this . queryParams ,
83
- fragment : this . fragment ,
84
- clearHistory : this . clearHistory ,
85
- animated : transition . animated ,
86
- transition : transition . transition
87
- } ;
88
-
89
- this . navigator . navigate ( this . commands , extras ) ;
90
- }
91
-
92
- private getTransition ( ) : { animated : boolean , transition ?: NavigationTransition } {
93
- if ( typeof this . pageTransition === "boolean" ) {
94
- return { animated : < boolean > this . pageTransition } ;
95
- } else if ( isString ( this . pageTransition ) ) {
96
- if ( this . pageTransition === "none" || this . pageTransition === "false" ) {
97
- return { animated : false } ;
98
- } else {
99
- return { animated : true , transition : { name : < string > this . pageTransition } } ;
100
- }
101
- } else {
102
- return {
103
- animated : true ,
104
- transition : this . pageTransition
105
- } ;
106
- }
107
- }
108
-
109
- ngOnChanges ( _ : { } ) : any {
110
- this . updateUrlTree ( ) ;
111
- }
112
52
113
- private updateUrlTree ( ) : void {
114
- this . urlTree = this . router . createUrlTree (
115
- this . commands ,
116
- { relativeTo : this . currentRoute , queryParams : this . queryParams , fragment : this . fragment } ) ;
117
- }
53
+ constructor (
54
+ private router : Router ,
55
+ private navigator : RouterExtensions ,
56
+ private route : ActivatedRoute ,
57
+ @Optional ( ) private pageRoute : PageRoute ) {
58
+
59
+ this . usePageRoute = ( this . pageRoute && this . route === this . pageRoute . activatedRoute . getValue ( ) ) ;
60
+ }
61
+
62
+ @Input ( "nsRouterLink" )
63
+ set params ( data : any [ ] | string ) {
64
+ if ( Array . isArray ( data ) ) {
65
+ this . commands = data ;
66
+ } else {
67
+ this . commands = [ data ] ;
68
+ }
69
+ }
70
+
71
+
72
+ @HostListener ( "tap" )
73
+ onTap ( ) {
74
+ routerLog ( "nsRouterLink.tapped: " + this . commands + " usePageRoute: " +
75
+ this . usePageRoute + " clearHistory: " + this . clearHistory + " transition: " +
76
+ JSON . stringify ( this . pageTransition ) ) ;
77
+
78
+ const extras = this . getExtras ( ) ;
79
+ this . navigator . navigate ( this . commands , extras ) ;
80
+ }
81
+
82
+ private getExtras ( ) {
83
+ const transition = this . getTransition ( ) ;
84
+ const extras : NavigationExtras & NavigationOptions = {
85
+ queryParams : this . queryParams ,
86
+ fragment : this . fragment ,
87
+ clearHistory : this . clearHistory ,
88
+ animated : transition . animated ,
89
+ transition : transition . transition ,
90
+ } ;
91
+
92
+ return ( < any > Object ) . assign ( extras ,
93
+ this . currentRoute . toString ( ) !== "Route(url:'', path:'')" && this . currentRoute ) ;
94
+ }
95
+
96
+ private getTransition ( ) : { animated : boolean , transition ?: NavigationTransition } {
97
+ if ( typeof this . pageTransition === "boolean" ) {
98
+ return { animated : < boolean > this . pageTransition } ;
99
+ } else if ( isString ( this . pageTransition ) ) {
100
+ if ( this . pageTransition === "none" || this . pageTransition === "false" ) {
101
+ return { animated : false } ;
102
+ } else {
103
+ return { animated : true , transition : { name : < string > this . pageTransition } } ;
104
+ }
105
+ } else {
106
+ return {
107
+ animated : true ,
108
+ transition : this . pageTransition
109
+ } ;
110
+ }
111
+ }
112
+
113
+ ngOnChanges ( _ : { } ) : any {
114
+ this . updateUrlTree ( ) ;
115
+ }
116
+
117
+ private updateUrlTree ( ) : void {
118
+ this . urlTree = this . router . createUrlTree (
119
+ this . commands ,
120
+ { relativeTo : this . currentRoute , queryParams : this . queryParams , fragment : this . fragment } ) ;
121
+ }
118
122
}
0 commit comments