Mercurial > libervia-web
comparison libervia/web/pages/_browser/dialog.py @ 1518:eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 16:49:28 +0200 |
parents | libervia/pages/_browser/dialog.py@409d10211b20 |
children | e47c24204449 |
comparison
equal
deleted
inserted
replaced
1517:b8ed9726525b | 1518:eb00d593801d |
---|---|
1 """manage common dialogs""" | |
2 | |
3 from browser import document, window, timer, console as log | |
4 from template import Template | |
5 | |
6 log.warning = log.warn | |
7 | |
8 | |
9 class Confirm: | |
10 | |
11 def __init__(self, message, ok_label="", cancel_label="", ok_color="success"): | |
12 self._tpl = Template("dialogs/confirm.html") | |
13 self.message = message | |
14 self.ok_label = ok_label | |
15 assert ok_color in ("success", "danger") | |
16 self.ok_color = ok_color | |
17 self.cancel_label = cancel_label | |
18 | |
19 def cancel_cb(self, evt, notif_elt): | |
20 notif_elt.remove() | |
21 | |
22 def show(self, ok_cb, cancel_cb=None): | |
23 if cancel_cb is None: | |
24 cancel_cb = self.cancel_cb | |
25 notif_elt = self._tpl.get_elt({ | |
26 "message": self.message, | |
27 "ok_label": self.ok_label, | |
28 "ok_color": self.ok_color, | |
29 "cancel_label": self.cancel_label, | |
30 }) | |
31 | |
32 document['notifs_area'] <= notif_elt | |
33 timer.set_timeout(lambda: notif_elt.classList.add('state_appended'), 0) | |
34 for cancel_elt in notif_elt.select(".click_to_cancel"): | |
35 cancel_elt.bind("click", lambda evt: cancel_cb(evt, notif_elt)) | |
36 for cancel_elt in notif_elt.select(".click_to_ok"): | |
37 cancel_elt.bind("click", lambda evt: ok_cb(evt, notif_elt)) | |
38 | |
39 def _ashow_cb(self, evt, notif_elt, resolve_cb, confirmed): | |
40 evt.stopPropagation() | |
41 notif_elt.remove() | |
42 resolve_cb(confirmed) | |
43 | |
44 async def ashow(self): | |
45 return window.Promise.new( | |
46 lambda resolve_cb, reject_cb: | |
47 self.show( | |
48 lambda evt, notif_elt: self._ashow_cb(evt, notif_elt, resolve_cb, True), | |
49 lambda evt, notif_elt: self._ashow_cb(evt, notif_elt, resolve_cb, False) | |
50 ) | |
51 ) | |
52 | |
53 | |
54 class Notification: | |
55 | |
56 def __init__(self): | |
57 self._tpl = Template("dialogs/notification.html") | |
58 | |
59 def close(self, notif_elt): | |
60 notif_elt.classList.remove('state_appended') | |
61 notif_elt.bind("transitionend", lambda __: notif_elt.remove()) | |
62 | |
63 def show( | |
64 self, | |
65 message: str, | |
66 level: str = "info", | |
67 delay: int = 5 | |
68 ) -> None: | |
69 # we log in console error messages, may be useful | |
70 if level in ("warning", "error"): | |
71 print(f"[{level}] {message}") | |
72 notif_elt = self._tpl.get_elt({ | |
73 "message": message, | |
74 "level": level, | |
75 }) | |
76 document["notifs_area"] <= notif_elt | |
77 timer.set_timeout(lambda: notif_elt.classList.add('state_appended'), 0) | |
78 timer.set_timeout(lambda: self.close(notif_elt), delay * 1000) | |
79 for elt in notif_elt.select('.click_to_close'): | |
80 elt.bind('click', lambda __: self.close(notif_elt)) | |
81 | |
82 | |
83 class RetryNotification: | |
84 def __init__(self, retry_cb): | |
85 self._tpl = Template("dialogs/retry-notification.html") | |
86 self.retry_cb = retry_cb | |
87 self.counter = 0 | |
88 self.timer = None | |
89 | |
90 def retry(self, notif_elt): | |
91 if self.timer is not None: | |
92 timer.clear_interval(self.timer) | |
93 self.timer = None | |
94 notif_elt.classList.remove('state_appended') | |
95 notif_elt.bind("transitionend", lambda __: notif_elt.remove()) | |
96 self.retry_cb() | |
97 | |
98 def update_counter(self, notif_elt): | |
99 counter = notif_elt.select_one(".retry_counter") | |
100 counter.text = str(self.counter) | |
101 self.counter -= 1 | |
102 if self.counter < 0: | |
103 self.retry(notif_elt) | |
104 | |
105 def show( | |
106 self, | |
107 message: str, | |
108 level: str = "warning", | |
109 delay: int = 5 | |
110 ) -> None: | |
111 # we log in console error messages, may be useful | |
112 if level == "error": | |
113 log.error(message) | |
114 elif level == "warning": | |
115 log.warning(message) | |
116 self.counter = delay | |
117 notif_elt = self._tpl.get_elt({ | |
118 "message": message, | |
119 "level": level, | |
120 }) | |
121 self.update_counter(notif_elt) | |
122 document["notifs_area"] <= notif_elt | |
123 timer.set_timeout(lambda: notif_elt.classList.add('state_appended'), 0) | |
124 self.timer = timer.set_interval(self.update_counter, 1000, notif_elt) | |
125 for elt in notif_elt.select('.click_to_retry'): | |
126 elt.bind('click', lambda __: self.retry(notif_elt)) | |
127 | |
128 | |
129 | |
130 | |
131 notification = Notification() |