Skip to content

Commit ea7c2af

Browse files
author
vakrilov
committed
feat(router): Implemented pageTransitionDuration for NSRouterLInks + improved relative url handling
1 parent a244072 commit ea7c2af

File tree

1 file changed

+52
-36
lines changed

1 file changed

+52
-36
lines changed
Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { Directive, HostListener, Input, Optional, OnChanges } from "@angular/core";
22
import { NavigationExtras } from "@angular/router";
3-
import { ActivatedRoute, Router, UrlTree } from "@angular/router";
3+
import { ActivatedRoute, Router, UrlTree, } from "@angular/router";
44
import { routerLog } from "../trace";
55
import { PageRoute } from "./page-router-outlet";
66
import { RouterExtensions } from "./router-extensions";
77
import { NavigationOptions } from "./ns-location-strategy";
88
import { NavigationTransition } from "tns-core-modules/ui/frame";
99
import { isString } from "tns-core-modules/utils/types";
1010

11+
// Copied from "@angular/router/src/config"
12+
export type QueryParamsHandling = 'merge' | 'preserve' | '';
13+
1114
/**
1215
* The nsRouterLink directive lets you link to specific parts of your app.
1316
*
@@ -33,30 +36,27 @@ import { isString } from "tns-core-modules/utils/types";
3336
* And if the segment begins with `../`, the router will go up one level.
3437
*/
3538
@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
3840
@Input() target: string;
3941
@Input() queryParams: { [k: string]: any };
4042
@Input() fragment: string;
4143

44+
@Input() queryParamsHandling: QueryParamsHandling;
45+
@Input() preserveQueryParams: boolean;
46+
@Input() preserveFragment: boolean;
47+
@Input() skipLocationChange: boolean;
48+
@Input() replaceUrl: boolean;
49+
4250
@Input() clearHistory: boolean;
4351
@Input() pageTransition: boolean | string | NavigationTransition = true;
52+
@Input() pageTransitionDuration;
4453

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[] = [];
5255

5356
constructor(
5457
private router: Router,
5558
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) {
6060
}
6161

6262
@Input("nsRouterLink")
@@ -68,12 +68,9 @@ export class NSRouterLink implements OnChanges { // tslint:disable-line:directiv
6868
}
6969
}
7070

71-
7271
@HostListener("tap")
7372
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}`);
7774

7875
const extras = this.getExtras();
7976
this.navigator.navigateByUrl(this.urlTree, extras);
@@ -82,43 +79,62 @@ export class NSRouterLink implements OnChanges { // tslint:disable-line:directiv
8279
private getExtras(): NavigationExtras & NavigationOptions {
8380
const transition = this.getTransition();
8481
return {
85-
queryParams: this.queryParams,
86-
fragment: this.fragment,
82+
skipLocationChange: attrBoolValue(this.skipLocationChange),
83+
replaceUrl: attrBoolValue(this.replaceUrl),
84+
8785
clearHistory: this.convertClearHistory(this.clearHistory),
8886
animated: transition.animated,
8987
transition: transition.transition,
90-
relativeTo: this.currentRoute,
9188
};
9289
}
9390

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+
94107
private convertClearHistory(value: boolean | string): boolean {
95108
return value === true || value === "true";
96109
}
97110

98111
private getTransition(): { animated: boolean, transition?: NavigationTransition } {
112+
let transition: NavigationTransition = {};
113+
let animated: boolean;
114+
99115
if (typeof this.pageTransition === "boolean") {
100-
return { animated: <boolean>this.pageTransition };
116+
animated = this.pageTransition;
101117
} else if (isString(this.pageTransition)) {
102118
if (this.pageTransition === "none" || this.pageTransition === "false") {
103-
return { animated: false };
119+
animated = false;
104120
} else {
105-
return { animated: true, transition: { name: <string>this.pageTransition } };
121+
animated = true;
122+
transition.name = <string>this.pageTransition;
106123
}
107124
} else {
108-
return {
109-
animated: true,
110-
transition: <NavigationTransition>this.pageTransition
111-
};
125+
animated = true;
126+
transition = <NavigationTransition>this.pageTransition;
112127
}
113-
}
114128

115-
ngOnChanges(_: {}): any {
116-
this.updateUrlTree();
117-
}
129+
let duration = +this.pageTransitionDuration;
130+
if(!isNaN(duration)) {
131+
transition.duration = duration;
132+
}
118133

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 };
123135
}
124136
}
137+
138+
function attrBoolValue(s: any): boolean {
139+
return s === '' || !!s;
140+
}

0 commit comments

Comments
 (0)