1 | import { Origin } from 'aurelia-metadata';
|
2 | import { relativeToFile } from 'aurelia-path';
|
3 | import { NavigationInstruction, RouteConfig, RouteLoader, Router } from 'aurelia-router';
|
4 | import { CompositionEngine, customElement, inlineView, useView, CompositionContext } from 'aurelia-templating';
|
5 | import { RouterViewLocator } from './router-view';
|
6 | import { Container } from 'aurelia-dependency-injection';
|
7 |
|
8 |
|
9 | export class EmptyClass { }
|
10 | inlineView('<template></template>')(EmptyClass);
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | export class TemplatingRouteLoader extends RouteLoader {
|
16 |
|
17 |
|
18 | static inject = [CompositionEngine];
|
19 |
|
20 |
|
21 | compositionEngine: CompositionEngine;
|
22 |
|
23 | constructor(
|
24 | compositionEngine: CompositionEngine
|
25 | ) {
|
26 | super();
|
27 | this.compositionEngine = compositionEngine;
|
28 | }
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 | resolveViewModel(router: Router, config: RouteConfig): Promise<string | null | Function> {
|
36 | return new Promise((resolve, reject) => {
|
37 | let viewModel: string | null | Function;
|
38 | if ('moduleId' in config) {
|
39 | let moduleId = config.moduleId;
|
40 | if (moduleId === null) {
|
41 | viewModel = EmptyClass;
|
42 | } else {
|
43 |
|
44 |
|
45 |
|
46 | moduleId = relativeToFile(moduleId, Origin.get(router.container.viewModel.constructor).moduleId);
|
47 | if (/\.html/i.test(moduleId)) {
|
48 | viewModel = createDynamicClass(moduleId);
|
49 | } else {
|
50 | viewModel = moduleId;
|
51 | }
|
52 | }
|
53 | return resolve(viewModel);
|
54 | }
|
55 |
|
56 | reject(new Error('Invalid route config. No "moduleId" found.'));
|
57 | });
|
58 | }
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 | createChildContainer(router: Router): Container {
|
66 | const childContainer = router.container.createChild();
|
67 |
|
68 | childContainer.registerSingleton(RouterViewLocator);
|
69 | childContainer.getChildRouter = function() {
|
70 | let childRouter: Router;
|
71 |
|
72 | childContainer.registerHandler(
|
73 | Router,
|
74 | () => childRouter || (childRouter = router.createChild(childContainer))
|
75 | );
|
76 |
|
77 | return childContainer.get(Router);
|
78 | };
|
79 | return childContainer;
|
80 | }
|
81 |
|
82 | |
83 |
|
84 |
|
85 |
|
86 | loadRoute(router: Router, config: RouteConfig, navInstruction: NavigationInstruction): Promise<any> {
|
87 | return this
|
88 | .resolveViewModel(router, config)
|
89 | .then(viewModel => this.compositionEngine.ensureViewModel({
|
90 | viewModel: viewModel,
|
91 | childContainer: this.createChildContainer(router),
|
92 | view: config.view || config.viewStrategy,
|
93 | router: router
|
94 | } as CompositionContext));
|
95 | }
|
96 | }
|
97 |
|
98 |
|
99 | export function createDynamicClass(moduleId: string) {
|
100 | const name = /([^\/^\?]+)\.html/i.exec(moduleId)[1];
|
101 |
|
102 | class DynamicClass {
|
103 |
|
104 | $parent: any;
|
105 |
|
106 | bind(bindingContext: any) {
|
107 | this.$parent = bindingContext;
|
108 | }
|
109 | }
|
110 |
|
111 | customElement(name)(DynamicClass);
|
112 | useView(moduleId)(DynamicClass);
|
113 |
|
114 | return DynamicClass;
|
115 | }
|