Mercurial > libervia-web
annotate libervia/web/pages/_browser/dialog.py @ 1574:4ab4bc3ae462
browser: new `tools` module with a first method to detect touch devices
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 22 Nov 2023 16:31:36 +0100 |
parents | e47c24204449 |
children | 7228fc3c4744 |
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 |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
22 self.reset() |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
24 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
|
25 """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
|
26 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
|
27 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
|
28 self._reject_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._notif_elt = None |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
30 |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
31 def _default_cancel_cb(self, evt, notif_elt): |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 notif_elt.remove() |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
34 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
|
35 """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
|
36 |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
37 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
|
38 """ |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
39 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
|
40 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
|
41 else: |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
42 self._notif_elt.remove() |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
43 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
|
44 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
|
45 else: |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
46 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
|
47 self.reset() |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
48 |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
49 def on_ok_click(self, evt): |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
50 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
|
51 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
|
52 self.reset() |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
53 |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
54 def on_cancel_click(self, evt) -> None: |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
55 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
|
56 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
|
57 self.reset() |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
58 |
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 self._reject_cb = reject_cb |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 notif_elt = self._tpl.get_elt({ |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 "message": self.message, |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 "ok_label": self.ok_label, |
1343
8729d2708f65
browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents:
1328
diff
changeset
|
68 "ok_color": self.ok_color, |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 "cancel_label": self.cancel_label, |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 }) |
1549
e47c24204449
browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
71 self._notif_elt = notif_elt |
1299
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 document['notifs_area'] <= notif_elt |
053141849206
browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 |
1385
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
80 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
|
81 evt.stopPropagation() |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
82 notif_elt.remove() |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
83 resolve_cb(confirmed) |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
84 |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
85 async def ashow(self): |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
86 return window.Promise.new( |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
87 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
|
88 self.show( |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
89 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
|
90 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
|
91 reject_cb |
1385
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 ) |
4b6f711b09cb
browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents:
1343
diff
changeset
|
94 |
1328
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
95 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
96 class Notification: |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
97 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
98 def __init__(self): |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
99 self._tpl = Template("dialogs/notification.html") |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
100 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
101 def close(self, notif_elt): |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
102 notif_elt.classList.remove('state_appended') |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
103 notif_elt.bind("transitionend", lambda __: notif_elt.remove()) |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
104 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
105 def show( |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
106 self, |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
107 message: str, |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
108 level: str = "info", |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
109 delay: int = 5 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
110 ) -> None: |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
111 # 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
|
112 if level in ("warning", "error"): |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
113 print(f"[{level}] {message}") |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
114 notif_elt = self._tpl.get_elt({ |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
115 "message": message, |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
116 "level": level, |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
117 }) |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
118 document["notifs_area"] <= notif_elt |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
119 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
|
120 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
|
121 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
|
122 elt.bind('click', lambda __: self.close(notif_elt)) |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
123 |
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
124 |
1504
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
125 class RetryNotification: |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
126 def __init__(self, retry_cb): |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
127 self._tpl = Template("dialogs/retry-notification.html") |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
128 self.retry_cb = retry_cb |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
129 self.counter = 0 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
130 self.timer = None |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
131 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
132 def retry(self, notif_elt): |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
133 if self.timer is not None: |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
134 timer.clear_interval(self.timer) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
135 self.timer = None |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
136 notif_elt.classList.remove('state_appended') |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
137 notif_elt.bind("transitionend", lambda __: notif_elt.remove()) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
138 self.retry_cb() |
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 update_counter(self, notif_elt): |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
141 counter = notif_elt.select_one(".retry_counter") |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
142 counter.text = str(self.counter) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
143 self.counter -= 1 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
144 if self.counter < 0: |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
145 self.retry(notif_elt) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
146 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
147 def show( |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
148 self, |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
149 message: str, |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
150 level: str = "warning", |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
151 delay: int = 5 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
152 ) -> None: |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
153 # we log in console error messages, may be useful |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
154 if level == "error": |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
155 log.error(message) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
156 elif level == "warning": |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
157 log.warning(message) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
158 self.counter = delay |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
159 notif_elt = self._tpl.get_elt({ |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
160 "message": message, |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
161 "level": level, |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
162 }) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
163 self.update_counter(notif_elt) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
164 document["notifs_area"] <= notif_elt |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
165 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
|
166 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
|
167 for elt in notif_elt.select('.click_to_retry'): |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
168 elt.bind('click', lambda __: self.retry(notif_elt)) |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
169 |
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 |
409d10211b20
server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents:
1385
diff
changeset
|
172 |
1328
683e50799d6d
browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents:
1299
diff
changeset
|
173 notification = Notification() |