annotate libervia/web/pages/_browser/dialog.py @ 1545:be20e6ac9f22

browser (bridge): add methods/properties to wait for Websocket/BroadcastChannel to be ready
author Goffi <goffi@goffi.org>
date Thu, 06 Jul 2023 12:10:25 +0200
parents eb00d593801d
children e47c24204449
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 class Confirm:
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10
1343
8729d2708f65 browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents: 1328
diff changeset
11 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
12 self._tpl = Template("dialogs/confirm.html")
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 self.message = message
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 self.ok_label = ok_label
1343
8729d2708f65 browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents: 1328
diff changeset
15 assert ok_color in ("success", "danger")
8729d2708f65 browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents: 1328
diff changeset
16 self.ok_color = ok_color
1299
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17 self.cancel_label = cancel_label
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 def cancel_cb(self, evt, notif_elt):
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 notif_elt.remove()
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 def show(self, ok_cb, cancel_cb=None):
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 if cancel_cb is None:
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 cancel_cb = self.cancel_cb
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 notif_elt = self._tpl.get_elt({
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 "message": self.message,
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 "ok_label": self.ok_label,
1343
8729d2708f65 browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents: 1328
diff changeset
28 "ok_color": self.ok_color,
1299
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 "cancel_label": self.cancel_label,
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 })
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 document['notifs_area'] <= notif_elt
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 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
34 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
35 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
36 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
37 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
38
1385
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
39 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
40 evt.stopPropagation()
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
41 notif_elt.remove()
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
42 resolve_cb(confirmed)
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
43
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
44 async def ashow(self):
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
45 return window.Promise.new(
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
46 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
47 self.show(
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
48 lambda evt, notif_elt: self._ashow_cb(evt, notif_elt, resolve_cb, True),
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
49 lambda evt, notif_elt: self._ashow_cb(evt, notif_elt, resolve_cb, False)
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
50 )
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
51 )
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
52
1328
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
53
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
54 class Notification:
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
55
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
56 def __init__(self):
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
57 self._tpl = Template("dialogs/notification.html")
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
58
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
59 def close(self, notif_elt):
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
60 notif_elt.classList.remove('state_appended')
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
61 notif_elt.bind("transitionend", lambda __: notif_elt.remove())
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
62
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
63 def show(
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
64 self,
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
65 message: str,
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
66 level: str = "info",
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
67 delay: int = 5
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
68 ) -> None:
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
69 # 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
70 if level in ("warning", "error"):
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
71 print(f"[{level}] {message}")
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
72 notif_elt = self._tpl.get_elt({
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
73 "message": message,
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
74 "level": level,
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
75 })
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
76 document["notifs_area"] <= notif_elt
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
77 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
78 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
79 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
80 elt.bind('click', lambda __: self.close(notif_elt))
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
81
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
82
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
83 class RetryNotification:
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
84 def __init__(self, retry_cb):
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
85 self._tpl = Template("dialogs/retry-notification.html")
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
86 self.retry_cb = retry_cb
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
87 self.counter = 0
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
88 self.timer = None
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
89
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
90 def retry(self, notif_elt):
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
91 if self.timer is not None:
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
92 timer.clear_interval(self.timer)
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
93 self.timer = None
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
94 notif_elt.classList.remove('state_appended')
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
95 notif_elt.bind("transitionend", lambda __: notif_elt.remove())
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
96 self.retry_cb()
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
97
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
98 def update_counter(self, notif_elt):
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
99 counter = notif_elt.select_one(".retry_counter")
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
100 counter.text = str(self.counter)
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
101 self.counter -= 1
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
102 if self.counter < 0:
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
103 self.retry(notif_elt)
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
104
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
105 def show(
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
106 self,
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
107 message: str,
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
108 level: str = "warning",
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
109 delay: int = 5
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
110 ) -> None:
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
111 # we log in console error messages, may be useful
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
112 if level == "error":
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
113 log.error(message)
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
114 elif level == "warning":
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
115 log.warning(message)
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
116 self.counter = delay
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
117 notif_elt = self._tpl.get_elt({
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
118 "message": message,
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
119 "level": level,
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
120 })
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
121 self.update_counter(notif_elt)
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
122 document["notifs_area"] <= notif_elt
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
123 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
124 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
125 for elt in notif_elt.select('.click_to_retry'):
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
126 elt.bind('click', lambda __: self.retry(notif_elt))
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
127
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
128
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
129
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
130
1328
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
131 notification = Notification()