diff libervia/web/pages/_browser/dialog.py @ 1619:a2cd4222c702

browser: Updates for new design: This patch add code to handle the new design for chat. New bridge method are used to invite users to MUC or get list of occupants. A new modules is used for components, with a first one for collapsible cards. rel 457
author Goffi <goffi@goffi.org>
date Sat, 12 Apr 2025 00:21:45 +0200
parents 0a4433a343a3
children 3a60bf3762ef
line wrap: on
line diff
--- a/libervia/web/pages/_browser/dialog.py	Sat Oct 26 23:07:01 2024 +0200
+++ b/libervia/web/pages/_browser/dialog.py	Sat Apr 12 00:21:45 2025 +0200
@@ -181,6 +181,62 @@
             elt.bind('click', lambda __: self.retry(notif_elt))
 
 
+class Modal:
+
+    def __init__(self, content_elt, is_card=False, closable=False):
+        """Init a Modal instance.
+
+        @param content_elt: Content of the modal.
+        @param is_card: If True, a Modal card will be used. The ``content_elt`` must be a
+            <div> with the "modal-card" class.
+        @param closable: if True, add a close cross at the top right of the modal.
+        """
+        self.is_card = is_card
+        if is_card:
+            if not content_elt.classList.contains("modal-card"):
+                raise ValueError(
+                    'Element must have a "modal-card" class when `is_card` is used'
+                )
+        self.closable = closable
+        self._tpl = Template("dialogs/modal.html")
+        self.content_elt = content_elt
+        self._modal_elt = None
+        self.reset()
+
+    def reset(self):
+        """Reset values of callbacks and notif element"""
+        if self._modal_elt is not None:
+            self._modal_elt.remove()
+            self._modal_elt = None
+
+    def _default_cancel_cb(self, evt, notif_elt):
+        notif_elt.remove()
+
+    def close(self):
+        """Close the dialog."""
+        if self._modal_elt is None:
+            log.warning("Calling close on an unshown dialog.")
+        self.reset()
+
+    def on_close_click(self, evt) -> None:
+        evt.preventDefault()
+        evt.stopPropagation()
+        self.close()
+
+    def show(self) -> None:
+        modal_elt = self._tpl.get_elt({
+            "closable": self.closable,
+        })
+        self._modal_elt = modal_elt
+        if self.is_card:
+            container_elt = modal_elt
+        else:
+            container_elt = modal_elt.select_one(".modal-content")
+        container_elt <= self.content_elt
+
+        document['notifs_area'] <= modal_elt
+        for ok_elt in modal_elt.select(".modal-close"):
+            ok_elt.bind("click", self.on_close_click)
 
 
 notification = Notification()