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}>
56 class UserNav extends React.Component {
61 let value = JSON.parse(e.currentTarget.value)
62 console.log('selected project', value)
65 let projects = this.props.projects.map((p,i) => {
72 <div className="userSection">
74 <SelectOption options={projects} onChange={this.selectProject} className="projectSelect"/>
80 UserNav.defaultProps = {
87 // Exported classes and functions
92 * Skyquake Nav Component. Provides navigation functionality between all plugins
94 export default class skyquakeNav extends React.Component {
98 this.state.validateErrorEvent = 0;
99 this.state.validateErrorMsg = '';
101 componentDidMount() {
102 this.props.store.openProjectSocket();
103 this.props.store.getUserProfile();
105 validateError = (msg) => {
107 validateErrorEvent: true,
108 validateErrorMsg: msg
111 validateReset = () => {
113 validateErrorEvent: false
116 returnCrouton = () => {
119 message={this.state.validateErrorMsg}
121 hidden={!(this.state.validateErrorEvent && this.state.validateErrorMsg)}
122 onDismiss={this.validateReset}
129 {this.returnCrouton()}
130 <nav className="skyquakeNav">
131 {buildNav.call(this, this.props.nav, this.props.currentPlugin, this.props)}
139 skyquakeNav.defaultProps = {
143 * Returns a React Component
144 * @param {object} link Information about the nav link
145 * @param {string} link.route Hash route that the SPA should resolve
146 * @param {string} link.name Link name to be displayed
147 * @param {number} index index of current array item
148 * @return {object} component A React LI Component
150 //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.
151 export function buildNavListItem (k, link, index) {
153 if (link.type == 'external') {
154 this.hasSubNav[k] = true;
157 {returnLinkItem(link)}
165 * Builds a link to a React Router route or a new plugin route.
166 * @param {object} link Routing information from nav object.
167 * @return {object} component returns a react component that links to a new route.
169 export function returnLinkItem(link) {
171 let route = link.route;
172 if(link.isExternal) {
174 <a href={route}>{link.label}</a>
177 if(link.path && link.path.replace(' ', '') != '') {
182 query[link.query] = '';
201 * Constructs nav for each plugin, along with available subnavs
202 * @param {array} nav List returned from /nav endpoint.
203 * @return {array} List of constructed nav element for each plugin
205 export function buildNav(nav, currentPlugin, props) {
207 let navListHTML = [];
208 let secondaryNav = [];
211 let secondaryNavHTML = (
212 <div className="secondaryNav" key="secondaryNav">
214 <UserNav projects={props.projects}/>
215 <LogoutAppMenuItem />
219 if (nav.hasOwnProperty(k)) {
220 self.hasSubNav[k] = false;
222 let navClass = "app";
223 let routes = nav[k].routes;
225 //Primary plugin title and link to dashboard.
229 route = routes[0].isExternal ? '/' + k + '/index.html?api_server=' + API_SERVER + '' + '&upload_server=' + UPLOAD_SERVER + '' : '';
231 route = routes[0].isExternal ? '/' + k + '/' : '';
233 let dashboardLink = returnLinkItem({
234 isExternal: routes[0].isExternal,
235 pluginName: nav[k].pluginName,
236 label: nav[k].label || k,
239 if (nav[k].pluginName == currentPlugin) {
240 navClass += " active";
242 NavList = nav[k].routes.map(buildNavListItem.bind(self, k));
243 navItem.priority = nav[k].priority;
244 navItem.order = nav[k].order;
246 <div key={k} className={navClass}>
247 <h2>{dashboardLink} {self.hasSubNav[k] ? <span className="oi" data-glyph="caret-bottom"></span> : ''}</h2>
248 <ul className="menu">
255 navList.push(navItem)
258 //Sorts nav items by order and returns only the markup
259 navListHTML = navList.sort((a,b) => a.order - b.order).map(function(n) {
260 if((n.priority < 2)){
263 secondaryNav.push(n.html);
266 navListHTML.push(secondaryNavHTML);