annotate libervia/web/pages/_browser/jid_search.py @ 1548:66aa6e140ebb

browser: make `jid_search` more generic: rel 423
author Goffi <goffi@goffi.org>
date Wed, 09 Aug 2023 00:22:16 +0200
parents 7f3f5ae7d65a
children e47c24204449
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1548
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
1 import json
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
2 from urllib.parse import quote, urljoin
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
3
1546
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from bridge import AsyncBridge as Bridge
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
5 from browser import aio, console as log, window
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
6 from cache import cache
1548
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
7 from template import Template
1546
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
8
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
9 log.warning = log.warn
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
10 profile = window.profile or ""
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
11 bridge = Bridge()
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
12
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
13 class JidSearch:
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
14
1548
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
15 def __init__(
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
16 self,
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
17 search_elt,
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
18 container_elt=None,
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
19 filter_cb=None,
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
20 empty_cb=None,
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
21 get_url=None,
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
22 click_cb=None
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
23 ):
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
24 """Initialize the JidSearch instance
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
25
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
26 @param search_elt: The HTML <input> element for search
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
27 @param container_elt: The HTML container to display the search results
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
28 @param filter_cb: The callback to filter the search results
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
29 @param empty_cb: The callback when the search box is empty
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
30 @param get_url: The function to get URL for each entity in the search result
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
31 @param click_cb: The function to handle the click event on each entity in the
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
32 search result
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
33 """
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
34 self.search_item_tpl = Template("components/search_item.html")
1546
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
35 self.search_elt = search_elt
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
36 self.search_elt.bind("input", self.on_search_input)
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
37 self.last_query = None
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
38 self.current_query = None
1548
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
39 self.container_elt = container_elt
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
40
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
41 if click_cb is not None and get_url is None:
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
42 self.get_url = lambda _: "#"
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
43 else:
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
44 self.get_url = get_url if get_url is not None else self.default_get_url
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
45 self.click_cb = click_cb
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
46
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
47 if filter_cb is None:
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
48 if container_elt is None:
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
49 raise ValueError("container_elt must be set if filter_cb is not set")
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
50 filter_cb = self.show_items
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
51 self.filter_cb = filter_cb
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
52
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
53 self.empty_cb = empty_cb or self.on_empty_search
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
54
1546
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
55 current_search = search_elt.value.strip() or None
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
56 if current_search is not None:
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
57 aio.run(self.perform_search(current_search))
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
58
1548
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
59 def default_get_url(self, item):
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
60 """Default method to get the URL for a given entity
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
61
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
62 @param item: The item (entity) for which the URL is required
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
63 """
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
64 return urljoin(f"{window.location.href}/", quote(item["entity"]))
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
65
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
66 def show_items(self, items):
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
67 """Display the search items in the specified container
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
68
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
69 @param items: The list of search items to be displayed
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
70 """
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
71 assert self.container_elt is not None
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
72 self.container_elt.clear()
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
73 for item in items:
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
74 search_item_elt = self.search_item_tpl.get_elt({
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
75 "url": self.get_url(item),
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
76 "item": item,
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
77 "identities": cache.identities
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
78 })
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
79 if self.click_cb is not None:
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
80 search_item_elt.bind('click', lambda evt, item=item: self.click_cb(item))
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
81 self.container_elt <= search_item_elt
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
82
1546
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
83 def on_search_input(self, evt):
1548
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
84 """Handle the 'input' event for the search element
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
85
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
86 @param evt: The event object
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
87 """
1546
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
88 search_text = evt.target.value.strip()
1548
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
89 if not search_text:
1546
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self.empty_cb()
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
91 elif len(search_text) > 2:
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
92 aio.run(self.perform_search(search_text))
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
93
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
94 async def perform_search(self, query):
1548
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
95 """Perform the search operation for a given query
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
96
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
97 @param query: The search query
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
98 """
1546
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
99 if self.current_query is None:
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
100 log.debug(f"performing search: {query=}")
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
101 self.current_query = query
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
102 jid_items = json.loads(await bridge.jid_search(query, ""))
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
103 await cache.fill_identities(i["entity"] for i in jid_items)
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
104
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
105 self.last_query = query
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
106 self.current_query = None
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
107 current_query = self.search_elt.value.strip()
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
108 if current_query != query:
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
109 await self.perform_search(current_query)
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
110 return
1548
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
111 self.filter_cb(jid_items)
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
112
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
113 def on_empty_search(self):
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
114 """Handle the situation when the search box is empty"""
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
115 assert self.container_elt is not None
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
116 items = [
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
117 {
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
118 "entity": jid_,
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
119 "groups": data["groups"]
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
120 }
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
121 for jid_, data in cache.roster.items()
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
122 ]
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
123 self.show_items(items)