0 | module Text.HTML.Select
4 | import FS.Concurrent.Signal
10 | data SelectEntry : (t : Type) -> Type where
11 | Title : String -> SelectEntry t
12 | Entry : (val : t) -> String -> SelectEntry t
15 | record SelectEv t where
22 | Functor SelectEv where
23 | map f (SE i l v) = SE i l $
f v
29 | selectIx : List (SelectEntry t) -> Nat -> Maybe (SelectEv t)
30 | selectIx vs n = go vs n
32 | go : List (SelectEntry t) -> Nat -> Maybe (SelectEv t)
34 | go (Entry v s :: xs) 0 = Just (SE n s v)
35 | go (Title _ :: xs) 0 = Nothing
36 | go (x :: xs) (S k) = go xs k
38 | app : {0 a : _} -> SnocList (a,List b) -> SnocList b -> a -> SnocList (a,List b)
40 | app sx sy x = sx :< (x, sy <>> [])
43 | SnocList (String,List (Nat,t,String))
44 | -> SnocList (Nat,t,String)
47 | -> List (SelectEntry t)
48 | -> List (String,List (Nat,t,String))
49 | groups sg sp l n [] = app sg sp l <>> []
50 | groups sg sp l n (Title s :: xs) = groups (app sg sp l) [<] s (S n) xs
51 | groups sg sp l n (Entry v s :: xs) = groups sg (sp :< (n,v,s)) l (S n) xs
55 | {auto snk : Sink (SelectEv e)}
56 | -> (entries : List (SelectEntry t))
57 | -> (sel : t -> Bool)
58 | -> (toEvent : t -> e)
59 | -> (attrs : List (Attribute Select))
61 | selectEntries es sel toEv attrs =
66 | (attrs ++ [onChangeMaybe (map (map toEv) . selectIx es . cast)])
67 | (groups [<] [<] "" 0 es >>= grp)
69 | opt : (Nat,t,String) -> HTMLNode
70 | opt (x,v,s) = option [value (show x), selected (sel v)] [Text s]
72 | grp : (String,List (Nat,t,String)) -> HTMLNodes
73 | grp ("",ps) = map opt ps
74 | grp (s,ps) = [optgroup [label s] $
map opt ps]
86 | {auto snk : Sink (SelectEv e)}
87 | -> (values : List t)
88 | -> (sel : t -> Bool)
89 | -> (display : t -> String)
90 | -> (toEvent : t -> e)
91 | -> (attrs : List (Attribute Select))
93 | selectFromListBy vs sel f = selectEntries ((\x => Entry x $
f x) <$> vs) sel
105 | {auto snk : Sink (SelectEv e)}
106 | -> {auto eq : Eq t}
107 | -> (values : List t)
108 | -> (init : Maybe t)
109 | -> (display : t -> String)
110 | -> (toEvent : t -> e)
111 | -> (attrs : List (Attribute Select))
113 | selectFromList vs i = selectFromListBy vs ((i ==) . Just)
115 | parameters {auto snk : Sink e}
121 | (entries : List (SelectEntry t))
122 | -> (sel : t -> Bool)
123 | -> (toEvent : t -> e)
124 | -> (attrs : List (Attribute Select))
126 | selectEntries' = selectEntries @{cmap value snk}
131 | selectFromListBy' :
133 | -> (sel : t -> Bool)
134 | -> (display : t -> String)
135 | -> (toEvent : t -> e)
136 | -> (attrs : List (Attribute Select))
138 | selectFromListBy' = selectFromListBy @{cmap value snk}
145 | -> (values : List t)
146 | -> (init : Maybe t)
147 | -> (display : t -> String)
148 | -> (toEvent : t -> e)
149 | -> (attrs : List (Attribute Select))
151 | selectFromList' = selectFromList @{cmap value snk}