1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 01:39:36 +01:00
onion/js/components/nav_routes_links.js

119 lines
3.9 KiB
JavaScript
Raw Normal View History

'use strict';
import React from 'react';
import Nav from 'react-bootstrap/lib/Nav';
import NavRoutesLinksLink from './nav_routes_links_link';
import AclProxy from './acl_proxy';
import { sanitizeList } from '../utils/general_utils';
2016-02-05 10:38:59 +01:00
const DISABLE_ENUM = ['hasPieces', 'noPieces'];
let NavRoutesLinks = React.createClass({
propTypes: {
2016-02-05 10:38:59 +01:00
hasPieces: React.PropTypes.bool,
2015-10-01 14:00:56 +02:00
routes: React.PropTypes.arrayOf(React.PropTypes.object),
userAcl: React.PropTypes.object
},
2016-02-05 10:38:59 +01:00
isRouteDisabled(disableOn) {
const { hasPieces } = this.props;
if (disableOn) {
if (!DISABLE_ENUM.includes(disableOn)) {
throw new Error(`"disableOn" must be one of: [${DISABLE_ENUM.join(', ')}] got "${disableOn}" instead`);
}
if (disableOn === 'hasPieces') {
return hasPieces;
} else if (disableOn === 'noPieces') {
return !hasPieces;
}
}
},
2015-09-08 14:26:56 +02:00
/**
2016-02-05 10:38:59 +01:00
* This method generates a bunch of react-bootstrap specific links
2015-09-08 14:26:56 +02:00
* from the routes we defined in one of the specific routes.js file
*
* We can define a headerTitle as well as a aclName and according to that the
* link will be created for a specific user
* @param {ReactElement} node Starts at the very top of a routes files root
* @param {object} userAcl ACL object we use throughout the whole app
* @param {number} i Depth of the route in comparison to the root
* @return {Array} Array of ReactElements that can be displayed to the user
*/
extractLinksFromRoutes(node, userAcl, i) {
2016-02-05 10:38:59 +01:00
if (!node) {
return;
}
2016-02-05 10:38:59 +01:00
const links = node.childRoutes.map((child, j) => {
const { aclName, disableOn, headerTitle, path, childRoutes } = child;
2015-09-08 14:26:56 +02:00
// We validate if the user has set the title correctly,
// otherwise we're not going to render his route
2016-02-05 10:38:59 +01:00
if (headerTitle && typeof headerTitle === 'string') {
let nestedChildren = null;
// If the node has children that could be rendered, then we want
// to execute this function again with the child as the root
//
// Otherwise we'll just pass nestedChildren as false
if (child.childRoutes && child.childRoutes.length) {
nestedChildren = this.extractLinksFromRoutes(child, userAcl, i++);
}
const navLinkProps = {
headerTitle,
children: nestedChildren,
depth: i,
disabled: this.isRouteDisabled(disableOn),
routePath: `/${path}`
};
2015-09-08 14:26:56 +02:00
// if there is an aclName present on the route definition,
// we evaluate it against the user's acl
2016-02-05 10:38:59 +01:00
if (aclName && typeof aclName !== 'undefined') {
return (
<AclProxy
2015-09-08 14:26:56 +02:00
key={j}
aclName={aclName}
aclObject={this.props.userAcl}>
2016-02-05 10:38:59 +01:00
<NavRoutesLinksLink {...navLinkProps} />
</AclProxy>
);
} else {
return (
<NavRoutesLinksLink
key={j}
2016-02-05 10:38:59 +01:00
{...navLinkProps} />
);
}
} else {
return null;
}
});
// remove all nulls from the list of generated links
return sanitizeList(links);
},
render() {
2016-02-05 10:38:59 +01:00
const {routes, userAcl} = this.props;
return (
<Nav {...this.props}>
2015-10-01 14:00:56 +02:00
{this.extractLinksFromRoutes(routes[0], userAcl, 0)}
</Nav>
);
}
});
2016-02-05 10:38:59 +01:00
export default NavRoutesLinks;