-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
route-loading.js
115 lines (91 loc) · 3.71 KB
/
route-loading.js
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
import {activationStrategy, _buildNavigationPlan} from './navigation-plan';
export class RouteLoader {
loadRoute(router: any, config: any, navigationInstruction: any) {
throw Error('Route loaders must implement "loadRoute(router, config, navigationInstruction)".');
}
}
export class LoadRouteStep {
static inject() { return [RouteLoader]; }
constructor(routeLoader: RouteLoader) {
this.routeLoader = routeLoader;
}
run(navigationInstruction: NavigationInstruction, next: Function) {
return loadNewRoute(this.routeLoader, navigationInstruction)
.then(next)
.catch(next.cancel);
}
}
function loadNewRoute(routeLoader: RouteLoader, navigationInstruction: NavigationInstruction) {
let toLoad = determineWhatToLoad(navigationInstruction);
let loadPromises = toLoad.map((current) => loadRoute(
routeLoader,
current.navigationInstruction,
current.viewPortPlan
)
);
return Promise.all(loadPromises);
}
function determineWhatToLoad(navigationInstruction: NavigationInstruction, toLoad: Array<Object> = []) {
let plan = navigationInstruction.plan;
for (let viewPortName in plan) {
let viewPortPlan = plan[viewPortName];
if (viewPortPlan.strategy === activationStrategy.replace) {
toLoad.push({ viewPortPlan, navigationInstruction });
if (viewPortPlan.childNavigationInstruction) {
determineWhatToLoad(viewPortPlan.childNavigationInstruction, toLoad);
}
} else {
let viewPortInstruction = navigationInstruction.addViewPortInstruction(
viewPortName,
viewPortPlan.strategy,
viewPortPlan.prevModuleId,
viewPortPlan.prevComponent);
if (viewPortPlan.childNavigationInstruction) {
viewPortInstruction.childNavigationInstruction = viewPortPlan.childNavigationInstruction;
determineWhatToLoad(viewPortPlan.childNavigationInstruction, toLoad);
}
}
}
return toLoad;
}
function loadRoute(routeLoader: RouteLoader, navigationInstruction: NavigationInstruction, viewPortPlan: any) {
let moduleId = viewPortPlan.config ? viewPortPlan.config.moduleId : null;
return loadComponent(routeLoader, navigationInstruction, viewPortPlan.config).then((component) => {
let viewPortInstruction = navigationInstruction.addViewPortInstruction(
viewPortPlan.name,
viewPortPlan.strategy,
moduleId,
component);
let childRouter = component.childRouter;
if (childRouter) {
let path = navigationInstruction.getWildcardPath();
return childRouter._createNavigationInstruction(path, navigationInstruction)
.then((childInstruction) => {
viewPortPlan.childNavigationInstruction = childInstruction;
return _buildNavigationPlan(childInstruction)
.then((childPlan) => {
childInstruction.plan = childPlan;
viewPortInstruction.childNavigationInstruction = childInstruction;
return loadNewRoute(routeLoader, childInstruction);
});
});
}
return undefined;
});
}
function loadComponent(routeLoader: RouteLoader, navigationInstruction: NavigationInstruction, config: any) {
let router = navigationInstruction.router;
let lifecycleArgs = navigationInstruction.lifecycleArgs;
return routeLoader.loadRoute(router, config, navigationInstruction).then((component) => {
let {viewModel, childContainer} = component;
component.router = router;
component.config = config;
if ('configureRouter' in viewModel) {
let childRouter = childContainer.getChildRouter();
component.childRouter = childRouter;
return childRouter.configure(c => viewModel.configureRouter(c, childRouter, ...lifecycleArgs))
.then(() => component);
}
return component;
});
}