Mercurial > libervia-web
annotate libervia/pages/_browser/dialog.py @ 1347:48e2a8b07c0b
browser: async version of bridge
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 05 Sep 2020 21:59:11 +0200 |
parents | 8729d2708f65 |
children | 4b6f711b09cb |
rev | line source |
---|---|
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 """manage common dialogs""" |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 from browser import document, timer |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 from template import Template |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 class Confirm: |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 |
1343
8729d2708f65
browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents:
1328
diff
changeset
|
9 def __init__(self, message, ok_label="", cancel_label="", ok_color="success"): |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 self._tpl = Template("dialogs/confirm.html") |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 self.message = message |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 self.ok_label = ok_label |
1343
8729d2708f65
browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents:
1328
diff
changeset
|
13 assert ok_color in ("success", "danger") |
8729d2708f65
browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents:
1328
diff
changeset
|
14 self.ok_color = ok_color |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 self.cancel_label = cancel_label |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 def cancel_cb(self, evt, notif_elt): |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 notif_elt.remove() |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 def show(self, ok_cb, cancel_cb=None): |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 if cancel_cb is None: |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 cancel_cb = self.cancel_cb |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 notif_elt = self._tpl.get_elt({ |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 "message": self.message, |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 "ok_label": self.ok_label, |
1343
8729d2708f65
browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents:
1328
diff
changeset
|
26 "ok_color": self.ok_color, |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 "cancel_label": self.cancel_label, |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 }) |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 document['notifs_area'] <= notif_elt |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 timer.set_timeout(lambda: notif_elt.classList.add('state_appended'), 0) |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 for cancel_elt in notif_elt.select(".click_to_cancel"): |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 cancel_elt.bind("click", lambda evt: cancel_cb(evt, notif_elt)) |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 for cancel_elt in notif_elt.select(".click_to_ok"): |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 cancel_elt.bind("click", lambda evt: ok_cb(evt, notif_elt)) |
1328
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
36 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
37 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
38 class Notification: |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
39 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
40 def __init__(self): |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
41 self._tpl = Template("dialogs/notification.html") |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
42 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
43 def close(self, notif_elt): |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
44 notif_elt.classList.remove('state_appended') |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
45 notif_elt.bind("transitionend", lambda __: notif_elt.remove()) |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
46 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
47 def show( |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
48 self, |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
49 message: str, |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
50 level: str = "info", |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
51 delay: int = 5 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
52 ) -> None: |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
53 # we log in console error messages, may be useful |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
54 if level in ("warning", "error"): |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
55 print(f"[{level}] {message}") |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
56 notif_elt = self._tpl.get_elt({ |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
57 "message": message, |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
58 "level": level, |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
59 }) |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
60 document["notifs_area"] <= notif_elt |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
61 timer.set_timeout(lambda: notif_elt.classList.add('state_appended'), 0) |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
62 timer.set_timeout(lambda: self.close(notif_elt), delay * 1000) |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
63 for elt in notif_elt.select('.click_to_close'): |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
64 elt.bind('click', lambda __: self.close(notif_elt)) |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
65 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
66 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
67 notification = Notification() |