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 import rw from 'utils/rw.js';
33 var API_SERVER = rw.getSearchParams(window.location).api_server;
34 var UPLOAD_SERVER = rw.getSearchParams(window.location).upload_server;
37 // Internal classes/functions
40 class SelectProject extends React.Component {
45 let value = JSON.parse(e.currentTarget.value);
46 console.log('selected project', value)
49 let props = this.props;
50 let currentValue = JSON.stringify(props.currentProject);
51 let projects = this.props.projects && this.props.projects.map((p,i) => {
57 let hasProjects = (this.props.projects && (this.props.projects.length > 0))
59 <div className="userSection app">
61 hasProjects ? 'Project:' : 'No Projects Assigned'
68 defaultValue={currentValue}
69 onChange={props.onSelectProject}
70 className="projectSelect" />
78 class UserNav extends React.Component {
83 Utils.clearAuthentication();
86 let value = JSON.parse(e.currentTarget.value)
87 console.log('selected project', value)
90 let props = this.props;
91 let userProfileLink = '';
92 this.props.nav['user_management'] && this.props.nav['user_management'].routes.map((r) => {
94 userProfileLink = returnLinkItem(r, props.currentUser)
100 USER: {userProfileLink}
101 <span className="oi" data-glyph="caret-bottom"></span>
103 <ul className="menu">
105 <a onClick={this.handleLogout}>
115 UserNav.defaultProps = {
122 // Exported classes and functions
127 * Skyquake Nav Component. Provides navigation functionality between all plugins
129 export default class skyquakeNav extends React.Component {
133 this.state.validateErrorEvent = 0;
134 this.state.validateErrorMsg = '';
136 componentDidMount() {
137 this.props.store.openProjectSocket();
138 this.props.store.getUserProfile();
140 validateError = (msg) => {
142 validateErrorEvent: true,
143 validateErrorMsg: msg
146 validateReset = () => {
148 validateErrorEvent: false
151 returnCrouton = () => {
154 message={this.state.validateErrorMsg}
156 hidden={!(this.state.validateErrorEvent && this.state.validateErrorMsg)}
157 onDismiss={this.validateReset}
164 {this.returnCrouton()}
165 <nav className="skyquakeNav">
166 {buildNav.call(this, this.props.nav, this.props.currentPlugin, this.props)}
174 skyquakeNav.defaultProps = {
177 skyquakeNav.contextTypes = {
178 userProfile: React.PropTypes.object
181 * Returns a React Component
182 * @param {object} link Information about the nav link
183 * @param {string} link.route Hash route that the SPA should resolve
184 * @param {string} link.name Link name to be displayed
185 * @param {number} index index of current array item
186 * @return {object} component A React LI Component
188 //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.
189 export function buildNavListItem (k, link, index) {
191 if (link.type == 'external') {
192 this.hasSubNav[k] = true;
195 {returnLinkItem(link)}
203 * Builds a link to a React Router route or a new plugin route.
204 * @param {object} link Routing information from nav object.
205 * @return {object} component returns a react component that links to a new route.
207 export function returnLinkItem(link, label) {
209 let route = link.route;
210 if(link.isExternal) {
212 <a href={route}>{label || link.label}</a>
215 if(link.path && link.path.replace(' ', '') != '') {
220 query[link.query] = '';
228 {label || link.label}
239 * Constructs nav for each plugin, along with available subnavs
240 * @param {array} nav List returned from /nav endpoint.
241 * @return {array} List of constructed nav element for each plugin
243 export function buildNav(nav, currentPlugin, props) {
245 let navListHTML = [];
246 let secondaryNav = [];
249 const User = this.context.userProfile;
251 let secondaryNavHTML = (
252 <div className="secondaryNav" key="secondaryNav">
254 <div className="app admin">
257 ADMIN <span className="oi" data-glyph="caret-bottom"></span>
260 <ul className="menu">
267 onSelectProject={props.store.selectActiveProject}
268 projects={props.projects}
269 currentProject={props.currentProject} />
271 currentUser={props.currentUser}
276 if (nav.hasOwnProperty(k)) {
277 self.hasSubNav[k] = false;
279 let navClass = "app";
280 let routes = nav[k].routes;
282 //Primary plugin title and link to dashboard.
286 route = routes[0].isExternal ? '/' + k + '/index.html?api_server=' + API_SERVER + '' + '&upload_server=' + UPLOAD_SERVER + '' : '';
288 route = routes[0].isExternal ? '/' + k + '/' : '';
290 let dashboardLink = returnLinkItem({
291 isExternal: routes[0].isExternal,
292 pluginName: nav[k].pluginName,
293 label: nav[k].label || k,
296 let shouldAllow = nav[k].allow || ['*'];
297 if (nav[k].pluginName == currentPlugin) {
298 navClass += " active";
300 NavList = nav[k].routes.map(buildNavListItem.bind(self, k));
301 navItem.priority = nav[k].priority;
302 navItem.order = nav[k].order;
303 if(nav[k].admin_link) {
305 if (isRBACValid(User, shouldAllow) ){
307 <li key={nav[k].name}>
313 if (isRBACValid(User, shouldAllow) ){
315 <div key={k} className={navClass}>
316 <h2>{dashboardLink} {self.hasSubNav[k] ? <span className="oi" data-glyph="caret-bottom"></span> : ''}</h2>
317 <ul className="menu">
325 navList.push(navItem)
330 //Sorts nav items by order and returns only the markup
331 navListHTML = navList.sort((a,b) => a.order - b.order).map(function(n) {
332 if((n.priority < 2)){
335 secondaryNav.push(n.html);
338 navListHTML.push(secondaryNavHTML);