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

246 lines
8.1 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
2015-05-20 16:19:40 +02:00
import React from 'react';
import { Link } from 'react-router';
2015-11-25 11:16:09 +01:00
import history from '../history';
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';
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';
import AclProxy from './acl_proxy';
import EventActions from '../actions/event_actions';
2015-09-01 18:11:18 +02:00
import UserActions from '../actions/user_actions';
import UserStore from '../stores/user_store';
import WhitelabelActions from '../actions/whitelabel_actions';
import WhitelabelStore from '../stores/whitelabel_store';
import HeaderNotifications from './header_notification';
import HeaderNotificationDebug from './header_notification_debug';
import NavRoutesLinks from './nav_routes_links';
2015-06-11 15:35:18 +02:00
2015-06-29 15:58:47 +02:00
import { mergeOptions } from '../utils/general_utils';
import { getLangText } from '../utils/lang_utils';
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: {
2015-10-01 14:00:56 +02:00
routes: React.PropTypes.arrayOf(React.PropTypes.object)
2015-07-14 21:06:11 +02:00
},
2015-05-20 16:19:40 +02:00
getInitialState() {
2015-09-01 14:45:14 +02:00
return mergeOptions(
WhitelabelStore.getState(),
2015-09-01 18:11:18 +02:00
UserStore.getState()
2015-09-01 14:45:14 +02:00
);
2015-05-20 16:19:40 +02:00
},
componentDidMount() {
2015-06-05 11:40:49 +02:00
UserStore.listen(this.onChange);
UserActions.fetchCurrentUser.defer();
2015-06-29 15:58:47 +02:00
WhitelabelStore.listen(this.onChange);
WhitelabelActions.fetchWhitelabel.defer();
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);
if (this.state.currentUser && this.state.currentUser.email) {
EventActions.profileDidLoad.defer(this.state.currentUser);
}
},
componentWillUpdate(nextProps, nextState) {
const { currentUser: { email: curEmail } = {} } = this.state;
const { currentUser: { email: nextEmail } = {} } = nextState;
if (nextEmail && curEmail !== nextEmail) {
EventActions.profileDidLoad.defer(nextState.currentUser);
}
2015-06-03 11:49:39 +02:00
},
componentWillUnmount() {
2015-06-04 13:16:19 +02:00
UserStore.unlisten(this.onChange);
2015-06-29 15:58:47 +02:00
WhitelabelStore.unlisten(this.onChange);
//history.unlisten(this.onRouteChange);
2015-05-20 16:19:40 +02:00
},
getLogo() {
2015-10-08 11:12:15 +02:00
let { whitelabel } = this.state;
2015-10-14 17:29:14 +02:00
if (whitelabel.head) {
constructHead(whitelabel.head);
2015-09-29 16:48:46 +02:00
}
if (whitelabel.subdomain && whitelabel.subdomain !== 'www' && whitelabel.logo){
return (
<Link to="/collection">
<img className="img-brand" src={whitelabel.logo} alt="Whitelabel brand"/>
</Link>
);
2015-06-29 15:58:47 +02:00
}
2015-09-29 16:48:46 +02:00
return (
2015-06-29 15:58:47 +02:00
<span>
<Link className="icon-ascribe-logo" to="/collection"/>
2015-09-29 16:48:46 +02:00
</span>
);
2015-06-29 15:58:47 +02:00
},
getPoweredBy(){
2015-09-29 16:48:46 +02:00
return (
<AclProxy
aclObject={this.state.whitelabel}
aclName="acl_view_powered_by">
<li>
2015-10-14 12:12:06 +02:00
<a className="pull-right ascribe-powered-by" href="https://www.ascribe.io/" target="_blank">
2015-09-29 16:48:46 +02:00
<span id="powered">{getLangText('powered by')} </span>
2015-10-13 13:32:48 +02:00
<span className="icon-ascribe-logo"></span>
2015-09-29 16:48:46 +02:00
</a>
</li>
</AclProxy>
);
2015-06-29 15:58:47 +02:00
},
2015-09-29 16:48:46 +02:00
2015-05-20 16:19:40 +02:00
onChange(state) {
this.setState(state);
},
2015-06-15 15:28:53 +02:00
onMenuItemClick() {
/*
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() {
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() {
2015-08-28 15:29:30 +02:00
let account;
let signup;
let navRoutesLinks;
2015-06-15 12:36:27 +02:00
if (this.state.currentUser.username){
account = (
<DropdownButton
ref='dropdownbutton'
eventKey="1"
title={this.state.currentUser.username}>
2015-10-01 14:00:56 +02:00
<LinkContainer
to="/settings"
onClick={this.onMenuItemClick}>
2015-10-01 14:00:56 +02:00
<MenuItem
eventKey="2">
{getLangText('Account Settings')}
</MenuItem>
</LinkContainer>
<AclProxy
2015-09-23 15:41:12 +02:00
aclObject={this.state.currentUser.acl}
aclName="acl_view_settings_contract">
2015-10-01 14:00:56 +02:00
<LinkContainer
to="/contract_settings"
onClick={this.onMenuItemClick}>
2015-10-01 14:00:56 +02:00
<MenuItem
eventKey="2">
{getLangText('Contract Settings')}
</MenuItem>
</LinkContainer>
</AclProxy>
2015-06-22 10:50:22 +02:00
<MenuItem divider />
2015-10-01 14:00:56 +02:00
<LinkContainer
to="/logout">
<MenuItem
eventKey="3">
{getLangText('Log out')}
</MenuItem>
</LinkContainer>
2015-09-01 14:45:14 +02:00
</DropdownButton>
2015-06-15 12:36:27 +02:00
);
navRoutesLinks = <NavRoutesLinks routes={this.props.routes} userAcl={this.state.currentUser.acl} navbar right/>;
2015-06-15 12:36:27 +02:00
}
2015-06-15 15:28:53 +02:00
else {
2015-10-01 14:00:56 +02:00
account = (
<LinkContainer
to="/login">
<NavItem>
{getLangText('LOGIN')}
</NavItem>
</LinkContainer>
);
signup = (
<LinkContainer
to="/signup">
<NavItem>
{getLangText('SIGNUP')}
</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-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}
ref="navbar">
<CollapsibleNav
eventKey={0}>
2015-07-14 19:01:14 +02:00
<Nav navbar left>
{this.getPoweredBy()}
</Nav>
2015-06-29 15:58:47 +02:00
<Nav navbar right>
<HeaderNotificationDebug show={false}/>
2015-06-29 15:58:47 +02:00
{account}
{signup}
</Nav>
2015-09-01 18:11:18 +02:00
<HeaderNotifications />
2015-08-28 15:29:30 +02:00
{navRoutesLinks}
2015-06-29 15:58:47 +02:00
</CollapsibleNav>
</Navbar>
</div>
2015-05-21 15:20:02 +02:00
);
2015-05-20 16:19:40 +02:00
}
});
export default Header;