annotate demo/more/grid.ur @ 944:da3ec6014d2f

Filters implementation type-checking
author Adam Chlipala <adamc@hcoop.net>
date Tue, 15 Sep 2009 15:48:53 -0400
parents e2194a6793ae
children 103ac1792c41
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@915 62 fun addRow cols rows 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@915 68 Monad.ignore (Dlist.append rows {Row = rowS,
adamc@915 69 Cols = colsS,
adamc@940 70 Updating = ud,
adamc@940 71 Selected = sd})
adamc@915 72
adamc@944 73 val grid =
adamc@944 74 cols <- Monad.mapR [colMeta M.row] [fst3]
adamc@944 75 (fn [nm :: Name] [p :: (Type * Type * Type)] meta => meta.Initialize)
adamc@944 76 [_] M.folder M.cols;
adamc@915 77
adamc@944 78 filters <- Monad.mapR2 [colMeta M.row] [fst3] [thd3]
adamc@944 79 (fn [nm :: Name] [p :: (Type * Type * Type)] meta state =>
adamc@944 80 (meta.Handlers state).CreateFilter)
adamc@944 81 [_] M.folder M.cols cols;
adamc@944 82
adamc@944 83 rows <- Dlist.create {Filter = fn all =>
adamc@944 84 row <- signal all.Row;
adamc@944 85 foldR3 [colMeta M.row] [fst3] [thd3] [fn _ => M.row -> signal bool]
adamc@944 86 (fn [nm :: Name] [p :: (Type * Type * Type)]
adamc@944 87 [rest :: {(Type * Type * Type)}] [[nm] ~ rest]
adamc@944 88 meta state filter combinedFilter row =>
adamc@944 89 previous <- combinedFilter row;
adamc@944 90 this <- (meta.Handlers state).Filter filter row;
adamc@944 91 return (previous && this))
adamc@944 92 (fn _ => return True)
adamc@944 93 [_] M.folder M.cols cols filters row};
adamc@940 94 sel <- source False;
adamc@944 95 return {Cols = cols, Rows = rows, Selection = sel, Filters = filters}
adamc@915 96
adamc@940 97 fun sync {Cols = cols, Rows = rows, ...} =
adamc@915 98 Dlist.clear rows;
adamc@915 99 init <- rpc M.list;
adamc@915 100 List.app (addRow cols rows) init
adamc@915 101
adamc@915 102 fun render grid = <xml>
adamc@915 103 <table class={tabl}>
adamc@915 104 <tr class={tr}>
adamc@943 105 <th/> <th/> <th/>
adamc@944 106 {foldRX2 [fst3] [colMeta M.row] [_]
adamc@944 107 (fn [nm :: Name] [p :: (Type * Type * Type)] [rest :: {(Type * Type * Type)}] [[nm] ~ rest]
adamc@937 108 data (meta : colMeta M.row p) =>
adamc@937 109 <xml><th class={th}>{[(meta.Handlers data).Header]}</th></xml>)
adamc@937 110 [_] M.folder grid.Cols M.cols}
adamc@937 111 </tr>
adamc@915 112
adamc@940 113 {Dlist.render (fn {Row = rowS, Cols = colsS, Updating = ud, Selected = sd} pos =>
adamc@937 114 let
adamc@937 115 val delete =
adamc@937 116 Dlist.delete pos;
adamc@937 117 row <- get rowS;
adamc@937 118 rpc (M.delete (M.keyOf row))
adamc@915 119
adamc@937 120 val update = set ud True
adamc@915 121
adamc@937 122 val cancel =
adamc@937 123 set ud False;
adamc@937 124 row <- get rowS;
adamc@937 125 cols <- makeAll grid.Cols row;
adamc@937 126 set colsS cols
adamc@937 127
adamc@937 128 val save =
adamc@937 129 cols <- get colsS;
adamc@944 130 errors <- Monad.foldR3 [fst3] [colMeta M.row] [snd3] [fn _ => option string]
adamc@944 131 (fn [nm :: Name] [p :: (Type * Type * Type)] [rest :: {(Type * Type * Type)}]
adamc@937 132 [[nm] ~ rest] data meta v errors =>
adamc@937 133 b <- current ((meta.Handlers data).Validate v);
adamc@937 134 return (if b then
adamc@937 135 errors
adamc@937 136 else
adamc@937 137 case errors of
adamc@937 138 None => Some ((meta.Handlers data).Header)
adamc@937 139 | Some s => Some ((meta.Handlers data).Header
adamc@937 140 ^ ", " ^ s)))
adamc@937 141 None [_] M.folder grid.Cols M.cols cols;
adamc@915 142
adamc@937 143 case errors of
adamc@937 144 Some s => alert ("Can't save because the following columns have invalid values:\n"
adamc@937 145 ^ s)
adamc@937 146 | None =>
adamc@937 147 set ud False;
adamc@937 148 row <- get rowS;
adamc@944 149 row' <- Monad.foldR3 [fst3] [colMeta M.row] [snd3] [fn _ => M.row]
adamc@944 150 (fn [nm :: Name] [t :: (Type * Type * Type)]
adamc@944 151 [rest :: {(Type * Type * Type)}]
adamc@937 152 [[nm] ~ rest] data meta v row' =>
adamc@937 153 (meta.Handlers data).Update row' v)
adamc@937 154 row [_] M.folder grid.Cols M.cols cols;
adamc@937 155 rpc (M.save (M.keyOf row) row');
adamc@937 156 set rowS row';
adamc@937 157
adamc@937 158 cols <- makeAll grid.Cols row';
adamc@937 159 set colsS cols
adamc@937 160 in
adamc@937 161 <xml><tr class={tr}>
adamc@937 162 <td>
adamc@940 163 <dyn signal={b <- signal grid.Selection;
adamc@941 164 return (if b then
adamc@940 165 <xml><ccheckbox source={sd}/></xml>
adamc@940 166 else
adamc@941 167 <xml/>)}/>
adamc@940 168 </td>
adamc@940 169
adamc@940 170 <td>
adamc@937 171 <dyn signal={b <- signal ud;
adamc@937 172 return (if b then
adamc@937 173 <xml><button value="Save" onclick={save}/></xml>
adamc@937 174 else
adamc@937 175 <xml><button value="Update" onclick={update}/></xml>)}/>
adamc@937 176 </td>
adamc@937 177
adamc@937 178 <td><dyn signal={b <- signal ud;
adamc@937 179 return (if b then
adamc@937 180 <xml><button value="Cancel" onclick={cancel}/></xml>
adamc@937 181 else
adamc@937 182 <xml><button value="Delete" onclick={delete}/></xml>)}/>
adamc@937 183 </td>
adamc@937 184
adamc@937 185 <dyn signal={cols <- signal colsS;
adamc@944 186 return (foldRX3 [fst3] [colMeta M.row] [snd3] [_]
adamc@944 187 (fn [nm :: Name] [t :: (Type * Type * Type)]
adamc@944 188 [rest :: {(Type * Type * Type)}]
adamc@937 189 [[nm] ~ rest] data meta v =>
adamc@937 190 <xml><td class={td}>
adamc@937 191 <dyn signal={b <- signal ud;
adamc@937 192 return (if b then
adamc@937 193 (meta.Handlers data).Edit v
adamc@937 194 else
adamc@937 195 (meta.Handlers data).Display
adamc@937 196 v)}/>
adamc@937 197 <dyn signal={b <- signal ud;
adamc@937 198 if b then
adamc@937 199 valid <-
adamc@937 200 (meta.Handlers data).Validate v;
adamc@937 201 return (if valid then
adamc@937 202 <xml/>
adamc@937 203 else
adamc@937 204 <xml>!</xml>)
adamc@937 205 else
adamc@937 206 return <xml/>}/>
adamc@937 207 </td></xml>)
adamc@937 208 [_] M.folder grid.Cols M.cols cols)}/>
adamc@937 209 </tr></xml>
adamc@937 210 end) grid.Rows}
adamc@915 211
adamc@937 212 <dyn signal={rows <- Dlist.foldl (fn row => Monad.mapR2 [aggregateMeta M.row] [id] [id]
adamc@937 213 (fn [nm :: Name] [t :: Type] meta acc =>
adamc@937 214 Monad.mp (fn v => meta.Step v acc)
adamc@937 215 (signal row.Row))
adamc@937 216 [_] M.aggFolder M.aggregates)
adamc@937 217 (mp [aggregateMeta M.row] [id]
adamc@937 218 (fn [t] meta => meta.Initial)
adamc@937 219 [_] M.aggFolder M.aggregates) grid.Rows;
adamc@937 220 return <xml><tr>
adamc@941 221 <th colspan={3}>Aggregates</th>
adamc@937 222 {foldRX2 [aggregateMeta M.row] [id] [_]
adamc@937 223 (fn [nm :: Name] [t :: Type] [rest :: {Type}] [[nm] ~ rest] meta acc =>
adamc@937 224 <xml><td class={agg}>{meta.Display acc}</td></xml>)
adamc@937 225 [_] M.aggFolder M.aggregates rows}
adamc@937 226 </tr></xml>}/>
adamc@944 227
adamc@944 228 <tr><th colspan={3}>Filters</th>
adamc@944 229 {foldRX3 [colMeta M.row] [fst3] [thd3] [_]
adamc@944 230 (fn [nm :: Name] [p :: (Type * Type * Type)] [rest :: {(Type * Type * Type)}] [[nm] ~ rest]
adamc@944 231 meta state filter => <xml><td>{(meta.Handlers state).DisplayFilter filter}</td></xml>)
adamc@944 232 [_] M.folder M.cols grid.Cols grid.Filters}
adamc@944 233 </tr>
adamc@915 234 </table>
adamc@915 235
adamc@915 236 <button value="New row" onclick={row <- rpc M.new;
adamc@915 237 addRow grid.Cols grid.Rows row}/>
adamc@915 238 <button value="Refresh" onclick={sync grid}/>
adamc@915 239 </xml>
adamc@940 240
adamc@940 241 fun showSelection grid = grid.Selection
adamc@940 242
adamc@940 243 fun selection grid = Dlist.foldl (fn {Row = rowS, Selected = sd, ...} ls =>
adamc@940 244 sd <- signal sd;
adamc@940 245 if sd then
adamc@940 246 row <- signal rowS;
adamc@940 247 return (row :: ls)
adamc@940 248 else
adamc@940 249 return ls) [] grid.Rows
adamc@915 250 end