Added widget selector predicate
authorThierry Florac <tflorac@ulthar.net>
Thu, 25 Jul 2019 12:06:22 +0200
changeset 178 afd8c5647c93
parent 177 58f8c7823449
child 179 f9147cd3fe5b
Added widget selector predicate
src/pyams_form/form.py
src/pyams_form/include.py
--- a/src/pyams_form/form.py	Fri Feb 01 18:35:07 2019 +0100
+++ b/src/pyams_form/form.py	Thu Jul 25 12:06:22 2019 +0200
@@ -682,6 +682,33 @@
         return False
 
 
+class WidgetSelector(object):
+    """Widget event selector
+
+    This selector can be used by subscribers to filter widgets events
+    """
+
+    def __init__(self, ifaces, config):
+        if not isinstance(ifaces, (list, tuple)):
+            ifaces = (ifaces,)
+        self.interfaces = ifaces
+
+    def text(self):
+        return 'widget_selector = %s' % str(self.interfaces)
+
+    phash = text
+
+    def __call__(self, event):
+        for intf in self.interfaces:
+            try:
+                if intf.providedBy(event.widget):
+                    return True
+            except (AttributeError, TypeError):
+                if isinstance(event.widget, intf):
+                    return True
+        return False
+
+
 class ajax_config(object):
     """Class decorator used to declare AJAX settings for a form.
 
--- a/src/pyams_form/include.py	Fri Feb 01 18:35:07 2019 +0100
+++ b/src/pyams_form/include.py	Thu Jul 25 12:06:22 2019 +0200
@@ -17,7 +17,7 @@
 # import interfaces
 
 # import packages
-from pyams_form.form import FormSelector
+from pyams_form.form import FormSelector, WidgetSelector
 
 
 def include_package(config):
@@ -28,6 +28,7 @@
 
     # custom subscriber predicate
     config.add_subscriber_predicate('form_selector', FormSelector)
+    config.add_subscriber_predicate('widget_selector', WidgetSelector)
 
     # load registry components
     config.scan()