Toggle Conditions, Enable Conditions
This commit is contained in:
parent
eb96d9d511
commit
d121928df6
3 changed files with 52 additions and 11 deletions
|
|
@ -6,6 +6,20 @@ import MenuItem from 'material-ui/MenuItem';
|
||||||
import Config from "./config";
|
import Config from "./config";
|
||||||
import R from "ramda";
|
import R from "ramda";
|
||||||
|
|
||||||
|
const keyOf = (map: Map<any,any>, value: any) : ?any =>
|
||||||
|
((keys) => keys[R.findIndex(k => map[k] == value, keys)])
|
||||||
|
(R.keys(map));
|
||||||
|
|
||||||
|
const enabled = (props: ControlUI, state: State) => {
|
||||||
|
const key = keyOf(Config.topics[props.topic].values,
|
||||||
|
state.values[props.topic]);
|
||||||
|
if (props.enableCondition == null) return true;
|
||||||
|
if (key == null) return false;
|
||||||
|
else {
|
||||||
|
return props.enableCondition(key);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const getValue = (topic: string, val: string) =>
|
const getValue = (topic: string, val: string) =>
|
||||||
Config.topics[topic].values[val];
|
Config.topics[topic].values[val];
|
||||||
|
|
||||||
|
|
@ -18,15 +32,27 @@ const onToggle = (topic: string, props: ControlUI, state: State) =>
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const toggle = (state: State, props: ControlUI) => (
|
export const toggle = (state: State, props: ControlUI) => {
|
||||||
<Toggle label={props.text}
|
const toggled = (() => {
|
||||||
toggled={
|
if (props.toggled != null) {
|
||||||
state.values[props.topic] ===
|
console.log(state.values[props.topic]);
|
||||||
getValue(props.topic, R.propOr("on", "on", props))
|
console.log(Config.topics[props.topic].values);
|
||||||
|
const key = keyOf(Config.topics[props.topic].values,
|
||||||
|
state.values[props.topic]);
|
||||||
|
if (key == null) return true;
|
||||||
|
console.log(key);
|
||||||
|
if (props.toggled != null) return props.toggled(key); //only for flow </3
|
||||||
|
} else {
|
||||||
|
return state.values[props.topic] ===
|
||||||
|
getValue(props.topic, R.propOr("on", "on", props));
|
||||||
}
|
}
|
||||||
|
})();
|
||||||
|
return (<Toggle label={props.text}
|
||||||
|
toggled={toggled}
|
||||||
onToggle={onToggle(props.topic, props, state)}
|
onToggle={onToggle(props.topic, props, state)}
|
||||||
/>
|
disabled={!(enabled(props, state))} />
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const onDropDownChange = (topic: string, props: ControlUI, state: State) =>
|
const onDropDownChange = (topic: string, props: ControlUI, state: State) =>
|
||||||
(event, index, value) => {
|
(event, index, value) => {
|
||||||
|
|
@ -41,7 +67,8 @@ const dropDownItem = (topic: string) => (text: string, key: string) => (
|
||||||
|
|
||||||
export const dropDown = (state: State, props: ControlUI) => (
|
export const dropDown = (state: State, props: ControlUI) => (
|
||||||
<SelectField value={state.values[props.topic]}
|
<SelectField value={state.values[props.topic]}
|
||||||
onChange={onDropDownChange(props.topic, props, state)}>
|
onChange={onDropDownChange(props.topic, props, state)}
|
||||||
|
disabled={!(enabled(props, state))}>
|
||||||
{R.values(R.mapObjIndexed(dropDownItem(props.topic), props.options))}
|
{R.values(R.mapObjIndexed(dropDownItem(props.topic), props.options))}
|
||||||
</SelectField>
|
</SelectField>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -88,18 +88,25 @@ const config : Config = {
|
||||||
position: [550,150],
|
position: [550,150],
|
||||||
icon: "",
|
icon: "",
|
||||||
ui: [
|
ui: [
|
||||||
|
{
|
||||||
|
type: "toggle",
|
||||||
|
text: "An/Aus",
|
||||||
|
topic: "artnet",
|
||||||
|
on: "cycle",
|
||||||
|
toggled: val => val != "off"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
type: "dropDown",
|
type: "dropDown",
|
||||||
text: "Artnet",
|
text: "Artnet",
|
||||||
topic: "artnet",
|
topic: "artnet",
|
||||||
options: {
|
options: {
|
||||||
off: "Aus",
|
|
||||||
yellow: "Gelb",
|
yellow: "Gelb",
|
||||||
red: "Rot",
|
red: "Rot",
|
||||||
purple: "Pink",
|
purple: "Pink",
|
||||||
green: "Grün",
|
green: "Grün",
|
||||||
cycle: "Cycle Random"
|
cycle: "Cycle Random"
|
||||||
}
|
},
|
||||||
|
enableCondition: val => val != "off"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,15 @@ declare type ControlUI = {
|
||||||
type: "toggle" | "dropDown",
|
type: "toggle" | "dropDown",
|
||||||
text: string,
|
text: string,
|
||||||
topic: string,
|
topic: string,
|
||||||
|
|
||||||
|
enableCondition?: (val: any) => boolean,
|
||||||
|
|
||||||
|
// TOGGLE optional properties
|
||||||
on?: string, // on override for toggle
|
on?: string, // on override for toggle
|
||||||
off?: string, // off override for toggle
|
off?: string, // off override for toggle
|
||||||
|
toggled?: (val: any) => boolean,
|
||||||
|
|
||||||
|
// DROPDOWN optional properties
|
||||||
options?: Map<string,any> //options for dropDown
|
options?: Map<string,any> //options for dropDown
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue