1 import * as React from 'react';
2 import { Theme, createStyles, WithStyles, withStyles, Typography, FormControlLabel, Checkbox } from '@material-ui/core';
4 import Button from '@material-ui/core/Button';
5 import TextField from '@material-ui/core/TextField';
6 import Select from '@material-ui/core/Select';
7 import Dialog from '@material-ui/core/Dialog';
8 import DialogActions from '@material-ui/core/DialogActions';
9 import DialogContent from '@material-ui/core/DialogContent';
10 import DialogContentText from '@material-ui/core/DialogContentText';
11 import DialogTitle from '@material-ui/core/DialogTitle';
13 import Tabs from '@material-ui/core/Tabs';
14 import Tab from '@material-ui/core/Tab';
16 import Fab from '@material-ui/core/Fab';
17 import AddIcon from '@material-ui/icons/Add';
18 import DeleteIcon from '@material-ui/icons/Delete';
19 import IconButton from '@material-ui/core/IconButton';
21 import { addMediatorConfigAsyncActionCreator, updateMediatorConfigAsyncActionCreator, removeMediatorConfigAsyncActionCreator } from '../actions/mediatorConfigActions';
22 import { MediatorConfig, ODLConfig } from '../models/mediatorServer';
23 import FormControl from '@material-ui/core/FormControl';
24 import InputLabel from '@material-ui/core/InputLabel';
25 import MenuItem from '@material-ui/core/MenuItem';
27 import { Panel } from '../../../../framework/src/components/material-ui/panel';
29 import { IDispatcher, connect, Connect } from '../../../../framework/src/flux/connect';
30 import { IApplicationStoreState } from '../../../../framework/src/store/applicationStore';
33 const styles = (theme: Theme) => createStyles({
36 flexDirection: 'column',
41 bottom: theme.spacing.unit,
42 right: theme.spacing.unit,
51 justifyContent: "center",
58 marginRight: theme.spacing.unit,
65 const TabContainer: React.SFC = ({ children }) => {
67 <div style={{ width: "430px", height: "530px", position: "relative", display: 'flex', flexDirection: 'column' }}>
73 export enum EditMediatorConfigDialogMode {
75 AddMediatorConfig = "addMediatorConfig",
76 EditMediatorConfig = "editMediatorConfig",
77 RemoveMediatorConfig = "removeMediatorConfig",
80 const mapProps = (state: IApplicationStoreState) => ({
81 supportedDevices: state.mediatorApp.mediatorServerState.supportedDevices
84 const mapDispatch = (dispatcher: IDispatcher) => ({
85 addMediatorConfig: (config: MediatorConfig) => {
86 dispatcher.dispatch(addMediatorConfigAsyncActionCreator(config));
88 updateMediatorConfig: (config: MediatorConfig) => {
89 dispatcher.dispatch(updateMediatorConfigAsyncActionCreator(config));
91 removeMediatorConfig: (config: MediatorConfig) => {
92 dispatcher.dispatch(removeMediatorConfigAsyncActionCreator(config));
96 type DialogSettings = {
98 dialogDescription: string;
99 applyButtonText: string;
100 cancelButtonText: string;
102 readonlyName: boolean;
105 const settings: { [key: string]: DialogSettings } = {
106 [EditMediatorConfigDialogMode.None]: {
108 dialogDescription: "",
110 cancelButtonText: "",
114 [EditMediatorConfigDialogMode.AddMediatorConfig]: {
115 dialogTitle: "Add Medator Configuration",
116 dialogDescription: "",
117 applyButtonText: "Add",
118 cancelButtonText: "Cancel",
122 [EditMediatorConfigDialogMode.EditMediatorConfig]: {
123 dialogTitle: "Edit Medator Configuration",
124 dialogDescription: "",
125 applyButtonText: "Update",
126 cancelButtonText: "Cancel",
130 [EditMediatorConfigDialogMode.RemoveMediatorConfig]: {
131 dialogTitle: "Remove Mediator Configuration",
132 dialogDescription: "",
133 applyButtonText: "Remove",
134 cancelButtonText: "Cancel",
140 type EditMediatorConfigDialogComponentProps = WithStyles<typeof styles> & Connect<typeof mapProps, typeof mapDispatch> & {
141 mode: EditMediatorConfigDialogMode;
142 mediatorConfig: MediatorConfig;
146 type EditMediatorConfigDialogComponentState = MediatorConfig & { activeTab: number; activeOdlConfig: string };
148 class EditMediatorConfigDialogComponent extends React.Component<EditMediatorConfigDialogComponentProps, EditMediatorConfigDialogComponentState> {
149 constructor (props: EditMediatorConfigDialogComponentProps) {
153 ...this.props.mediatorConfig,
159 private odlConfigValueChangeHandlerCreator = <THtmlElement extends HTMLElement, TKey extends keyof ODLConfig>(index: number, property: TKey, mapValue: (event: React.ChangeEvent<THtmlElement>) => any) => (event: React.ChangeEvent<THtmlElement>) => {
160 event.stopPropagation();
161 event.preventDefault();
164 ...this.state.ODLConfig.slice(0, index),
165 { ...this.state.ODLConfig[index], [property]: mapValue(event) },
166 ...this.state.ODLConfig.slice(index + 1)
171 render(): JSX.Element {
172 const setting = settings[this.props.mode];
173 const { classes } = this.props;
175 <Dialog open={this.props.mode !== EditMediatorConfigDialogMode.None}>
176 <DialogTitle id="form-dialog-title">{setting.dialogTitle}</DialogTitle>
179 {setting.dialogDescription}
181 <Tabs value={this.state.activeTab} indicatorColor="primary" textColor="primary" onChange={(event, value) => this.setState({ activeTab: value })} >
182 <Tab label="Config" />
183 <Tab label="ODL AutoConnect" />
185 { this.state.activeTab === 0 ? <TabContainer >
186 <TextField disabled={setting.readonly || setting.readonlyName} spellCheck={false} autoFocus margin="dense" id="name" label="Name" type="text" fullWidth value={this.state.Name} onChange={(event) => { this.setState({ Name: event.target.value }); }} />
187 <FormControl fullWidth disabled={setting.readonly}>
188 <InputLabel htmlFor="deviceType">Device</InputLabel>
189 <Select value={this.state.DeviceType} onChange={(event) => {
190 const device = this.props.supportedDevices.find(device => device.id === +event.target.value);
193 DeviceType: device.id,
194 NeXMLFile: device.xml
202 }} inputProps={{ name: 'deviceType', id: 'deviceType' }} fullWidth >
203 <MenuItem value={-1}><em>None</em></MenuItem>
204 {this.props.supportedDevices.map(device => (<MenuItem key={device.id} value={device.id} >{`${device.vendor} - ${device.device} (${device.version || '0.0.0'}) `}</MenuItem>))}
207 <TextField disabled={setting.readonly} spellCheck={false} autoFocus margin="dense" id="ipAddress" label="IP Address" type="text" fullWidth value={this.state.DeviceIp} onChange={(event) => { this.setState({ DeviceIp: event.target.value }); }} />
208 <TextField disabled={setting.readonly} spellCheck={false} autoFocus margin="dense" id="devicePort" label="Port" type="number" fullWidth value={this.state.DevicePort || ""} onChange={(event) => { this.setState({ DevicePort: +event.target.value }); }} />
209 <TextField disabled={setting.readonly} spellCheck={false} autoFocus margin="dense" id="trapsPort" label="TrapsPort" type="number" fullWidth value={this.state.TrapPort || ""} onChange={(event) => { this.setState({ TrapPort: +event.target.value }); }} />
210 <TextField disabled={setting.readonly} spellCheck={false} autoFocus margin="dense" id="ncUser" label="Netconf User" type="text" fullWidth value={this.state.NcUsername} onChange={(event) => { this.setState({ NcUsername: event.target.value }); }} />
211 <TextField disabled={setting.readonly} spellCheck={false} autoFocus margin="dense" id="ncPassword" label="Netconf Password" type="password" fullWidth value={this.state.NcPassword} onChange={(event) => { this.setState({ NcPassword: event.target.value }); }} />
212 <TextField disabled={setting.readonly} spellCheck={false} autoFocus margin="dense" id="ncPort" label="Netconf Port" type="number" fullWidth value={this.state.NcPort || ""} onChange={(event) => { this.setState({ NcPort: +event.target.value }); }} />
213 </TabContainer> : null}
214 { this.state.activeTab === 1 ? <TabContainer >
215 { this.state.ODLConfig && this.state.ODLConfig.length > 0
216 ? this.state.ODLConfig.map((cfg, ind) => {
217 const panelId = `panel-${ind}`;
218 const deleteButton = (<IconButton onClick={() => {
221 ...this.state.ODLConfig.slice(0, ind),
222 ...this.state.ODLConfig.slice(ind + 1)
225 }} ><DeleteIcon /></IconButton>)
227 <Panel title={cfg.Server && `${cfg.User ? `${cfg.User}@` : ''}${cfg.Protocol}://${cfg.Server}:${cfg.Port}` || "new odl config"} key={panelId} panelId={panelId} activePanel={this.state.activeOdlConfig} customActionButtons={[deleteButton]}
228 onToggle={(id) => this.setState({ activeOdlConfig: (this.state.activeOdlConfig === id) ? "" : (id || "") })} >
229 <div className={classes.alignInOneLine}>
230 <FormControl className={classes.left} margin={"dense"} >
231 <InputLabel htmlFor={`protocol-${ind}`}>Protocoll</InputLabel>
232 <Select value={cfg.Protocol} onChange={ this.odlConfigValueChangeHandlerCreator(ind, "Protocol", e => (e.target.value)) } inputProps={{ name: `protocol-${ind}`, id: `protocol-${ind}` }} fullWidth >
233 <MenuItem value={"http"}>http</MenuItem>
234 <MenuItem value={"https"}>https</MenuItem>
237 <TextField className={classes.left} spellCheck={false} margin="dense" id="hostname" label="Hostname" type="text" value={cfg.Server} onChange={this.odlConfigValueChangeHandlerCreator(ind, "Server", e => e.target.value)} />
238 <TextField className={classes.right} style={{ maxWidth: "65px"}} spellCheck={false} margin="dense" id="port" label="Port" type="number" value={cfg.Port|| ""} onChange={this.odlConfigValueChangeHandlerCreator(ind, "Port", e => +e.target.value)} />
240 <div className={classes.alignInOneLine}>
241 <TextField className={classes.left} spellCheck={false} margin="dense" id="username" label="Username" type="text" value={cfg.User} onChange={this.odlConfigValueChangeHandlerCreator(ind, "User", e => e.target.value)} />
242 <TextField className={classes.right} spellCheck={false} margin="dense" id="password" label="Password" type="password" value={cfg.Password} onChange={this.odlConfigValueChangeHandlerCreator(ind, "Password", e => e.target.value)} />
244 <div className={classes.alignInOneLine}>
245 <FormControlLabel className={classes.right} control={<Checkbox checked={cfg.Trustall} onChange={this.odlConfigValueChangeHandlerCreator(ind, "Trustall", e => e.target.checked)} />} label="Trustall" />
250 : <div className={classes.center} >
251 <Typography component={"div"} className={classes.title} color="textSecondary" gutterBottom>Please add an ODL auto connect configuration.</Typography>
254 <Fab className={classes.fab} color="primary" aria-label="Add" onClick={() => this.setState({
255 ODLConfig: [...this.state.ODLConfig, { Server: '', Port: 8181, Protocol: 'https', User: 'admin', Password: 'admin', Trustall: false }]
256 })} > <AddIcon /> </Fab>
258 </TabContainer> : null}
262 <Button onClick={(event) => {
263 this.onApply(Object.keys(this.state).reduce<MediatorConfig & { [kex: string]: any }>((acc, key) => {
264 // do not copy activeTab and activeOdlConfig
265 if (key !== "activeTab" && key !== "activeOdlConfig" && key !== "_initialMediatorConfig") acc[key] = (this.state as any)[key];
267 }, {} as MediatorConfig));
268 event.preventDefault();
269 event.stopPropagation();
270 }} > {setting.applyButtonText} </Button>
271 <Button onClick={(event) => {
273 event.preventDefault();
274 event.stopPropagation();
275 }} color="secondary"> {setting.cancelButtonText} </Button>
281 private onApply = (config: MediatorConfig) => {
282 this.props.onClose && this.props.onClose();
283 switch (this.props.mode) {
284 case EditMediatorConfigDialogMode.AddMediatorConfig:
285 config && this.props.addMediatorConfig(config);
287 case EditMediatorConfigDialogMode.EditMediatorConfig:
288 config && this.props.updateMediatorConfig(config);
290 case EditMediatorConfigDialogMode.RemoveMediatorConfig:
291 config && this.props.removeMediatorConfig(config);
296 private onCancel = () => {
297 this.props.onClose && this.props.onClose();
300 static getDerivedStateFromProps(props: EditMediatorConfigDialogComponentProps, state: EditMediatorConfigDialogComponentState & { _initialMediatorConfig: MediatorConfig }): EditMediatorConfigDialogComponentState & { _initialMediatorConfig: MediatorConfig } {
301 if (props.mediatorConfig !== state._initialMediatorConfig) {
304 ...props.mediatorConfig,
305 _initialMediatorConfig: props.mediatorConfig,
312 export const EditMediatorConfigDialog = withStyles(styles)(connect(mapProps, mapDispatch)(EditMediatorConfigDialogComponent));
313 export default EditMediatorConfigDialog;