3 * Copyright 2016 RIFT.IO Inc
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import React from 'react';
20 import { Link } from 'react-router';
21 import Utils from 'utils/utils.js';
22 import Crouton from 'react-crouton';
23 import 'style/common.scss';
25 import './skyquakeNav.scss';
26 import SelectOption from '../form_controls/selectOption.jsx';
27 import {FormSection} from '../form_controls/formControls.jsx';
29 //Temporary, until api server is on same port as webserver
30 var rw = require('utils/rw.js');
31 var API_SERVER = rw.getSearchParams(window.location).api_server;
32 var UPLOAD_SERVER = rw.getSearchParams(window.location).upload_server;
35 // Internal classes/functions
38 class LogoutAppMenuItem extends React.Component {
40 Utils.clearAuthentication();
46 <a onClick={this.handleLogout}>
55 class SelectProject extends React.Component {
60 let value = JSON.parse(e.currentTarget.value);
61 console.log('selected project', value)
64 let props = this.props;
65 let currentValue = JSON.stringify(props.currentProject);
66 let projects = this.props.projects && this.props.projects.map((p,i) => {
72 let hasProjects = (this.props.projects && (this.props.projects.length > 0))
74 <div className="userSection app">
76 hasProjects ? 'Project:' : 'No Projects Assigned'
83 defaultValue={currentValue}
84 onChange={props.onSelectProject}
85 className="projectSelect" />
93 class UserNav extends React.Component {
98 Utils.clearAuthentication();
101 let value = JSON.parse(e.currentTarget.value)
102 console.log('selected project', value)
105 let props = this.props;
106 let userProfileLink = '';
107 this.props.nav['user_management'] && this.props.nav['user_management'].routes.map((r) => {
109 userProfileLink = returnLinkItem(r, props.currentUser)
113 <div className="app">
116 <span className="oi" data-glyph="caret-bottom"></span>
118 <ul className="menu">
120 <a onClick={this.handleLogout}>
130 UserNav.defaultProps = {
137 // Exported classes and functions
142 * Skyquake Nav Component. Provides navigation functionality between all plugins
144 export default class skyquakeNav extends React.Component {
148 this.state.validateErrorEvent = 0;
149 this.state.validateErrorMsg = '';
151 componentDidMount() {
152 this.props.store.openProjectSocket();
153 this.props.store.getUserProfile();
155 validateError = (msg) => {
157 validateErrorEvent: true,
158 validateErrorMsg: msg
161 validateReset = () => {
163 validateErrorEvent: false
166 returnCrouton = () => {
169 message={this.state.validateErrorMsg}
171 hidden={!(this.state.validateErrorEvent && this.state.validateErrorMsg)}
172 onDismiss={this.validateReset}
179 {this.returnCrouton()}
180 <nav className="skyquakeNav">
181 {buildNav.call(this, this.props.nav, this.props.currentPlugin, this.props)}
189 skyquakeNav.defaultProps = {
193 * Returns a React Component
194 * @param {object} link Information about the nav link
195 * @param {string} link.route Hash route that the SPA should resolve
196 * @param {string} link.name Link name to be displayed
197 * @param {number} index index of current array item
198 * @return {object} component A React LI Component
200 //This should be extended to also make use of internal/external links and determine if the link should refer to an outside plugin or itself.
201 export function buildNavListItem (k, link, index) {
203 if (link.type == 'external') {
204 this.hasSubNav[k] = true;
207 {returnLinkItem(link)}
215 * Builds a link to a React Router route or a new plugin route.
216 * @param {object} link Routing information from nav object.
217 * @return {object} component returns a react component that links to a new route.
219 export function returnLinkItem(link, label) {
221 let route = link.route;
222 if(link.isExternal) {
224 <a href={route}>{label || link.label}</a>
227 if(link.path && link.path.replace(' ', '') != '') {
232 query[link.query] = '';
240 {label || link.label}
251 * Constructs nav for each plugin, along with available subnavs
252 * @param {array} nav List returned from /nav endpoint.
253 * @return {array} List of constructed nav element for each plugin
255 export function buildNav(nav, currentPlugin, props) {
257 let navListHTML = [];
258 let secondaryNav = [];
261 let secondaryNavHTML = (
262 <div className="secondaryNav" key="secondaryNav">
265 onSelectProject={props.store.selectActiveProject}
266 projects={props.projects}
267 currentProject={props.currentProject} />
269 currentUser={props.currentUser}
274 if (nav.hasOwnProperty(k)) {
275 self.hasSubNav[k] = false;
277 let navClass = "app";
278 let routes = nav[k].routes;
280 //Primary plugin title and link to dashboard.
284 route = routes[0].isExternal ? '/' + k + '/index.html?api_server=' + API_SERVER + '' + '&upload_server=' + UPLOAD_SERVER + '' : '';
286 route = routes[0].isExternal ? '/' + k + '/' : '';
288 let dashboardLink = returnLinkItem({
289 isExternal: routes[0].isExternal,
290 pluginName: nav[k].pluginName,
291 label: nav[k].label || k,
294 if (nav[k].pluginName == currentPlugin) {
295 navClass += " active";
297 NavList = nav[k].routes.map(buildNavListItem.bind(self, k));
298 navItem.priority = nav[k].priority;
299 navItem.order = nav[k].order;
301 <div key={k} className={navClass}>
302 <h2>{dashboardLink} {self.hasSubNav[k] ? <span className="oi" data-glyph="caret-bottom"></span> : ''}</h2>
303 <ul className="menu">
310 navList.push(navItem)
313 //Sorts nav items by order and returns only the markup
314 navListHTML = navList.sort((a,b) => a.order - b.order).map(function(n) {
315 if((n.priority < 2)){
318 secondaryNav.push(n.html);
321 navListHTML.push(secondaryNavHTML);