update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[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 /**
26  * This is the Skyquake App wrapper that all plugins use to be a Skyquake plugin. This 
27  * is not a react component although it does return a react component that will be the
28  * app.
29  * This function will also set the title into the <head>
30  * 
31  * @export
32  * @param {any} config 
33  * @param {any} context 
34  * @returns a react component to be rendered manually.
35  */
36 export default function(config, context) {
37     let routes = [];
38     let index = null;
39     let components = null;
40     
41     document.title = config.name || "OpenMANO";
42
43     if (config && config.routes) {
44         routes =  buildRoutes(config.routes)
45         function buildRoutes(routes) {
46             return routes.map(function(route, index) {
47                 let routeConfig = {};
48                 if (route.route && route.component) {
49                     // ES6 modules need to specify default
50                     let path = route.route;
51                     if(route.path && route.path.replace(' ', '') != '') {
52                         path = route.path
53                     }
54                     routeConfig = {
55                         path: path,
56                         name: route.label,
57                         getComponent: function(location, cb) {
58                             require.ensure([], (require) => {
59                                 cb(null, context(route.component).default)
60                             })
61                         }
62                     }
63                     if(route.routes && (route.routes.length > 0)){
64                         routeConfig.childRoutes = buildRoutes(route.routes)
65                     }
66                 } else {
67                     console.error('Route not properly configured. Check that both path and component are specified');
68                 }
69                 return routeConfig;
70             });
71         }
72         routes.push({
73             path: '/login',
74             name: 'Login',
75             component:  require('../login/login.jsx').default
76         });
77         routes.push({
78             path:'*',
79             name: 'Dashboard',
80             getComponent: function(loc, cb) {
81                 cb(null, context(config.dashboard).default);
82             }
83         });
84         if (config.dashboard) {
85             // ES6 modules need to specify default
86             index = <IndexRoute component={context(config.dashboard).default} />;
87         } else {
88             index = DefaultDashboard
89         }
90
91         const rootRoute = {
92             component: SkyquakeContainer,
93             path: '/',
94             name: 'Dashboard',
95             indexRoute: {
96                 component: (config.dashboard) ? context(config.dashboard).default : DefaultDashboard
97             },
98             childRoutes: routes
99         }
100         return((
101             <Router history={hashHistory} routes={rootRoute}>
102             </Router>
103         ))
104     } else {
105         console.error('There are no routes configured in the config.json file');
106     }
107 }
108
109 //When no default dashboard is specified in the plugin_config.json, use this component.
110 class DefaultDashboard extends React.Component {
111     constructor(props) {
112         super(props)
113     }
114     render() {
115         let html;
116         html = <div> This is a default dashboard page component </div>;
117         return html;
118     }
119 }