annotate demo/more/grid.ur @ 954:2a50da66ffd8

Basic tail recursion introduction seems to be working
author Adam Chlipala <adamc@hcoop.net>
date Thu, 17 Sep 2009 16:35:11 -0400
parents 301530da2062
children 3aaac251a5af
rev   line source
adamc@944 1 con colMeta' = fn (row :: Type) (input :: Type) (filter :: Type) =>
adamc@915 2 {Header : string,
adamc@944 3 Project : row -> transaction input,
adamc@944 4 Update : row -> input -> transaction row,
adamc@944 5 Display : input -> xbody,
adamc@944 6 Edit : input -> xbody,
adamc@944 7 Validate : input -> signal bool,
adamc@944 8 CreateFilter : transaction filter,
adamc@944 9 DisplayFilter : filter -> xbody,
adamc@944 10 Filter : filter -> row -> signal bool}
adamc@944 11
adamc@944 12 con colMeta = fn (row :: Type) (global_input_filter :: (Type * Type * Type)) =>
adamc@944 13 {Initialize : transaction global_input_filter.1,
adamc@944 14 Handlers : global_input_filter.1 -> colMeta' row global_input_filter.2 global_input_filter.3}
adamc@915 15
adamc@935 16 con aggregateMeta = fn (row :: Type) (acc :: Type) =>
adamc@935 17 {Initial : acc,
adamc@935 18 Step : row -> acc -> acc,
adamc@935 19 Display : acc -> xbody}
adamc@935 20
adamc@915 21 functor Make(M : sig
adamc@915 22 type row
adamc@936 23 type key
adamc@936 24 val keyOf : row -> key
adamc@936 25
adamc@915 26 val list : transaction (list row)
adamc@915 27 val new : transaction row
adamc@936 28 val save : key -> row -> transaction unit
adamc@936 29 val delete : key -> transaction unit
adamc@915 30
adamc@944 31 con cols :: {(Type * Type * Type)}
adamc@915 32 val cols : $(map (colMeta row) cols)
adamc@915 33
adamc@915 34 val folder : folder cols
adamc@935 35
adamc@935 36 con aggregates :: {Type}
adamc@935 37 val aggregates : $(map (aggregateMeta row) aggregates)
adamc@937 38 val aggFolder : folder aggregates
adamc@915 39 end) = struct
adamc@915 40 style tabl
adamc@915 41 style tr
adamc@915 42 style th
adamc@915 43 style td
adamc@937 44 style agg
adamc@915 45
adamc@944 46 fun make (row : M.row) [input] [filter] (m : colMeta' M.row input filter) : transaction input = m.Project row
adamc@915 47
adamc@944 48 fun makeAll cols row = @@Monad.exec [transaction] _ [map snd3 M.cols]
adamc@944 49 (map2 [fst3] [colMeta M.row] [fn p => transaction (snd3 p)]
adamc@944 50 (fn [p] data meta => make row [_] [_] (meta.Handlers data))
adamc@915 51 [_] M.folder cols M.cols)
adamc@915 52 (@@Folder.mp [_] [_] M.folder)
adamc@915 53
adamc@944 54 type grid = {Cols : $(map fst3 M.cols),
adamc@940 55 Rows : Dlist.dlist {Row : source M.row,
adamc@944 56 Cols : source ($(map snd3 M.cols)),
adamc@940 57 Updating : source bool,
adamc@940 58 Selected : source bool},
adamc@944 59 Selection : source bool,
adamc@944 60 Filters : $(map thd3 M.cols)}
adamc@940 61
adamc@954 62 fun newRow cols row =
adamc@915 63 rowS <- source row;
adamc@915 64 cols <- makeAll cols row;
adamc@915 65 colsS <- source cols;
adamc@915 66 ud <- source False;
adamc@940 67 sd <- source False;
adamc@954 68 return {Row = rowS,
adamc@954 69 Cols = colsS,
adamc@954 70 Updating = ud,
adamc@954 71 Selected = sd}
adamc@954 72
adamc@954 73 fun addRow cols rows row =
adamc@954 74 r <- newRow cols row;
adamc@954 75 Monad.ignore (Dlist.append rows r)
adamc@915 76
adamc@944 77 val grid =
adamc@944 78 cols <- Monad.mapR [colMeta M.row] [fst3]
adamc@944 79 (fn [nm :: Name] [p :: (Type * Type * Type)] meta => meta.Initialize)
adamc@944 80 [_] M.folder M.cols;
adamc@915 81
adamc@944 82 filters <- Monad.mapR2 [colMeta M.row] [fst3] [thd3]
adamc@944 83 (fn [nm :: Name] [p :: (Type * Type * Type)] meta state =>
adamc@944 84 (meta.Handlers state).CreateFilter)
adamc@944 85 [_] M.folder M.cols cols;
adamc@944 86
adamc@951 87 rows <- Dlist.create;
adamc@940 88 sel <- source False;
adamc@951 89
adamc@951 90 return {Cols = cols,
adamc@951 91 Rows = rows,
adamc@951 92 Selection = sel,
adamc@951 93 Filters = filters}
adamc@915 94
adamc@940 95 fun sync {Cols = cols, Rows = rows, ...} =
adamc@915 96 Dlist.clear rows;
adamc@915 97 init <- rpc M.list;
adamc@954 98 rs <- List.mapM (newRow cols) init;
adamc@954 99 Dlist.replace rows rs
adamc@915 100
adamc@915 101 fun render grid = <xml>
adamc@915 102 <table class={tabl}>
adamc@915 103 <tr class={tr}>
adamc@943 104 <th/> <th/> <th/>
adamc@944 105 {foldRX2 [fst3] [colMeta M.row] [_]
adamc@944 106 (fn [nm :: Name] [p :: (Type * Type * Type)] [rest :: {(Type * Type * Type)}] [[nm] ~ rest]
adamc@937 107 data (meta : colMeta M.row p) =>
adamc@937 108 <xml><th class={th}>{[(meta.Handlers data).Header]}</th></xml>)
adamc@937 109 [_] M.folder grid.Cols M.cols}
adamc@937 110 </tr>
adamc@915 111
adamc@940 112 {Dlist.render (fn {Row = rowS, Cols = colsS, Updating = ud, Selected = sd} pos =>
adamc@937 113 let
adamc@937 114 val delete =
adamc@937 115 Dlist.delete pos;
adamc@937 116 row <- get rowS;
adamc@937 117 rpc (M.delete (M.keyOf row))
adamc@915 118
adamc@937 119 val update = set ud True
adamc@915 120
adamc@937 121 val cancel =
adamc@937 122 set ud False;
adamc@937 123 row <- get rowS;
adamc@937 124 cols <- makeAll grid.Cols row;
adamc@937 125 set colsS cols
adamc@937 126
adamc@937 127 val save =
adamc@937 128 cols <- get colsS;
adamc@944 129 errors <- Monad.foldR3 [fst3] [colMeta M.row] [snd3] [fn _ => option string]
adamc@944 130 (fn [nm :: Name] [p :: (Type * Type * Type)] [rest :: {(Type * Type * Type)}]
adamc@937 131 [[nm] ~ rest] data meta v errors =>
adamc@937 132 b <- current ((meta.Handlers data).Validate v);
adamc@937 133 return (if b then
adamc@937 134 errors
adamc@937 135 else
adamc@937 136 case errors of
adamc@937 137 None => Some ((meta.Handlers data).Header)
adamc@937 138 | Some s => Some ((meta.Handlers data).Header
adamc@937 139 ^ ", " ^ s)))
adamc@937 140 None [_] M.folder grid.Cols M.cols cols;
adamc@915 141
adamc@937 142 case errors of
adamc@937 143 Some s => alert ("Can't save because the following columns have invalid values:\n"
adamc@937 144 ^ s)
adamc@937 145 | None =>
adamc@937 146 set ud False;
adamc@937 147 row <- get rowS;
adamc@944 148 row' <- Monad.foldR3 [fst3] [colMeta M.row] [snd3] [fn _ => M.row]
adamc@944 149 (fn [nm :: Name] [t :: (Type * Type * Type)]
adamc@944 150 [rest :: {(Type * Type * Type)}]
adamc@937 151 [[nm] ~ rest] data meta v row' =>
adamc@937 152 (meta.Handlers data).Update row' v)
adamc@937 153 row [_] M.folder grid.Cols M.cols cols;
adamc@937 154 rpc (M.save (M.keyOf row) row');
adamc@937 155 set rowS row';
adamc@937 156
adamc@937 157 cols <- makeAll grid.Cols row';
adamc@937 158 set colsS cols
adamc@937 159 in
adamc@937 160 <xml><tr class={tr}>
adamc@937 161 <td>
adamc@940 162 <dyn signal={b <- signal grid.Selection;
adamc@941 163 return (if b then
adamc@940 164 <xml><ccheckbox source={sd}/></xml>
adamc@940 165 else
adamc@941 166 <xml/>)}/>
adamc@940 167 </td>
adamc@940 168
adamc@940 169 <td>
adamc@937 170 <dyn signal={b <- signal ud;
adamc@937 171 return (if b then
adamc@937 172 <xml><button value="Save" onclick={save}/></xml>
adamc@937 173 else
adamc@937 174 <xml><button value="Update" onclick={update}/></xml>)}/>
adamc@937 175 </td>
adamc@937 176
adamc@937 177 <td><dyn signal={b <- signal ud;
adamc@937 178 return (if b then
adamc@937 179 <xml><button value="Cancel" onclick={cancel}/></xml>
adamc@937 180 else
adamc@937 181 <xml><button value="Delete" onclick={delete}/></xml>)}/>
adamc@937 182 </td>
adamc@937 183
adamc@937 184 <dyn signal={cols <- signal colsS;
adamc@944 185 return (foldRX3 [fst3] [colMeta M.row] [snd3] [_]
adamc@944 186 (fn [nm :: Name] [t :: (Type * Type * Type)]
adamc@944 187 [rest :: {(Type * Type * Type)}]
adamc@937 188 [[nm] ~ rest] data meta v =>
adamc@937 189 <xml><td class={td}>
adamc@937 190 <dyn signal={b <- signal ud;
adamc@937 191 return (if b then
adamc@937 192 (meta.Handlers data).Edit v
adamc@937 193 else
adamc@937 194 (meta.Handlers data).Display
adamc@937 195 v)}/>
adamc@937 196 <dyn signal={b <- signal ud;
adamc@937 197 if b then
adamc@937 198 valid <-
adamc@937 199 (meta.Handlers data).Validate v;
adamc@937 200 return (if valid then
adamc@937 201 <xml/>
adamc@937 202 else
adamc@937 203 <xml>!</xml>)
adamc@937 204 else
adamc@937 205 return <xml/>}/>
adamc@937 206 </td></xml>)
adamc@937 207 [_] M.folder grid.Cols M.cols cols)}/>
adamc@937 208 </tr></xml>
adamc@951 209 end)
adamc@951 210 {Filter = fn all =>
adamc@951 211 row <- signal all.Row;
adamc@951 212 foldR3 [colMeta M.row] [fst3] [thd3] [fn _ => M.row -> signal bool]
adamc@951 213 (fn [nm :: Name] [p :: (Type * Type * Type)]
adamc@951 214 [rest :: {(Type * Type * Type)}] [[nm] ~ rest]
adamc@951 215 meta state filter combinedFilter row =>
adamc@951 216 previous <- combinedFilter row;
adamc@951 217 this <- (meta.Handlers state).Filter filter row;
adamc@951 218 return (previous && this))
adamc@951 219 (fn _ => return True)
adamc@952 220 [_] M.folder M.cols grid.Cols grid.Filters row,
adamc@953 221 Sort = return (Some (fn _ _ => return False))}
adamc@951 222 grid.Rows}
adamc@915 223
adamc@937 224 <dyn signal={rows <- Dlist.foldl (fn row => Monad.mapR2 [aggregateMeta M.row] [id] [id]
adamc@937 225 (fn [nm :: Name] [t :: Type] meta acc =>
adamc@937 226 Monad.mp (fn v => meta.Step v acc)
adamc@937 227 (signal row.Row))
adamc@937 228 [_] M.aggFolder M.aggregates)
adamc@937 229 (mp [aggregateMeta M.row] [id]
adamc@937 230 (fn [t] meta => meta.Initial)
adamc@937 231 [_] M.aggFolder M.aggregates) grid.Rows;
adamc@937 232 return <xml><tr>
adamc@941 233 <th colspan={3}>Aggregates</th>
adamc@937 234 {foldRX2 [aggregateMeta M.row] [id] [_]
adamc@937 235 (fn [nm :: Name] [t :: Type] [rest :: {Type}] [[nm] ~ rest] meta acc =>
adamc@937 236 <xml><td class={agg}>{meta.Display acc}</td></xml>)
adamc@937 237 [_] M.aggFolder M.aggregates rows}
adamc@937 238 </tr></xml>}/>
adamc@944 239
adamc@944 240 <tr><th colspan={3}>Filters</th>
adamc@944 241 {foldRX3 [colMeta M.row] [fst3] [thd3] [_]
adamc@944 242 (fn [nm :: Name] [p :: (Type * Type * Type)] [rest :: {(Type * Type * Type)}] [[nm] ~ rest]
adamc@944 243 meta state filter => <xml><td>{(meta.Handlers state).DisplayFilter filter}</td></xml>)
adamc@944 244 [_] M.folder M.cols grid.Cols grid.Filters}
adamc@944 245 </tr>
adamc@915 246 </table>
adamc@915 247
adamc@915 248 <button value="New row" onclick={row <- rpc M.new;
adamc@915 249 addRow grid.Cols grid.Rows row}/>
adamc@915 250 <button value="Refresh" onclick={sync grid}/>
adamc@915 251 </xml>
adamc@940 252
adamc@940 253 fun showSelection grid = grid.Selection
adamc@940 254
adamc@940 255 fun selection grid = Dlist.foldl (fn {Row = rowS, Selected = sd, ...} ls =>
adamc@940 256 sd <- signal sd;
adamc@940 257 if sd then
adamc@940 258 row <- signal rowS;
adamc@940 259 return (row :: ls)
adamc@940 260 else
adamc@940 261 return ls) [] grid.Rows
adamc@915 262 end