-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditGrid.ur
171 lines (147 loc) · 7.26 KB
/
editGrid.ur
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
open Bootstrap3
functor Make(M : sig
con key :: {Type}
con rest :: {Type}
constraint key ~ rest
table tab : (key ++ rest)
val keyFl : folder key
val restFl : folder rest
val keyShow : $(map show key)
val keyRead : $(map read key)
val keyEq : $(map eq key)
val keyInj : $(map sql_injectable key)
val restShow : $(map show rest)
val restRead : $(map read rest)
val restInj : $(map sql_injectable rest)
val labels : $(map (fn _ => string) (key ++ rest))
val authorized : transaction bool
end) = struct
open M
type a = _
con all = key ++ rest
val allFl = @Folder.concat ! keyFl restFl
val allInj = keyInj ++ restInj
val keyEq' : eq $key = @Record.eq keyEq keyFl
val create =
vs <- List.mapQueryM (SELECT *
FROM tab
ORDER BY {{{@Sql.order_by keyFl
(@Sql.some_fields [#Tab] [key] ! ! keyFl)
sql_asc}}})
(fn {Tab = r} =>
curKey <- @foldR2 [show] [ident]
[fn r => transaction $(map (fn _ => source string) r)]
(fn [nm ::_] [t ::_] [r ::_] [[nm] ~ r] (_ : show t) v acc =>
v <- source (show v);
vs <- acc;
return ({nm = v} ++ vs))
(return {}) keyFl keyShow (r --- rest);
rest <- @foldR2 [show] [ident]
[fn r => transaction $(map (fn _ => source string) r)]
(fn [nm ::_] [t ::_] [r ::_] [[nm] ~ r] (_ : show t) v acc =>
v <- source (show v);
vs <- acc;
return ({nm = v} ++ vs))
(return {}) restFl restShow (r --- key);
return {OldKey = r --- rest,
Cur = curKey ++ rest});
vs <- source vs;
new <- @fold [fn r => transaction $(map (fn _ => source string) r)]
(fn [nm ::_] [t ::_] [r ::_] [[nm] ~ r] acc =>
v <- source "";
vs <- acc;
return ({nm = v} ++ vs))
(return {}) allFl;
return {Rows = vs, New = new}
val ensure =
b <- authorized;
if b then
return ()
else
error <xml>Authorization failure</xml>
fun del key =
ensure;
dml (DELETE FROM tab
WHERE {@@Sql.easy_where [#T] [key] [_] [_] [_] [_]
! ! keyInj keyFl key})
fun save rows =
ensure;
List.app (fn r =>
@@Sql.easy_update' [key] [rest] [_] ! keyInj restInj
keyFl restFl
tab r.OldKey r.New) rows
fun add row =
ensure;
@@Sql.easy_insert [all] [_] allInj allFl tab row
fun render ctx t = <xml>
<button class="btn btn-primary"
value="Save"
onclick={fn _ =>
vs <- get t.Rows;
vs <- List.mapM (fn r =>
new <- @foldR2 [read] [fn _ => source string] [fn r => transaction $r]
(fn [nm ::_] [t ::_] [r ::_] [[nm] ~ r] (_ : read t) src acc =>
v <- get src;
vs <- acc;
return ({nm = readError v} ++ vs))
(return {}) allFl (keyRead ++ restRead) r.Cur;
return {OldKey = r.OldKey,
New = new}) vs;
rpc (save vs)}/>
<table class="bs3-table table-striped">
<tr>
<th/>
{@mapX [fn _ => string] [tr]
(fn [nm ::_] [t ::_] [r ::_] [[nm] ~ r] lab =>
<xml><th>{[lab]}</th></xml>)
allFl labels}
</tr>
<dyn signal={vs <- signal t.Rows;
return (List.mapX (fn r => <xml>
<tr>
<td>
{Ui.modalButton ctx (CLASS "btn glyphicon glyphicon-remove") <xml/>
(return (Ui.modal
(rpc (del r.OldKey);
set t.Rows (List.filter (fn r' => r'.OldKey <> r.OldKey) vs))
<xml>Are you sure you want to delete this row?</xml>
<xml/>
<xml>Yes!</xml>))}
</td>
{@mapX [fn _ => source string] [tr]
(fn [nm ::_] [t ::_] [r ::_] [[nm] ~ r] src =>
<xml><td><ctextbox class="form-control" source={src}/></td></xml>)
allFl r.Cur}
</tr>
</xml>) vs)}/>
<tr/>
<tr>
<td>
<button class="btn btn-primary"
value="Add Row"
onclick={fn _ =>
(dups, new) <- @foldR2 [read] [fn _ => source string]
[fn r => transaction ($(map (fn _ => source string) r) * $r)]
(fn [nm ::_] [t ::_] [r ::_] [[nm] ~ r] (_ : read t) src acc =>
v <- get src;
dup <- source v;
set src "";
(dups, vs) <- acc;
return ({nm = dup} ++ dups,
{nm = readError v} ++ vs))
(return ({}, {})) allFl (keyRead ++ restRead) t.New;
rpc (add new);
vs <- get t.Rows;
set t.Rows (List.append vs ({OldKey = new --- rest, Cur = dups} :: []))}/>
</td>
{@mapX [fn _ => source string] [tr]
(fn [nm ::_] [t ::_] [r ::_] [[nm] ~ r] src =>
<xml><td><ctextbox class="form-control" source={src}/></td></xml>)
allFl t.New}
</tr>
</table>
</xml>
val ui = {Create = create,
Render = render,
Onload = fn _ => return ()}
end