diff libervia/web/pages/_browser/jid_search.py @ 1546:7f3f5ae7d65a

browser (jid_search): implement a mechanism to get JIDs from search terms
author Goffi <goffi@goffi.org>
date Thu, 06 Jul 2023 12:11:17 +0200
parents
children 66aa6e140ebb
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libervia/web/pages/_browser/jid_search.py	Thu Jul 06 12:11:17 2023 +0200
@@ -0,0 +1,44 @@
+from bridge import AsyncBridge as Bridge
+from browser import aio, console as log, window
+from cache import cache
+import json
+
+log.warning = log.warn
+profile = window.profile or ""
+bridge = Bridge()
+
+
+class JidSearch:
+
+    def __init__(self, search_elt, callback, empty_cb=None):
+        self.search_elt = search_elt
+        self.search_elt.bind("input", self.on_search_input)
+        self.last_query = None
+        self.current_query = None
+        self.callback = callback
+        self.empty_cb = empty_cb
+        current_search = search_elt.value.strip() or None
+        if current_search is not None:
+            aio.run(self.perform_search(current_search))
+
+    def on_search_input(self, evt):
+        search_text = evt.target.value.strip()
+        if not search_text and self.empty_cb is not None:
+            self.empty_cb()
+        elif len(search_text) > 2:
+            aio.run(self.perform_search(search_text))
+
+    async def perform_search(self, query):
+        if self.current_query is None:
+            log.debug(f"performing search: {query=}")
+            self.current_query = query
+            jid_items = json.loads(await bridge.jid_search(query, ""))
+            await cache.fill_identities(i["entity"] for i in jid_items)
+
+            self.last_query = query
+            self.current_query = None
+            current_query = self.search_elt.value.strip()
+            if current_query != query:
+                await self.perform_search(current_query)
+                return
+            self.callback(jid_items)