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';
28 import {isRBACValid, SkyquakeRBAC} from 'widgets/skyquake_rbac/skyquakeRBAC.jsx';
30 //Temporary, until api server is on same port as webserver
31 var rw = require('utils/rw.js');
32 var API_SERVER = rw.getSearchParams(window.location).api_server;
33 var UPLOAD_SERVER = rw.getSearchParams(window.location).upload_server;
36 // Internal classes/functions
39 class LogoutAppMenuItem extends React.Component {
41 Utils.clearAuthentication();
47 <a onClick={this.handleLogout}>
56 class SelectProject extends React.Component {
61 let value = JSON.parse(e.currentTarget.value);
62 console.log('selected project', value)
65 let props = this.props;
66 let currentValue = JSON.stringify(props.currentProject);
67 let projects = this.props.projects && this.props.projects.map((p,i) => {
73 let hasProjects = (this.props.projects && (this.props.projects.length > 0))
75 <div className="userSection app">
77 hasProjects ? 'Project:' : 'No Projects Assigned'
84 defaultValue={currentValue}
85 onChange={props.onSelectProject}
86 className="projectSelect" />
94 class UserNav extends React.Component {
99 Utils.clearAuthentication();
102 let value = JSON.parse(e.currentTarget.value)
103 console.log('selected project', value)
106 let props = this.props;
107 let userProfileLink = '';
108 this.props.nav['user_management'] && this.props.nav['user_management'].routes.map((r) => {
110 userProfileLink = returnLinkItem(r, props.currentUser)
114 <div className="app">
116 USER: {userProfileLink}
117 <span className="oi" data-glyph="caret-bottom"></span>
119 <ul className="menu">
121 <a onClick={this.handleLogout}>
131 UserNav.defaultProps = {
138 // Exported classes and functions
143 * Skyquake Nav Component. Provides navigation functionality between all plugins
145 export default class skyquakeNav extends React.Component {
149 this.state.validateErrorEvent = 0;
150 this.state.validateErrorMsg = '';
152 componentDidMount() {
153 this.props.store.openProjectSocket();
154 this.props.store.getUserProfile();
156 validateError = (msg) => {
158 validateErrorEvent: true,
159 validateErrorMsg: msg
162 validateReset = () => {
164 validateErrorEvent: false
167 returnCrouton = () => {
170 message={this.state.validateErrorMsg}
172 hidden={!(this.state.validateErrorEvent && this.state.validateErrorMsg)}
173 onDismiss={this.validateReset}
180 {this.returnCrouton()}
181 <nav className="skyquakeNav">
182 {buildNav.call(this, this.props.nav, this.props.currentPlugin, this.props)}
190 skyquakeNav.defaultProps = {
193 skyquakeNav.contextTypes = {
194 userProfile: React.PropTypes.object
197 * Returns a React Component
198 * @param {object} link Information about the nav link
199 * @param {string} link.route Hash route that the SPA should resolve
200 * @param {string} link.name Link name to be displayed
201 * @param {number} index index of current array item
202 * @return {object} component A React LI Component
204 //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.
205 export function buildNavListItem (k, link, index) {
207 if (link.type == 'external') {
208 this.hasSubNav[k] = true;
211 {returnLinkItem(link)}
219 * Builds a link to a React Router route or a new plugin route.
220 * @param {object} link Routing information from nav object.
221 * @return {object} component returns a react component that links to a new route.
223 export function returnLinkItem(link, label) {
225 let route = link.route;
226 if(link.isExternal) {
228 <a href={route}>{label || link.label}</a>
231 if(link.path && link.path.replace(' ', '') != '') {
236 query[link.query] = '';
244 {label || link.label}
255 * Constructs nav for each plugin, along with available subnavs
256 * @param {array} nav List returned from /nav endpoint.
257 * @return {array} List of constructed nav element for each plugin
259 export function buildNav(nav, currentPlugin, props) {
261 let navListHTML = [];
262 let secondaryNav = [];
265 const User = this.context.userProfile;
267 let secondaryNavHTML = (
268 <div className="secondaryNav" key="secondaryNav">
270 <div className="app admin">
273 ADMIN <span className="oi" data-glyph="caret-bottom"></span>
276 <ul className="menu">
283 onSelectProject={props.store.selectActiveProject}
284 projects={props.projects}
285 currentProject={props.currentProject} />
287 currentUser={props.currentUser}
292 if (nav.hasOwnProperty(k)) {
293 self.hasSubNav[k] = false;
295 let navClass = "app";
296 let routes = nav[k].routes;
298 //Primary plugin title and link to dashboard.
302 route = routes[0].isExternal ? '/' + k + '/index.html?api_server=' + API_SERVER + '' + '&upload_server=' + UPLOAD_SERVER + '' : '';
304 route = routes[0].isExternal ? '/' + k + '/' : '';
306 let dashboardLink = returnLinkItem({
307 isExternal: routes[0].isExternal,
308 pluginName: nav[k].pluginName,
309 label: nav[k].label || k,
312 let shouldAllow = nav[k].allow || ['*'];
313 if (nav[k].pluginName == currentPlugin) {
314 navClass += " active";
316 NavList = nav[k].routes.map(buildNavListItem.bind(self, k));
317 navItem.priority = nav[k].priority;
318 navItem.order = nav[k].order;
319 if(nav[k].admin_link) {
321 if (isRBACValid(User, shouldAllow) ){
323 <li key={nav[k].name}>
329 if (isRBACValid(User, shouldAllow) ){
331 <div key={k} className={navClass}>
332 <h2>{dashboardLink} {self.hasSubNav[k] ? <span className="oi" data-glyph="caret-bottom"></span> : ''}</h2>
333 <ul className="menu">
341 navList.push(navItem)
346 //Sorts nav items by order and returns only the markup
347 navListHTML = navList.sort((a,b) => a.order - b.order).map(function(n) {
348 if((n.priority < 2)){
351 secondaryNav.push(n.html);
354 navListHTML.push(secondaryNavHTML);