/ client_code / routing / _alert.py
_alert.py
 1  # SPDX-License-Identifier: MIT
 2  #
 3  # Copyright (c) 2021 The Anvil Extras project team members listed at
 4  # https://github.com/anvilistas/anvil-extras/graphs/contributors
 5  #
 6  # This software is published at https://github.com/anvilistas/anvil-extras
 7  
 8  __version__ = "3.1.0"
 9  
10  import anvil
11  from anvil import alert as anvil_alert
12  
13  active_alerts = []
14  
15  
16  def handle_alert_unload() -> bool:
17      for alert in reversed(active_alerts):
18          if alert.blocking:
19              from . import _navigation
20  
21              _navigation.stopUnload()
22              return True
23          else:
24              alert.content.raise_event("x-close-alert")
25  
26      return False
27  
28  
29  class ActiveAlert:
30      def __init__(self, content, blocking):
31          self.content = content
32          self.blocking = blocking
33  
34      def __enter__(self):
35          active_alerts.append(self)
36  
37      def __exit__(self, *args):
38          global active_alerts
39          active_alerts = [x for x in active_alerts if x is not self]
40  
41  
42  def alert(content, *args, dismissible=True, **kws):
43      if type(content) is str:
44          content = anvil.Label(text=content)
45  
46      with ActiveAlert(content, blocking=not dismissible):
47          return anvil_alert(content, *args, dismissible=dismissible, **kws)