changeset 1556:5c4703870088

browser (jid_search): search items can now be customised: - a new `options` argument let specify custom behaviour such as hidding groups - template can now be specified
author Goffi <goffi@goffi.org>
date Tue, 15 Aug 2023 17:14:53 +0200
parents 0796bb8bad2f
children 855729ef75f2
files libervia/web/pages/_browser/jid_search.py
diffstat 1 files changed, 26 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/web/pages/_browser/jid_search.py	Mon Aug 14 17:10:27 2023 +0200
+++ b/libervia/web/pages/_browser/jid_search.py	Tue Aug 15 17:14:53 2023 +0200
@@ -19,8 +19,10 @@
         filter_cb=None,
         empty_cb=None,
         get_url=None,
-        click_cb=None
-    ):
+        click_cb=None,
+        options: dict|None = None,
+        template: str = "components/search_item.html"
+    ) -> None:
         """Initialize the JidSearch instance
 
         @param search_elt: The HTML <input> element for search
@@ -30,13 +32,23 @@
         @param get_url: The function to get URL for each entity in the search result
         @param click_cb: The function to handle the click event on each entity in the
             search result
+        @param options: extra options. Key can be:
+            no_group(bool)
+                True if groups should not be visible
+            extra_cb(dict)
+                a map from CSS selector to callback, the callback will be binded to the
+                "click" event, and will be called with the ``item`` as argument
+        @param template: template to use
         """
-        self.search_item_tpl = Template("components/search_item.html")
+        self.search_item_tpl = Template(template)
         self.search_elt = search_elt
         self.search_elt.bind("input", self.on_search_input)
         self.last_query = None
         self.current_query = None
         self.container_elt = container_elt
+        if options is None:
+            options = {}
+        self.options = options
 
         if click_cb is not None and get_url is None:
             self.get_url = lambda _: "#"
@@ -76,10 +88,20 @@
             search_item_elt = self.search_item_tpl.get_elt({
                 "url": self.get_url(item),
                 "item": item,
-                "identities": cache.identities
+                "identities": cache.identities,
+                "options": self.options,
             })
             if self.click_cb is not None:
                 search_item_elt.bind('click', lambda evt, item=item: self.click_cb(item))
+            extra_cb = self.options.get("extra_cb")
+            if extra_cb:
+                for selector, cb in extra_cb.items():
+                    for elt in search_item_elt.select(selector):
+                        log.debug(f"binding {selector=} {elt=} {cb=} {id(cb)=}")
+                        elt.bind(
+                            "click",
+                            lambda evt, item=item, cb=cb: cb(evt, item)
+                        )
             self.container_elt <= search_item_elt
 
     def on_search_input(self, evt):