/
HasAuthorization.jsx
91 lines (78 loc) · 2.58 KB
/
HasAuthorization.jsx
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
import React from "react";
import ShallowComponent from "robe-react-commons/lib/components/ShallowComponent";
import Router from "react-router/lib/Router";
import BrowserHistory from "react-router/lib/browserHistory";
import Workspace from "app/workspace/Workspace";
import loader from "../loader";
export default class HasAuthorization extends ShallowComponent {
static ROUTES = [];
static ROUTER;
constructor(props:Object) {
super(props);
HasAuthorization.ROUTER = HasAuthorization.createRoutes(this.props.menu);
}
render():Object {
return (<Router key="root"
history={BrowserHistory}
onUpdate={HasAuthorization.scrollTop}
routes={HasAuthorization.ROUTER}/>);
}
static createRoutes(menu:Object):Object {
const INDEX_ROUTE = {
getComponent(location:string, cb:Function) {
loader("./app/welcome/Welcome", cb);
}
};
const NOT_FOUND_ROUTE = {
path: "*",
getComponent(location:string, cb:Function) {
loader("./app/common/NotFound", cb);
}
};
const items = menu[0].items;
HasAuthorization.importMenu(items);
HasAuthorization.ROUTES.push(NOT_FOUND_ROUTE);
return ({
menu: menu,
path: window.applicationRootPath,
component: Workspace,
indexRoute: INDEX_ROUTE,
childRoutes: HasAuthorization.ROUTES
});
}
static importMenu(items:Array) {
for (let i = 0; i < items.length; i++) {
let item = items[i];
if (item.items && item.items.length > 0) {
HasAuthorization.importMenu(item.items);
} else {
const path = HasAuthorization.normalizePath(item.path);
let obj = {
path: item.module,
getComponent(location:string, cb:Function) {
loader(path, cb);
}
};
HasAuthorization.ROUTES.push(obj);
}
}
}
static importComponent(component:Object):Object {
if (component.default) {
return component.default;
}
return component;
}
/**
* this function changing absolute path to relative path
*/
static normalizePath(path:string):string {
if (path) {
return path.replace("app", "./app");
}
return path;
}
static scrollTop() {
window.scrollTo(0, 0);
}
}