comparison libervia/web/pages/_browser/jid_search.py @ 1573:02432346e9b2

browser (jid_search): `submit_filter` option: add an option (activated by default) to only submit search when a valid JID is present in search input.
author Goffi <goffi@goffi.org>
date Wed, 22 Nov 2023 16:31:36 +0100
parents 5c4703870088
children f1d09a4d38dc
comparison
equal deleted inserted replaced
1572:7006b55001a4 1573:02432346e9b2
3 3
4 from bridge import AsyncBridge as Bridge 4 from bridge import AsyncBridge as Bridge
5 from browser import aio, console as log, window 5 from browser import aio, console as log, window
6 from cache import cache 6 from cache import cache
7 from template import Template 7 from template import Template
8 import jid
8 9
9 log.warning = log.warn 10 log.warning = log.warn
10 profile = window.profile or "" 11 profile = window.profile or ""
11 bridge = Bridge() 12 bridge = Bridge()
12 13
19 filter_cb=None, 20 filter_cb=None,
20 empty_cb=None, 21 empty_cb=None,
21 get_url=None, 22 get_url=None,
22 click_cb=None, 23 click_cb=None,
23 options: dict|None = None, 24 options: dict|None = None,
25 submit_filter: bool = True,
24 template: str = "components/search_item.html" 26 template: str = "components/search_item.html"
25 ) -> None: 27 ) -> None:
26 """Initialize the JidSearch instance 28 """Initialize the JidSearch instance
27 29
28 @param search_elt: The HTML <input> element for search 30 @param search_elt: The HTML <input> element for search
36 no_group(bool) 38 no_group(bool)
37 True if groups should not be visible 39 True if groups should not be visible
38 extra_cb(dict) 40 extra_cb(dict)
39 a map from CSS selector to callback, the callback will be binded to the 41 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 42 "click" event, and will be called with the ``item`` as argument
43 @param submit_filter: if True, only submit when a seemingly valid JID is entered
41 @param template: template to use 44 @param template: template to use
42 """ 45 """
43 self.search_item_tpl = Template(template) 46 self.search_item_tpl = Template(template)
44 self.search_elt = search_elt 47 self.search_elt = search_elt
45 self.search_elt.bind("input", self.on_search_input) 48 self.search_elt.bind("input", self.on_search_input)
49 if submit_filter:
50 form_elt = self.search_elt.closest("form")
51 form_elt.bind("submit", self.on_form_submit)
46 self.last_query = None 52 self.last_query = None
47 self.current_query = None 53 self.current_query = None
48 self.container_elt = container_elt 54 self.container_elt = container_elt
49 if options is None: 55 if options is None:
50 options = {} 56 options = {}
107 def on_search_input(self, evt): 113 def on_search_input(self, evt):
108 """Handle the 'input' event for the search element 114 """Handle the 'input' event for the search element
109 115
110 @param evt: The event object 116 @param evt: The event object
111 """ 117 """
118 evt.stopPropagation()
119 evt.preventDefault()
112 search_text = evt.target.value.strip() 120 search_text = evt.target.value.strip()
113 if not search_text: 121 if not search_text:
114 self.empty_cb() 122 self.empty_cb()
115 elif len(search_text) > 2: 123 elif len(search_text) > 2:
116 aio.run(self.perform_search(search_text)) 124 aio.run(self.perform_search(search_text))
125
126 def on_form_submit(self, evt):
127 search_text = self.search_elt.value.strip()
128 search_jid = jid.JID(search_text)
129 if not search_jid.is_valid():
130 evt.stopPropagation()
131 evt.preventDefault()
117 132
118 async def perform_search(self, query): 133 async def perform_search(self, query):
119 """Perform the search operation for a given query 134 """Perform the search operation for a given query
120 135
121 @param query: The search query 136 @param query: The search query