/
notification.opa
339 lines (290 loc) · 10.3 KB
/
notification.opa
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* A widget displaying popups
*
* @author Hugo Heuzard, Guillem Rieu, 2011
* @author Adam Koprowski, 2011 (fade-in/out animations, better customization, ...)
* @category WIDGET
* @destination PUBLIC
* @stability EXPERIMENTAL
* @version 0.1
*/
import stdlib.widgets.core
import stdlib.widgets.button
type WNotification.config_box = {
style_top: WStyler.styler
style_content:WStyler.styler
style_buttons:WStyler.styler
style_main:WStyler.styler
sizex: int
sizey: int
}
type WNotification.config = {
style_box : WNotification.config_box
style_back : WStyler.styler
}
type WNotification.box_id = (string, string)
type WNotification.button = {
label : string
action : WNotification.box_id -> void
}
type WNotification.box = {
/**
* Whether the notification dialog blocks actions on the page or not:
* - if 'modal', a user action is required on the dialog box itself.
* - if 'loose', the dialog disappears when clicking outside.
* - if 'free', actions on the page are still possible.
*/
mode: { modal } / { loose } / { free }
/** Fade in/out speed when showing/hiding the box (no fade effect for {none}) */
fade: {none} / {slow} / {default} / {fast}
/** Delay in ms to keep the notification open */
delay: option(int)
/** Title of dialog box **/
title : xhtml
/** Content of dialog box **/
content : xhtml
/** Buttons of dialog box **/
buttons : { standard : list(WNotification.button) } / { customized : WNotification.box_id -> xhtml } / { no_buttons }
}
WNotification =
get_notification_id(id) = id ^ "_notification"
get_notification_box_id(id) = id ^ "_notificationbox"
get_notification_modal_id(id) = id ^ "_notificationmodal"
get_notification_background_id(id) = id ^ "_background"
animate(immediate, fade, speed) : dom -> void =
do_fade(speed) = id ->
effect = Dom.Effect.with_duration(speed, fade())
Dom.transition(id, effect) |> ignore
match speed with
| {none} -> immediate
| {slow} -> do_fade({slow})
| {default} -> do_fade({default})
| {fast} -> do_fade({fast})
add_notification(fade, cid, id, modal, new_html) =
_ = Log.debug("add notif",new_html)
show = animate(Dom.show, Dom.Effect.fade_in, fade)
if modal
then
do Dom.transform([#{get_notification_modal_id(cid)} +<- new_html])
do show(#{get_notification_box_id(id)})
do show(#{get_notification_background_id(cid)})
do show(#{get_notification_modal_id(cid)})
void
else
do Dom.transform([#{get_notification_id(cid)} +<- new_html])
do show(#{get_notification_box_id(id)})
do show(#{get_notification_id(cid)})
void
rem_notification(fade, cid, id, modal) =
_ = Log.debug("rem notif",id)
hide = animate(Dom.hide, Dom.Effect.fade_out, fade)
do hide(#{get_notification_box_id(id)})
do Dom.remove(#{get_notification_box_id(id)})
if modal
then do
if Dom.is_empty(Dom.select_children(#{get_notification_modal_id(cid)}))
then (do hide(#{get_notification_modal_id(cid)})
do hide(#{get_notification_background_id(cid)})
Dom.unbind_event(#{get_notification_background_id(cid)},{click}))
void
else
do
if Dom.is_empty(Dom.select_children(#{get_notification_id(cid)}))
then hide(#{get_notification_id(cid)})
void
notification_html(id: string, new_id:string, modal:bool, notification: WNotification.box, conf): xhtml =
ids = (id, new_id)
rem = ( -> rem_notification(notification.fade, id, new_id, modal))
// TODO: add drag'n'drop when available
prefix = "{id}_notification_{new_id}"
// action_close = [("Close", rem)]
(kind_html, content_html, actions) = (notification.title, notification.content, notification.buttons)
// | {error} -> (<>Error</>, notification.value, action_close)
// | {warning} -> (<>Warning</>, notification.value, action_close)
// | {info} -> (<>Info</>, notification.value, action_close)
// | {~custom} -> custom(notification.value))
make_button(~{label action}) : xhtml =
uniq_id = Dom.fresh_id()
WSimpleButton.html(uniq_id, (_ -> action(ids)), label)
|> WCore.make_inline_default(uniq_id, _)
index = match notification.mode with
| {modal} -> 1001
| {loose} -> _ = Dom.bind(#{get_notification_background_id(id)},{click},(_ -> rem())) 1001
| {free} -> 999
top =
// <span style="text-align: right; position: relative; float: right;">
// <a onclick={rem}>X</a>
// </span>
// <span style="clear:both"></span>
<div id="{prefix}_notification_bar">
<span>{kind_html}</span>
</div> |> WStyler.add(conf.style_top,_)
content =
<div>
<div id="{prefix}_notification_content">
{content_html}
</div>
</div> |> WStyler.add(conf.style_content,_)
buttons =
match actions with
| {standard=buttons} ->
<div id="{prefix}_notification_actions">
{List.map((action -> make_button(action)), buttons)}
</div> |> WStyler.add(conf.style_buttons,_)
| {customized=f} -> f(ids)
| {no_buttons} -> <></>;
<div id="{get_notification_box_id(new_id)}"
style="z-index:{index};display:none;
height: {conf.sizey}px;
width: {conf.sizex}px; position: fixed; top: 50%; left: 50%;
margin-left: -{conf.sizex/2}px;
margin-top: -{conf.sizey/2}px;
padding-bottom: 20px;">
{top}
{content}
{buttons}
</div>
|> WStyler.add(conf.style_main,_)
{{
default_config_box : WNotification.config_box = {
sizex = 300
sizey = 160
style_top = WStyler.make_style( css {
width: 100%;
height: 30px;
border-bottom: 1px solid black;
})
style_content = WStyler.make_style( css {
height: 100px;
overflow: auto;
})
style_buttons = WStyler.make_style( css {
height: 30px;
text-align: right;
width: 100%;
border-top: 1px solid black;
})
style_main = WStyler.make_style( css {
background: gray;
border: 1px solid black;
})
}
default_config : WNotification.config = {
style_box = default_config_box
style_back = WStyler.make_style(
(css {
display:none;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: 1000;
background: black;
opacity: .6
}))
}
/* Add this xhtml to your page before using notify function */
html(config : WNotification.config, id : string) : xhtml =
back = <div id="{get_notification_background_id(id)}" style=""></div> |> WStyler.add(config.style_back,_)
<>
{back}
<div id="{get_notification_id(id)}" style="display:none"/>
<div id="{get_notification_modal_id(id)}" style="display:none"/>
</>
/* default warning notification box with custom content */
default_warning(xhtml : xhtml) : WNotification.box =
fade = {default}
{
title = <>Warning</>
content = xhtml
buttons = {standard = [{label="Ok" action=destroy(fade, _)}]}
mode = {loose}
delay = none
~fade
}
/* default error notification box with custom content */
default_error(xhtml : xhtml) : WNotification.box =
fade = {default}
{
title = <>Error</>
content = xhtml
buttons = {standard = [{label="Ok" action=destroy(fade, _)}]}
mode = {modal}
delay = none
~fade
}
/* default info notificaiton box with custom content */
default_info(xhtml : xhtml) : WNotification.box =
fade = {default}
{
title = <>Info</>
content = xhtml
buttons = {standard = [{label="Ok" action=destroy(fade, _)}]}
mode = {free}
delay = none
~fade
}
default_confirm(xhtml : xhtml, result : (bool -> void)) : WNotification.box =
fade = {default}
yes_no(label, v) = {~label action=(box_id -> do result(v) destroy(fade, box_id))}
yes = yes_no("Yes", true)
no = yes_no("No", false)
{
title = <>Confirm</>
content = xhtml
buttons = {standard = [yes, no]}
mode = {modal}
delay = none
~fade
}
// default_prompt(xhtml : xhtml, result : (string -> void)) : WNotification.box =
// yes = ("Yes",(_ -> result(true)))
// no = ("No", (_ -> result(false)))
// {
// kind = content : xhtml -> (<>Confirm</>, content, [yes,no]) }
// mode = {modal}
// delay = none
// value = xhtml
// }
/* display a notification box
* notify(config,id,newid,notification)
* where :
* - 'id' is the id used in the 'html' function
* - 'newid' is a new id to identify the popup
*/
notify(config : WNotification.config,(id : string, id_box : string) : WNotification.box_id, notification: WNotification.box) : void =
modal = match notification.mode with
| {modal} -> {true}
| {loose} -> {true}
| {free} -> {false}
html = notification_html(id, id_box, modal, notification, config.style_box)
do add_notification(notification.fade, id, id_box, modal, html)
match notification.delay with
| {none} -> void
| {~some} -> sleep(some, (-> rem_notification(notification.fade, id, id_box, modal) ))
/* remove a notification box
* destroy(id,newid)
* where :
* - 'id' is the id used in the 'html' function
* - 'newid' is a id identifying the popup
*/
destroy(fade, (id : string, new_id : string) : WNotification.box_id) : void =
do rem_notification(fade, id, new_id, true)
do rem_notification(fade, id, new_id, false)
void
box_id(id : string) : WNotification.box_id = (id, Dom.fresh_id())
}}