comparison libervia/web/pages/_browser/jid_search.py @ 1556:5c4703870088

browser (jid_search): search items can now be customised: - a new `options` argument let specify custom behaviour such as hidding groups - template can now be specified
author Goffi <goffi@goffi.org>
date Tue, 15 Aug 2023 17:14:53 +0200
parents e47c24204449
children 02432346e9b2
comparison
equal deleted inserted replaced
1555:0796bb8bad2f 1556:5c4703870088
17 search_elt, 17 search_elt,
18 container_elt=None, 18 container_elt=None,
19 filter_cb=None, 19 filter_cb=None,
20 empty_cb=None, 20 empty_cb=None,
21 get_url=None, 21 get_url=None,
22 click_cb=None 22 click_cb=None,
23 ): 23 options: dict|None = None,
24 template: str = "components/search_item.html"
25 ) -> None:
24 """Initialize the JidSearch instance 26 """Initialize the JidSearch instance
25 27
26 @param search_elt: The HTML <input> element for search 28 @param search_elt: The HTML <input> element for search
27 @param container_elt: The HTML container to display the search results 29 @param container_elt: The HTML container to display the search results
28 @param filter_cb: The callback to filter the search results 30 @param filter_cb: The callback to filter the search results
29 @param empty_cb: The callback when the search box is empty 31 @param empty_cb: The callback when the search box is empty
30 @param get_url: The function to get URL for each entity in the search result 32 @param get_url: The function to get URL for each entity in the search result
31 @param click_cb: The function to handle the click event on each entity in the 33 @param click_cb: The function to handle the click event on each entity in the
32 search result 34 search result
35 @param options: extra options. Key can be:
36 no_group(bool)
37 True if groups should not be visible
38 extra_cb(dict)
39 a map from CSS selector to callback, the callback will be binded to the
40 "click" event, and will be called with the ``item`` as argument
41 @param template: template to use
33 """ 42 """
34 self.search_item_tpl = Template("components/search_item.html") 43 self.search_item_tpl = Template(template)
35 self.search_elt = search_elt 44 self.search_elt = search_elt
36 self.search_elt.bind("input", self.on_search_input) 45 self.search_elt.bind("input", self.on_search_input)
37 self.last_query = None 46 self.last_query = None
38 self.current_query = None 47 self.current_query = None
39 self.container_elt = container_elt 48 self.container_elt = container_elt
49 if options is None:
50 options = {}
51 self.options = options
40 52
41 if click_cb is not None and get_url is None: 53 if click_cb is not None and get_url is None:
42 self.get_url = lambda _: "#" 54 self.get_url = lambda _: "#"
43 else: 55 else:
44 self.get_url = get_url if get_url is not None else self.default_get_url 56 self.get_url = get_url if get_url is not None else self.default_get_url
74 self.container_elt.clear() 86 self.container_elt.clear()
75 for item in items: 87 for item in items:
76 search_item_elt = self.search_item_tpl.get_elt({ 88 search_item_elt = self.search_item_tpl.get_elt({
77 "url": self.get_url(item), 89 "url": self.get_url(item),
78 "item": item, 90 "item": item,
79 "identities": cache.identities 91 "identities": cache.identities,
92 "options": self.options,
80 }) 93 })
81 if self.click_cb is not None: 94 if self.click_cb is not None:
82 search_item_elt.bind('click', lambda evt, item=item: self.click_cb(item)) 95 search_item_elt.bind('click', lambda evt, item=item: self.click_cb(item))
96 extra_cb = self.options.get("extra_cb")
97 if extra_cb:
98 for selector, cb in extra_cb.items():
99 for elt in search_item_elt.select(selector):
100 log.debug(f"binding {selector=} {elt=} {cb=} {id(cb)=}")
101 elt.bind(
102 "click",
103 lambda evt, item=item, cb=cb: cb(evt, item)
104 )
83 self.container_elt <= search_item_elt 105 self.container_elt <= search_item_elt
84 106
85 def on_search_input(self, evt): 107 def on_search_input(self, evt):
86 """Handle the 'input' event for the search element 108 """Handle the 'input' event for the search element
87 109