Mercurial > libervia-web
annotate libervia/web/pages/_browser/jid_search.py @ 1591:f1d09a4d38dc
browser (jid_search): fix `submit_filter` when there is no parent `<form>`
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 10 Dec 2023 11:01:57 +0100 |
parents | 02432346e9b2 |
children | c6976c5b85a1 |
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 |
1573
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
8 import jid |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 log.warning = log.warn |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 profile = window.profile or "" |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 bridge = Bridge() |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 class JidSearch: |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
16 def __init__( |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
17 self, |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
18 search_elt, |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
19 container_elt=None, |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
20 filter_cb=None, |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
21 empty_cb=None, |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
22 get_url=None, |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
23 click_cb=None, |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
24 options: dict|None = None, |
1573
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
25 submit_filter: bool = True, |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
26 template: str = "components/search_item.html" |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
27 ) -> None: |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
28 """Initialize the JidSearch instance |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
29 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
30 @param search_elt: The HTML <input> element for search |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
31 @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
|
32 @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
|
33 @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
|
34 @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
|
35 @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
|
36 search result |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
37 @param options: extra options. Key can be: |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
38 no_group(bool) |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
39 True if groups should not be visible |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
40 extra_cb(dict) |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
41 a map from CSS selector to callback, the callback will be binded to the |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
42 "click" event, and will be called with the ``item`` as argument |
1573
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
43 @param submit_filter: if True, only submit when a seemingly valid JID is entered |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
44 @param template: template to use |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
45 """ |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
46 self.search_item_tpl = Template(template) |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 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
|
48 self.search_elt.bind("input", self.on_search_input) |
1573
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
49 if submit_filter: |
1591
f1d09a4d38dc
browser (jid_search): fix `submit_filter` when there is no parent `<form>`
Goffi <goffi@goffi.org>
parents:
1573
diff
changeset
|
50 try: |
f1d09a4d38dc
browser (jid_search): fix `submit_filter` when there is no parent `<form>`
Goffi <goffi@goffi.org>
parents:
1573
diff
changeset
|
51 form_elt = self.search_elt.closest("form") |
f1d09a4d38dc
browser (jid_search): fix `submit_filter` when there is no parent `<form>`
Goffi <goffi@goffi.org>
parents:
1573
diff
changeset
|
52 except KeyError: |
f1d09a4d38dc
browser (jid_search): fix `submit_filter` when there is no parent `<form>`
Goffi <goffi@goffi.org>
parents:
1573
diff
changeset
|
53 log.debug("No parent <form> found, can't apply submit filter.") |
f1d09a4d38dc
browser (jid_search): fix `submit_filter` when there is no parent `<form>`
Goffi <goffi@goffi.org>
parents:
1573
diff
changeset
|
54 else: |
f1d09a4d38dc
browser (jid_search): fix `submit_filter` when there is no parent `<form>`
Goffi <goffi@goffi.org>
parents:
1573
diff
changeset
|
55 form_elt.bind("submit", self.on_form_submit) |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 self.last_query = None |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 self.current_query = None |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
58 self.container_elt = container_elt |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
59 if options is None: |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
60 options = {} |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
61 self.options = options |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
62 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
63 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
|
64 self.get_url = lambda _: "#" |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
65 else: |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
66 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
|
67 self.click_cb = click_cb |
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 if filter_cb is None: |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
70 if container_elt is None: |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
71 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
|
72 filter_cb = self.show_items |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
73 self.filter_cb = filter_cb |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
74 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
75 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
|
76 |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 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
|
78 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
|
79 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
|
80 else: |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 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
|
82 |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
83 def default_get_url(self, item): |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
84 """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
|
85 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
86 @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
|
87 """ |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
88 return urljoin(f"{window.location.href}/", quote(item["entity"])) |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
89 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
90 def show_items(self, items): |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
91 """Display the search items in the specified container |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
92 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
93 @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
|
94 """ |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
95 assert self.container_elt is not None |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
96 self.container_elt.clear() |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
97 for item in items: |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
98 search_item_elt = self.search_item_tpl.get_elt({ |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
99 "url": self.get_url(item), |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
100 "item": item, |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
101 "identities": cache.identities, |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
102 "options": self.options, |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
103 }) |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
104 if self.click_cb is not None: |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
105 search_item_elt.bind('click', lambda evt, item=item: self.click_cb(item)) |
1556
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
106 extra_cb = self.options.get("extra_cb") |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
107 if extra_cb: |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
108 for selector, cb in extra_cb.items(): |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
109 for elt in search_item_elt.select(selector): |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
110 log.debug(f"binding {selector=} {elt=} {cb=} {id(cb)=}") |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
111 elt.bind( |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
112 "click", |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
113 lambda evt, item=item, cb=cb: cb(evt, item) |
5c4703870088
browser (jid_search): search items can now be customised:
Goffi <goffi@goffi.org>
parents:
1549
diff
changeset
|
114 ) |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
115 self.container_elt <= search_item_elt |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
116 |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 def on_search_input(self, evt): |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
118 """Handle the 'input' event for the search element |
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 @param evt: The event object |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
121 """ |
1573
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
122 evt.stopPropagation() |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
123 evt.preventDefault() |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 search_text = evt.target.value.strip() |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
125 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
|
126 self.empty_cb() |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 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
|
128 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
|
129 |
1573
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
130 def on_form_submit(self, evt): |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
131 search_text = self.search_elt.value.strip() |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
132 search_jid = jid.JID(search_text) |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
133 if not search_jid.is_valid(): |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
134 evt.stopPropagation() |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
135 evt.preventDefault() |
02432346e9b2
browser (jid_search): `submit_filter` option:
Goffi <goffi@goffi.org>
parents:
1556
diff
changeset
|
136 |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 async def perform_search(self, query): |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
138 """Perform the search operation for a given query |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
139 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
140 @param query: The search query |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
141 """ |
1546
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 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
|
143 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
|
144 self.current_query = query |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 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
|
146 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
|
147 |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 self.last_query = query |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 self.current_query = None |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 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
|
151 if current_query != query: |
7f3f5ae7d65a
browser (jid_search): implement a mechanism to get JIDs from search terms
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 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
|
153 return |
1548
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
154 self.filter_cb(jid_items) |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
155 |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
156 def on_empty_search(self): |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
157 """Handle the situation when the search box is empty""" |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
158 assert self.container_elt is not None |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
159 items = [ |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
160 { |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
161 "entity": jid_, |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
162 "groups": data["groups"] |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
163 } |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
164 for jid_, data in cache.roster.items() |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
165 ] |
66aa6e140ebb
browser: make `jid_search` more generic:
Goffi <goffi@goffi.org>
parents:
1546
diff
changeset
|
166 self.show_items(items) |