Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / framework / widgets / skyquake_container / skyquakeRouter.jsx
1 /*
2  * 
3  *   Copyright 2016 RIFT.IO Inc
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  *
17  */
18 import React from 'react';
19 import {
20     Router, Route, hashHistory, IndexRoute
21 }
22 from 'react-router';
23 import SkyquakeContainer from 'widgets/skyquake_container/skyquakeContainer.jsx';
24
25 export default function(config, context) {
26     let routes = [];
27     let index = null;
28     let components = null;
29     if (config && config.routes) {
30         routes =  buildRoutes(config.routes)
31         function buildRoutes(routes) {
32             return routes.map(function(route, index) {
33                 let routeConfig = {};
34                 if (route.route && route.component) {
35                     // ES6 modules need to specify default
36                     let path = route.route;
37                     if(route.path && route.path.replace(' ', '') != '') {
38                         path = route.path
39                     }
40                     routeConfig = {
41                         path: path,
42                         name: route.label,
43                         getComponent: function(location, cb) {
44                             require.ensure([], (require) => {
45                                 cb(null, context(route.component).default)
46                             })
47                         }
48                     }
49                     if(route.routes && (route.routes.length > 0)){
50                         routeConfig.childRoutes = buildRoutes(route.routes)
51                     }
52                 } else {
53                     console.error('Route not properly configured. Check that both path and component are specified');
54                 }
55                 return routeConfig;
56             });
57         }
58         routes.push({
59             path: '/login',
60             name: 'Login',
61             component:  require('../login/login.jsx').default
62         });
63         routes.push({
64             path:'*',
65             name: 'Dashboard',
66             getComponent: function(loc, cb) {
67                 cb(null, context(config.dashboard).default);
68             }
69         });
70         if (config.dashboard) {
71             // ES6 modules need to specify default
72             index = <IndexRoute component={context(config.dashboard).default} />;
73         } else {
74             index = DefaultDashboard
75         }
76
77         const rootRoute = {
78             component: SkyquakeContainer,
79             path: '/',
80             name: 'Dashboard',
81             indexRoute: {
82                 component: (config.dashboard) ? context(config.dashboard).default : DefaultDashboard
83             },
84             childRoutes: routes
85         }
86
87         return((
88             <Router history={hashHistory} routes={rootRoute}>
89             </Router>
90         ))
91     } else {
92         console.error('There are no routes configured in the config.json file');
93     }
94 }
95
96 //When no default dashboard is specified in the plugin_config.json, use this component.
97 class DefaultDashboard extends React.Component {
98     constructor(props) {
99         super(props)
100     }
101     render() {
102         let html;
103         html = <div> This is a default dashboard page component </div>;
104         return html;
105     }
106 }