0 | module Web.Async.Form
2 | import Data.List.Quantifiers.Extra
4 | import public Control.Barbie
5 | import public Data.Singleton
6 | import public Monocle
7 | import public Web.Async
8 | import public Web.Async.Widget
9 | import Text.HTML.DomID
17 | record FormField where
22 | toField : (String,Widget t) -> Maybe FormField
23 | toField (nm, W Empty _) = Nothing
24 | toField (nm, W n _) = Just (FF nm n)
26 | formStream : List (Widget (t -> t)) -> t -> JSStream t
27 | formStream ws ini = merge (map events ws) |> scanFrom1 ini
29 | parameters {0 f : Type}
30 | {auto ipf : Interpolation f}
31 | (0 rec : (f -> Type) -> Type)
32 | (formNode : List FormField -> HTMLNode)
34 | {auto sings : rec Singleton}
35 | {auto fuc : FunctorB f rec}
36 | {auto app : ApplicativeB f rec}
37 | {auto trv : TraversableB f rec}
38 | {auto rrd : RecordB f rec}
44 | WForm = (String,Widget (rec (EditRes . g) -> rec (EditRes . g)))
57 | editField (Val v) (E fun) mrec = do
59 | W n s <- fun $
map (field g v).get_ mrec
63 | let s2 := s |> P.mapOutput (set (field' v))
65 | pure $
(interpolate v, W n s2)
83 | form : rec (Editor . g) -> Editor (rec g)
86 | let eflds := bzipWith editField sings edits
92 | missAll := the (rec (EditRes . g)) (bpure @{app} Missing)
96 | recw <- btraverse (flip apply recm) eflds
99 | let ws := bfoldMap (Prelude.Lin:<) recw <>> []
102 | node := formNode (mapMaybe toField ws)
104 | pure $
W node (formStream (map snd ws) missAll |> mapOutput bsequence)
106 | parameters {0 ts : List Type}
107 | (formNode : List HTMLNode -> HTMLNode)
108 | {auto els : All (`Elem` ts) ts}
111 | HForm = (Widget (All EditRes ts -> All EditRes ts))
113 | hfield : Elem t ts -> Editor t -> Maybe (HList ts) -> Act HForm
114 | hfield e (E fun) mrec = do
115 | W n s <- fun $
Lens.get_ (prod t) <$> mrec
116 | let s2 := s |> P.mapOutput (set $
prod t)
135 | hform : All Editor ts -> Editor (HList ts)
137 | let eflds := hzipWith hfield els edits
138 | miss := mapProperty {q = EditRes} (\_ => Missing) edits
142 | recw <- hsequence $
mapProperty (flip apply recm) eflds
144 | let ws := hfoldMap (Prelude.Lin:<) recw <>> []
147 | node := formNode (map node ws)
149 | pure $
W node (formStream ws miss |> mapOutput hsequence)