annotate libervia/web/pages/_browser/jid_search.py @ 1549:e47c24204449

browser (calls): update call to handle search, control buttons, and better UI/UX: - adapt to backend changes - UI and WebRTC parts are not separated - users can now be searched - add mute/fullscreen buttons - ring - cancellable dialog when a call is received - status of the call - animations - various UI/UX improvments rel 423
author Goffi <goffi@goffi.org>
date Wed, 09 Aug 2023 00:22:18 +0200
parents 66aa6e140ebb
children 5c4703870088
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
1549
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1548
diff changeset
56 if not current_search:
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1548
diff changeset
57 self.empty_cb()
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1548
diff changeset
58 else:
1546
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
59 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
60
1548
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
61 def default_get_url(self, item):
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
62 """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
63
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
64 @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
65 """
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
66 return urljoin(f"{window.location.href}/", quote(item["entity"]))
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
67
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
68 def show_items(self, items):
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
69 """Display the search items in the specified container
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 @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
72 """
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
73 assert self.container_elt is not None
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
74 self.container_elt.clear()
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
75 for item in items:
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
76 search_item_elt = self.search_item_tpl.get_elt({
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
77 "url": self.get_url(item),
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
78 "item": item,
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
79 "identities": cache.identities
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
80 })
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
81 if self.click_cb is not None:
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
82 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
83 self.container_elt <= search_item_elt
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
84
1546
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
85 def on_search_input(self, evt):
1548
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
86 """Handle the 'input' event for the search element
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
87
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
88 @param evt: The event object
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
89 """
1546
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
90 search_text = evt.target.value.strip()
1548
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
91 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
92 self.empty_cb()
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
93 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
94 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
95
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
96 async def perform_search(self, query):
1548
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
97 """Perform the search operation for a given query
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
98
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
99 @param query: The search query
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
100 """
1546
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
101 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
102 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
103 self.current_query = query
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
104 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
105 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
106
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
107 self.last_query = query
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
108 self.current_query = None
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
109 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
110 if current_query != query:
7f3f5ae7d65a browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff changeset
111 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
112 return
1548
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
113 self.filter_cb(jid_items)
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
114
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
115 def on_empty_search(self):
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
116 """Handle the situation when the search box is empty"""
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
117 assert self.container_elt is not None
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
118 items = [
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
119 {
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
120 "entity": jid_,
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
121 "groups": data["groups"]
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 for jid_, data in cache.roster.items()
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
124 ]
66aa6e140ebb browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents: 1546
diff changeset
125 self.show_items(items)