# HG changeset patch # User Thierry Florac # Date 1564049182 -7200 # Node ID afd8c5647c9346273812f463d421f47bd67de6d8 # Parent 58f8c7823449ee8235d3624a6550f78d913a390f Added widget selector predicate diff -r 58f8c7823449 -r afd8c5647c93 src/pyams_form/form.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. diff -r 58f8c7823449 -r afd8c5647c93 src/pyams_form/include.py --- 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()