Mercurial > libervia-web
annotate libervia/web/pages/_browser/dialog.py @ 1596:52098b5bab8d
browser (chat): add data for reactions
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 13 Dec 2023 22:06:05 +0100 |
parents | 7228fc3c4744 |
children | 0a4433a343a3 |
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 |
1504
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
3 from browser import document, window, timer, console as log |
1299
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 |
1504
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
6 log.warning = log.warn |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
7 |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
9 class CancelError(Exception): |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
10 """Dialog is cancelled""" |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
11 |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
12 |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 class Confirm: |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 |
1343
8729d2708f65
browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents:
1328
diff
changeset
|
15 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
|
16 self._tpl = Template("dialogs/confirm.html") |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 self.message = message |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 self.ok_label = ok_label |
1343
8729d2708f65
browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents:
1328
diff
changeset
|
19 assert ok_color in ("success", "danger") |
8729d2708f65
browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents:
1328
diff
changeset
|
20 self.ok_color = ok_color |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 self.cancel_label = cancel_label |
1589
7228fc3c4744
browser (dialog): be sure to remove dialog on `OK` and `Cancel` click + avoid side effects
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
22 self._notif_elt = None |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
23 self.reset() |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
25 def reset(self): |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
26 """Reset values of callbacks and notif element""" |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
27 self._ok_cb = None |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
28 self._cancel_cb = None |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
29 self._reject_cb = None |
1589
7228fc3c4744
browser (dialog): be sure to remove dialog on `OK` and `Cancel` click + avoid side effects
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
30 if self._notif_elt is not None: |
7228fc3c4744
browser (dialog): be sure to remove dialog on `OK` and `Cancel` click + avoid side effects
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
31 self._notif_elt.remove() |
7228fc3c4744
browser (dialog): be sure to remove dialog on `OK` and `Cancel` click + avoid side effects
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
32 self._notif_elt = None |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
33 |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
34 def _default_cancel_cb(self, evt, notif_elt): |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 notif_elt.remove() |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
37 def cancel(self): |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
38 """Cancel the dialog, without calling any callback |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
39 |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
40 will raise a CancelError |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
41 """ |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
42 if self._notif_elt is None: |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
43 log.warning("calling cancel on an unshown dialog") |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
44 else: |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
45 self._notif_elt.remove() |
1589
7228fc3c4744
browser (dialog): be sure to remove dialog on `OK` and `Cancel` click + avoid side effects
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
46 self._notif_elt = None |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
47 if self._reject_cb is not None: |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
48 self._reject_cb(CancelError) |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
49 else: |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
50 log.warning("no reject callback set") |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
51 self.reset() |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
52 |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
53 def on_ok_click(self, evt): |
1589
7228fc3c4744
browser (dialog): be sure to remove dialog on `OK` and `Cancel` click + avoid side effects
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
54 evt.preventDefault() |
7228fc3c4744
browser (dialog): be sure to remove dialog on `OK` and `Cancel` click + avoid side effects
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
55 evt.stopPropagation() |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
56 assert self._ok_cb is not None |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
57 self._ok_cb(evt, self._notif_elt) |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
58 self.reset() |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
59 |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
60 def on_cancel_click(self, evt) -> None: |
1589
7228fc3c4744
browser (dialog): be sure to remove dialog on `OK` and `Cancel` click + avoid side effects
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
61 evt.preventDefault() |
7228fc3c4744
browser (dialog): be sure to remove dialog on `OK` and `Cancel` click + avoid side effects
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
62 evt.stopPropagation() |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
63 assert self._cancel_cb is not None |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
64 self._cancel_cb(evt, self._notif_elt) |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
65 self.reset() |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
66 |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
67 def show(self, ok_cb, cancel_cb=None, reject_cb=None): |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 if cancel_cb is None: |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
69 cancel_cb = self._default_cancel_cb |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
70 self._ok_cb = ok_cb |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
71 self._cancel_cb = cancel_cb |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
72 self._reject_cb = reject_cb |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 notif_elt = self._tpl.get_elt({ |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 "message": self.message, |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 "ok_label": self.ok_label, |
1343
8729d2708f65
browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents:
1328
diff
changeset
|
76 "ok_color": self.ok_color, |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 "cancel_label": self.cancel_label, |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 }) |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
79 self._notif_elt = notif_elt |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 document['notifs_area'] <= notif_elt |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 timer.set_timeout(lambda: notif_elt.classList.add('state_appended'), 0) |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
83 for ok_elt in notif_elt.select(".click_to_ok"): |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
84 ok_elt.bind("click", self.on_ok_click) |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
85 for ok_elt in notif_elt.select(".click_to_cancel"): |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
86 ok_elt.bind("click", self.on_cancel_click) |
1328
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
87 |
1385
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
88 def _ashow_cb(self, evt, notif_elt, resolve_cb, confirmed): |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
89 evt.stopPropagation() |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
90 notif_elt.remove() |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
91 resolve_cb(confirmed) |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
92 |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
93 async def ashow(self): |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
94 return window.Promise.new( |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
95 lambda resolve_cb, reject_cb: |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
96 self.show( |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
97 lambda evt, notif_elt: self._ashow_cb(evt, notif_elt, resolve_cb, True), |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
98 lambda evt, notif_elt: self._ashow_cb(evt, notif_elt, resolve_cb, False), |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
99 reject_cb |
1385
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
100 ) |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
101 ) |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
102 |
1328
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
103 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
104 class Notification: |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
105 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
106 def __init__(self): |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
107 self._tpl = Template("dialogs/notification.html") |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
108 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
109 def close(self, notif_elt): |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
110 notif_elt.classList.remove('state_appended') |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
111 notif_elt.bind("transitionend", lambda __: notif_elt.remove()) |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
112 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
113 def show( |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
114 self, |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
115 message: str, |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
116 level: str = "info", |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
117 delay: int = 5 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
118 ) -> None: |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
119 # 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
|
120 if level in ("warning", "error"): |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
121 print(f"[{level}] {message}") |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
122 notif_elt = self._tpl.get_elt({ |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
123 "message": message, |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
124 "level": level, |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
125 }) |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
126 document["notifs_area"] <= notif_elt |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
127 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
|
128 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
|
129 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
|
130 elt.bind('click', lambda __: self.close(notif_elt)) |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
131 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
132 |
1504
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
133 class RetryNotification: |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
134 def __init__(self, retry_cb): |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
135 self._tpl = Template("dialogs/retry-notification.html") |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
136 self.retry_cb = retry_cb |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
137 self.counter = 0 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
138 self.timer = None |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
139 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
140 def retry(self, notif_elt): |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
141 if self.timer is not None: |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
142 timer.clear_interval(self.timer) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
143 self.timer = None |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
144 notif_elt.classList.remove('state_appended') |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
145 notif_elt.bind("transitionend", lambda __: notif_elt.remove()) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
146 self.retry_cb() |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
147 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
148 def update_counter(self, notif_elt): |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
149 counter = notif_elt.select_one(".retry_counter") |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
150 counter.text = str(self.counter) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
151 self.counter -= 1 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
152 if self.counter < 0: |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
153 self.retry(notif_elt) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
154 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
155 def show( |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
156 self, |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
157 message: str, |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
158 level: str = "warning", |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
159 delay: int = 5 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
160 ) -> None: |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
161 # we log in console error messages, may be useful |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
162 if level == "error": |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
163 log.error(message) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
164 elif level == "warning": |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
165 log.warning(message) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
166 self.counter = delay |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
167 notif_elt = self._tpl.get_elt({ |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
168 "message": message, |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
169 "level": level, |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
170 }) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
171 self.update_counter(notif_elt) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
172 document["notifs_area"] <= notif_elt |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
173 timer.set_timeout(lambda: notif_elt.classList.add('state_appended'), 0) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
174 self.timer = timer.set_interval(self.update_counter, 1000, notif_elt) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
175 for elt in notif_elt.select('.click_to_retry'): |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
176 elt.bind('click', lambda __: self.retry(notif_elt)) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
177 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
178 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
179 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
180 |
1328
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
181 notification = Notification() |