From f4b611149bf953046b20728c015a7d8c6c6a94db Mon Sep 17 00:00:00 2001 From: uwap Date: Sat, 4 Nov 2017 06:29:34 +0100 Subject: [PATCH] Toggle if icon is clicked with ctrl. Fixes #9 --- src/UiItems.js | 23 ++++++++++++----------- src/index.jsx | 2 +- src/map.js | 5 +++-- src/state.js | 12 +++++++++++- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/UiItems.js b/src/UiItems.js index 763d5cd..4ef3b8d 100644 --- a/src/UiItems.js +++ b/src/UiItems.js @@ -34,7 +34,7 @@ const enabled = (props: ControlUI, state: State) => { const getValue = (topic: string, val: string) => 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) => { if (state.mqtt != null) { 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) => { - const toggled = (() => { - const val = state.values[props.topic]; - if (props.toggled != null) { - return props.toggled(val.internal == null ? val.actual : val.internal, +export const isToggled = (state: State, props: ControlUI) => { + const val = state.values[props.topic]; + if (props.toggled != null) { + return props.toggled(val.internal == null ? val.actual : val.internal, val.actual); - } else { - return val.internal === R.propOr("on", "on", props); - } - })(); + } else { + return val.internal === R.propOr("on", "on", props); + } +} + +export const toggle = (state: State, props: ControlUI) => { return ( {props.icon && {props.icon}} diff --git a/src/index.jsx b/src/index.jsx index 8c38ea2..6e4fcd3 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -77,7 +77,7 @@ class app extends React.Component<{state: State, classes: Object}> { - + ); } diff --git a/src/map.js b/src/map.js index c4d9992..bc0bccc 100644 --- a/src/map.js +++ b/src/map.js @@ -31,9 +31,10 @@ const Markers = (props) => R.values(R.mapObjIndexed((el,key) => ( icon={Leaflet.divIcon( { 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})}> ), R.propOr({}, "controls", Config))); diff --git a/src/state.js b/src/state.js index e1a0b3b..ed4f9dd 100644 --- a/src/state.js +++ b/src/state.js @@ -3,6 +3,7 @@ import R from "ramda"; import { createStore } from "redux"; import Config from "./config"; import { keyOf } from "./util"; +import { onSwitch, isToggled } from "./UiItems"; export const Actions = Object.freeze({ MQTT_CONNECT: "CONNECT", @@ -55,7 +56,16 @@ const handleEvent = (state: State = initState, action: StateAction) => { return match(action.type, { [Actions.MQTT_CONNECT ]: R.merge(state, { mqtt: action.payload }), [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 }); }