changeset 1547:383e1fee29f6

browser (chat/select): update dynamically JID items when search box is used
author Goffi <goffi@goffi.org>
date Thu, 06 Jul 2023 12:12:07 +0200
parents 7f3f5ae7d65a
children 66aa6e140ebb
files libervia/web/pages/chat/select/_browser/__init__.py
diffstat 1 files changed, 51 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libervia/web/pages/chat/select/_browser/__init__.py	Thu Jul 06 12:12:07 2023 +0200
@@ -0,0 +1,51 @@
+from bridge import AsyncBridge as Bridge
+from browser import aio, bind, console as log, document, window
+from urllib.parse import urljoin, quote
+from jid_search import JidSearch
+from template import Template
+from cache import cache
+from pprint import pp
+
+log.warning = log.warn
+profile = window.profile or ""
+bridge = Bridge()
+
+
+class ChatSelect:
+
+    def __init__(self):
+        self.search_item_tpl = Template("chat/search_item.html")
+        self.chat_items_elt = document["chat_items"]
+
+    def show_items(self, items) -> None:
+        for item in items:
+            search_item_elt = self.search_item_tpl.get_elt({
+                "url": urljoin(f"{window.location.href}/", quote(item["entity"])),
+                "item": item,
+                "identities": cache.identities
+            })
+            self.chat_items_elt <= search_item_elt
+
+    def on_empty_search(self):
+        items = [
+            {
+                "entity": jid_,
+                "groups": data["groups"]
+            }
+            for jid_, data in cache.roster.items()
+        ]
+        self.chat_items_elt.clear()
+        self.show_items(items)
+
+    def on_search_filter(self, search_items):
+        self.chat_items_elt.clear()
+        pp(search_items)
+        self.show_items(search_items)
+
+
+chat_select = ChatSelect()
+jid_search = JidSearch(
+    document["search"],
+    chat_select.on_search_filter,
+    chat_select.on_empty_search
+)