|
| 1 | +import React, { useState } from 'react' |
| 2 | +import { makeStyles } from '@material-ui/core/styles'; |
| 3 | +import Drawer from '@material-ui/core/Drawer'; |
| 4 | + |
| 5 | +import IconButton from '@material-ui/core/IconButton' |
| 6 | +import Toolbar from '@material-ui/core/Toolbar'; |
| 7 | +import List from '@material-ui/core/List'; |
| 8 | + |
| 9 | +import ListItem from '@material-ui/core/ListItem'; |
| 10 | +import ListItemIcon from '@material-ui/core/ListItemIcon'; |
| 11 | +import ListItemText from '@material-ui/core/ListItemText'; |
| 12 | + |
| 13 | +import ChevronRightIcon from '@material-ui/icons/ChevronRight'; |
| 14 | +import ChevronLeftIcon from '@material-ui/icons/ChevronLeft'; |
| 15 | +import Typography from '@material-ui/core/Typography'; |
| 16 | + |
| 17 | +import AdjustIcon from '@material-ui/icons/Adjust'; |
| 18 | +import { |
| 19 | + HLS_WIDGETS, PYTHON_CONSOLE_WIDGET, |
| 20 | + FLEXLAYOUT_DEFAULT_STATE, MORPHOLOGY_WIDGET |
| 21 | +} from '../../redux/reducers/flexlayout' |
| 22 | + |
| 23 | + |
| 24 | +const drawerOpenWidth = 200; |
| 25 | +const drawerCloseWidth = 54; |
| 26 | + |
| 27 | +const createTransition = (entering, transitions) => ( |
| 28 | + transitions.create('width', { |
| 29 | + easing: transitions.easing.sharp, |
| 30 | + duration: entering ? transitions.duration.enteringScreen : transitions.duration.leavingScreen, |
| 31 | + }) |
| 32 | +) |
| 33 | + |
| 34 | +const drawerCss = (entering, transitions, palette) => ({ |
| 35 | + overflow: 'hidden', |
| 36 | + width: props => props.width, |
| 37 | + flexShrink: 0, |
| 38 | + backgroundColor: props => props.dark ? palette.grey[900] : palette.grey[800], |
| 39 | + borderRight: 'none', |
| 40 | + transition: transitions.create('width', { |
| 41 | + easing: transitions.easing.sharp, |
| 42 | + duration: entering ? transitions.duration.enteringScreen : transitions.duration.leavingScreen, |
| 43 | + }) |
| 44 | +}) |
| 45 | + |
| 46 | +const useStyles = makeStyles(({ transitions, palette }) => ({ |
| 47 | + openDrawer: drawerCss(true, transitions, palette), |
| 48 | + |
| 49 | + closeDrawer: drawerCss(false, transitions, palette), |
| 50 | + |
| 51 | + colapse: { |
| 52 | + color: 'white', |
| 53 | + position: 'absolute', |
| 54 | + bottom: 0, |
| 55 | + right: 2 |
| 56 | + }, |
| 57 | + paper: { backgroundColor: palette.grey['900'] }, |
| 58 | + |
| 59 | + selected: { height: 60, color: palette.primary.main }, |
| 60 | + unselected: { height: 60, color: palette.common.white }, |
| 61 | + noColor: { color: 'inherit' } |
| 62 | +})) |
| 63 | + |
| 64 | +export default ({ widgets, newWidget, editMode }) => { |
| 65 | + const [expand, setExpand] = useState({ dark: !editMode }) |
| 66 | + |
| 67 | + const classes = useStyles({ dark: !editMode, width: expand ? drawerOpenWidth : drawerCloseWidth }); |
| 68 | + |
| 69 | + function createWidget (widgetId) { |
| 70 | + if (!widgets[widgetId]) { |
| 71 | + let widget = HLS_WIDGETS[widgetId] |
| 72 | + if (!editMode) { |
| 73 | + widget = MORPHOLOGY_WIDGET |
| 74 | + } |
| 75 | + |
| 76 | + newWidget({ path: widget.id, ...widget }) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + function getMenu () { |
| 81 | + if (editMode) { |
| 82 | + return Object.values({ ...HLS_WIDGETS, python: PYTHON_CONSOLE_WIDGET }) |
| 83 | + } else { |
| 84 | + return Object.values({ ...FLEXLAYOUT_DEFAULT_STATE.widgetsBackground, python: PYTHON_CONSOLE_WIDGET }) |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + return ( |
| 90 | + <div> |
| 91 | + <Drawer |
| 92 | + variant="permanent" |
| 93 | + className={expand ? classes.openDrawer : classes.closeDrawer} |
| 94 | + classes={{ paper: expand ? classes.openDrawer : classes.closeDrawer }} |
| 95 | + > |
| 96 | + <div> |
| 97 | + <Toolbar/> |
| 98 | + <List> |
| 99 | + {getMenu().map(({ name, id }) => ( |
| 100 | + <ListItem |
| 101 | + button |
| 102 | + key={name} |
| 103 | + className={widgets[id] ? classes.selected : classes.unselected} |
| 104 | + onClick={() => createWidget(id)} |
| 105 | + > |
| 106 | + <ListItemIcon className={classes.noColor}> |
| 107 | + <AdjustIcon/> |
| 108 | + </ListItemIcon> |
| 109 | + <ListItemText> |
| 110 | + <Typography noWrap>{name}</Typography> |
| 111 | + </ListItemText> |
| 112 | + </ListItem> |
| 113 | + ))} |
| 114 | + </List> |
| 115 | + |
| 116 | + |
| 117 | + <IconButton |
| 118 | + className={classes.colapse} |
| 119 | + onClick={() => setExpand(!expand)} |
| 120 | + > |
| 121 | + {expand ? <ChevronLeftIcon/> : <ChevronRightIcon/>} |
| 122 | + </IconButton> |
| 123 | + |
| 124 | + |
| 125 | + </div> |
| 126 | + |
| 127 | + </Drawer> |
| 128 | + </div> |
| 129 | + ) |
| 130 | + |
| 131 | +} |
0 commit comments