2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-20 16:19:40 +02:00
|
|
|
import React from 'react';
|
2015-10-27 11:44:40 +01:00
|
|
|
import { Link } from 'react-router';
|
|
|
|
|
2015-11-25 11:16:09 +01:00
|
|
|
import history from '../history';
|
|
|
|
|
2015-06-01 18:20:15 +02:00
|
|
|
import Nav from 'react-bootstrap/lib/Nav';
|
|
|
|
import Navbar from 'react-bootstrap/lib/Navbar';
|
2015-06-22 10:50:22 +02:00
|
|
|
import CollapsibleNav from 'react-bootstrap/lib/CollapsibleNav';
|
2015-06-01 18:20:15 +02:00
|
|
|
import DropdownButton from 'react-bootstrap/lib/DropdownButton';
|
|
|
|
import MenuItem from 'react-bootstrap/lib/MenuItem';
|
2015-10-01 14:00:56 +02:00
|
|
|
import NavItem from 'react-bootstrap/lib/NavItem';
|
|
|
|
|
|
|
|
import LinkContainer from 'react-router-bootstrap/lib/LinkContainer';
|
2015-06-01 18:20:15 +02:00
|
|
|
|
2015-12-14 16:30:38 +01:00
|
|
|
import EventActions from '../actions/event_actions';
|
|
|
|
|
2016-02-05 10:38:59 +01:00
|
|
|
import PieceListStore from '../stores/piece_list_store';
|
|
|
|
|
2016-01-11 18:41:07 +01:00
|
|
|
import AclProxy from './acl_proxy';
|
2016-03-10 13:43:07 +01:00
|
|
|
import HeaderNotifications from './header_notifications';
|
2015-07-08 15:00:11 +02:00
|
|
|
import HeaderNotificationDebug from './header_notification_debug';
|
2015-08-25 16:33:26 +02:00
|
|
|
import NavRoutesLinks from './nav_routes_links';
|
2015-06-11 15:35:18 +02:00
|
|
|
|
2015-06-02 14:25:26 +02:00
|
|
|
import { getLangText } from '../utils/lang_utils';
|
2015-10-28 17:55:44 +01:00
|
|
|
import { constructHead } from '../utils/dom_utils';
|
2015-05-20 16:19:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
let Header = React.createClass({
|
2015-07-14 21:06:11 +02:00
|
|
|
propTypes: {
|
2016-01-11 18:41:07 +01:00
|
|
|
currentUser: React.PropTypes.object.isRequired,
|
|
|
|
routes: React.PropTypes.arrayOf(React.PropTypes.object).isRequired,
|
|
|
|
whitelabel: React.PropTypes.object.isRequired
|
2015-05-20 16:19:40 +02:00
|
|
|
},
|
|
|
|
|
2016-02-05 10:38:59 +01:00
|
|
|
getInitialState() {
|
|
|
|
return PieceListStore.getState();
|
|
|
|
},
|
|
|
|
|
2015-05-20 16:19:40 +02:00
|
|
|
componentDidMount() {
|
2016-02-05 10:38:59 +01:00
|
|
|
// Listen to the piece list store, but don't fetch immediately to avoid
|
|
|
|
// conflicts with routes that may need to wait to load the piece list
|
|
|
|
PieceListStore.listen(this.onChange);
|
|
|
|
|
2015-11-25 11:16:09 +01:00
|
|
|
// react-bootstrap 0.25.1 has a bug in which it doesn't
|
|
|
|
// close the mobile expanded navigation after a click by itself.
|
|
|
|
// To get rid of this, we set the state of the component ourselves.
|
|
|
|
history.listen(this.onRouteChange);
|
2015-06-03 11:49:39 +02:00
|
|
|
},
|
|
|
|
|
2015-06-04 13:17:59 +02:00
|
|
|
componentWillUnmount() {
|
2016-02-05 10:38:59 +01:00
|
|
|
PieceListStore.unlisten(this.onChange);
|
2015-12-07 17:41:10 +01:00
|
|
|
//history.unlisten(this.onRouteChange);
|
2015-05-20 16:19:40 +02:00
|
|
|
},
|
2015-07-08 15:00:11 +02:00
|
|
|
|
2016-02-05 10:38:59 +01:00
|
|
|
onChange(state) {
|
|
|
|
this.setState(state);
|
|
|
|
},
|
|
|
|
|
2015-10-22 12:00:59 +02:00
|
|
|
getLogo() {
|
2016-01-11 18:41:07 +01:00
|
|
|
const { whitelabel } = this.props;
|
2015-10-22 12:00:59 +02:00
|
|
|
|
2015-10-14 17:29:14 +02:00
|
|
|
if (whitelabel.head) {
|
2015-10-08 15:39:09 +02:00
|
|
|
constructHead(whitelabel.head);
|
2015-09-29 16:48:46 +02:00
|
|
|
}
|
2015-10-22 12:00:59 +02:00
|
|
|
|
2015-10-14 17:41:44 +02:00
|
|
|
if (whitelabel.subdomain && whitelabel.subdomain !== 'www' && whitelabel.logo){
|
2015-10-27 11:44:40 +01:00
|
|
|
return (
|
|
|
|
<Link to="/collection">
|
|
|
|
<img className="img-brand" src={whitelabel.logo} alt="Whitelabel brand"/>
|
|
|
|
</Link>
|
|
|
|
);
|
2016-01-11 18:41:07 +01:00
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
<Link className="icon-ascribe-logo" to="/collection"/>
|
|
|
|
</span>
|
|
|
|
);
|
2015-06-29 15:58:47 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-01-08 14:59:45 +01:00
|
|
|
getPoweredBy() {
|
2015-09-29 16:48:46 +02:00
|
|
|
return (
|
2016-03-07 13:45:40 +01:00
|
|
|
<li>
|
|
|
|
<a className="pull-right ascribe-powered-by" href="https://www.ascribe.io/" target="_blank">
|
|
|
|
<span id="powered">{getLangText('powered by')} </span>
|
|
|
|
<span className="icon-ascribe-logo"></span>
|
|
|
|
</a>
|
|
|
|
</li>
|
2015-09-29 16:48:46 +02:00
|
|
|
);
|
2015-06-29 15:58:47 +02:00
|
|
|
},
|
2015-09-29 16:48:46 +02:00
|
|
|
|
2015-09-23 15:16:46 +02:00
|
|
|
onMenuItemClick() {
|
2015-09-03 14:15:00 +02:00
|
|
|
/*
|
|
|
|
This is a hack to make the dropdown close after clicking on an item
|
|
|
|
The function just need to be defined
|
|
|
|
|
|
|
|
from https://github.com/react-bootstrap/react-bootstrap/issues/368:
|
|
|
|
|
|
|
|
@jvillasante - Have you tried to use onSelect with the DropdownButton?
|
|
|
|
I don't have a working example that is exactly like yours,
|
|
|
|
but I just noticed that the Dropdown closes when I've attached an event handler to OnSelect:
|
|
|
|
|
|
|
|
<DropdownButton eventKey={3} title="Admin" onSelect={ this.OnSelected } >
|
|
|
|
|
|
|
|
onSelected: function(e) {
|
|
|
|
// doesn't need to have functionality (necessarily) ... just wired up
|
|
|
|
}
|
|
|
|
Internally, a call to DropdownButton.setDropDownState(false) is made which will hide the dropdown menu.
|
|
|
|
So, you should be able to call that directly on the DropdownButton instance as well if needed.
|
|
|
|
|
|
|
|
NOW, THAT DIDN'T WORK - the onSelect routine isnt triggered in all cases
|
|
|
|
Hence, we do this manually
|
|
|
|
*/
|
|
|
|
this.refs.dropdownbutton.setDropdownState(false);
|
|
|
|
},
|
|
|
|
|
2015-11-27 14:13:30 +01:00
|
|
|
// On route change, close expanded navbar again since react-bootstrap doesn't close
|
|
|
|
// the collapsibleNav by itself on click. setState() isn't available on a ref so
|
|
|
|
// doing this explicitly is the only way for now.
|
2015-11-25 11:16:09 +01:00
|
|
|
onRouteChange() {
|
2015-12-10 19:19:01 +01:00
|
|
|
if (this.refs.navbar) {
|
|
|
|
this.refs.navbar.state.navExpanded = false;
|
|
|
|
}
|
2015-11-25 11:16:09 +01:00
|
|
|
},
|
|
|
|
|
2015-05-20 16:19:40 +02:00
|
|
|
render() {
|
2016-03-07 13:45:40 +01:00
|
|
|
const { currentUser, routes, whitelabel } = this.props;
|
2016-02-05 10:38:59 +01:00
|
|
|
const { unfilteredPieceListCount } = this.state;
|
|
|
|
|
2015-08-28 15:29:30 +02:00
|
|
|
let account;
|
|
|
|
let navRoutesLinks;
|
2016-01-11 18:41:07 +01:00
|
|
|
|
|
|
|
if (currentUser.username) {
|
2015-06-15 12:36:27 +02:00
|
|
|
account = (
|
2015-09-01 18:38:24 +02:00
|
|
|
<DropdownButton
|
2015-09-03 14:15:00 +02:00
|
|
|
ref='dropdownbutton'
|
2016-02-05 10:38:59 +01:00
|
|
|
id="nav-route-user-dropdown"
|
2015-09-01 18:38:24 +02:00
|
|
|
eventKey="1"
|
2016-01-11 18:41:07 +01:00
|
|
|
title={currentUser.username}>
|
2015-10-01 14:00:56 +02:00
|
|
|
<LinkContainer
|
|
|
|
to="/settings"
|
2015-09-03 14:15:00 +02:00
|
|
|
onClick={this.onMenuItemClick}>
|
2016-01-11 18:41:07 +01:00
|
|
|
<MenuItem eventKey="2">
|
2015-10-01 14:00:56 +02:00
|
|
|
{getLangText('Account Settings')}
|
|
|
|
</MenuItem>
|
|
|
|
</LinkContainer>
|
2015-09-23 15:16:46 +02:00
|
|
|
<AclProxy
|
2016-01-11 18:41:07 +01:00
|
|
|
aclObject={currentUser.acl}
|
2015-09-23 15:16:46 +02:00
|
|
|
aclName="acl_view_settings_contract">
|
2015-10-01 14:00:56 +02:00
|
|
|
<LinkContainer
|
|
|
|
to="/contract_settings"
|
2015-09-23 15:16:46 +02:00
|
|
|
onClick={this.onMenuItemClick}>
|
2016-01-11 18:41:07 +01:00
|
|
|
<MenuItem eventKey="2">
|
2015-10-01 14:00:56 +02:00
|
|
|
{getLangText('Contract Settings')}
|
|
|
|
</MenuItem>
|
|
|
|
</LinkContainer>
|
2015-09-23 15:16:46 +02:00
|
|
|
</AclProxy>
|
2015-06-22 10:50:22 +02:00
|
|
|
<MenuItem divider />
|
2016-01-11 18:41:07 +01:00
|
|
|
<LinkContainer to="/logout">
|
|
|
|
<MenuItem eventKey="3">
|
2015-10-01 14:00:56 +02:00
|
|
|
{getLangText('Log out')}
|
|
|
|
</MenuItem>
|
|
|
|
</LinkContainer>
|
2015-09-01 14:45:14 +02:00
|
|
|
</DropdownButton>
|
2015-06-15 12:36:27 +02:00
|
|
|
);
|
2016-01-11 18:41:07 +01:00
|
|
|
|
2016-02-05 10:38:59 +01:00
|
|
|
// Let's assume that if the piece list hasn't loaded yet (ie. when unfilteredPieceListCount === -1)
|
|
|
|
// then the user has pieces
|
|
|
|
// FIXME: this doesn't work that well as the user may not load their piece list
|
|
|
|
// until much later, so we would show the 'Collection' header as available until
|
|
|
|
// they actually click on it and get redirected to piece registration.
|
2016-01-11 18:41:07 +01:00
|
|
|
navRoutesLinks = (
|
|
|
|
<NavRoutesLinks
|
|
|
|
navbar
|
2016-02-05 10:38:59 +01:00
|
|
|
right
|
|
|
|
hasPieces={!!unfilteredPieceListCount}
|
|
|
|
routes={routes}
|
|
|
|
userAcl={currentUser.acl} />
|
2016-01-11 18:41:07 +01:00
|
|
|
);
|
|
|
|
} else {
|
2015-10-01 14:00:56 +02:00
|
|
|
account = (
|
2016-01-11 18:41:07 +01:00
|
|
|
<LinkContainer to="/login">
|
2015-10-01 14:00:56 +02:00
|
|
|
<NavItem>
|
|
|
|
{getLangText('LOGIN')}
|
|
|
|
</NavItem>
|
|
|
|
</LinkContainer>
|
|
|
|
);
|
2015-06-15 15:28:53 +02:00
|
|
|
}
|
2015-06-22 10:50:22 +02:00
|
|
|
|
2015-06-29 15:58:47 +02:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Navbar
|
2015-12-23 09:36:08 +01:00
|
|
|
ref="navbar"
|
2015-10-01 14:00:56 +02:00
|
|
|
brand={this.getLogo()}
|
2015-07-07 10:28:39 +02:00
|
|
|
toggleNavKey={0}
|
2015-11-25 11:16:09 +01:00
|
|
|
fixedTop={true}
|
2015-12-23 09:36:08 +01:00
|
|
|
className="hidden-print">
|
2016-01-11 18:41:07 +01:00
|
|
|
<CollapsibleNav eventKey={0}>
|
2015-07-14 19:01:14 +02:00
|
|
|
<Nav navbar left>
|
2016-03-07 13:45:40 +01:00
|
|
|
<AclProxy
|
|
|
|
aclObject={whitelabel}
|
|
|
|
aclName="acl_view_powered_by">
|
|
|
|
{this.getPoweredBy()}
|
|
|
|
</AclProxy>
|
2015-07-14 19:01:14 +02:00
|
|
|
</Nav>
|
2015-06-29 15:58:47 +02:00
|
|
|
<Nav navbar right>
|
2016-01-11 18:41:07 +01:00
|
|
|
<HeaderNotificationDebug show={false} />
|
2015-06-29 15:58:47 +02:00
|
|
|
{account}
|
|
|
|
</Nav>
|
2016-03-10 13:41:36 +01:00
|
|
|
<HeaderNotifications currentUser={currentUser} />
|
2015-08-28 15:29:30 +02:00
|
|
|
{navRoutesLinks}
|
2015-06-29 15:58:47 +02:00
|
|
|
</CollapsibleNav>
|
|
|
|
</Navbar>
|
2015-12-23 09:36:08 +01:00
|
|
|
<p className="ascribe-print-header visible-print">
|
|
|
|
<span className="icon-ascribe-logo" />
|
|
|
|
</p>
|
2015-06-29 15:58:47 +02:00
|
|
|
</div>
|
2015-05-21 15:20:02 +02:00
|
|
|
);
|
2015-05-20 16:19:40 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default Header;
|