annotate libervia/web/pages/_browser/dialog.py @ 1608:29eb1ea35869

taks (sass): replace deprecated `node-sass` by `sass`.
author Goffi <goffi@goffi.org>
date Fri, 31 May 2024 11:10:02 +0200
parents 0a4433a343a3
children
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
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
1600
0a4433a343a3 browser (calls): implement WebRTC file sharing:
Goffi <goffi@goffi.org>
parents: 1589
diff changeset
12 def __init__(self, reason: str = "", text: str = "") -> None:
0a4433a343a3 browser (calls): implement WebRTC file sharing:
Goffi <goffi@goffi.org>
parents: 1589
diff changeset
13 self.reason = reason
0a4433a343a3 browser (calls): implement WebRTC file sharing:
Goffi <goffi@goffi.org>
parents: 1589
diff changeset
14 self.text = text
0a4433a343a3 browser (calls): implement WebRTC file sharing:
Goffi <goffi@goffi.org>
parents: 1589
diff changeset
15 super().__init__(text)
0a4433a343a3 browser (calls): implement WebRTC file sharing:
Goffi <goffi@goffi.org>
parents: 1589
diff changeset
16
1549
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
17
1299
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 class Confirm:
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
1343
8729d2708f65 browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents: 1328
diff changeset
20 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
21 self._tpl = Template("dialogs/confirm.html")
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 self.message = message
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 self.ok_label = ok_label
1343
8729d2708f65 browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents: 1328
diff changeset
24 assert ok_color in ("success", "danger")
8729d2708f65 browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents: 1328
diff changeset
25 self.ok_color = ok_color
1299
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 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
27 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
28 self.reset()
1299
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29
1549
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
30 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
31 """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
32 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
33 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
34 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
35 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
36 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
37 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
38
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
39 def _default_cancel_cb(self, evt, notif_elt):
1299
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 notif_elt.remove()
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41
1600
0a4433a343a3 browser (calls): implement WebRTC file sharing:
Goffi <goffi@goffi.org>
parents: 1589
diff changeset
42 def cancel(self, reason: str = "", text: str = ""):
1549
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
43 """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
44
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
45 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
46 """
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._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
48 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
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 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
51 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
52 if self._reject_cb is not None:
1600
0a4433a343a3 browser (calls): implement WebRTC file sharing:
Goffi <goffi@goffi.org>
parents: 1589
diff changeset
53 self._reject_cb(CancelError(reason, text))
1549
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
54 else:
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
55 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
56 self.reset()
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
57
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
58 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
59 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
60 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
61 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
62 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
63 self.reset()
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
64
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
65 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
66 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
67 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
68 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
69 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
70 self.reset()
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
71
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
72 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
73 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
74 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
75 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
76 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
77 self._reject_cb = reject_cb
1299
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 notif_elt = self._tpl.get_elt({
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 "message": self.message,
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80 "ok_label": self.ok_label,
1343
8729d2708f65 browser (dialog): color of `OK` button can be specified:
Goffi <goffi@goffi.org>
parents: 1328
diff changeset
81 "ok_color": self.ok_color,
1299
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 "cancel_label": self.cancel_label,
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 })
1549
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
84 self._notif_elt = notif_elt
1299
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 document['notifs_area'] <= notif_elt
053141849206 browser: module to handle dialogs, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 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
88 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
89 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
90 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
91 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
92
1385
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
93 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
94 evt.stopPropagation()
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
95 notif_elt.remove()
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
96 resolve_cb(confirmed)
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
97
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
98 async def ashow(self):
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
99 return window.Promise.new(
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
100 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
101 self.show(
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
102 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
103 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
104 reject_cb
1385
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
105 )
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
106 )
4b6f711b09cb browser (dialog): new `ashow` method to use `Confirm` in async methods
Goffi <goffi@goffi.org>
parents: 1343
diff changeset
107
1328
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 class Notification:
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
110
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
111 def __init__(self):
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
112 self._tpl = Template("dialogs/notification.html")
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
113
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
114 def close(self, notif_elt):
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
115 notif_elt.classList.remove('state_appended')
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
116 notif_elt.bind("transitionend", lambda __: notif_elt.remove())
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 def show(
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
119 self,
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
120 message: str,
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
121 level: str = "info",
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
122 delay: int = 5
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
123 ) -> None:
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
124 # 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
125 if level in ("warning", "error"):
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
126 print(f"[{level}] {message}")
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
127 notif_elt = self._tpl.get_elt({
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
128 "message": message,
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
129 "level": level,
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
130 })
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
131 document["notifs_area"] <= notif_elt
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
132 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
133 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
134 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
135 elt.bind('click', lambda __: self.close(notif_elt))
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
136
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
137
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
138 class RetryNotification:
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
139 def __init__(self, retry_cb):
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
140 self._tpl = Template("dialogs/retry-notification.html")
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
141 self.retry_cb = retry_cb
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
142 self.counter = 0
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
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
145 def retry(self, notif_elt):
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
146 if self.timer is not None:
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
147 timer.clear_interval(self.timer)
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
148 self.timer = None
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
149 notif_elt.classList.remove('state_appended')
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
150 notif_elt.bind("transitionend", lambda __: notif_elt.remove())
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
151 self.retry_cb()
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
152
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
153 def update_counter(self, notif_elt):
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
154 counter = notif_elt.select_one(".retry_counter")
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
155 counter.text = str(self.counter)
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
156 self.counter -= 1
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
157 if self.counter < 0:
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
158 self.retry(notif_elt)
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
159
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
160 def show(
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
161 self,
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
162 message: str,
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
163 level: str = "warning",
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
164 delay: int = 5
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
165 ) -> None:
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
166 # we log in console error messages, may be useful
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
167 if level == "error":
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
168 log.error(message)
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
169 elif level == "warning":
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
170 log.warning(message)
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
171 self.counter = delay
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
172 notif_elt = self._tpl.get_elt({
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
173 "message": message,
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
174 "level": level,
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
175 })
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
176 self.update_counter(notif_elt)
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
177 document["notifs_area"] <= notif_elt
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
178 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
179 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
180 for elt in notif_elt.select('.click_to_retry'):
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
181 elt.bind('click', lambda __: self.retry(notif_elt))
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
182
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
183
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
184
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1385
diff changeset
185
1328
683e50799d6d browser (dialog): new class to handle notifications
Goffi <goffi@goffi.org>
parents: 1299
diff changeset
186 notification = Notification()