Toggle if icon is clicked with ctrl.

Fixes #9
This commit is contained in:
uwap 2017-11-04 06:29:34 +01:00
parent 4e8253efca
commit f4b611149b
4 changed files with 27 additions and 15 deletions

View file

@ -34,7 +34,7 @@ const enabled = (props: ControlUI, state: State) => {
const getValue = (topic: string, val: string) => const getValue = (topic: string, val: string) =>
Config.topics[topic].values[val]; Config.topics[topic].values[val];
const onSwitch = (topic: string, props: ControlUI, state: State) => export const onSwitch = (topic: string, props: ControlUI, state: State) =>
(x, toggled: boolean) => { (x, toggled: boolean) => {
if (state.mqtt != null) { if (state.mqtt != null) {
state.mqtt.publish(Config.topics[topic].command, state.mqtt.publish(Config.topics[topic].command,
@ -43,23 +43,24 @@ const onSwitch = (topic: string, props: ControlUI, state: State) =>
} }
}; };
export const toggle = (state: State, props: ControlUI) => { export const isToggled = (state: State, props: ControlUI) => {
const toggled = (() => { const val = state.values[props.topic];
const val = state.values[props.topic]; if (props.toggled != null) {
if (props.toggled != null) { return props.toggled(val.internal == null ? val.actual : val.internal,
return props.toggled(val.internal == null ? val.actual : val.internal,
val.actual); val.actual);
} else { } else {
return val.internal === R.propOr("on", "on", props); return val.internal === R.propOr("on", "on", props);
} }
})(); }
export const toggle = (state: State, props: ControlUI) => {
return ( return (
<ListItem> <ListItem>
{props.icon && <ListItemIcon><Icon>{props.icon}</Icon></ListItemIcon>} {props.icon && <ListItemIcon><Icon>{props.icon}</Icon></ListItemIcon>}
<ListItemText primary={props.text} /> <ListItemText primary={props.text} />
<ListItemSecondaryAction> <ListItemSecondaryAction>
<Switch label={props.text} <Switch label={props.text}
checked={toggled} checked={isToggled(state,props)}
onChange={onSwitch(props.topic, props, state)} onChange={onSwitch(props.topic, props, state)}
disabled={!(enabled(props, state))} /> disabled={!(enabled(props, state))} />
</ListItemSecondaryAction> </ListItemSecondaryAction>

View file

@ -77,7 +77,7 @@ class app extends React.Component<{state: State, classes: Object}> {
</Drawer> </Drawer>
</div> </div>
</MuiThemeProvider> </MuiThemeProvider>
<SpaceMap width={1000} height={700} image="rzl.png" zoom={0.1} state={state} /> <SpaceMap width={1000} height={700} image="rzl.png" zoom={0} state={state} />
</div> </div>
); );
} }

View file

@ -31,9 +31,10 @@ const Markers = (props) => R.values(R.mapObjIndexed((el,key) => (
icon={Leaflet.divIcon( icon={Leaflet.divIcon(
{ {
html: iconHtml(el, props.state), html: iconHtml(el, props.state),
iconSize: Leaflet.point(32,32) iconSize: Leaflet.point(32,32),
iconAnchor: Leaflet.point(16,16)
})} })}
onClick={() => store.dispatch({type: Actions.CHANGE_UI, payload: key})}> onClick={(e) => store.dispatch({type: Actions.CHANGE_UI, payload: key, toggle: e.originalEvent.ctrlKey})}>
</Marker> </Marker>
), R.propOr({}, "controls", Config))); ), R.propOr({}, "controls", Config)));

View file

@ -3,6 +3,7 @@ import R from "ramda";
import { createStore } from "redux"; import { createStore } from "redux";
import Config from "./config"; import Config from "./config";
import { keyOf } from "./util"; import { keyOf } from "./util";
import { onSwitch, isToggled } from "./UiItems";
export const Actions = Object.freeze({ export const Actions = Object.freeze({
MQTT_CONNECT: "CONNECT", MQTT_CONNECT: "CONNECT",
@ -55,7 +56,16 @@ const handleEvent = (state: State = initState, action: StateAction) => {
return match(action.type, { return match(action.type, {
[Actions.MQTT_CONNECT ]: R.merge(state, { mqtt: action.payload }), [Actions.MQTT_CONNECT ]: R.merge(state, { mqtt: action.payload }),
[Actions.MQTT_MESSAGE ]: onMessage(state, action), [Actions.MQTT_MESSAGE ]: onMessage(state, action),
[Actions.CHANGE_UI ]: R.merge(state, { uiOpened: action.payload }), [Actions.CHANGE_UI ]: (() => {
const control = Config.controls[action.payload];
if (action.toggle && control.ui.length > 0 && control.ui[0].type == "toggle") {
const props = control.ui[0];
onSwitch(props.topic, props, state)(null, !isToggled(state, props));
return state;
} else {
return R.merge(state, { uiOpened: action.payload });
}
})(),
[null]: state [null]: state
}); });
} }