adamc@915
|
1 con colMeta' = fn (row :: Type) (t :: Type) =>
|
adamc@915
|
2 {Header : string,
|
adamc@915
|
3 Project : row -> transaction t,
|
adamc@915
|
4 Update : row -> t -> transaction row,
|
adamc@915
|
5 Display : t -> xbody,
|
adamc@915
|
6 Edit : t -> xbody,
|
adamc@915
|
7 Validate : t -> signal bool}
|
adamc@915
|
8
|
adamc@915
|
9 con colMeta = fn (row :: Type) (global_t :: (Type * Type)) =>
|
adamc@915
|
10 {Initialize : transaction global_t.1,
|
adamc@915
|
11 Handlers : global_t.1 -> colMeta' row global_t.2}
|
adamc@915
|
12
|
adamc@935
|
13 con aggregateMeta = fn (row :: Type) (acc :: Type) =>
|
adamc@935
|
14 {Initial : acc,
|
adamc@935
|
15 Step : row -> acc -> acc,
|
adamc@935
|
16 Display : acc -> xbody}
|
adamc@935
|
17
|
adamc@915
|
18 functor Make(M : sig
|
adamc@915
|
19 type row
|
adamc@936
|
20 type key
|
adamc@936
|
21 val keyOf : row -> key
|
adamc@936
|
22
|
adamc@915
|
23 val list : transaction (list row)
|
adamc@915
|
24 val new : transaction row
|
adamc@936
|
25 val save : key -> row -> transaction unit
|
adamc@936
|
26 val delete : key -> transaction unit
|
adamc@915
|
27
|
adamc@915
|
28 con cols :: {(Type * Type)}
|
adamc@915
|
29 val cols : $(map (colMeta row) cols)
|
adamc@915
|
30
|
adamc@915
|
31 val folder : folder cols
|
adamc@935
|
32
|
adamc@935
|
33 con aggregates :: {Type}
|
adamc@935
|
34 val aggregates : $(map (aggregateMeta row) aggregates)
|
adamc@915
|
35 end) = struct
|
adamc@915
|
36 style tabl
|
adamc@915
|
37 style tr
|
adamc@915
|
38 style th
|
adamc@915
|
39 style td
|
adamc@915
|
40
|
adamc@915
|
41 fun make (row : M.row) [t] (m : colMeta' M.row t) : transaction t = m.Project row
|
adamc@915
|
42
|
adamc@915
|
43 fun makeAll cols row = @@Monad.exec [transaction] _ [map snd M.cols]
|
adamc@915
|
44 (map2 [fst] [colMeta M.row] [fn p :: (Type * Type) => transaction p.2]
|
adamc@915
|
45 (fn [p] data meta => make row [_] (meta.Handlers data))
|
adamc@915
|
46 [_] M.folder cols M.cols)
|
adamc@915
|
47 (@@Folder.mp [_] [_] M.folder)
|
adamc@915
|
48
|
adamc@915
|
49 fun addRow cols rows row =
|
adamc@915
|
50 rowS <- source row;
|
adamc@915
|
51 cols <- makeAll cols row;
|
adamc@915
|
52 colsS <- source cols;
|
adamc@915
|
53 ud <- source False;
|
adamc@915
|
54 Monad.ignore (Dlist.append rows {Row = rowS,
|
adamc@915
|
55 Cols = colsS,
|
adamc@915
|
56 Updating = ud})
|
adamc@915
|
57
|
adamc@915
|
58 type grid = {Cols : $(map fst M.cols),
|
adamc@915
|
59 Rows : Dlist.dlist {Row : source M.row, Cols : source ($(map snd M.cols)), Updating : source bool}}
|
adamc@915
|
60
|
adamc@915
|
61 val createMetas = Monad.mapR [colMeta M.row] [fst]
|
adamc@915
|
62 (fn [nm :: Name] [p :: (Type * Type)] meta => meta.Initialize)
|
adamc@915
|
63 [_] M.folder M.cols
|
adamc@915
|
64
|
adamc@915
|
65 val grid =
|
adamc@915
|
66 cols <- createMetas;
|
adamc@915
|
67 rows <- Dlist.create;
|
adamc@915
|
68 return {Cols = cols, Rows = rows}
|
adamc@915
|
69
|
adamc@915
|
70 fun sync {Cols = cols, Rows = rows} =
|
adamc@915
|
71 Dlist.clear rows;
|
adamc@915
|
72 init <- rpc M.list;
|
adamc@915
|
73 List.app (addRow cols rows) init
|
adamc@915
|
74
|
adamc@915
|
75 fun render grid = <xml>
|
adamc@915
|
76 <table class={tabl}>
|
adamc@915
|
77 <tr class={tr}>
|
adamc@915
|
78 <th/> <th/>
|
adamc@915
|
79 {foldRX2 [fst] [colMeta M.row] [_]
|
adamc@915
|
80 (fn [nm :: Name] [p :: (Type * Type)] [rest :: {(Type * Type)}] [[nm] ~ rest]
|
adamc@915
|
81 data (meta : colMeta M.row p) =>
|
adamc@915
|
82 <xml><th class={th}>{[(meta.Handlers data).Header]}</th></xml>)
|
adamc@915
|
83 [_] M.folder grid.Cols M.cols}
|
adamc@915
|
84 </tr>
|
adamc@915
|
85
|
adamc@915
|
86 {Dlist.render (fn {Row = rowS, Cols = colsS, Updating = ud} pos =>
|
adamc@915
|
87 let
|
adamc@915
|
88 val delete =
|
adamc@915
|
89 Dlist.delete pos;
|
adamc@915
|
90 row <- get rowS;
|
adamc@936
|
91 rpc (M.delete (M.keyOf row))
|
adamc@915
|
92
|
adamc@915
|
93 val update = set ud True
|
adamc@915
|
94
|
adamc@915
|
95 val cancel =
|
adamc@915
|
96 set ud False;
|
adamc@915
|
97 row <- get rowS;
|
adamc@915
|
98 cols <- makeAll grid.Cols row;
|
adamc@915
|
99 set colsS cols
|
adamc@915
|
100
|
adamc@915
|
101 val save =
|
adamc@915
|
102 cols <- get colsS;
|
adamc@915
|
103 errors <- Monad.foldR3 [fst] [colMeta M.row] [snd] [fn _ => option string]
|
adamc@915
|
104 (fn [nm :: Name] [p :: (Type * Type)] [rest :: {(Type * Type)}]
|
adamc@915
|
105 [[nm] ~ rest] data meta v errors =>
|
adamc@915
|
106 b <- current ((meta.Handlers data).Validate v);
|
adamc@915
|
107 return (if b then
|
adamc@915
|
108 errors
|
adamc@915
|
109 else
|
adamc@915
|
110 case errors of
|
adamc@915
|
111 None => Some ((meta.Handlers data).Header)
|
adamc@915
|
112 | Some s => Some ((meta.Handlers data).Header
|
adamc@915
|
113 ^ ", " ^ s)))
|
adamc@915
|
114 None [_] M.folder grid.Cols M.cols cols;
|
adamc@915
|
115
|
adamc@915
|
116 case errors of
|
adamc@915
|
117 Some s => alert ("Can't save because the following columns have invalid values:\n"
|
adamc@915
|
118 ^ s)
|
adamc@915
|
119 | None =>
|
adamc@915
|
120 set ud False;
|
adamc@915
|
121 row <- get rowS;
|
adamc@915
|
122 row' <- Monad.foldR3 [fst] [colMeta M.row] [snd] [fn _ => M.row]
|
adamc@915
|
123 (fn [nm :: Name] [t :: (Type * Type)]
|
adamc@915
|
124 [rest :: {(Type * Type)}]
|
adamc@915
|
125 [[nm] ~ rest] data meta v row' =>
|
adamc@915
|
126 (meta.Handlers data).Update row' v)
|
adamc@915
|
127 row [_] M.folder grid.Cols M.cols cols;
|
adamc@936
|
128 rpc (M.save (M.keyOf row) row');
|
adamc@915
|
129 set rowS row';
|
adamc@915
|
130
|
adamc@915
|
131 cols <- makeAll grid.Cols row';
|
adamc@915
|
132 set colsS cols
|
adamc@915
|
133 in
|
adamc@915
|
134 <xml><tr class={tr}>
|
adamc@915
|
135 <td>
|
adamc@915
|
136 <dyn signal={b <- signal ud;
|
adamc@915
|
137 return (if b then
|
adamc@915
|
138 <xml><button value="Save" onclick={save}/></xml>
|
adamc@915
|
139 else
|
adamc@915
|
140 <xml><button value="Update" onclick={update}/></xml>)}/>
|
adamc@915
|
141 </td>
|
adamc@915
|
142 <td><dyn signal={b <- signal ud;
|
adamc@915
|
143 return (if b then
|
adamc@915
|
144 <xml><button value="Cancel" onclick={cancel}/></xml>
|
adamc@915
|
145 else
|
adamc@915
|
146 <xml><button value="Delete" onclick={delete}/></xml>)}/>
|
adamc@915
|
147 </td>
|
adamc@915
|
148
|
adamc@915
|
149 <dyn signal={cols <- signal colsS;
|
adamc@915
|
150 return (foldRX3 [fst] [colMeta M.row] [snd] [_]
|
adamc@915
|
151 (fn [nm :: Name] [t :: (Type * Type)]
|
adamc@915
|
152 [rest :: {(Type * Type)}]
|
adamc@915
|
153 [[nm] ~ rest] data meta v =>
|
adamc@915
|
154 <xml><td class={td}>
|
adamc@915
|
155 <dyn signal={b <- signal ud;
|
adamc@915
|
156 return (if b then
|
adamc@915
|
157 (meta.Handlers data).Edit v
|
adamc@915
|
158 else
|
adamc@915
|
159 (meta.Handlers data).Display
|
adamc@915
|
160 v)}/>
|
adamc@915
|
161 <dyn signal={b <- signal ud;
|
adamc@915
|
162 if b then
|
adamc@915
|
163 valid <-
|
adamc@915
|
164 (meta.Handlers data).Validate v;
|
adamc@915
|
165 return (if valid then
|
adamc@915
|
166 <xml/>
|
adamc@915
|
167 else
|
adamc@915
|
168 <xml>!</xml>)
|
adamc@915
|
169 else
|
adamc@915
|
170 return <xml/>}/>
|
adamc@915
|
171 </td></xml>)
|
adamc@915
|
172 [_] M.folder grid.Cols M.cols cols)}/>
|
adamc@915
|
173 </tr></xml>
|
adamc@915
|
174 end) grid.Rows}
|
adamc@915
|
175 </table>
|
adamc@915
|
176
|
adamc@915
|
177 <button value="New row" onclick={row <- rpc M.new;
|
adamc@915
|
178 addRow grid.Cols grid.Rows row}/>
|
adamc@915
|
179 <button value="Refresh" onclick={sync grid}/>
|
adamc@915
|
180 </xml>
|
adamc@915
|
181 end
|