-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathpage-router-outlet.ts
450 lines (372 loc) · 15.8 KB
/
page-router-outlet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import {
Attribute, ChangeDetectorRef,
ComponentFactory, ComponentFactoryResolver, ComponentRef,
Directive, Inject, InjectionToken, Injector,
OnDestroy, EventEmitter, Output,
Type, ViewContainerRef, ElementRef
} from "@angular/core";
import {
ActivatedRoute,
ActivatedRouteSnapshot,
ChildrenOutletContexts,
PRIMARY_OUTLET,
} from "@angular/router";
import { Device } from "tns-core-modules/platform";
import { Frame } from "tns-core-modules/ui/frame";
import { Page, NavigatedData } from "tns-core-modules/ui/page";
import { profile } from "tns-core-modules/profiling";
import { BehaviorSubject } from "rxjs";
import { DEVICE, PAGE_FACTORY, PageFactory } from "../platform-providers";
import { routerLog as log, routerError as error, isLogEnabled } from "../trace";
import { DetachedLoader } from "../common/detached-loader";
import { ViewUtil } from "../view-util";
import { NSLocationStrategy, Outlet } from "./ns-location-strategy";
import { NSRouteReuseStrategy } from "./ns-route-reuse-strategy";
export class PageRoute {
activatedRoute: BehaviorSubject<ActivatedRoute>;
constructor(startRoute: ActivatedRoute) {
this.activatedRoute = new BehaviorSubject(startRoute);
}
}
// Used to "mark" ActivatedRoute snapshots that are handled in PageRouterOutlet
export const pageRouterActivatedSymbol = Symbol("page-router-activated");
export const loaderRefSymbol = Symbol("loader-ref");
export function destroyComponentRef(componentRef: ComponentRef<any>) {
if (componentRef) {
const loaderRef = componentRef[loaderRefSymbol];
if (loaderRef) {
loaderRef.destroy();
}
componentRef.destroy();
}
}
class ChildInjector implements Injector {
constructor(
private providers: ProviderMap,
private parent: Injector
) { }
get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T): T {
let localValue = this.providers.get(token);
if (localValue) {
return localValue;
}
return this.parent.get(token, notFoundValue);
}
}
type ProviderMap = Map<Type<any> | InjectionToken<any>, any>;
/**
* There are cases where multiple activatedRoute nodes should be associated/handled by the same PageRouterOutlet.
* We can gat additional ActivatedRoutes nodes when there is:
* - Lazy loading - there is an additional ActivatedRoute node for the RouteConfig with the `loadChildren` setup
* - Componentless routes - there is an additional ActivatedRoute node for the componentless RouteConfig
*
* Example:
* R <-- root
* |
* feature (lazy module) <-- RouteConfig: { path: "lazy", loadChildren: "./feature/feature.module#FeatureModule" }
* |
* module (componentless route) <-- RouteConfig: { path: "module", children: [...] } // Note: No 'component'
* |
* home <-- RouteConfig: { path: "module", component: MyComponent } - this is what we get as activatedRoute param
*
* In these cases we will mark the top-most node (feature). NSRouteReuseStrategy will detach the tree there and
* use this ActivateRoute as a kay for caching.
*/
export function findTopActivatedRouteNodeForOutlet(activatedRoute: ActivatedRouteSnapshot): ActivatedRouteSnapshot {
let outletActivatedRoute = activatedRoute;
while (outletActivatedRoute.parent &&
outletActivatedRoute.parent.routeConfig &&
!outletActivatedRoute.parent.routeConfig.component) {
outletActivatedRoute = outletActivatedRoute.parent;
}
return outletActivatedRoute;
}
function routeToString(activatedRoute: ActivatedRoute | ActivatedRouteSnapshot): string {
return activatedRoute.pathFromRoot.join("->");
}
@Directive({ selector: "page-router-outlet" }) // tslint:disable-line:directive-selector
export class PageRouterOutlet implements OnDestroy { // tslint:disable-line:directive-class-suffix
private activated: ComponentRef<any> | null = null;
private _activatedRoute: ActivatedRoute | null = null;
private detachedLoaderFactory: ComponentFactory<DetachedLoader>;
private outlet: Outlet;
private name: string;
private isEmptyOutlet: boolean;
private viewUtil: ViewUtil;
private frame: Frame;
@Output("activate") activateEvents = new EventEmitter<any>(); // tslint:disable-line:no-output-rename
@Output("deactivate") deactivateEvents = new EventEmitter<any>(); // tslint:disable-line:no-output-rename
/** @deprecated from Angular since v4 */
get locationInjector(): Injector { return this.location.injector; }
/** @deprecated from Angular since v4 */
get locationFactoryResolver(): ComponentFactoryResolver { return this.resolver; }
get isActivated(): boolean {
return !!this.activated;
}
get component(): Object {
if (!this.activated) {
if (isLogEnabled()) {
log("Outlet is not activated");
}
return;
}
return this.activated.instance;
}
get activatedRoute(): ActivatedRoute {
if (!this.activated) {
if (isLogEnabled()) {
log("Outlet is not activated");
}
return;
}
return this._activatedRoute;
}
constructor(
private parentContexts: ChildrenOutletContexts,
private location: ViewContainerRef,
@Attribute("name") name: string,
@Attribute("actionBarVisibility") actionBarVisibility: string,
@Attribute("isEmptyOutlet") isEmptyOutlet: boolean,
private locationStrategy: NSLocationStrategy,
private componentFactoryResolver: ComponentFactoryResolver,
private resolver: ComponentFactoryResolver,
private changeDetector: ChangeDetectorRef,
@Inject(DEVICE) device: Device,
@Inject(PAGE_FACTORY) private pageFactory: PageFactory,
private routeReuseStrategy: NSRouteReuseStrategy,
elRef: ElementRef
) {
this.isEmptyOutlet = isEmptyOutlet;
this.frame = elRef.nativeElement;
this.setActionBarVisibility(actionBarVisibility);
if (isLogEnabled()) {
log(`PageRouterOutlet.constructor frame: ${this.frame}`);
}
this.name = name || PRIMARY_OUTLET;
parentContexts.onChildOutletCreated(this.name, <any>this);
this.viewUtil = new ViewUtil(device);
this.detachedLoaderFactory = resolver.resolveComponentFactory(DetachedLoader);
}
setActionBarVisibility(actionBarVisibility: string): void {
switch (actionBarVisibility) {
case "always":
case "never":
this.frame.actionBarVisibility = actionBarVisibility;
return;
default:
this.frame.actionBarVisibility = "auto";
}
}
ngOnDestroy(): void {
// Clear accumulated modal view page cache when page-router-outlet
// destroyed on modal view closing
this.parentContexts.onChildOutletDestroyed(this.name);
if (this.outlet) {
this.outlet.outletKeys.forEach(key => {
this.routeReuseStrategy.clearModalCache(key);
});
this.locationStrategy.clearOutlet(this.frame);
} else {
log("NSLocationStrategy.ngOnDestroy: no outlet available for page-router-outlet");
}
}
deactivate(): void {
if (!this.outlet || !this.outlet.isPageNavigationBack) {
if (isLogEnabled()) {
log("Currently not in page back navigation - component should be detached instead of deactivated.");
}
return;
}
if (isLogEnabled()) {
log("PageRouterOutlet.deactivate() while going back - should destroy");
}
if (!this.isActivated) {
return;
}
const c = this.activated.instance;
destroyComponentRef(this.activated);
this.activated = null;
this._activatedRoute = null;
this.deactivateEvents.emit(c);
}
/**
* Called when the `RouteReuseStrategy` instructs to detach the subtree
*/
detach(): ComponentRef<any> {
if (!this.isActivated) {
if (isLogEnabled()) {
log("Outlet is not activated");
}
return;
}
if (isLogEnabled()) {
log(`PageRouterOutlet.detach() - ${routeToString(this._activatedRoute)}`);
}
// Detach from ChangeDetection
this.activated.hostView.detach();
const component = this.activated;
this.activated = null;
this._activatedRoute = null;
return component;
}
/**
* Called when the `RouteReuseStrategy` instructs to re-attach a previously detached subtree
*/
attach(ref: ComponentRef<any>, activatedRoute: ActivatedRoute) {
if (isLogEnabled()) {
log(`PageRouterOutlet.attach() - ${routeToString(activatedRoute)}`);
}
this.activated = ref;
// reattach to ChangeDetection
this.activated.hostView.reattach();
this._activatedRoute = activatedRoute;
this.markActivatedRoute(activatedRoute);
this.locationStrategy._finishBackPageNavigation(this.frame);
}
/**
* Called by the Router to instantiate a new component during the commit phase of a navigation.
* This method in turn is responsible for calling the `routerOnActivate` hook of its child.
*/
@profile
activateWith(
activatedRoute: ActivatedRoute,
resolver: ComponentFactoryResolver | null): void {
this.outlet = this.outlet || this.getOutlet(activatedRoute.snapshot);
if (!this.outlet) {
if (isLogEnabled()) {
error("No outlet found relative to activated route");
}
return;
}
this.outlet.isNSEmptyOutlet = this.isEmptyOutlet;
this.locationStrategy.updateOutletFrame(this.outlet, this.frame);
if (this.outlet && this.outlet.isPageNavigationBack) {
if (isLogEnabled()) {
log("Currently in page back navigation - component should be reattached instead of activated.");
}
this.locationStrategy._finishBackPageNavigation(this.frame);
}
if (isLogEnabled()) {
log(`PageRouterOutlet.activateWith() - ${routeToString(activatedRoute)}`);
}
this._activatedRoute = activatedRoute;
this.markActivatedRoute(activatedRoute);
resolver = resolver || this.resolver;
this.activateOnGoForward(activatedRoute, resolver);
this.activateEvents.emit(this.activated.instance);
}
private activateOnGoForward(
activatedRoute: ActivatedRoute,
loadedResolver: ComponentFactoryResolver
): void {
if (isLogEnabled()) {
log("PageRouterOutlet.activate() forward navigation - " +
"create detached loader in the loader container");
}
const factory = this.getComponentFactory(activatedRoute, loadedResolver);
const page = this.pageFactory({
isNavigation: true,
componentType: factory.componentType,
});
const providers = new Map();
providers.set(Page, page);
providers.set(Frame, this.frame);
providers.set(PageRoute, new PageRoute(activatedRoute));
providers.set(ActivatedRoute, activatedRoute);
providers.set(ChildrenOutletContexts, this.parentContexts.getOrCreateContext(this.name).children);
const childInjector = new ChildInjector(providers, this.location.injector);
const loaderRef = this.location.createComponent(
this.detachedLoaderFactory, this.location.length, childInjector, []);
this.changeDetector.markForCheck();
this.activated = loaderRef.instance.loadWithFactory(factory);
this.loadComponentInPage(page, this.activated);
this.activated[loaderRefSymbol] = loaderRef;
}
@profile
private loadComponentInPage(page: Page, componentRef: ComponentRef<any>): void {
// Component loaded. Find its root native view.
const componentView = componentRef.location.nativeElement;
// Remove it from original native parent.
this.viewUtil.removeChild(componentView.parent, componentView);
// Add it to the new page
page.content = componentView;
page.on(Page.navigatedFromEvent, (<any>global).Zone.current.wrap((args: NavigatedData) => {
if (args.isBackNavigation) {
this.locationStrategy._beginBackPageNavigation(this.frame);
this.locationStrategy.back();
}
}));
const navOptions = this.locationStrategy._beginPageNavigation(this.frame);
// Clear refCache if navigation with clearHistory
if (navOptions.clearHistory) {
const clearCallback = () => setTimeout(() => {
if (this.outlet) {
this.routeReuseStrategy.clearCache(this.outlet.outletKeys[0]);
}
page.off(Page.navigatedToEvent, clearCallback);
});
page.on(Page.navigatedToEvent, clearCallback);
}
this.frame.navigate({
create: () => { return page; },
clearHistory: navOptions.clearHistory,
animated: navOptions.animated,
transition: navOptions.transition
});
}
// Find and mark the top activated route as an activated one.
// In ns-location-strategy we are reusing components only if their corresponing routes
// are marked as activated from this method.
private markActivatedRoute(activatedRoute: ActivatedRoute) {
const queue = [];
queue.push(activatedRoute.snapshot);
let currentRoute = queue.shift();
while (currentRoute) {
currentRoute.children.forEach(childRoute => {
queue.push(childRoute);
});
const nodeToMark = findTopActivatedRouteNodeForOutlet(currentRoute);
let outletKeyForRoute = this.locationStrategy.getRouteFullPath(nodeToMark);
let outlet = this.locationStrategy.findOutletByKey(outletKeyForRoute);
if (outlet && outlet.frames.length) {
nodeToMark[pageRouterActivatedSymbol] = true;
if (isLogEnabled()) {
log("Activated route marked as page: " + routeToString(nodeToMark));
}
}
currentRoute = queue.shift();
}
}
private getComponentFactory(
activatedRoute: ActivatedRoute,
loadedResolver: ComponentFactoryResolver
): ComponentFactory<any> {
const { component } = activatedRoute.routeConfig;
return loadedResolver ?
loadedResolver.resolveComponentFactory(component) :
this.componentFactoryResolver.resolveComponentFactory(component);
}
private getOutlet(activatedRouteSnapshot: ActivatedRouteSnapshot): Outlet {
const topActivatedRoute = findTopActivatedRouteNodeForOutlet(activatedRouteSnapshot);
const modalNavigation = this.locationStrategy._modalNavigationDepth;
const outletKey = this.locationStrategy.getRouteFullPath(topActivatedRoute);
let outlet;
if (modalNavigation > 0) { // Modal with 'primary' p-r-o
outlet = this.locationStrategy.findOutletByModal(modalNavigation);
} else {
outlet = this.locationStrategy.findOutletByKey(outletKey);
}
// Named lazy loaded outlet.
if (!outlet && this.isEmptyOutlet) {
const parentOutletKey = this.locationStrategy.getRouteFullPath(topActivatedRoute.parent);
outlet = this.locationStrategy.findOutletByKey(parentOutletKey);
if (outlet) {
outlet.outletKeys.push(outletKey);
}
} else if (!outlet) {
const pathByOutlets = this.locationStrategy.getPathByOutlets(topActivatedRoute);
outlet = this.locationStrategy.findOutletByOutletPath(pathByOutlets);
}
return outlet;
}
}