|
| 1 | +import React, { Component } from "react"; |
| 2 | +import Paper from "@material-ui/core/Paper"; |
| 3 | +import ConfigListView from "./ConfigListView/ConfigListView"; |
| 4 | +import FCConnector from "../utilities/FCConnector"; |
| 5 | +import AssistantView from "./Assistants/AssistantView"; |
| 6 | +import "./Connected.css"; |
| 7 | +import { FCConfigContext } from "../App"; |
| 8 | +import ResponsiveDrawerView from "./ResponsiveDrawerView"; |
| 9 | +import Typography from "@material-ui/core/Typography"; |
| 10 | + |
| 11 | +export default class Connected extends Component { |
| 12 | + constructor(props) { |
| 13 | + super(props); |
| 14 | + this.routes = props.fcConfig.routes; |
| 15 | + this.routeFeatures = props.fcConfig.routeFeatures; |
| 16 | + this.state = { |
| 17 | + theme: props.theme, |
| 18 | + isBxF: props.fcConfig.isBxF, |
| 19 | + pid_profile: props.fcConfig.currentPidProfile, |
| 20 | + rate_profile: props.fcConfig.currentRateProfile, |
| 21 | + craftName: props.fcConfig.name, |
| 22 | + isDirty: false, |
| 23 | + mobileOpen: false, |
| 24 | + currentRoute: props.fcConfig.startingRoute |
| 25 | + }; |
| 26 | + } |
| 27 | + |
| 28 | + getRouteFeatures(key) { |
| 29 | + if (this.routeFeatures[key]) { |
| 30 | + return this.props.fcConfig.features.values |
| 31 | + .filter(feat => { |
| 32 | + return this.routeFeatures[key].indexOf(feat.id) > -1; |
| 33 | + }) |
| 34 | + .map(feature => { |
| 35 | + if (feature.hasPort) { |
| 36 | + feature.ports = this.props.fcConfig.ports; |
| 37 | + } |
| 38 | + return feature; |
| 39 | + }); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + getRouteItems = (fcConfig, sort = false) => { |
| 44 | + let keys = Object.keys(fcConfig); |
| 45 | + if (sort) { |
| 46 | + keys.sort(); |
| 47 | + } |
| 48 | + return keys |
| 49 | + .filter(key => { |
| 50 | + if (this.state.filterOn) { |
| 51 | + return ( |
| 52 | + fcConfig[key].id && |
| 53 | + fcConfig[key].id.indexOf(this.state.filterOn) > -1 |
| 54 | + ); |
| 55 | + } |
| 56 | + if (this.state.currentRoute.key === "ADVANCED") { |
| 57 | + return fcConfig[key].id && !fcConfig[key].route; |
| 58 | + } |
| 59 | + return fcConfig[key].route === this.state.currentRoute.key; |
| 60 | + }) |
| 61 | + .map(k => fcConfig[k]); |
| 62 | + }; |
| 63 | + |
| 64 | + handleDrawerToggle = () => { |
| 65 | + if (this.state.mobileOpen) { |
| 66 | + FCConnector.pauseTelemetry(); |
| 67 | + } else { |
| 68 | + FCConnector.resumeTelemetry(); |
| 69 | + } |
| 70 | + this.setState({ mobileOpen: !this.state.mobileOpen }); |
| 71 | + }; |
| 72 | + |
| 73 | + handleClickAway = () => { |
| 74 | + if (this.state.mobileOpen) { |
| 75 | + this.setState({ mobileOpen: false }); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + handleSearch = event => { |
| 80 | + this.setState({ filterOn: event.target.value }); |
| 81 | + }; |
| 82 | + |
| 83 | + handleSave = () => { |
| 84 | + this.setState({ isDirty: false }); |
| 85 | + return this.props.handleSave().then(config => { |
| 86 | + this.setState({ fcConfig: config }); |
| 87 | + }); |
| 88 | + }; |
| 89 | + |
| 90 | + handleMenuItemClick = key => { |
| 91 | + let newRoute = this.routes.find(route => route.key === key); |
| 92 | + if (this.state.isDirty) { |
| 93 | + // this.handleSave(); |
| 94 | + //TODO: save EEPROM |
| 95 | + } |
| 96 | + this.setState({ |
| 97 | + filterOn: undefined, |
| 98 | + mobileOpen: false, |
| 99 | + currentRoute: newRoute |
| 100 | + }); |
| 101 | + }; |
| 102 | + |
| 103 | + notifyDirty = (isDirty, item, newValue) => { |
| 104 | + if (isDirty) { |
| 105 | + this.setState({ isDirty: isDirty }); |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + openAssistant(routeName) { |
| 110 | + FCConnector.pauseTelemetry(); |
| 111 | + this.setState({ openAssistant: true, assistantType: routeName }); |
| 112 | + } |
| 113 | + closeAssistant() { |
| 114 | + this.setState({ openAssistant: false, assistantType: "" }); |
| 115 | + FCConnector.resumeTelemetry(); |
| 116 | + } |
| 117 | + render() { |
| 118 | + let contents; |
| 119 | + let mergedProfile = this.props.fcConfig; |
| 120 | + switch (this.state.currentRoute.key) { |
| 121 | + case "PFC": |
| 122 | + contents = ( |
| 123 | + <Paper> |
| 124 | + <div> |
| 125 | + <Typography variant="h5">IMUF only version</Typography> |
| 126 | + </div> |
| 127 | + </Paper> |
| 128 | + ); |
| 129 | + break; |
| 130 | + default: |
| 131 | + contents = ( |
| 132 | + <ConfigListView |
| 133 | + fcConfig={mergedProfile} |
| 134 | + features={this.getRouteFeatures(this.state.currentRoute.key)} |
| 135 | + notifyDirty={(isDirty, item, newValue) => |
| 136 | + this.notifyDirty(isDirty, item, newValue) |
| 137 | + } |
| 138 | + items={this.getRouteItems(mergedProfile, true)} |
| 139 | + /> |
| 140 | + ); |
| 141 | + break; |
| 142 | + } |
| 143 | + |
| 144 | + return ( |
| 145 | + <Paper className={`connected-root ${mergedProfile.version.fw}`}> |
| 146 | + <FCConfigContext.Provider value={mergedProfile}> |
| 147 | + <ResponsiveDrawerView |
| 148 | + routes={this.routes} |
| 149 | + goToImuf={this.props.goToImuf} |
| 150 | + fcConfig={mergedProfile} |
| 151 | + mobileOpen={this.state.mobileOpen} |
| 152 | + onClose={() => { |
| 153 | + this.setState({ mobileOpen: false }); |
| 154 | + }} |
| 155 | + handleMenuItemClick={this.handleMenuItemClick} |
| 156 | + handleClickAway={this.handleClickAway} |
| 157 | + appVersion={this.props.appVersion} |
| 158 | + /> |
| 159 | + {contents} |
| 160 | + </FCConfigContext.Provider> |
| 161 | + </Paper> |
| 162 | + ); |
| 163 | + } |
| 164 | +} |
0 commit comments