annotate demo/more/grid.ur @ 935:2422360c78a3

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