annotate demo/more/grid.ur @ 953:301530da2062

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