{-# language GADTs #-}
{-# language OverloadedStrings #-}
{-# language TypeOperators #-}
module FlexTask.Form.Core (
Formify(FormTypes),
FormPiece,
SimpleFormPiece,
ListFormPiece,
AnyFormPiece,
CompleteForm,
BaseField(..),
basic,
singleChoice,
singleChoiceEnum,
multipleChoice,
multipleChoiceEnum,
list,
listRepeatedly,
listWithoutLabels,
TypeField,
basicField,
singleChoiceField,
singleChoiceEnumField,
(>|),
beside,
(>-),
above,
formify,
formifyComponents,
formifyComponentsFlat,
Alignment(..),
ChoiceShape(..),
OneField,
ManyFields,
(:>),
type (++),
CanBeOptional,
) where
import Data.List.Extra (
intercalate,
nubOrd,
singleton,
zipWithLongest,
)
import Data.Maybe (catMaybes)
import Data.Text (Text, pack)
import Yesod (
AForm,
Field,
FieldSettings(..),
PathPiece,
SomeMessage,
Textarea,
areq,
aopt,
boolField,
convertField,
doubleField,
hiddenField,
intField,
multiSelectField,
optionsPairs,
textareaField,
textField,
)
import FlexTask.Form.Formify (Formify(..))
import FlexTask.Form.Util (applyToWidget)
import FlexTask.Form.Widgets
( checkboxField
, radioField
, joinWidgets
, renderForm
, selectField
)
import FlexTask.Form.TypeLevel
import FlexTask.Form.Types (
FlexForm(..),
Handler,
Rendered,
Widget,
)
import FlexTask.InputTypes (
Hidden(..),
MultipleChoice(..),
MultipleChoiceSelection,
SingleChoiceSelection,
SingleInputList(..),
singleChoiceAnswer,
)
data TypeField a where
Basic :: BaseField a => (FieldSettings FlexForm) -> TypeField a
SingleChoiceField
:: Eq a
=> ChoiceShape
-> (FieldSettings FlexForm)
-> [(SomeMessage FlexForm, a)]
-> TypeField a
MultipleChoiceField
:: Eq a
=> ChoiceShape
-> (FieldSettings FlexForm)
-> [(SomeMessage FlexForm, a)]
-> TypeField (MultipleChoice a)
data FormPiece finalType fields where
Single :: TypeField a -> SimpleFormPiece t a
Combine :: Alignment -> FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
List :: Alignment -> [TypeField a] -> ListFormPiece t a
type CompleteForm a = AnyFormPiece a a
type SimpleFormPiece t a = FormPiece t (OneField a)
type ListFormPiece t a = FormPiece t (ManyFields a)
type AnyFormPiece t a = FormPiece t (FormTypes a)
data Alignment = Horizontal | Vertical deriving (Alignment -> Alignment -> Bool
(Alignment -> Alignment -> Bool)
-> (Alignment -> Alignment -> Bool) -> Eq Alignment
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Alignment -> Alignment -> Bool
== :: Alignment -> Alignment -> Bool
$c/= :: Alignment -> Alignment -> Bool
/= :: Alignment -> Alignment -> Bool
Eq,Int -> Alignment -> ShowS
[Alignment] -> ShowS
Alignment -> String
(Int -> Alignment -> ShowS)
-> (Alignment -> String)
-> ([Alignment] -> ShowS)
-> Show Alignment
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Alignment -> ShowS
showsPrec :: Int -> Alignment -> ShowS
$cshow :: Alignment -> String
show :: Alignment -> String
$cshowList :: [Alignment] -> ShowS
showList :: [Alignment] -> ShowS
Show)
data ChoiceShape
= Buttons Alignment
| Dropdown
class (Formify a, FormTypes a ~ OneField a) => BaseField a where
baseField :: Field Handler a
instance BaseField Integer where
baseField :: Field Handler Integer
baseField = Field Handler Integer
forall (m :: * -> *) i.
(Monad m, Integral i, RenderMessage (HandlerSite m) FormMessage) =>
Field m i
intField
instance BaseField Int where
baseField :: Field Handler Int
baseField = Field Handler Int
forall (m :: * -> *) i.
(Monad m, Integral i, RenderMessage (HandlerSite m) FormMessage) =>
Field m i
intField
instance BaseField Text where
baseField :: Field Handler Text
baseField = Field Handler Text
forall (m :: * -> *).
(Monad m, RenderMessage (HandlerSite m) FormMessage) =>
Field m Text
textField
instance BaseField Textarea where
baseField :: Field Handler Textarea
baseField = Field Handler Textarea
forall (m :: * -> *).
(Monad m, RenderMessage (HandlerSite m) FormMessage) =>
Field m Textarea
textareaField
instance BaseField Bool where
baseField :: Field Handler Bool
baseField = Field Handler Bool
forall (m :: * -> *).
(Monad m, RenderMessage (HandlerSite m) FormMessage) =>
Field m Bool
boolField
instance BaseField Double where
baseField :: Field Handler Double
baseField = Field Handler Double
forall (m :: * -> *).
(Monad m, RenderMessage (HandlerSite m) FormMessage) =>
Field m Double
doubleField
instance (Formify a, PathPiece a) => BaseField (Hidden a) where
baseField :: Field Handler (Hidden a)
baseField = Field Handler (Hidden a)
forall (m :: * -> *) p.
(Monad m, PathPiece p,
RenderMessage (HandlerSite m) FormMessage) =>
Field m p
hiddenField
instance Show a => BaseField (SingleInputList a) where
baseField :: Field Handler (SingleInputList a)
baseField = (Text -> SingleInputList a)
-> (SingleInputList a -> Text)
-> Field Handler Text
-> Field Handler (SingleInputList a)
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> (b -> a) -> Field m a -> Field m b
convertField Text -> SingleInputList a
forall a. HasCallStack => a
undefined (String -> Text
pack (String -> Text)
-> (SingleInputList a -> String) -> SingleInputList a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ([String] -> String)
-> (SingleInputList a -> [String]) -> SingleInputList a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> String) -> [a] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map a -> String
forall a. Show a => a -> String
show ([a] -> [String])
-> (SingleInputList a -> [a]) -> SingleInputList a -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SingleInputList a -> [a]
forall a. SingleInputList a -> [a]
getList) Field Handler Text
forall (m :: * -> *).
(Monad m, RenderMessage (HandlerSite m) FormMessage) =>
Field m Text
textField
combineWidgets
:: Alignment
-> Rendered [[a]]
-> Rendered [[a]]
-> Rendered [[a]]
combineWidgets :: forall a.
Alignment -> Rendered [[a]] -> Rendered [[a]] -> Rendered [[a]]
combineWidgets Alignment
align Rendered [[a]]
f1 Rendered [[a]]
f2 = do
res1 <- Rendered [[a]]
ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[a]]))
f1
res2 <- f2
pure $ do
(ids1,names1,xss) <- res1
(ids2,names2,yss) <- res2
pure
( ids1 ++ ids2
, nubOrd $ names1 ++ names2
, appendWidgets xss yss)
where
appendWidgets :: [[a]] -> [[a]] -> [[a]]
appendWidgets = case Alignment
align of
Alignment
Vertical -> [[a]] -> [[a]] -> [[a]]
forall a. [a] -> [a] -> [a]
(++)
Alignment
Horizontal -> (Maybe [a] -> Maybe [a] -> [a]) -> [[a]] -> [[a]] -> [[a]]
forall a b c. (Maybe a -> Maybe b -> c) -> [a] -> [b] -> [c]
zipWithLongest (\Maybe [a]
xs Maybe [a]
ys -> [[a]] -> [a]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[a]] -> [a]) -> [[a]] -> [a]
forall a b. (a -> b) -> a -> b
$ [Maybe [a]] -> [[a]]
forall a. [Maybe a] -> [a]
catMaybes [Maybe [a]
xs,Maybe [a]
ys])
formify
:: Formify a
=> Maybe a
-> CompleteForm a
-> Rendered Widget
formify :: forall a. Formify a => Maybe a -> CompleteForm a -> Rendered Widget
formify Maybe a
mDefault = ([[Widget]] -> Widget)
-> Rendered' (ReaderT Html Identity) [[Widget]] -> Rendered Widget
forall (m :: * -> *) w w'.
Functor m =>
(w -> w') -> Rendered' m w -> Rendered' m w'
applyToWidget [[Widget]] -> Widget
joinWidgets (ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))
-> ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], Widget)))
-> (FormPiece a (FormTypes a)
-> ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]])))
-> FormPiece a (FormTypes a)
-> ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], Widget))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a
-> FormPiece a (FormTypes a)
-> Rendered' (ReaderT Html Identity) [[Widget]]
forall a.
Formify a =>
Maybe a
-> CompleteForm a -> Rendered' (ReaderT Html Identity) [[Widget]]
formifyComponents Maybe a
mDefault
formifyComponents
:: Formify a
=> Maybe a
-> CompleteForm a
-> Rendered [[Widget]]
formifyComponents :: forall a.
Formify a =>
Maybe a
-> CompleteForm a -> Rendered' (ReaderT Html Identity) [[Widget]]
formifyComponents Maybe a
mDefault = TypeList (FormTypes a)
-> FormPiece a (FormTypes a)
-> Rendered' (ReaderT Html Identity) [[Widget]]
forall a t.
TypeList a
-> FormPiece t a -> Rendered' (ReaderT Html Identity) [[Widget]]
renderLayout (Maybe a -> TypeList (FormTypes a)
forall a. Formify a => Maybe a -> TypeList (FormTypes a)
formDefaults Maybe a
mDefault)
formifyComponentsFlat
:: Formify a
=> Maybe a
-> CompleteForm a
-> Rendered [Widget]
formifyComponentsFlat :: forall a.
Formify a =>
Maybe a -> CompleteForm a -> Rendered [Widget]
formifyComponentsFlat Maybe a
mDefault = ([[Widget]] -> [Widget])
-> Rendered' (ReaderT Html Identity) [[Widget]]
-> Rendered [Widget]
forall (m :: * -> *) w w'.
Functor m =>
(w -> w') -> Rendered' m w -> Rendered' m w'
applyToWidget [[Widget]] -> [Widget]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))
-> ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [Widget])))
-> (FormPiece a (FormTypes a)
-> ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]])))
-> FormPiece a (FormTypes a)
-> ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [Widget]))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a
-> FormPiece a (FormTypes a)
-> Rendered' (ReaderT Html Identity) [[Widget]]
forall a.
Formify a =>
Maybe a
-> CompleteForm a -> Rendered' (ReaderT Html Identity) [[Widget]]
formifyComponents Maybe a
mDefault
renderRequiredness :: OneDefault a -> TypeField a -> Rendered Widget
renderRequiredness :: forall a. OneDefault a -> TypeField a -> Rendered Widget
renderRequiredness (OptionalDefault Maybe a
mDefault) TypeField a
field = (Field Handler a
-> FieldSettings FlexForm -> AForm Handler (Maybe a))
-> TypeField a -> Rendered Widget
forall a c.
(Field Handler a -> FieldSettings FlexForm -> AForm Handler c)
-> TypeField a -> Rendered Widget
renderField (\Field Handler a
f FieldSettings FlexForm
fs -> Field Handler a
-> FieldSettings (HandlerSite Handler)
-> Maybe (Maybe a)
-> AForm Handler (Maybe a)
forall (m :: * -> *) a.
MonadHandler m =>
Field m a
-> FieldSettings (HandlerSite m)
-> Maybe (Maybe a)
-> AForm m (Maybe a)
aopt Field Handler a
f FieldSettings (HandlerSite Handler)
FieldSettings FlexForm
fs (Maybe (Maybe a) -> AForm Handler (Maybe a))
-> Maybe (Maybe a) -> AForm Handler (Maybe a)
forall a b. (a -> b) -> a -> b
$ Maybe a -> Maybe (Maybe a)
forall a. a -> Maybe a
Just Maybe a
mDefault) TypeField a
field
renderRequiredness (RequiredDefault Maybe a
mDefault) TypeField a
field = (Field Handler a -> FieldSettings FlexForm -> AForm Handler a)
-> TypeField a -> Rendered Widget
forall a c.
(Field Handler a -> FieldSettings FlexForm -> AForm Handler c)
-> TypeField a -> Rendered Widget
renderField (\Field Handler a
f FieldSettings FlexForm
fs -> Field Handler a
-> FieldSettings FlexForm -> Maybe a -> AForm Handler a
forall site (m :: * -> *) a.
(RenderMessage site FormMessage, HandlerSite m ~ site,
MonadHandler m) =>
Field m a -> FieldSettings site -> Maybe a -> AForm m a
areq Field Handler a
f FieldSettings FlexForm
fs Maybe a
mDefault) TypeField a
field
renderField :: (Field Handler a -> FieldSettings FlexForm -> AForm Handler c) -> TypeField a -> Rendered Widget
renderField :: forall a c.
(Field Handler a -> FieldSettings FlexForm -> AForm Handler c)
-> TypeField a -> Rendered Widget
renderField Field Handler a -> FieldSettings FlexForm -> AForm Handler c
req TypeField a
info = case TypeField a
info of
Basic FieldSettings FlexForm
fs -> (FieldSettings FlexForm -> AForm Handler c)
-> FieldSettings FlexForm -> Rendered Widget
forall a.
(FieldSettings FlexForm -> AForm Handler a)
-> FieldSettings FlexForm -> Rendered Widget
renderForm (Field Handler a -> FieldSettings FlexForm -> AForm Handler c
req Field Handler a
forall a. BaseField a => Field Handler a
baseField) FieldSettings FlexForm
fs
SingleChoiceField ChoiceShape
k FieldSettings FlexForm
fs [(SomeMessage FlexForm, a)]
xs -> (FieldSettings FlexForm -> AForm Handler c)
-> FieldSettings FlexForm -> Rendered Widget
forall a.
(FieldSettings FlexForm -> AForm Handler a)
-> FieldSettings FlexForm -> Rendered Widget
renderForm (Field Handler a -> FieldSettings FlexForm -> AForm Handler c
req (Field Handler a -> FieldSettings FlexForm -> AForm Handler c)
-> Field Handler a -> FieldSettings FlexForm -> AForm Handler c
forall a b. (a -> b) -> a -> b
$ case ChoiceShape
k of
ChoiceShape
Dropdown -> Handler (OptionList a) -> Field Handler a
forall a. Eq a => Handler (OptionList a) -> Field Handler a
selectField (Handler (OptionList a) -> Field Handler a)
-> Handler (OptionList a) -> Field Handler a
forall a b. (a -> b) -> a -> b
$ [(SomeMessage FlexForm, a)] -> Handler (OptionList a)
forall (m :: * -> *) msg a.
(MonadHandler m, RenderMessage (HandlerSite m) msg) =>
[(msg, a)] -> m (OptionList a)
optionsPairs [(SomeMessage FlexForm, a)]
xs
Buttons Alignment
Vertical -> Bool -> Handler (OptionList a) -> Field Handler a
forall a. Eq a => Bool -> Handler (OptionList a) -> Field Handler a
radioField Bool
True (Handler (OptionList a) -> Field Handler a)
-> Handler (OptionList a) -> Field Handler a
forall a b. (a -> b) -> a -> b
$ [(SomeMessage FlexForm, a)] -> Handler (OptionList a)
forall (m :: * -> *) msg a.
(MonadHandler m, RenderMessage (HandlerSite m) msg) =>
[(msg, a)] -> m (OptionList a)
optionsPairs [(SomeMessage FlexForm, a)]
xs
Buttons Alignment
Horizontal -> Bool -> Handler (OptionList a) -> Field Handler a
forall a. Eq a => Bool -> Handler (OptionList a) -> Field Handler a
radioField Bool
False (Handler (OptionList a) -> Field Handler a)
-> Handler (OptionList a) -> Field Handler a
forall a b. (a -> b) -> a -> b
$ [(SomeMessage FlexForm, a)] -> Handler (OptionList a)
forall (m :: * -> *) msg a.
(MonadHandler m, RenderMessage (HandlerSite m) msg) =>
[(msg, a)] -> m (OptionList a)
optionsPairs [(SomeMessage FlexForm, a)]
xs) FieldSettings FlexForm
fs
MultipleChoiceField ChoiceShape
k FieldSettings FlexForm
fs [(SomeMessage FlexForm, a)]
xs -> (FieldSettings FlexForm -> AForm Handler c)
-> FieldSettings FlexForm -> Rendered Widget
forall a.
(FieldSettings FlexForm -> AForm Handler a)
-> FieldSettings FlexForm -> Rendered Widget
renderForm (Field Handler a -> FieldSettings FlexForm -> AForm Handler c
req (Field Handler a -> FieldSettings FlexForm -> AForm Handler c)
-> Field Handler a -> FieldSettings FlexForm -> AForm Handler c
forall a b. (a -> b) -> a -> b
$
([a] -> a) -> (a -> [a]) -> Field Handler [a] -> Field Handler a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> (b -> a) -> Field m a -> Field m b
convertField [a] -> a
[a] -> MultipleChoice a
forall a. [a] -> MultipleChoice a
MultipleChoice a -> [a]
MultipleChoice a -> [a]
forall a. MultipleChoice a -> [a]
getChoices (Field Handler [a] -> Field Handler a)
-> Field Handler [a] -> Field Handler a
forall a b. (a -> b) -> a -> b
$ case ChoiceShape
k of
ChoiceShape
Dropdown -> HandlerFor FlexForm (OptionList a) -> Field Handler [a]
forall a site.
Eq a =>
HandlerFor site (OptionList a) -> Field (HandlerFor site) [a]
multiSelectField (HandlerFor FlexForm (OptionList a) -> Field Handler [a])
-> HandlerFor FlexForm (OptionList a) -> Field Handler [a]
forall a b. (a -> b) -> a -> b
$ [(SomeMessage FlexForm, a)] -> HandlerFor FlexForm (OptionList a)
forall (m :: * -> *) msg a.
(MonadHandler m, RenderMessage (HandlerSite m) msg) =>
[(msg, a)] -> m (OptionList a)
optionsPairs [(SomeMessage FlexForm, a)]
xs
Buttons Alignment
Vertical -> Bool -> HandlerFor FlexForm (OptionList a) -> Field Handler [a]
forall a.
Eq a =>
Bool -> Handler (OptionList a) -> Field Handler [a]
checkboxField Bool
True (HandlerFor FlexForm (OptionList a) -> Field Handler [a])
-> HandlerFor FlexForm (OptionList a) -> Field Handler [a]
forall a b. (a -> b) -> a -> b
$ [(SomeMessage FlexForm, a)] -> HandlerFor FlexForm (OptionList a)
forall (m :: * -> *) msg a.
(MonadHandler m, RenderMessage (HandlerSite m) msg) =>
[(msg, a)] -> m (OptionList a)
optionsPairs [(SomeMessage FlexForm, a)]
xs
Buttons Alignment
Horizontal -> Bool -> HandlerFor FlexForm (OptionList a) -> Field Handler [a]
forall a.
Eq a =>
Bool -> Handler (OptionList a) -> Field Handler [a]
checkboxField Bool
False (HandlerFor FlexForm (OptionList a) -> Field Handler [a])
-> HandlerFor FlexForm (OptionList a) -> Field Handler [a]
forall a b. (a -> b) -> a -> b
$ [(SomeMessage FlexForm, a)] -> HandlerFor FlexForm (OptionList a)
forall (m :: * -> *) msg a.
(MonadHandler m, RenderMessage (HandlerSite m) msg) =>
[(msg, a)] -> m (OptionList a)
optionsPairs [(SomeMessage FlexForm, a)]
xs) FieldSettings FlexForm
fs
renderLayout :: TypeList a -> FormPiece t a -> Rendered [[Widget]]
renderLayout :: forall a t.
TypeList a
-> FormPiece t a -> Rendered' (ReaderT Html Identity) [[Widget]]
renderLayout (TOne OneDefault x
mDefault) (Single TypeField a
x) = (Widget -> [[Widget]])
-> Rendered Widget -> Rendered' (ReaderT Html Identity) [[Widget]]
forall (m :: * -> *) w w'.
Functor m =>
(w -> w') -> Rendered' m w -> Rendered' m w'
applyToWidget ([Widget] -> [[Widget]]
forall a. a -> [a]
singleton ([Widget] -> [[Widget]])
-> (Widget -> [Widget]) -> Widget -> [[Widget]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Widget -> [Widget]
forall a. a -> [a]
singleton) (Rendered Widget -> Rendered' (ReaderT Html Identity) [[Widget]])
-> Rendered Widget -> Rendered' (ReaderT Html Identity) [[Widget]]
forall a b. (a -> b) -> a -> b
$
OneDefault x -> TypeField x -> Rendered Widget
forall a. OneDefault a -> TypeField a -> Rendered Widget
renderRequiredness OneDefault x
mDefault TypeField x
TypeField a
x
renderLayout TypeList a
mDefault (Combine Alignment
align FormPiece t xs
x FormPiece t ys
y) = TypeList xs
-> FormPiece t xs -> Rendered' (ReaderT Html Identity) [[Widget]]
forall a t.
TypeList a
-> FormPiece t a -> Rendered' (ReaderT Html Identity) [[Widget]]
renderLayout TypeList xs
a FormPiece t xs
x Rendered' (ReaderT Html Identity) [[Widget]]
-> Rendered' (ReaderT Html Identity) [[Widget]]
-> Rendered' (ReaderT Html Identity) [[Widget]]
`how` TypeList ys
-> FormPiece t ys -> Rendered' (ReaderT Html Identity) [[Widget]]
forall a t.
TypeList a
-> FormPiece t a -> Rendered' (ReaderT Html Identity) [[Widget]]
renderLayout TypeList ys
b FormPiece t ys
y
where
(TypeList xs
a,TypeList ys
b) = TypeShape xs -> TypeList (xs ++ ys) -> (TypeList xs, TypeList ys)
forall xs ys.
TypeShape xs -> TypeList (xs ++ ys) -> (TypeList xs, TypeList ys)
splitTypeList (FormPiece t xs -> TypeShape xs
forall t xs. FormPiece t xs -> TypeShape xs
pieceShape FormPiece t xs
x) TypeList a
TypeList (xs ++ ys)
mDefault
how :: Rendered' (ReaderT Html Identity) [[Widget]]
-> Rendered' (ReaderT Html Identity) [[Widget]]
-> Rendered' (ReaderT Html Identity) [[Widget]]
how = Alignment
-> Rendered' (ReaderT Html Identity) [[Widget]]
-> Rendered' (ReaderT Html Identity) [[Widget]]
-> Rendered' (ReaderT Html Identity) [[Widget]]
forall a.
Alignment -> Rendered [[a]] -> Rendered [[a]] -> Rendered [[a]]
combineWidgets Alignment
align
renderLayout (TMany OneDefault x
t Maybe [OneDefault x]
mDefault) (List Alignment
align [TypeField a]
fs) =
(ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))
-> ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))
-> ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]])))
-> [ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))]
-> ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))
-> ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))
-> ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))
addParams [TypeList (OneField x)
-> FormPiece (ZonkAny 0) (OneField x)
-> Rendered' (ReaderT Html Identity) [[Widget]]
forall a t.
TypeList a
-> FormPiece t a -> Rendered' (ReaderT Html Identity) [[Widget]]
renderLayout (OneDefault x -> TypeList (OneField x)
forall x. OneDefault x -> TypeList (OneField x)
TOne OneDefault x
d) (TypeField x -> FormPiece (ZonkAny 0) (OneField x)
forall a t. TypeField a -> SimpleFormPiece t a
Single TypeField x
TypeField a
f) | (OneDefault x
d,TypeField a
f) <- [OneDefault x] -> [TypeField a] -> [(OneDefault x, TypeField a)]
forall a b. [a] -> [b] -> [(a, b)]
zip [OneDefault x]
defaults [TypeField a]
fs]
where
defaults :: [OneDefault x]
defaults = case Maybe [OneDefault x]
mDefault of
Maybe [OneDefault x]
Nothing -> OneDefault x -> [OneDefault x]
forall a. a -> [a]
repeat OneDefault x
t
Just [OneDefault x]
ds
| [OneDefault x] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [OneDefault x]
ds Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= [TypeField a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TypeField a]
fs
-> String -> [OneDefault x]
forall a. HasCallStack => String -> a
error
String
"Lengths of form default value and FieldInfo list do not match!"
| Bool
otherwise -> [OneDefault x]
ds
addParams :: ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))
-> ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))
-> ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))
addParams ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))
f1 ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))
f2 = do
res1 <- ReaderT
Html
Identity
(RWST
(Maybe (Env, FileEnv), FlexForm, [Text])
Enctype
Ints
Handler
([Text], [[Text]], [[Widget]]))
f1
res2 <- f2
pure $ do
(ids1,names1,wid1) <- res1
(ids2,names2,wid2) <- res2
pure
( ids1 ++ ids2
, [nubOrd $ concat $ names1 ++ names2]
, case align of
Alignment
Vertical -> [[Widget]]
wid1 [[Widget]] -> [[Widget]] -> [[Widget]]
forall a. [a] -> [a] -> [a]
++ [[Widget]]
wid2
Alignment
Horizontal -> [[[Widget]] -> [Widget]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Widget]] -> [Widget]) -> [[Widget]] -> [Widget]
forall a b. (a -> b) -> a -> b
$ [[Widget]]
wid1 [[Widget]] -> [[Widget]] -> [[Widget]]
forall a. [a] -> [a] -> [a]
++ [[Widget]]
wid2]
)
basicField :: BaseField a => FieldSettings FlexForm -> TypeField a
basicField :: forall a. BaseField a => FieldSettings FlexForm -> TypeField a
basicField = FieldSettings FlexForm -> TypeField a
forall a. BaseField a => FieldSettings FlexForm -> TypeField a
Basic
singleChoiceEnumField
:: (Eq a, Bounded a, Enum a)
=> ChoiceShape
-> FieldSettings FlexForm
-> (a -> SomeMessage FlexForm)
-> TypeField a
singleChoiceEnumField :: forall a.
(Eq a, Bounded a, Enum a) =>
ChoiceShape
-> FieldSettings FlexForm
-> (a -> SomeMessage FlexForm)
-> TypeField a
singleChoiceEnumField ChoiceShape
shape FieldSettings FlexForm
fs = ChoiceShape
-> FieldSettings FlexForm
-> [(SomeMessage FlexForm, a)]
-> TypeField a
forall a.
Eq a =>
ChoiceShape
-> FieldSettings FlexForm
-> [(SomeMessage FlexForm, a)]
-> TypeField a
SingleChoiceField ChoiceShape
shape FieldSettings FlexForm
fs ([(SomeMessage FlexForm, a)] -> TypeField a)
-> ((a -> SomeMessage FlexForm) -> [(SomeMessage FlexForm, a)])
-> (a -> SomeMessage FlexForm)
-> TypeField a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> SomeMessage FlexForm) -> [(SomeMessage FlexForm, a)]
forall b a. (Bounded b, Enum b) => (b -> a) -> [(a, b)]
optionsFromType
singleChoiceField
:: ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> TypeField SingleChoiceSelection
singleChoiceField :: ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> TypeField SingleChoiceSelection
singleChoiceField ChoiceShape
shape FieldSettings FlexForm
fs = ChoiceShape
-> FieldSettings FlexForm
-> [(SomeMessage FlexForm, SingleChoiceSelection)]
-> TypeField SingleChoiceSelection
forall a.
Eq a =>
ChoiceShape
-> FieldSettings FlexForm
-> [(SomeMessage FlexForm, a)]
-> TypeField a
SingleChoiceField ChoiceShape
shape FieldSettings FlexForm
fs ([(SomeMessage FlexForm, SingleChoiceSelection)]
-> TypeField SingleChoiceSelection)
-> ([SomeMessage FlexForm]
-> [(SomeMessage FlexForm, SingleChoiceSelection)])
-> [SomeMessage FlexForm]
-> TypeField SingleChoiceSelection
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [SomeMessage FlexForm]
-> [(SomeMessage FlexForm, SingleChoiceSelection)]
forall a. [a] -> [(a, SingleChoiceSelection)]
options
multipleChoiceField
:: ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> TypeField MultipleChoiceSelection
multipleChoiceField :: ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> TypeField MultipleChoiceSelection
multipleChoiceField ChoiceShape
shape FieldSettings FlexForm
fs = ChoiceShape
-> FieldSettings FlexForm
-> [(SomeMessage FlexForm, SingleChoiceSelection)]
-> TypeField MultipleChoiceSelection
forall a.
Eq a =>
ChoiceShape
-> FieldSettings FlexForm
-> [(SomeMessage FlexForm, a)]
-> TypeField (MultipleChoice a)
MultipleChoiceField ChoiceShape
shape FieldSettings FlexForm
fs ([(SomeMessage FlexForm, SingleChoiceSelection)]
-> TypeField MultipleChoiceSelection)
-> ([SomeMessage FlexForm]
-> [(SomeMessage FlexForm, SingleChoiceSelection)])
-> [SomeMessage FlexForm]
-> TypeField MultipleChoiceSelection
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [SomeMessage FlexForm]
-> [(SomeMessage FlexForm, SingleChoiceSelection)]
forall a. [a] -> [(a, SingleChoiceSelection)]
options
multipleChoiceEnumField
:: (Eq a, Bounded a, Enum a)
=> ChoiceShape
-> FieldSettings FlexForm
-> (a -> SomeMessage FlexForm)
-> TypeField (MultipleChoice a)
multipleChoiceEnumField :: forall a.
(Eq a, Bounded a, Enum a) =>
ChoiceShape
-> FieldSettings FlexForm
-> (a -> SomeMessage FlexForm)
-> TypeField (MultipleChoice a)
multipleChoiceEnumField ChoiceShape
shape FieldSettings FlexForm
fs = ChoiceShape
-> FieldSettings FlexForm
-> [(SomeMessage FlexForm, a)]
-> TypeField (MultipleChoice a)
forall a.
Eq a =>
ChoiceShape
-> FieldSettings FlexForm
-> [(SomeMessage FlexForm, a)]
-> TypeField (MultipleChoice a)
MultipleChoiceField ChoiceShape
shape FieldSettings FlexForm
fs ([(SomeMessage FlexForm, a)] -> TypeField (MultipleChoice a))
-> ((a -> SomeMessage FlexForm) -> [(SomeMessage FlexForm, a)])
-> (a -> SomeMessage FlexForm)
-> TypeField (MultipleChoice a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> SomeMessage FlexForm) -> [(SomeMessage FlexForm, a)]
forall b a. (Bounded b, Enum b) => (b -> a) -> [(a, b)]
optionsFromType
single :: TypeField a -> SimpleFormPiece t a
single :: forall a t. TypeField a -> SimpleFormPiece t a
single = TypeField a -> SimpleFormPiece t a
forall a t. TypeField a -> SimpleFormPiece t a
Single
basic :: BaseField a => FieldSettings FlexForm -> SimpleFormPiece t a
basic :: forall a t.
BaseField a =>
FieldSettings FlexForm -> SimpleFormPiece t a
basic = TypeField a -> SimpleFormPiece t a
forall a t. TypeField a -> SimpleFormPiece t a
single (TypeField a -> SimpleFormPiece t a)
-> (FieldSettings FlexForm -> TypeField a)
-> FieldSettings FlexForm
-> SimpleFormPiece t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldSettings FlexForm -> TypeField a
forall a. BaseField a => FieldSettings FlexForm -> TypeField a
basicField
singleChoice
:: ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> SimpleFormPiece t SingleChoiceSelection
singleChoice :: forall t.
ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> SimpleFormPiece t SingleChoiceSelection
singleChoice ChoiceShape
shape FieldSettings FlexForm
fs = TypeField SingleChoiceSelection
-> SimpleFormPiece t SingleChoiceSelection
forall a t. TypeField a -> SimpleFormPiece t a
single (TypeField SingleChoiceSelection
-> SimpleFormPiece t SingleChoiceSelection)
-> ([SomeMessage FlexForm] -> TypeField SingleChoiceSelection)
-> [SomeMessage FlexForm]
-> SimpleFormPiece t SingleChoiceSelection
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> TypeField SingleChoiceSelection
singleChoiceField ChoiceShape
shape FieldSettings FlexForm
fs
singleChoiceEnum
:: (Eq a, Bounded a, Enum a)
=> ChoiceShape
-> FieldSettings FlexForm
-> (a -> SomeMessage FlexForm)
-> SimpleFormPiece t a
singleChoiceEnum :: forall a t.
(Eq a, Bounded a, Enum a) =>
ChoiceShape
-> FieldSettings FlexForm
-> (a -> SomeMessage FlexForm)
-> SimpleFormPiece t a
singleChoiceEnum ChoiceShape
shape FieldSettings FlexForm
fs = TypeField a -> SimpleFormPiece t a
forall a t. TypeField a -> SimpleFormPiece t a
single (TypeField a -> SimpleFormPiece t a)
-> ((a -> SomeMessage FlexForm) -> TypeField a)
-> (a -> SomeMessage FlexForm)
-> SimpleFormPiece t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceShape
-> FieldSettings FlexForm
-> (a -> SomeMessage FlexForm)
-> TypeField a
forall a.
(Eq a, Bounded a, Enum a) =>
ChoiceShape
-> FieldSettings FlexForm
-> (a -> SomeMessage FlexForm)
-> TypeField a
singleChoiceEnumField ChoiceShape
shape FieldSettings FlexForm
fs
multipleChoice
:: ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> SimpleFormPiece t MultipleChoiceSelection
multipleChoice :: forall t.
ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> SimpleFormPiece t MultipleChoiceSelection
multipleChoice ChoiceShape
shape FieldSettings FlexForm
fs = TypeField MultipleChoiceSelection
-> SimpleFormPiece t MultipleChoiceSelection
forall a t. TypeField a -> SimpleFormPiece t a
single (TypeField MultipleChoiceSelection
-> SimpleFormPiece t MultipleChoiceSelection)
-> ([SomeMessage FlexForm] -> TypeField MultipleChoiceSelection)
-> [SomeMessage FlexForm]
-> SimpleFormPiece t MultipleChoiceSelection
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceShape
-> FieldSettings FlexForm
-> [SomeMessage FlexForm]
-> TypeField MultipleChoiceSelection
multipleChoiceField ChoiceShape
shape FieldSettings FlexForm
fs
multipleChoiceEnum
:: (Eq a, Bounded a, Enum a)
=> ChoiceShape
-> FieldSettings FlexForm
-> (a -> SomeMessage FlexForm)
-> SimpleFormPiece t (MultipleChoice a)
multipleChoiceEnum :: forall a t.
(Eq a, Bounded a, Enum a) =>
ChoiceShape
-> FieldSettings FlexForm
-> (a -> SomeMessage FlexForm)
-> SimpleFormPiece t (MultipleChoice a)
multipleChoiceEnum ChoiceShape
shape FieldSettings FlexForm
fs = TypeField (MultipleChoice a)
-> SimpleFormPiece t (MultipleChoice a)
forall a t. TypeField a -> SimpleFormPiece t a
single (TypeField (MultipleChoice a)
-> SimpleFormPiece t (MultipleChoice a))
-> ((a -> SomeMessage FlexForm) -> TypeField (MultipleChoice a))
-> (a -> SomeMessage FlexForm)
-> SimpleFormPiece t (MultipleChoice a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChoiceShape
-> FieldSettings FlexForm
-> (a -> SomeMessage FlexForm)
-> TypeField (MultipleChoice a)
forall a.
(Eq a, Bounded a, Enum a) =>
ChoiceShape
-> FieldSettings FlexForm
-> (a -> SomeMessage FlexForm)
-> TypeField (MultipleChoice a)
multipleChoiceEnumField ChoiceShape
shape FieldSettings FlexForm
fs
infixr 5 >|
(>|) :: FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
>| :: forall t xs ys.
FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
(>|) = Alignment
-> FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
forall t a ys.
Alignment
-> FormPiece t a -> FormPiece t ys -> FormPiece t (a ++ ys)
Combine Alignment
Horizontal
beside :: FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
beside :: forall t xs ys.
FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
beside = FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
forall t xs ys.
FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
(>|)
infixr 4 >-
(>-) :: FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
>- :: forall t xs ys.
FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
(>-) = Alignment
-> FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
forall t a ys.
Alignment
-> FormPiece t a -> FormPiece t ys -> FormPiece t (a ++ ys)
Combine Alignment
Vertical
above :: FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
above :: forall t xs ys.
FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
above = FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
forall t xs ys.
FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys)
(>-)
list
:: Alignment
-> (a -> TypeField b)
-> [a]
-> ListFormPiece t b
list :: forall a b t.
Alignment -> (a -> TypeField b) -> [a] -> ListFormPiece t b
list Alignment
align a -> TypeField b
builder = Alignment -> [TypeField b] -> ListFormPiece t b
forall a t. Alignment -> [TypeField a] -> ListFormPiece t a
List Alignment
align ([TypeField b] -> ListFormPiece t b)
-> ([a] -> [TypeField b]) -> [a] -> ListFormPiece t b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> TypeField b) -> [a] -> [TypeField b]
forall a b. (a -> b) -> [a] -> [b]
map a -> TypeField b
builder
listWithoutLabels
:: Alignment
-> Int
-> (FieldSettings FlexForm -> TypeField a)
-> [(Text,Text)]
-> ListFormPiece t a
listWithoutLabels :: forall a t.
Alignment
-> Int
-> (FieldSettings FlexForm -> TypeField a)
-> [(Text, Text)]
-> ListFormPiece t a
listWithoutLabels Alignment
align Int
amount FieldSettings FlexForm -> TypeField a
req [(Text, Text)]
attrs =
Alignment
-> (FieldSettings FlexForm -> TypeField a)
-> [FieldSettings FlexForm]
-> ListFormPiece t a
forall a b t.
Alignment -> (a -> TypeField b) -> [a] -> ListFormPiece t b
list Alignment
align FieldSettings FlexForm -> TypeField a
req ([FieldSettings FlexForm] -> ListFormPiece t a)
-> [FieldSettings FlexForm] -> ListFormPiece t a
forall a b. (a -> b) -> a -> b
$ Int -> FieldSettings FlexForm -> [FieldSettings FlexForm]
forall a. Int -> a -> [a]
replicate Int
amount FieldSettings FlexForm
"" {fsAttrs = attrs}
listRepeatedly
:: Alignment
-> Int
-> TypeField a
-> ListFormPiece t a
listRepeatedly :: forall a t. Alignment -> Int -> TypeField a -> ListFormPiece t a
listRepeatedly Alignment
alignment Int
amount = Alignment
-> (TypeField a -> TypeField a)
-> [TypeField a]
-> ListFormPiece t a
forall a b t.
Alignment -> (a -> TypeField b) -> [a] -> ListFormPiece t b
list Alignment
alignment TypeField a -> TypeField a
forall a. a -> a
id ([TypeField a] -> ListFormPiece t a)
-> (TypeField a -> [TypeField a])
-> TypeField a
-> ListFormPiece t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> TypeField a -> [TypeField a]
forall a. Int -> a -> [a]
replicate Int
amount
options :: [a] -> [(a, SingleChoiceSelection)]
options :: forall a. [a] -> [(a, SingleChoiceSelection)]
options [a]
opts = [a] -> [SingleChoiceSelection] -> [(a, SingleChoiceSelection)]
forall a b. [a] -> [b] -> [(a, b)]
zip [a]
opts ([SingleChoiceSelection] -> [(a, SingleChoiceSelection)])
-> [SingleChoiceSelection] -> [(a, SingleChoiceSelection)]
forall a b. (a -> b) -> a -> b
$ (Int -> SingleChoiceSelection) -> [Int] -> [SingleChoiceSelection]
forall a b. (a -> b) -> [a] -> [b]
map Int -> SingleChoiceSelection
singleChoiceAnswer [Int
1..]
optionsFromType :: (Bounded b, Enum b) => (b -> a) -> [(a, b)]
optionsFromType :: forall b a. (Bounded b, Enum b) => (b -> a) -> [(a, b)]
optionsFromType b -> a
f = (b -> (a, b)) -> [b] -> [(a, b)]
forall a b. (a -> b) -> [a] -> [b]
map (\b
x -> (b -> a
f b
x, b
x)) [b
forall a. Bounded a => a
minBound .. b
forall a. Bounded a => a
maxBound]
pieceShape :: FormPiece t xs -> TypeShape xs
pieceShape :: forall t xs. FormPiece t xs -> TypeShape xs
pieceShape Single {} = TypeShape xs
TypeShape (OneField a)
forall a. TypeShape (OneField a)
OneShape
pieceShape List {} = TypeShape xs
TypeShape (ManyFields a)
forall a. TypeShape (ManyFields a)
ManyShape
pieceShape (Combine Alignment
_ FormPiece t xs
x FormPiece t ys
y) = TypeShape xs -> TypeShape ys -> TypeShape (xs ++ ys)
forall xs ys. TypeShape xs -> TypeShape ys -> TypeShape (xs ++ ys)
appendShape (FormPiece t xs -> TypeShape xs
forall t xs. FormPiece t xs -> TypeShape xs
pieceShape FormPiece t xs
x) (TypeShape ys -> TypeShape (xs ++ ys))
-> TypeShape ys -> TypeShape (xs ++ ys)
forall a b. (a -> b) -> a -> b
$ FormPiece t ys -> TypeShape ys
forall t xs. FormPiece t xs -> TypeShape xs
pieceShape FormPiece t ys
y