2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt odlux
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
18 import React from 'react';
19 import { withRouter, RouteComponentProps } from 'react-router-dom';
21 import { Theme } from '@mui/material/styles';
22 import { WithStyles } from '@mui/styles';
23 import withStyles from '@mui/styles/withStyles';
24 import createStyles from '@mui/styles/createStyles';
25 import AppBar from '@mui/material/AppBar';
26 import Toolbar from '@mui/material/Toolbar';
27 import Typography from '@mui/material/Typography';
28 import Button from '@mui/material/Button';
29 import IconButton from '@mui/material/IconButton';
30 import AccountCircle from '@mui/icons-material/AccountCircle';
31 import MenuItem from '@mui/material/MenuItem';
32 import Menu from '@mui/material/Menu';
34 import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
35 import { faBan } from '@fortawesome/free-solid-svg-icons';
36 import { faDotCircle } from '@fortawesome/free-solid-svg-icons';
38 import { logoutUser } from '../actions/authentication';
39 import { PushAction, ReplaceAction } from '../actions/navigationActions';
41 import { connect, Connect, IDispatcher } from '../flux/connect';
42 import { MenuAction, MenuClosedByUser } from '../actions/menuAction';
44 import MenuIcon from './icons/menuIcon';
45 import Logo from './logo';
47 const styles = (theme: Theme) => createStyles({
49 zIndex: theme.zIndex.drawer + 1,
74 const mapDispatch = (dispatcher: IDispatcher) => {
77 dispatcher.dispatch(logoutUser());
78 dispatcher.dispatch(new ReplaceAction("/login"));
81 dispatcher.dispatch(new PushAction("/settings"));
83 toggleMainMenu: (value: boolean, value2: boolean) => {
84 dispatcher.dispatch(new MenuAction(value));
85 dispatcher.dispatch(new MenuClosedByUser(value2))
90 type TitleBarProps = RouteComponentProps<{}> & WithStyles<typeof styles> & Connect<undefined, typeof mapDispatch>
92 class TitleBarComponent extends React.Component<TitleBarProps, { anchorEl: HTMLElement | null }> {
94 constructor(props: TitleBarProps) {
101 render(): JSX.Element {
102 const { classes, state, history, location } = this.props;
103 const open = !!this.state.anchorEl;
104 let toolbarElements: Array<JSX.Element>;
105 toolbarElements = [];
107 // create notificationInfo element
108 const notificationInfo = state.framework.applicationState.isWebsocketAvailable != undefined ?
109 (state.framework.applicationState.isWebsocketAvailable ?
110 <Typography aria-label="notifications-are-active" variant="body1" className={classes.notificationInfo}>Notifications <FontAwesomeIcon className={classes.connected} icon={faDotCircle} /> |</Typography> : <Typography aria-label="notifications-are-inactive" variant="body1" className={classes.notificationInfo}>Notifications <FontAwesomeIcon className={classes.notConnected} icon={faBan} /> |</Typography>)
111 : <Typography variant="body1" aria-label="notifications-are-not-available" className={classes.notificationInfo}>Notifications N/A |</Typography>;
114 // add notificationInfo element before help
115 if (state.framework.applicationRegistration) {
116 let isNotificationInfoAdded = false;
117 Object.keys(state.framework.applicationRegistration).map(key => {
118 const reg = state.framework.applicationRegistration[key];
119 if (reg && reg.statusBarElement) {
120 if (key === "help") {
121 isNotificationInfoAdded = true;
122 toolbarElements.push(notificationInfo);
124 toolbarElements.push(<reg.statusBarElement key={key} />);
128 // add notificationInfo in case help wasn't found
129 if (!isNotificationInfoAdded) {
130 toolbarElements.push(notificationInfo);
134 const stateIcon = state.framework.applicationState.icon;
135 const customIconHeight = 22;
136 const icon = !stateIcon
138 : (typeof stateIcon === 'string'
139 ? <img className={classes.icon} height={customIconHeight} src={stateIcon} />
140 : <FontAwesomeIcon className={classes.icon} icon={stateIcon} />)
144 <AppBar enableColorOnDark position="absolute" className={classes.appBar}>
147 className={classes.menuButton}
150 onClick={this.toggleMainMenu}
155 <Typography variant="h6" color="inherit" >
157 {state.framework.applicationState.title}
159 <div className={classes.grow}></div>
162 toolbarElements.map((item) => {
167 {state.framework.authenticationState.user
169 <Button aria-label="current user menu button"
170 aria-owns={open ? 'menu-appbar' : undefined}
172 onClick={this.openMenu}
176 {state.framework.authenticationState.user.user}
180 anchorEl={this.state.anchorEl}
190 onClose={this.closeMenu}
192 {/* <MenuItem onClick={ this.closeMenu }>Profile</MenuItem> */}
194 aria-label="settings-button"
196 this.props.openSettings();
197 this.closeMenu(); }}>Settings</MenuItem>
199 aria-label="logout-button"
206 : (<Button onClick={() => { history.push('/login') }} color="inherit" disabled={location.pathname == "/login"}>Login</Button>)}
212 private toggleMainMenu = (event: React.MouseEvent<HTMLElement>) => {
213 console.log(this.props);
214 if (this.props.state.framework.authenticationState.user && this.props.state.framework.authenticationState.user.isValid) {
215 const isMainMenuOpen = this.props.state.framework.applicationState.isMenuOpen
216 const isClosedByUser = this.props.state.framework.applicationState.isMenuClosedByUser
217 this.props.toggleMainMenu(!isMainMenuOpen, !isClosedByUser);
221 private openMenu = (event: React.MouseEvent<HTMLElement>) => {
222 this.setState({ anchorEl: event.currentTarget });
225 private closeMenu = () => {
226 this.setState({ anchorEl: null });
230 //todo: ggf. https://github.com/acdlite/recompose verwenden zur Vereinfachung
232 export const TitleBar = withStyles(styles)(withRouter(connect(undefined, mapDispatch)(TitleBarComponent)));
233 export default TitleBar;