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
18 | ix : List (SelectEntry t) -> Nat -> Maybe t
20 | ix (Entry v _ :: xs) 0 = Just v
21 | ix (Title s :: xs) 0 = Nothing
22 | ix (x :: xs) (S k) = ix xs k
24 | app : {0 a : _} -> SnocList (a,List b) -> SnocList b -> a -> SnocList (a,List b)
26 | app sx sy x = sx :< (x, sy <>> [])
29 | SnocList (String,List (Nat,t,String))
30 | -> SnocList (Nat,t,String)
33 | -> List (SelectEntry t)
34 | -> List (String,List (Nat,t,String))
35 | groups sg sp l n [] = app sg sp l <>> []
36 | groups sg sp l n (Title s :: xs) = groups (app sg sp l) [<] s (S n) xs
37 | groups sg sp l n (Entry v s :: xs) = groups sg (sp :< (n,v,s)) l (S n) xs
42 | -> (entries : List (SelectEntry t))
43 | -> (sel : t -> Bool)
44 | -> (toEvent : t -> e)
45 | -> (attrs : List (Attribute Select))
47 | selectEntries es sel toEv attrs =
52 | (attrs ++ [onChangeMaybe (map toEv . ix es . cast)])
53 | (groups [<] [<] "" 0 es >>= grp)
55 | opt : (Nat,t,String) -> HTMLNode
56 | opt (x,v,s) = option [value (show x), selected (sel v)] [Text s]
58 | grp : (String,List (Nat,t,String)) -> HTMLNodes
59 | grp ("",ps) = map opt ps
60 | grp (s,ps) = [optgroup [label s] $
map opt ps]
73 | -> (values : List t)
74 | -> (sel : t -> Bool)
75 | -> (display : t -> String)
76 | -> (toEvent : t -> e)
77 | -> (attrs : List (Attribute Select))
79 | selectFromListBy vs sel f = selectEntries ((\x => Entry x $
f x) <$> vs) sel
93 | -> (values : List t)
95 | -> (display : t -> String)
96 | -> (toEvent : t -> e)
97 | -> (attrs : List (Attribute Select))
99 | selectFromList vs i = selectFromListBy vs ((i ==) . Just)