comparison libervia/pages/_browser/dialog.py @ 1328:683e50799d6d

browser (dialog): new class to handle notifications
author Goffi <goffi@goffi.org>
date Fri, 14 Aug 2020 09:31:32 +0200
parents 053141849206
children 8729d2708f65
comparison
equal deleted inserted replaced
1327:e35de70f5295 1328:683e50799d6d
28 timer.set_timeout(lambda: notif_elt.classList.add('state_appended'), 0) 28 timer.set_timeout(lambda: notif_elt.classList.add('state_appended'), 0)
29 for cancel_elt in notif_elt.select(".click_to_cancel"): 29 for cancel_elt in notif_elt.select(".click_to_cancel"):
30 cancel_elt.bind("click", lambda evt: cancel_cb(evt, notif_elt)) 30 cancel_elt.bind("click", lambda evt: cancel_cb(evt, notif_elt))
31 for cancel_elt in notif_elt.select(".click_to_ok"): 31 for cancel_elt in notif_elt.select(".click_to_ok"):
32 cancel_elt.bind("click", lambda evt: ok_cb(evt, notif_elt)) 32 cancel_elt.bind("click", lambda evt: ok_cb(evt, notif_elt))
33
34
35 class Notification:
36
37 def __init__(self):
38 self._tpl = Template("dialogs/notification.html")
39
40 def close(self, notif_elt):
41 notif_elt.classList.remove('state_appended')
42 notif_elt.bind("transitionend", lambda __: notif_elt.remove())
43
44 def show(
45 self,
46 message: str,
47 level: str = "info",
48 delay: int = 5
49 ) -> None:
50 # we log in console error messages, may be useful
51 if level in ("warning", "error"):
52 print(f"[{level}] {message}")
53 notif_elt = self._tpl.get_elt({
54 "message": message,
55 "level": level,
56 })
57 document["notifs_area"] <= notif_elt
58 timer.set_timeout(lambda: notif_elt.classList.add('state_appended'), 0)
59 timer.set_timeout(lambda: self.close(notif_elt), delay * 1000)
60 for elt in notif_elt.select('.click_to_close'):
61 elt.bind('click', lambda __: self.close(notif_elt))
62
63
64 notification = Notification()