Commit dff65fff authored by David Davó's avatar David Davó

Select widgets

parent 362bef6e
...@@ -62,13 +62,11 @@ pattern B64Value = S.SB64Value ...@@ -62,13 +62,11 @@ pattern B64Value = S.SB64Value
pattern ImageFormat = S.SImageFormat pattern ImageFormat = S.SImageFormat
pattern BoolValue = S.SBoolValue pattern BoolValue = S.SBoolValue
pattern Options = S.SOptions pattern Options = S.SOptions
pattern SelectedLabel = S.SSelectedLabel pattern Index = S.SIndex
pattern SelectedValue = S.SSelectedValue
pattern SelectionHandler = S.SSelectionHandler pattern SelectionHandler = S.SSelectionHandler
pattern Tooltips = S.STooltips pattern Tooltips = S.STooltips
pattern Icons = S.SIcons pattern Icons = S.SIcons
pattern SelectedLabels = S.SSelectedLabels pattern Indices = S.SIndices
pattern SelectedValues = S.SSelectedValues
pattern IntValue = S.SIntValue pattern IntValue = S.SIntValue
pattern StepInt = S.SStepInt pattern StepInt = S.SStepInt
pattern MaxInt = S.SMaxInt pattern MaxInt = S.SMaxInt
......
...@@ -18,6 +18,7 @@ import Prelude ...@@ -18,6 +18,7 @@ import Prelude
import Control.Monad (void) import Control.Monad (void)
import Data.Aeson import Data.Aeson
import Data.IORef (newIORef) import Data.IORef (newIORef)
import qualified Data.Scientific as Sci
import Data.Vinyl (Rec(..), (<+>)) import Data.Vinyl (Rec(..), (<+>))
import IHaskell.Display import IHaskell.Display
...@@ -52,18 +53,8 @@ mkDropdown = do ...@@ -52,18 +53,8 @@ mkDropdown = do
instance IHaskellWidget Dropdown where instance IHaskellWidget Dropdown where
getCommUUID = uuid getCommUUID = uuid
comm widget val _ = comm widget val _ =
case nestedObjectLookup val ["sync_data", "selected_label"] of case nestedObjectLookup val ["state", "index"] of
Just (String label) -> do Just (Number index) -> do
opts <- getField widget Options void $ setField' widget Index (Sci.coefficient index)
case opts of
OptionLabels _ -> do
void $ setField' widget SelectedLabel label
void $ setField' widget SelectedValue label
OptionDict ps ->
case lookup label ps of
Nothing -> return ()
Just value -> do
void $ setField' widget SelectedLabel label
void $ setField' widget SelectedValue value
triggerSelection widget triggerSelection widget
_ -> pure () _ -> pure ()
...@@ -18,6 +18,7 @@ import Prelude ...@@ -18,6 +18,7 @@ import Prelude
import Control.Monad (void) import Control.Monad (void)
import Data.Aeson import Data.Aeson
import Data.IORef (newIORef) import Data.IORef (newIORef)
import qualified Data.Scientific as Sci
import IHaskell.Display import IHaskell.Display
import IHaskell.Eval.Widgets import IHaskell.Eval.Widgets
...@@ -49,18 +50,8 @@ mkRadioButtons = do ...@@ -49,18 +50,8 @@ mkRadioButtons = do
instance IHaskellWidget RadioButtons where instance IHaskellWidget RadioButtons where
getCommUUID = uuid getCommUUID = uuid
comm widget val _ = comm widget val _ =
case nestedObjectLookup val ["sync_data", "selected_label"] of case nestedObjectLookup val ["state", "index"] of
Just (String label) -> do Just (Number index) -> do
opts <- getField widget Options void $ setField' widget Index (Sci.coefficient index)
case opts of
OptionLabels _ -> do
void $ setField' widget SelectedLabel label
void $ setField' widget SelectedValue label
OptionDict ps ->
case lookup label ps of
Nothing -> pure ()
Just value -> do
void $ setField' widget SelectedLabel label
void $ setField' widget SelectedValue value
triggerSelection widget triggerSelection widget
_ -> pure () _ -> pure ()
...@@ -18,6 +18,7 @@ import Prelude ...@@ -18,6 +18,7 @@ import Prelude
import Control.Monad (void) import Control.Monad (void)
import Data.Aeson import Data.Aeson
import Data.IORef (newIORef) import Data.IORef (newIORef)
import qualified Data.Scientific as Sci
import IHaskell.Display import IHaskell.Display
import IHaskell.Eval.Widgets import IHaskell.Eval.Widgets
...@@ -49,18 +50,8 @@ mkSelect = do ...@@ -49,18 +50,8 @@ mkSelect = do
instance IHaskellWidget Select where instance IHaskellWidget Select where
getCommUUID = uuid getCommUUID = uuid
comm widget val _ = comm widget val _ =
case nestedObjectLookup val ["sync_data", "selected_label"] of case nestedObjectLookup val ["state", "index"] of
Just (String label) -> do Just (Number index) -> do
opts <- getField widget Options void $ setField' widget Index (Sci.coefficient index)
case opts of
OptionLabels _ -> do
void $ setField' widget SelectedLabel label
void $ setField' widget SelectedValue label
OptionDict ps ->
case lookup label ps of
Nothing -> pure ()
Just value -> do
void $ setField' widget SelectedLabel label
void $ setField' widget SelectedValue value
triggerSelection widget triggerSelection widget
_ -> pure () _ -> pure ()
...@@ -18,6 +18,7 @@ import Prelude ...@@ -18,6 +18,7 @@ import Prelude
import Control.Monad (void) import Control.Monad (void)
import Data.Aeson import Data.Aeson
import Data.IORef (newIORef) import Data.IORef (newIORef)
import qualified Data.Scientific as Sci
import qualified Data.Vector as V import qualified Data.Vector as V
import IHaskell.Display import IHaskell.Display
...@@ -50,19 +51,9 @@ mkSelectMultiple = do ...@@ -50,19 +51,9 @@ mkSelectMultiple = do
instance IHaskellWidget SelectMultiple where instance IHaskellWidget SelectMultiple where
getCommUUID = uuid getCommUUID = uuid
comm widget val _ = comm widget val _ =
case nestedObjectLookup val ["sync_data", "selected_labels"] of case nestedObjectLookup val ["state", "index"] of
Just (Array labels) -> do Just (Array indices) -> do
let labelList = map (\(String x) -> x) $ V.toList labels let indicesList = map (\(Number x) -> Sci.coefficient x) $ V.toList indices
opts <- getField widget Options void $ setField' widget Indices indicesList
case opts of
OptionLabels _ -> do
void $ setField' widget SelectedLabels labelList
void $ setField' widget SelectedValues labelList
OptionDict ps ->
case mapM (`lookup` ps) labelList of
Nothing -> pure ()
Just valueList -> do
void $ setField' widget SelectedLabels labelList
void $ setField' widget SelectedValues valueList
triggerSelection widget triggerSelection widget
_ -> pure () _ -> pure ()
...@@ -18,6 +18,7 @@ import Prelude ...@@ -18,6 +18,7 @@ import Prelude
import Control.Monad (void) import Control.Monad (void)
import Data.Aeson import Data.Aeson
import Data.IORef (newIORef) import Data.IORef (newIORef)
import qualified Data.Scientific as Sci
import Data.Vinyl (Rec(..), (<+>)) import Data.Vinyl (Rec(..), (<+>))
import IHaskell.Display import IHaskell.Display
...@@ -55,18 +56,8 @@ mkToggleButtons = do ...@@ -55,18 +56,8 @@ mkToggleButtons = do
instance IHaskellWidget ToggleButtons where instance IHaskellWidget ToggleButtons where
getCommUUID = uuid getCommUUID = uuid
comm widget val _ = comm widget val _ =
case nestedObjectLookup val ["sync_data", "selected_label"] of case nestedObjectLookup val ["state", "index"] of
Just (String label) -> do Just (Number index) -> do
opts <- getField widget Options void $ setField' widget Index (Sci.coefficient index)
case opts of
OptionLabels _ -> void $ do
void $ setField' widget SelectedLabel label
void $ setField' widget SelectedValue label
OptionDict ps ->
case lookup label ps of
Nothing -> pure ()
Just value -> do
void $ setField' widget SelectedLabel label
void $ setField' widget SelectedValue value
triggerSelection widget triggerSelection widget
_ -> pure () _ -> pure ()
...@@ -68,13 +68,11 @@ singletons ...@@ -68,13 +68,11 @@ singletons
| ImageFormat | ImageFormat
| BoolValue | BoolValue
| Options | Options
| SelectedLabel | Index
| SelectedValue
| SelectionHandler | SelectionHandler
| Tooltips | Tooltips
| Icons | Icons
| SelectedLabels | Indices
| SelectedValues
| IntValue | IntValue
| StepInt | StepInt
| MaxInt | MaxInt
......
...@@ -136,10 +136,10 @@ type StringClass = DOMWidgetClass :++ ['S.StringValue, 'S.Disabled, 'S.Descripti ...@@ -136,10 +136,10 @@ type StringClass = DOMWidgetClass :++ ['S.StringValue, 'S.Disabled, 'S.Descripti
type BoolClass = DOMWidgetClass :++ ['S.BoolValue, 'S.Disabled, 'S.Description, 'S.ChangeHandler] type BoolClass = DOMWidgetClass :++ ['S.BoolValue, 'S.Disabled, 'S.Description, 'S.ChangeHandler]
type SelectionClass = DOMWidgetClass :++ ['S.Options, 'S.SelectedValue, 'S.SelectedLabel, 'S.Disabled, type SelectionClass = DOMWidgetClass :++ ['S.Options, 'S.Index, 'S.Disabled,
'S.Description, 'S.SelectionHandler] 'S.Description, 'S.SelectionHandler]
type MultipleSelectionClass = DOMWidgetClass :++ ['S.Options, 'S.SelectedValues, 'S.SelectedLabels, 'S.Disabled, type MultipleSelectionClass = DOMWidgetClass :++ ['S.Options, 'S.Indices, 'S.Disabled,
'S.Description, 'S.SelectionHandler] 'S.Description, 'S.SelectionHandler]
type IntClass = DOMWidgetClass :++ ['S.IntValue, 'S.Disabled, 'S.Description, 'S.ChangeHandler] type IntClass = DOMWidgetClass :++ ['S.IntValue, 'S.Disabled, 'S.Description, 'S.ChangeHandler]
...@@ -202,13 +202,11 @@ type family FieldType (f :: Field) :: * where ...@@ -202,13 +202,11 @@ type family FieldType (f :: Field) :: * where
FieldType 'S.ImageFormat = ImageFormatValue FieldType 'S.ImageFormat = ImageFormatValue
FieldType 'S.BoolValue = Bool FieldType 'S.BoolValue = Bool
FieldType 'S.Options = SelectionOptions FieldType 'S.Options = SelectionOptions
FieldType 'S.SelectedLabel = Text FieldType 'S.Index = Integer
FieldType 'S.SelectedValue = Text
FieldType 'S.SelectionHandler = IO () FieldType 'S.SelectionHandler = IO ()
FieldType 'S.Tooltips = [Text] FieldType 'S.Tooltips = [Text]
FieldType 'S.Icons = [Text] FieldType 'S.Icons = [Text]
FieldType 'S.SelectedLabels = [Text] FieldType 'S.Indices = [Integer]
FieldType 'S.SelectedValues = [Text]
FieldType 'S.IntValue = Integer FieldType 'S.IntValue = Integer
FieldType 'S.StepInt = Integer FieldType 'S.StepInt = Integer
FieldType 'S.MinInt = Integer FieldType 'S.MinInt = Integer
...@@ -490,11 +488,8 @@ instance ToPairs (Attr 'S.ImageFormat) where ...@@ -490,11 +488,8 @@ instance ToPairs (Attr 'S.ImageFormat) where
instance ToPairs (Attr 'S.BoolValue) where instance ToPairs (Attr 'S.BoolValue) where
toPairs x = ["value" .= toJSON x] toPairs x = ["value" .= toJSON x]
instance ToPairs (Attr 'S.SelectedLabel) where instance ToPairs (Attr 'S.Index) where
toPairs x = ["selected_label" .= toJSON x] toPairs x = ["index" .= toJSON x]
instance ToPairs (Attr 'S.SelectedValue) where
toPairs x = ["value" .= toJSON x]
instance ToPairs (Attr 'S.Options) where instance ToPairs (Attr 'S.Options) where
toPairs x = toPairs x =
...@@ -514,11 +509,8 @@ instance ToPairs (Attr 'S.Tooltips) where ...@@ -514,11 +509,8 @@ instance ToPairs (Attr 'S.Tooltips) where
instance ToPairs (Attr 'S.Icons) where instance ToPairs (Attr 'S.Icons) where
toPairs x = ["icons" .= toJSON x] toPairs x = ["icons" .= toJSON x]
instance ToPairs (Attr 'S.SelectedLabels) where instance ToPairs (Attr 'S.Indices) where
toPairs x = ["selected_labels" .= toJSON x] toPairs x = ["index" .= toJSON x]
instance ToPairs (Attr 'S.SelectedValues) where
toPairs x = ["values" .= toJSON x]
instance ToPairs (Attr 'S.IntValue) where instance ToPairs (Attr 'S.IntValue) where
toPairs x = ["value" .= toJSON x] toPairs x = ["value" .= toJSON x]
...@@ -710,8 +702,7 @@ defaultSelectionWidget :: FieldType 'S.ViewName -> FieldType 'S.ModelName -> Rec ...@@ -710,8 +702,7 @@ defaultSelectionWidget :: FieldType 'S.ViewName -> FieldType 'S.ModelName -> Rec
defaultSelectionWidget viewName modelName = defaultDOMWidget viewName modelName <+> selectionAttrs defaultSelectionWidget viewName modelName = defaultDOMWidget viewName modelName <+> selectionAttrs
where where
selectionAttrs = (Options =:: OptionLabels []) selectionAttrs = (Options =:: OptionLabels [])
:& (SelectedValue =:: "") :& (Index =:: 0)
:& (SelectedLabel =:: "")
:& (Disabled =:: False) :& (Disabled =:: False)
:& (Description =:: "") :& (Description =:: "")
:& (SelectionHandler =:: return ()) :& (SelectionHandler =:: return ())
...@@ -722,8 +713,7 @@ defaultMultipleSelectionWidget :: FieldType 'S.ViewName -> FieldType 'S.ModelNam ...@@ -722,8 +713,7 @@ defaultMultipleSelectionWidget :: FieldType 'S.ViewName -> FieldType 'S.ModelNam
defaultMultipleSelectionWidget viewName modelName = defaultDOMWidget viewName modelName <+> mulSelAttrs defaultMultipleSelectionWidget viewName modelName = defaultDOMWidget viewName modelName <+> mulSelAttrs
where where
mulSelAttrs = (Options =:: OptionLabels []) mulSelAttrs = (Options =:: OptionLabels [])
:& (SelectedValues =:: []) :& (Indices =:: [])
:& (SelectedLabels =:: [])
:& (Disabled =:: False) :& (Disabled =:: False)
:& (Description =:: "") :& (Description =:: "")
:& (SelectionHandler =:: return ()) :& (SelectionHandler =:: return ())
......
...@@ -43,7 +43,7 @@ module IHaskell.Types ( ...@@ -43,7 +43,7 @@ module IHaskell.Types (
import IHaskellPrelude import IHaskellPrelude
import qualified Data.HashMap.Strict as HashMap import qualified Data.HashMap.Strict as HashMap
import Data.Aeson (ToJSON (..), Value, (.=), object, Object, Value(String)) import Data.Aeson (ToJSON (..), Value, (.=), object, Value(String))
import Data.Function (on) import Data.Function (on)
import Data.Text (pack) import Data.Text (pack)
import Data.Serialize import Data.Serialize
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment