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 //Temporary, until api server is on same port as webserver
26 var rw = require('utils/rw.js');
27 var API_SERVER = rw.getSearchParams(window.location).api_server;
28 var UPLOAD_SERVER = rw.getSearchParams(window.location).upload_server;
31 // Internal classes/functions
34 class LogoutAppMenuItem extends React.Component {
36 Utils.clearAuthentication();
42 <a onClick={this.handleLogout}>
53 // Exported classes and functions
58 * Skyquake Nav Component. Provides navigation functionality between all plugins
60 export default class skyquakeNav extends React.Component {
64 this.state.validateErrorEvent = 0;
65 this.state.validateErrorMsg = '';
67 validateError = (msg) => {
69 validateErrorEvent: true,
73 validateReset = () => {
75 validateErrorEvent: false
78 returnCrouton = () => {
81 message={this.state.validateErrorMsg}
83 hidden={!(this.state.validateErrorEvent && this.state.validateErrorMsg)}
84 onDismiss={this.validateReset}
91 {this.returnCrouton()}
92 <nav className="skyquakeNav">
93 {buildNav.call(this, this.props.nav, this.props.currentPlugin)}
101 skyquakeNav.defaultProps = {
105 * Returns a React Component
106 * @param {object} link Information about the nav link
107 * @param {string} link.route Hash route that the SPA should resolve
108 * @param {string} link.name Link name to be displayed
109 * @param {number} index index of current array item
110 * @return {object} component A React LI Component
112 //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.
113 export function buildNavListItem (k, link, index) {
115 if (link.type == 'external') {
116 this.hasSubNav[k] = true;
119 {returnLinkItem(link)}
127 * Builds a link to a React Router route or a new plugin route.
128 * @param {object} link Routing information from nav object.
129 * @return {object} component returns a react component that links to a new route.
131 export function returnLinkItem(link) {
133 let route = link.route;
134 if(link.isExternal) {
136 <a href={route}>{link.label}</a>
139 if(link.path && link.path.replace(' ', '') != '') {
144 query[link.query] = '';
160 * Constructs nav for each plugin, along with available subnavs
161 * @param {array} nav List returned from /nav endpoint.
162 * @return {array} List of constructed nav element for each plugin
164 export function buildNav(nav, currentPlugin) {
166 let navListHTML = [];
167 let secondaryNav = [];
170 let secondaryNavHTML = (
171 <div className="secondaryNav" key="secondaryNav">
173 <LogoutAppMenuItem />
177 if (nav.hasOwnProperty(k)) {
178 self.hasSubNav[k] = false;
180 let navClass = "app";
181 let routes = nav[k].routes;
183 //Primary plugin title and link to dashboard.
187 route = routes[0].isExternal ? '/' + k + '/index.html?api_server=' + API_SERVER + '' + '&upload_server=' + UPLOAD_SERVER + '' : '';
189 route = routes[0].isExternal ? '/' + k + '/' : '';
191 let dashboardLink = returnLinkItem({
192 isExternal: routes[0].isExternal,
193 pluginName: nav[k].pluginName,
194 label: nav[k].label || k,
197 if (nav[k].pluginName == currentPlugin) {
198 navClass += " active";
200 NavList = nav[k].routes.map(buildNavListItem.bind(self, k));
201 navItem.priority = nav[k].priority;
202 navItem.order = nav[k].order;
204 <div key={k} className={navClass}>
205 <h2>{dashboardLink} {self.hasSubNav[k] ? <span className="oi" data-glyph="caret-bottom"></span> : ''}</h2>
206 <ul className="menu">
213 navList.push(navItem)
216 //Sorts nav items by order and returns only the markup
217 navListHTML = navList.sort((a,b) => a.order - b.order).map(function(n) {
218 if((n.priority < 2)){
221 secondaryNav.push(n.html);
224 navListHTML.push(secondaryNavHTML);