-
Notifications
You must be signed in to change notification settings - Fork 16
/
calendarAddons.ur
267 lines (248 loc) · 14.1 KB
/
calendarAddons.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
open Bootstrap
type event_data = {
Id : option string,
Start : time,
End : option time,
Title : string,
Background : bool,
TextColor : option string,
BackgroundColor : option string
}
type t (r :: {Type}) = {
ExtraEvents : option string -> transaction (list event_data),
AboveCalendar : Ui.context -> FullCalendar.t -> xbody,
WithinEvent : Ui.context -> FullCalendar.t -> FullCalendar.event -> {Header : xbody, Body : xbody},
MaySee : tab :: Name -> tab' :: Name -> [[tab] ~ [tab']] => option string -> option (sql_exp [tab = r] [] [] bool),
MayEdit : tab :: Name -> tab' :: Name -> [[tab] ~ [tab']] => option string -> option (sql_exp [tab = r] [] [] bool)
}
fun extraEvents [r] t = t.ExtraEvents
fun aboveCalendar [r] t = t.AboveCalendar
fun withinEvent [r] t = t.WithinEvent
fun maySee [r] (t : t r) [tab ::_] [tab' ::_] [[tab] ~ [tab']] uo =
case t.MaySee [tab] [tab'] ! uo of
None => t.MayEdit [tab] [tab'] ! uo
| Some e1 =>
Some (case t.MayEdit [tab] [tab'] ! uo of
None => e1
| Some e2 => (WHERE {e1} OR {e2}))
fun mayEdit [r] t = t.MayEdit
val empty [r] = {
ExtraEvents = fn _ => return [],
AboveCalendar = fn _ _ => <xml></xml>,
WithinEvent = fn _ _ _ => {Header = <xml></xml>, Body = <xml></xml>},
MaySee = fn [tab ::_] [tab' ::_] [[tab] ~ [tab']] _ => None,
MayEdit = fn [tab ::_] [tab' ::_] [[tab] ~ [tab']] _ => None
}
fun compose [r] (x : t r) (y : t r) = {
ExtraEvents = fn uo =>
evs1 <- x.ExtraEvents uo;
evs2 <- y.ExtraEvents uo;
return (List.append evs1 evs2),
AboveCalendar = fn ctx cal => <xml>{x.AboveCalendar ctx cal}{y.AboveCalendar ctx cal}</xml>,
WithinEvent = fn ctx cal ev => let
val x = x.WithinEvent ctx cal ev
val y = y.WithinEvent ctx cal ev
in
{Header = <xml>{x.Header}{y.Header}</xml>,
Body = <xml>{x.Body}{y.Body}</xml>}
end,
MaySee = fn [tab ::_] [tab' ::_] [[tab] ~ [tab']] uo =>
case x.MaySee [tab] [tab'] ! uo of
None => y.MaySee [tab] [tab'] ! uo
| Some e1 =>
case y.MaySee [tab] [tab'] ! uo of
None => Some e1
| Some e2 => Some (WHERE {e1} OR {e2}),
MayEdit = fn [tab ::_] [tab' ::_] [[tab] ~ [tab']] uo =>
case x.MayEdit [tab] [tab'] ! uo of
None => y.MayEdit [tab] [tab'] ! uo
| Some e1 =>
case y.MayEdit [tab] [tab'] ! uo of
None => Some e1
| Some e2 => Some (WHERE {e1} OR {e2})
}
functor EventSource(M : sig
con r :: {Type}
con key :: Name
type keyT
con tm :: Name
con others :: {Type}
constraint [key] ~ [tm]
constraint [key, tm] ~ others
con keyName :: Name
con otherKeys :: {{Unit}}
constraint [keyName] ~ otherKeys
val source : sql_table ([key = keyT, tm = option time] ++ others)
([keyName = [key]] ++ otherKeys)
val title : string
val show_key : show keyT
val prefix : string
val background : bool
val textColor : option string
val backgroundColor : option string
val addons : t ([key = keyT, tm = option time] ++ others)
val duration : option string
end) = struct
open M
val t = {
ExtraEvents = fn uo =>
ls <- queryL1 (SELECT source.{key}, source.{tm}
FROM source
WHERE NOT (source.{tm} IS NULL)
AND {Option.get (SQL TRUE) (maySee addons [#Source] [#Other] uo)});
return (List.mapi (fn i r =>
{Id = Some (title ^ "_" ^ show i),
Start = Option.unsafeGet r.tm,
End = (d <- duration;
tm <- r.tm;
return (addSeconds tm (FullCalendar.durationToSeconds d))),
Title = prefix ^ show r.key,
Background = background,
TextColor = textColor,
BackgroundColor = backgroundColor}) ls),
AboveCalendar = fn _ _ => <xml></xml>,
WithinEvent = fn _ _ _ => {Header = <xml></xml>, Body = <xml></xml>},
MaySee = fn [tab ::_] [tab' ::_] [[tab] ~ [tab']] _ => None,
MayEdit = fn [tab ::_] [tab' ::_] [[tab] ~ [tab']] _ => None
}
end
functor OnlyOwnerMayEdit(M : sig
con owner :: Name
con r :: {Type}
constraint [owner] ~ r
end) = struct
open M
val t = {
ExtraEvents = fn _ => return [],
AboveCalendar = fn _ _ => <xml></xml>,
WithinEvent = fn _ _ _ => {Header = <xml></xml>, Body = <xml></xml>},
MaySee = fn [tab ::_] [tab' ::_] [[tab] ~ [tab']] uo => None,
MayEdit = fn [tab ::_] [tab' ::_] [[tab] ~ [tab']] uo =>
Some (case uo of
None => (WHERE FALSE)
| Some _ => (WHERE {{tab}}.{owner} = {[uo]}))
}
end
functor OnlyOwnerMaySee(M : sig
con owner :: Name
con r :: {Type}
constraint [owner] ~ r
end) = struct
open M
val t = {
ExtraEvents = fn _ => return [],
AboveCalendar = fn _ _ => <xml></xml>,
WithinEvent = fn _ _ _ => {Header = <xml></xml>, Body = <xml></xml>},
MaySee = fn [tab ::_] [tab' ::_] [[tab] ~ [tab']] uo =>
Some (case uo of
None => (WHERE FALSE)
| Some _ => (WHERE {{tab}}.{owner} = {[uo]})),
MayEdit = fn [tab ::_] [tab' ::_] [[tab] ~ [tab']] uo =>
Some (case uo of
None => (WHERE FALSE)
| Some _ => (WHERE {{tab}}.{owner} = {[uo]}))
}
end
functor ConnectedMaySee(M : sig
con key :: Name
type keyT
con r :: {Type}
constraint [key] ~ r
con keyLabels = [key]
con cev :: Name
con cuser :: Name
con cother :: {Type}
constraint [cev] ~ [cuser]
constraint [cev, cuser] ~ cother
table ctr : ([cev = keyT, cuser = string] ++ cother)
end) = struct
open M
val t = {
ExtraEvents = fn _ => return [],
AboveCalendar = fn _ _ => <xml></xml>,
WithinEvent = fn _ _ _ => {Header = <xml></xml>, Body = <xml></xml>},
MaySee = fn [tab ::_] [tab' ::_] [[tab] ~ [tab']] uo =>
Some (case uo of
None => (WHERE FALSE)
| Some u => (WHERE COALESCE((SELECT COUNT( * ) > 0
FROM ctr AS {tab'}
WHERE {{tab'}}.{cev} = {{tab}}.{key}
AND {{tab'}}.{cuser} = {[u]}),
FALSE))),
MayEdit = fn [tab ::_] [tab' ::_] [[tab] ~ [tab']] _ => None
}
end
functor ShowExternalCalendar(M : sig
con r :: {Type}
type calendar_id
val calendars : transaction (list (calendar_id * string (* description *)))
(* Retrieve user's own calendars. *)
val events : calendar_id -> {Min : option time, Max : option time}
-> transaction (list {Summary : option string, Start : option time, End : option time})
(* Get all events (in some time wondow) from a calendar. *)
val label : string
val idPrefix : string
end) = struct
val calendars = M.calendars
fun events cals =
tm <- now;
List.mapConcatM (fn cal =>
M.events cal
{Min = Some tm,
Max = Some (addSeconds tm (30 * 24 * 60 * 60))}) cals
val prefix = M.idPrefix ^ "_"
val t = {
ExtraEvents = fn _ => return [],
AboveCalendar = fn ctx cal => Ui.modalButton ctx
(CLASS "btn btn-primary")
<xml>{[M.label]}</xml>
(cals <- rpc calendars;
cals <- List.mapM (fn (id, sum) =>
chosen <- source False;
return (id, sum, chosen)) cals;
return (Ui.modal
(cals <- List.mapPartialM (fn (id, _, chosen) =>
ch <- get chosen;
return (if ch then
Some id
else
None)) cals;
evs <- rpc (events cals);
oldEvs <- FullCalendar.events cal;
List.app (fn ev =>
id <- FullCalendar.eventId ev;
case id of
None => return ()
| Some id =>
if String.isPrefix {Full = id, Prefix = prefix} then
FullCalendar.removeEvent ev
else
return ()) oldEvs;
evs <- return (List.filter (fn ev =>
case ev.Start of
None => False
| _ => True) evs);
FullCalendar.addEvents cal (List.mapi (fn i ev =>
case ev.Start of
None => error <xml>Impossible</xml>
| Some start => {Id = Some (prefix ^ show i),
AllDay = False,
Start = start,
End = ev.End,
Rendering = FullCalendar.Background,
Title = Option.get "Busy" ev.Summary,
TextColor = Some (return (Some "black")),
BackgroundColor = Some (return (Some "gray"))}) evs))
<xml>Which calendar(s) would you like to show?</xml>
<xml><ul class="list-group">
{List.mapX (fn (_, sum, chosen) => <xml>
<li class="list-group-item"><ccheckbox source={chosen}/> {[sum]}</li>
</xml>) cals}
</ul></xml>
<xml>Show</xml>)),
WithinEvent = fn _ cal ev => {Header = <xml></xml>,
Body = <xml></xml>},
MaySee = fn [tab ::_] [tab' ::_] [[tab] ~ [tab']] _ => None,
MayEdit = fn [tab ::_] [tab' ::_] [[tab] ~ [tab']] _ => None
}
end