changeset 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 7006b55001a4
children 4ab4bc3ae462
files libervia/web/pages/_browser/jid_search.py
diffstat 1 files changed, 15 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/web/pages/_browser/jid_search.py	Wed Nov 22 16:31:36 2023 +0100
+++ b/libervia/web/pages/_browser/jid_search.py	Wed Nov 22 16:31:36 2023 +0100
@@ -5,6 +5,7 @@
 from browser import aio, console as log, window
 from cache import cache
 from template import Template
+import jid
 
 log.warning = log.warn
 profile = window.profile or ""
@@ -21,6 +22,7 @@
         get_url=None,
         click_cb=None,
         options: dict|None = None,
+        submit_filter: bool = True,
         template: str = "components/search_item.html"
     ) -> None:
         """Initialize the JidSearch instance
@@ -38,11 +40,15 @@
             extra_cb(dict)
                 a map from CSS selector to callback, the callback will be binded to the
                 "click" event, and will be called with the ``item`` as argument
+        @param submit_filter: if True, only submit when a seemingly valid JID is entered
         @param template: template to use
         """
         self.search_item_tpl = Template(template)
         self.search_elt = search_elt
         self.search_elt.bind("input", self.on_search_input)
+        if submit_filter:
+            form_elt = self.search_elt.closest("form")
+            form_elt.bind("submit", self.on_form_submit)
         self.last_query = None
         self.current_query = None
         self.container_elt = container_elt
@@ -109,12 +115,21 @@
 
         @param evt: The event object
         """
+        evt.stopPropagation()
+        evt.preventDefault()
         search_text = evt.target.value.strip()
         if not search_text:
             self.empty_cb()
         elif len(search_text) > 2:
             aio.run(self.perform_search(search_text))
 
+    def on_form_submit(self, evt):
+        search_text = self.search_elt.value.strip()
+        search_jid = jid.JID(search_text)
+        if not search_jid.is_valid():
+            evt.stopPropagation()
+            evt.preventDefault()
+
     async def perform_search(self, query):
         """Perform the search operation for a given query