annotate demo/more/grid.ur @ 937:37dd42935dad

Summary row with aggregates
author Adam Chlipala <adamc@hcoop.net>
date Tue, 15 Sep 2009 10:18:56 -0400
parents 6966d98c80b5
children e2be476673f2
rev   line source
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@937 35 val aggFolder : folder aggregates
adamc@915 36 end) = struct
adamc@915 37 style tabl
adamc@915 38 style tr
adamc@915 39 style th
adamc@915 40 style td
adamc@937 41 style agg
adamc@915 42
adamc@915 43 fun make (row : M.row) [t] (m : colMeta' M.row t) : transaction t = m.Project row
adamc@915 44
adamc@915 45 fun makeAll cols row = @@Monad.exec [transaction] _ [map snd M.cols]
adamc@915 46 (map2 [fst] [colMeta M.row] [fn p :: (Type * Type) => transaction p.2]
adamc@915 47 (fn [p] data meta => make row [_] (meta.Handlers data))
adamc@915 48 [_] M.folder cols M.cols)
adamc@915 49 (@@Folder.mp [_] [_] M.folder)
adamc@915 50
adamc@915 51 fun addRow cols rows row =
adamc@915 52 rowS <- source row;
adamc@915 53 cols <- makeAll cols row;
adamc@915 54 colsS <- source cols;
adamc@915 55 ud <- source False;
adamc@915 56 Monad.ignore (Dlist.append rows {Row = rowS,
adamc@915 57 Cols = colsS,
adamc@915 58 Updating = ud})
adamc@915 59
adamc@915 60 type grid = {Cols : $(map fst M.cols),
adamc@915 61 Rows : Dlist.dlist {Row : source M.row, Cols : source ($(map snd M.cols)), Updating : source bool}}
adamc@915 62
adamc@915 63 val createMetas = Monad.mapR [colMeta M.row] [fst]
adamc@915 64 (fn [nm :: Name] [p :: (Type * Type)] meta => meta.Initialize)
adamc@915 65 [_] M.folder M.cols
adamc@915 66
adamc@915 67 val grid =
adamc@915 68 cols <- createMetas;
adamc@915 69 rows <- Dlist.create;
adamc@915 70 return {Cols = cols, Rows = rows}
adamc@915 71
adamc@915 72 fun sync {Cols = cols, Rows = rows} =
adamc@915 73 Dlist.clear rows;
adamc@915 74 init <- rpc M.list;
adamc@915 75 List.app (addRow cols rows) init
adamc@915 76
adamc@915 77 fun render grid = <xml>
adamc@915 78 <table class={tabl}>
adamc@915 79 <tr class={tr}>
adamc@915 80 <th/> <th/>
adamc@915 81 {foldRX2 [fst] [colMeta M.row] [_]
adamc@937 82 (fn [nm :: Name] [p :: (Type * Type)] [rest :: {(Type * Type)}] [[nm] ~ rest]
adamc@937 83 data (meta : colMeta M.row p) =>
adamc@937 84 <xml><th class={th}>{[(meta.Handlers data).Header]}</th></xml>)
adamc@937 85 [_] M.folder grid.Cols M.cols}
adamc@937 86 </tr>
adamc@915 87
adamc@937 88 {Dlist.render (fn {Row = rowS, Cols = colsS, Updating = ud} pos =>
adamc@937 89 let
adamc@937 90 val delete =
adamc@937 91 Dlist.delete pos;
adamc@937 92 row <- get rowS;
adamc@937 93 rpc (M.delete (M.keyOf row))
adamc@915 94
adamc@937 95 val update = set ud True
adamc@915 96
adamc@937 97 val cancel =
adamc@937 98 set ud False;
adamc@937 99 row <- get rowS;
adamc@937 100 cols <- makeAll grid.Cols row;
adamc@937 101 set colsS cols
adamc@937 102
adamc@937 103 val save =
adamc@937 104 cols <- get colsS;
adamc@937 105 errors <- Monad.foldR3 [fst] [colMeta M.row] [snd] [fn _ => option string]
adamc@937 106 (fn [nm :: Name] [p :: (Type * Type)] [rest :: {(Type * Type)}]
adamc@937 107 [[nm] ~ rest] data meta v errors =>
adamc@937 108 b <- current ((meta.Handlers data).Validate v);
adamc@937 109 return (if b then
adamc@937 110 errors
adamc@937 111 else
adamc@937 112 case errors of
adamc@937 113 None => Some ((meta.Handlers data).Header)
adamc@937 114 | Some s => Some ((meta.Handlers data).Header
adamc@937 115 ^ ", " ^ s)))
adamc@937 116 None [_] M.folder grid.Cols M.cols cols;
adamc@915 117
adamc@937 118 case errors of
adamc@937 119 Some s => alert ("Can't save because the following columns have invalid values:\n"
adamc@937 120 ^ s)
adamc@937 121 | None =>
adamc@937 122 set ud False;
adamc@937 123 row <- get rowS;
adamc@937 124 row' <- Monad.foldR3 [fst] [colMeta M.row] [snd] [fn _ => M.row]
adamc@937 125 (fn [nm :: Name] [t :: (Type * Type)]
adamc@937 126 [rest :: {(Type * Type)}]
adamc@937 127 [[nm] ~ rest] data meta v row' =>
adamc@937 128 (meta.Handlers data).Update row' v)
adamc@937 129 row [_] M.folder grid.Cols M.cols cols;
adamc@937 130 rpc (M.save (M.keyOf row) row');
adamc@937 131 set rowS row';
adamc@937 132
adamc@937 133 cols <- makeAll grid.Cols row';
adamc@937 134 set colsS cols
adamc@937 135 in
adamc@937 136 <xml><tr class={tr}>
adamc@937 137 <td>
adamc@937 138 <dyn signal={b <- signal ud;
adamc@937 139 return (if b then
adamc@937 140 <xml><button value="Save" onclick={save}/></xml>
adamc@937 141 else
adamc@937 142 <xml><button value="Update" onclick={update}/></xml>)}/>
adamc@937 143 </td>
adamc@937 144
adamc@937 145 <td><dyn signal={b <- signal ud;
adamc@937 146 return (if b then
adamc@937 147 <xml><button value="Cancel" onclick={cancel}/></xml>
adamc@937 148 else
adamc@937 149 <xml><button value="Delete" onclick={delete}/></xml>)}/>
adamc@937 150 </td>
adamc@937 151
adamc@937 152 <dyn signal={cols <- signal colsS;
adamc@937 153 return (foldRX3 [fst] [colMeta M.row] [snd] [_]
adamc@915 154 (fn [nm :: Name] [t :: (Type * Type)]
adamc@915 155 [rest :: {(Type * Type)}]
adamc@937 156 [[nm] ~ rest] data meta v =>
adamc@937 157 <xml><td class={td}>
adamc@937 158 <dyn signal={b <- signal ud;
adamc@937 159 return (if b then
adamc@937 160 (meta.Handlers data).Edit v
adamc@937 161 else
adamc@937 162 (meta.Handlers data).Display
adamc@937 163 v)}/>
adamc@937 164 <dyn signal={b <- signal ud;
adamc@937 165 if b then
adamc@937 166 valid <-
adamc@937 167 (meta.Handlers data).Validate v;
adamc@937 168 return (if valid then
adamc@937 169 <xml/>
adamc@937 170 else
adamc@937 171 <xml>!</xml>)
adamc@937 172 else
adamc@937 173 return <xml/>}/>
adamc@937 174 </td></xml>)
adamc@937 175 [_] M.folder grid.Cols M.cols cols)}/>
adamc@937 176 </tr></xml>
adamc@937 177 end) grid.Rows}
adamc@915 178
adamc@937 179 <dyn signal={rows <- Dlist.foldl (fn row => Monad.mapR2 [aggregateMeta M.row] [id] [id]
adamc@937 180 (fn [nm :: Name] [t :: Type] meta acc =>
adamc@937 181 Monad.mp (fn v => meta.Step v acc)
adamc@937 182 (signal row.Row))
adamc@937 183 [_] M.aggFolder M.aggregates)
adamc@937 184 (mp [aggregateMeta M.row] [id]
adamc@937 185 (fn [t] meta => meta.Initial)
adamc@937 186 [_] M.aggFolder M.aggregates) grid.Rows;
adamc@937 187 return <xml><tr>
adamc@937 188 <td/><td/>
adamc@937 189 {foldRX2 [aggregateMeta M.row] [id] [_]
adamc@937 190 (fn [nm :: Name] [t :: Type] [rest :: {Type}] [[nm] ~ rest] meta acc =>
adamc@937 191 <xml><td class={agg}>{meta.Display acc}</td></xml>)
adamc@937 192 [_] M.aggFolder M.aggregates rows}
adamc@937 193 </tr></xml>}/>
adamc@915 194 </table>
adamc@915 195
adamc@915 196 <button value="New row" onclick={row <- rpc M.new;
adamc@915 197 addRow grid.Cols grid.Rows row}/>
adamc@915 198 <button value="Refresh" onclick={sync grid}/>
adamc@915 199 </xml>
adamc@915 200 end