UNPKG

1.81 kBJavaScriptView Raw
1export class RouteCollection {
2 constructor() {
3 this.routeClasses = {};
4 }
5 register(routeClass) {
6 if (routeClass.default) routeClass = routeClass.default;
7 // todo: find annotation with type Route properly
8 var annotation = routeClass.annotations[0];
9 this.routeClasses[annotation.route] = routeClass;
10 }
11 getPageForRoute(route) {
12 for (var k in this.routeClasses) {
13 var rc = this.routeClasses[k];
14 var annotation = rc.annotations[0];
15 var match = true;
16 var params = {};
17 for (let i = 0; i < annotation.routeArray.length; i++) {
18 var ra = annotation.routeArray[i];
19 var rp = annotation.parameterNames[i];
20 var c = route[i];
21 if (!ra) {
22 // ignore initial slash
23 continue;
24 } else if (rp && c) {
25 // anything other than blank is a valid parameter value
26 params[rp] = c;
27 } else if (ra == c) {
28 // literal match
29 continue;
30 } else {
31 // route not matched
32 match = false;
33 break;
34 }
35 }
36
37 if (match) {
38 // match found
39 this.route = route;
40 var subroute = route.slice(annotation.routeArray.length);
41 // todo: get from cache if section is already open
42 var section = new rc();
43 for (let pn in params) {
44 section[pn] = params[pn];
45 }
46 var newPage = section.getPageForRoute(subroute);
47
48 return newPage;
49 }
50 }
51 }
52}