WIP: Refactor code

This commit is contained in:
uwap 2017-11-07 06:02:01 +01:00
parent 19b91cfd0a
commit 6a3bd14343
7 changed files with 245 additions and 87 deletions

63
src/components/App.js Normal file
View file

@ -0,0 +1,63 @@
// @flow
import React from "react";
import MuiThemeProvider from "material-ui/styles/MuiThemeProvider";
import createMuiTheme from "material-ui/styles/createMuiTheme";
import withStyles from "material-ui/styles/withStyles";
import * as Colors from "material-ui/colors";
import SideBar from "components/SideBar";
import ControlMap from "components/ControlMap";
export type AppProps = {
config: Config
};
export type AppState = {
selectedControl: string
};
class App extends React.Component<AppProps & Classes> {
constructor(props: AppProps & Classes) {
super(props);
}
static styles(_theme: Object) {
return {
drawerPaper: {
width: 320
}
};
}
get theme() {
return createMuiTheme({
palette: {
primary: Colors[this.props.config.space.color]
}
});
}
get selectedControl(): Control {
return this.props.config.controls[this.state.selectedControl];
}
// <SpaceMapBar title={`${this.props.config.space.name} Map`} />
render() {
return (
<div>
<MuiThemeProvider theme={this.theme}>
<div>
{false && <SideBar />}
</div>
</MuiThemeProvider>
<ControlMap width={1000} height={700} zoom={0}
layers={this.props.config.layers}
controls={this.props.config.controls}
/>
</div>
);
}
}
export default withStyles(App.styles)(App);

View file

@ -0,0 +1,86 @@
// @flow
import React from "react";
import { Map, ImageOverlay, Marker, LayersControl } from "react-leaflet";
import Leaflet from "leaflet";
export type Point = [number, number];
const convertPoint = (p: Point) => [-p[1], p[0]];
export type ControlMapProps = {
width: number,
height: number,
zoom: number,
layers: Array<Layer>,
controls: Controls
};
export default class ControlMap extends React.Component<ControlMapProps> {
constructor(props: SpaceMapProps) {
super(props);
}
get center(): Point {
return convertPoint([
this.props.width / 2,
this.props.height / 2
]);
}
render() {
return (
<Map center={this.center}
zoom={this.props.zoom}
crs={Leaflet.CRS.Simple}>
{this.renderMarkers()}
{this.renderLayers()}
</Map>
);
}
renderMarkers() {
return Object.values(this.props.controls).map(this.renderMarker.bind(this));
}
createLeafletIcon(iconRaw: string) {
const icon = iconRaw.split(" ").map(name => "mdi-".concat(name)).join(" ");
return Leaflet.divIcon({
className: `mdi mdi-36px ${icon}`,
iconSize: Leaflet.point(36, 36),
iconAnchor: Leaflet.point(18, 18)
});
}
renderMarker(control: Control) {
return (
<Marker position={convertPoint(control.position)}
key={control.name}
icon={this.createLeafletIcon(control.icon)}
>
</Marker>
);
}
renderLayers() {
return (
<LayersControl position="topright">
{this.props.layers.map(this.renderLayer)}
</LayersControl>
);
}
renderLayer(layer: Layer) {
const LayersControlType =
layer.baseLayer ? LayersControl.BaseLayer : LayersControl.Overlay;
return (
<LayersControlType
key={layer.name}
name={layer.name}
checked={layer.defaultVisibility === "visible"}>
<ImageOverlay url={layer.image}
bounds={Object.values(layer.bounds).map(convertPoint)}
opacity={layer.opacity} />
</LayersControlType>
);
}
}

62
src/components/SideBar.js Normal file
View file

@ -0,0 +1,62 @@
// @flow
import React from "react";
import withStyles from "material-ui/styles/withStyles";
import Drawer from "material-ui/Drawer";
import Typography from "material-ui/Typography";
import IconButton from "material-ui/IconButton";
import AppBar from "material-ui/AppBar";
import Toolbar from "material-ui/Toolbar";
import List from "material-ui/List";
export type SideBarProps = {
control: Control,
onCloseRequest: () => void
};
export type SideBarState = {
};
class SideBar extends React.Component<SideBarProps & Classes, SideBarState> {
constructor(props: SideBarProps & Classes) {
super(props);
}
static styles(_theme: Object): Object {
return {
drawerPaper: {
width: 320
}
};
}
close() {
this.props.onCloseRequest();
}
render() {
return (
<Drawer open={true}
anchor="right"
onRequestClose={this.close}
classes={{paper: this.props.classes.drawerPaper}}
type="persistent"
>
<AppBar position="static">
<Toolbar>
<IconButton onClick={this.close}>
<i className="mdi mdi-format-horizontal-align-right mdi-36px"></i>
</IconButton>
<Typography type="title">
{this.props.control.name}
</Typography>
</Toolbar>
</AppBar>
<List id="drawer_uiComponents">
</List>
</Drawer>
);
}
}
export default withStyles(SideBar.styles)(SideBar);