From d121928df69f6a7eff3953a94080c35597d06424 Mon Sep 17 00:00:00 2001 From: uwap Date: Sun, 17 Sep 2017 05:00:07 +0200 Subject: [PATCH] Toggle Conditions, Enable Conditions --- src/UiItems.js | 45 ++++++++++++++++++++++++++++++++++++--------- src/config.js | 11 +++++++++-- types/types.js | 7 +++++++ 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/UiItems.js b/src/UiItems.js index 0e14351..f65f4b9 100644 --- a/src/UiItems.js +++ b/src/UiItems.js @@ -6,6 +6,20 @@ import MenuItem from 'material-ui/MenuItem'; import Config from "./config"; import R from "ramda"; +const keyOf = (map: Map, 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) => Config.topics[topic].values[val]; @@ -18,15 +32,27 @@ const onToggle = (topic: string, props: ControlUI, state: State) => } }; -export const toggle = (state: State, props: ControlUI) => ( - { + const toggled = (() => { + if (props.toggled != null) { + console.log(state.values[props.topic]); + 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 -); + disabled={!(enabled(props, state))} /> + ); +} const onDropDownChange = (topic: string, props: ControlUI, state: State) => (event, index, value) => { @@ -41,7 +67,8 @@ const dropDownItem = (topic: string) => (text: string, key: string) => ( export const dropDown = (state: State, props: ControlUI) => ( + onChange={onDropDownChange(props.topic, props, state)} + disabled={!(enabled(props, state))}> {R.values(R.mapObjIndexed(dropDownItem(props.topic), props.options))} ); diff --git a/src/config.js b/src/config.js index 40bf8f8..ad23351 100644 --- a/src/config.js +++ b/src/config.js @@ -88,18 +88,25 @@ const config : Config = { position: [550,150], icon: "", ui: [ + { + type: "toggle", + text: "An/Aus", + topic: "artnet", + on: "cycle", + toggled: val => val != "off" + }, { type: "dropDown", text: "Artnet", topic: "artnet", options: { - off: "Aus", yellow: "Gelb", red: "Rot", purple: "Pink", green: "GrĂ¼n", cycle: "Cycle Random" - } + }, + enableCondition: val => val != "off" } ] } diff --git a/types/types.js b/types/types.js index 145289f..f2afd18 100644 --- a/types/types.js +++ b/types/types.js @@ -13,8 +13,15 @@ declare type ControlUI = { type: "toggle" | "dropDown", text: string, topic: string, + + enableCondition?: (val: any) => boolean, + + // TOGGLE optional properties on?: string, // on override for toggle off?: string, // off override for toggle + toggled?: (val: any) => boolean, + + // DROPDOWN optional properties options?: Map //options for dropDown };