annotate demo/more/grid.ur @ 962:7e7edfb6fe82

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