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.map((p,i) => {
73 <div className="userSection app">
78 defaultValue={currentValue}
79 onChange={props.onSelectProject}
80 className="projectSelect" />
86 class UserNav extends React.Component {
91 Utils.clearAuthentication();
94 let value = JSON.parse(e.currentTarget.value)
95 console.log('selected project', value)
98 let props = this.props;
100 <div className="app">
105 <span className="oi" data-glyph="caret-bottom"></span>
107 <ul className="menu">
109 <a onClick={this.handleLogout}>
119 UserNav.defaultProps = {
126 // Exported classes and functions
131 * Skyquake Nav Component. Provides navigation functionality between all plugins
133 export default class skyquakeNav extends React.Component {
137 this.state.validateErrorEvent = 0;
138 this.state.validateErrorMsg = '';
140 componentDidMount() {
141 this.props.store.openProjectSocket();
142 this.props.store.getUserProfile();
144 validateError = (msg) => {
146 validateErrorEvent: true,
147 validateErrorMsg: msg
150 validateReset = () => {
152 validateErrorEvent: false
155 returnCrouton = () => {
158 message={this.state.validateErrorMsg}
160 hidden={!(this.state.validateErrorEvent && this.state.validateErrorMsg)}
161 onDismiss={this.validateReset}
168 {this.returnCrouton()}
169 <nav className="skyquakeNav">
170 {buildNav.call(this, this.props.nav, this.props.currentPlugin, this.props)}
178 skyquakeNav.defaultProps = {
182 * Returns a React Component
183 * @param {object} link Information about the nav link
184 * @param {string} link.route Hash route that the SPA should resolve
185 * @param {string} link.name Link name to be displayed
186 * @param {number} index index of current array item
187 * @return {object} component A React LI Component
189 //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.
190 export function buildNavListItem (k, link, index) {
192 if (link.type == 'external') {
193 this.hasSubNav[k] = true;
196 {returnLinkItem(link)}
204 * Builds a link to a React Router route or a new plugin route.
205 * @param {object} link Routing information from nav object.
206 * @return {object} component returns a react component that links to a new route.
208 export function returnLinkItem(link) {
210 let route = link.route;
211 if(link.isExternal) {
213 <a href={route}>{link.label}</a>
216 if(link.path && link.path.replace(' ', '') != '') {
221 query[link.query] = '';
240 * Constructs nav for each plugin, along with available subnavs
241 * @param {array} nav List returned from /nav endpoint.
242 * @return {array} List of constructed nav element for each plugin
244 export function buildNav(nav, currentPlugin, props) {
246 let navListHTML = [];
247 let secondaryNav = [];
250 let secondaryNavHTML = (
251 <div className="secondaryNav" key="secondaryNav">
254 onSelectProject={props.store.selectActiveProject}
255 projects={props.projects}
256 currentProject={props.currentProject} />
258 currentUser={props.currentUser} />
262 if (nav.hasOwnProperty(k)) {
263 self.hasSubNav[k] = false;
265 let navClass = "app";
266 let routes = nav[k].routes;
268 //Primary plugin title and link to dashboard.
272 route = routes[0].isExternal ? '/' + k + '/index.html?api_server=' + API_SERVER + '' + '&upload_server=' + UPLOAD_SERVER + '' : '';
274 route = routes[0].isExternal ? '/' + k + '/' : '';
276 let dashboardLink = returnLinkItem({
277 isExternal: routes[0].isExternal,
278 pluginName: nav[k].pluginName,
279 label: nav[k].label || k,
282 if (nav[k].pluginName == currentPlugin) {
283 navClass += " active";
285 NavList = nav[k].routes.map(buildNavListItem.bind(self, k));
286 navItem.priority = nav[k].priority;
287 navItem.order = nav[k].order;
289 <div key={k} className={navClass}>
290 <h2>{dashboardLink} {self.hasSubNav[k] ? <span className="oi" data-glyph="caret-bottom"></span> : ''}</h2>
291 <ul className="menu">
298 navList.push(navItem)
301 //Sorts nav items by order and returns only the markup
302 navListHTML = navList.sort((a,b) => a.order - b.order).map(function(n) {
303 if((n.priority < 2)){
306 secondaryNav.push(n.html);
309 navListHTML.push(secondaryNavHTML);