Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / framework / src / components / navigationMenu.tsx
1 /**
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 import React from 'react';
19 import { Theme } from '@mui/material/styles';
20 import classNames from 'classnames';
21
22 import { WithStyles } from '@mui/styles';
23 import withStyles from '@mui/styles/withStyles';
24 import createStyles from '@mui/styles/createStyles';
25
26 import Drawer from '@mui/material/Drawer';
27 import List from '@mui/material/List';
28
29 import Divider from '@mui/material/Divider';
30
31 import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
32 import { faProjectDiagram } from '@fortawesome/free-solid-svg-icons';
33
34 import ListItemLink from '../components/material-ui/listItemLink';
35
36 import { connect, Connect } from '../flux/connect';
37 import { MenuAction } from '../actions/menuAction';
38 import { transportPCEUrl } from '../app';
39
40 const aboutIcon = require('../assets/icons/About.svg');
41 const homeIcon = require('../assets/icons/Home.svg');
42 const loginIcon = require('../assets/icons/User.svg');
43 const settingsIcon = require('../assets/icons/Tools.svg');
44
45 const drawerWidth = 240;
46
47 const extraLinks = (window as any)._odluxExtraLinks as [string, string][];
48
49 const styles = (theme: Theme) => createStyles({
50   drawerPaper: {
51     position: 'relative',
52     width: drawerWidth,
53   },
54   toolbar: theme.mixins.toolbar as any,
55
56   drawerOpen: {
57     width: drawerWidth,
58     transition: theme.transitions.create('width', {
59       easing: theme.transitions.easing.sharp,
60       duration: theme.transitions.duration.enteringScreen,
61     }),
62   },
63   drawerClose: {
64     transition: theme.transitions.create('width', {
65       easing: theme.transitions.easing.sharp,
66       duration: theme.transitions.duration.leavingScreen,
67     }),
68     overflowX: 'hidden',
69     width: theme.spacing(7),
70     [theme.breakpoints.up('sm')]: {
71       width: theme.spacing(9),
72     },
73   },
74   drawer: {
75
76   },
77   menu: {
78     flex: "1 0 0%",
79   },
80   optLinks: {
81     borderTop: "2px solid #cfcfcf",
82     display: "flex",
83     flexDirection: "row",
84     flexWrap: "wrap",
85     justifyContent: "space-around"
86   },
87   link: {
88     margin: theme.spacing(1)+1,
89     fontSize: theme.typography.fontSize-2,
90   },
91 });
92
93 const tabletWidthBreakpoint = 768;
94
95 export const NavigationMenu = withStyles(styles)(connect()(({ classes, state, dispatch }: WithStyles<typeof styles> & Connect & Connect) => {
96   const { user } = state.framework.authenticationState;
97   const isOpen = state.framework.applicationState.isMenuOpen;
98   const closedByUser = state.framework.applicationState.isMenuClosedByUser;
99   const transportUrl = state.framework.applicationState.transportpceUrl;
100
101   const [responsive, setResponsive] = React.useState(false);
102
103   //collapse menu on mount if necessary
104   React.useEffect(()=>{
105
106     if(isOpen && window.innerWidth <= tabletWidthBreakpoint){
107
108       setResponsive(true);
109       dispatch(new MenuAction(false));
110     }
111
112   },[]);
113
114   React.useEffect(() => {
115
116     function handleResize() {
117       if (user && user.isValid) {
118         if (window.innerWidth < tabletWidthBreakpoint && !responsive) {
119           setResponsive(true);
120           if (!closedByUser) {
121             dispatch(new MenuAction(false));
122           }
123
124         } else if (window.innerWidth > tabletWidthBreakpoint && responsive) {
125           setResponsive(false);
126           if (!closedByUser) {
127             dispatch(new MenuAction(true));
128           }
129
130         }
131       }
132     }
133     window.addEventListener("resize", handleResize);
134
135
136     return () => {
137       window.removeEventListener("resize", handleResize);
138     }
139   })
140
141   React.useEffect(()=>{
142     // trigger a resize if menu changed in case elements have to re-arrange
143     window.dispatchEvent(new Event('menu-resized'));
144   }, [isOpen])
145
146   let menuItems = state.framework.applicationRegistration && Object.keys(state.framework.applicationRegistration).map(key => {
147     const reg = state.framework.applicationRegistration[key];
148     return reg && (
149       <ListItemLink
150         key={reg.name}
151         to={reg.path || `/${reg.name}`}
152         primary={reg.menuEntry || reg.name}
153         secondary={reg.subMenuEntry}
154         icon={reg.icon || null} />
155     ) || null;
156   }) || null;
157
158   if(transportUrl.length>0){
159
160     const transportPCELink = <ListItemLink
161       key={"transportPCE"}
162       to={transportUrl}
163       primary={"TransportPCE"}
164       icon={faProjectDiagram}
165       external />;
166
167     const linkFound = menuItems.find(obj => obj.key === "microwave");
168     
169     if (linkFound) {
170       const index = menuItems.indexOf(linkFound);
171       menuItems.splice(index + 1, 0, transportPCELink);
172     } else {
173       menuItems.push(transportPCELink);
174     }
175   }
176   
177
178   return (
179     <Drawer
180       variant="permanent"
181       className={
182         classNames(classes.drawer, {
183           [classes.drawerOpen]: isOpen,
184           [classes.drawerClose]: !isOpen
185         })
186       }
187       classes={{
188         paper: classes.drawerPaper,
189       }}
190     >
191       {user && user.isValid && <>
192         <div className={classes.toolbar} />
193         { /* https://fiffty.github.io/react-treeview-mui/ */}
194         <List className={classes.menu} component="nav">
195           <ListItemLink exact to="/" primary="Home" icon={homeIcon} />
196           <Divider />
197           {
198           menuItems
199           }
200           <Divider />
201           <ListItemLink to="/about" primary="About" icon={aboutIcon} />
202           {(false && process.env.NODE_ENV === "development")
203             ? <>
204               <Divider />
205               <ListItemLink to="/test" primary="Test" icon={settingsIcon} />
206             </>
207             : null
208           }
209         </List>
210         {isOpen && extraLinks && <div className={classes.optLinks}>
211           {extraLinks.map(linkInfo => (<a className={classes.link} href={linkInfo[1]}>{linkInfo[0]}</a>))}
212         </div> || null}
213       </> || null
214       }
215     </Drawer>)
216 }));
217
218 export default NavigationMenu;