Introduce DropDowns...
By the way, someone surely should cleanup this code...
This commit is contained in:
parent
021eaac0a2
commit
eb96d9d511
5 changed files with 58 additions and 40 deletions
5
.babelrc
Normal file
5
.babelrc
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"presets": [
|
||||||
|
"es2015", "react"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
@ -1,20 +1,47 @@
|
||||||
// @flow
|
// @flow
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import Toggle from "material-ui/Toggle";
|
import Toggle from "material-ui/Toggle";
|
||||||
|
import SelectField from 'material-ui/SelectField';
|
||||||
|
import MenuItem from 'material-ui/MenuItem';
|
||||||
import Config from "./config";
|
import Config from "./config";
|
||||||
import R from "ramda";
|
import R from "ramda";
|
||||||
|
|
||||||
const onToggle = (topic, props, state) => (x, toggled) =>
|
const getValue = (topic: string, val: string) =>
|
||||||
state.mqtt.publish(Config.topics[topic].command,
|
Config.topics[topic].values[val];
|
||||||
toggled ? Config.topics[topic].values[R.propOr("on", "on", props)]
|
|
||||||
: Config.topics[topic].values[R.propOr("off", "off", props)]);
|
|
||||||
|
|
||||||
export const toggle = (props: Object) => (
|
const onToggle = (topic: string, props: ControlUI, state: State) =>
|
||||||
|
(x, toggled: boolean) => {
|
||||||
|
if (state.mqtt != null) {
|
||||||
|
state.mqtt.publish(Config.topics[topic].command,
|
||||||
|
toggled ? getValue(topic, R.propOr("on", "on", props))
|
||||||
|
: getValue(topic, R.propOr("off", "off", props)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const toggle = (state: State, props: ControlUI) => (
|
||||||
<Toggle label={props.text}
|
<Toggle label={props.text}
|
||||||
toggled={
|
toggled={
|
||||||
props.state.values[props.topic] ===
|
state.values[props.topic] ===
|
||||||
Config.topics[props.topic].values[R.propOr("on", "on", props)]
|
getValue(props.topic, R.propOr("on", "on", props))
|
||||||
}
|
}
|
||||||
onToggle={onToggle(props.topic, props, props.state)}
|
onToggle={onToggle(props.topic, props, state)}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const onDropDownChange = (topic: string, props: ControlUI, state: State) =>
|
||||||
|
(event, index, value) => {
|
||||||
|
if (state.mqtt != null) {
|
||||||
|
state.mqtt.publish(Config.topics[topic].command, value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const dropDownItem = (topic: string) => (text: string, key: string) => (
|
||||||
|
<MenuItem value={Config.topics[topic].values[key]} primaryText={text} />
|
||||||
|
);
|
||||||
|
|
||||||
|
export const dropDown = (state: State, props: ControlUI) => (
|
||||||
|
<SelectField value={state.values[props.topic]}
|
||||||
|
onChange={onDropDownChange(props.topic, props, state)}>
|
||||||
|
{R.values(R.mapObjIndexed(dropDownItem(props.topic), props.options))}
|
||||||
|
</SelectField>
|
||||||
|
);
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ const config : Config = {
|
||||||
led_stahltraeger: {
|
led_stahltraeger: {
|
||||||
state: "/service/openhab/out/pca301_ledstrips/state",
|
state: "/service/openhab/out/pca301_ledstrips/state",
|
||||||
command: "/service/openhab/in/pca301_ledstrips/command",
|
command: "/service/openhab/in/pca301_ledstrips/command",
|
||||||
value: "OFF", # defaultValue
|
value: "OFF", // defaultValue
|
||||||
values: { on: "ON", off: "OFF" }
|
values: { on: "ON", off: "OFF" }
|
||||||
},
|
},
|
||||||
snackbar: {
|
snackbar: {
|
||||||
|
|
@ -89,34 +89,17 @@ const config : Config = {
|
||||||
icon: "",
|
icon: "",
|
||||||
ui: [
|
ui: [
|
||||||
{
|
{
|
||||||
type: "toggle",
|
type: "dropDown",
|
||||||
text: "Gelb",
|
text: "Artnet",
|
||||||
topic: "artnet",
|
topic: "artnet",
|
||||||
on: "yellow"
|
options: {
|
||||||
},
|
off: "Aus",
|
||||||
{
|
yellow: "Gelb",
|
||||||
type: "toggle",
|
red: "Rot",
|
||||||
text: "Rot",
|
purple: "Pink",
|
||||||
topic: "artnet",
|
green: "Grün",
|
||||||
on: "red"
|
cycle: "Cycle Random"
|
||||||
},
|
}
|
||||||
{
|
|
||||||
type: "toggle",
|
|
||||||
text: "Pink",
|
|
||||||
topic: "artnet",
|
|
||||||
on: "purple"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: "toggle",
|
|
||||||
text: "Grün",
|
|
||||||
topic: "artnet",
|
|
||||||
on: "green"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: "toggle",
|
|
||||||
text: "Cycle Random",
|
|
||||||
topic: "artnet",
|
|
||||||
on: "cycle"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,8 +54,8 @@ const handleEvent = (state = appState, action) => {
|
||||||
|
|
||||||
const store = createStore(handleEvent);
|
const store = createStore(handleEvent);
|
||||||
|
|
||||||
const UiItem = (state) => (props) =>
|
const UiItem = (state) => (props : ControlUI) =>
|
||||||
UiItems[props.type](R.merge(props, {state:state}));
|
UiItems[props.type](state, props);
|
||||||
|
|
||||||
const renderUi = (state, key) => key != null && Config.controls[key] != null ?
|
const renderUi = (state, key) => key != null && Config.controls[key] != null ?
|
||||||
R.map(UiItem(state), Config.controls[key].ui) : null;
|
R.map(UiItem(state), Config.controls[key].ui) : null;
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,12 @@ declare type Topic = {
|
||||||
declare type Topics = Map<string,Topic>;
|
declare type Topics = Map<string,Topic>;
|
||||||
|
|
||||||
declare type ControlUI = {
|
declare type ControlUI = {
|
||||||
type: "toggle",
|
type: "toggle" | "dropDown",
|
||||||
text: string,
|
text: string,
|
||||||
topic: string
|
topic: string,
|
||||||
|
on?: string, // on override for toggle
|
||||||
|
off?: string, // off override for toggle
|
||||||
|
options?: Map<string,any> //options for dropDown
|
||||||
};
|
};
|
||||||
|
|
||||||
declare type Control = {
|
declare type Control = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue