From e4ea1388ed6ac6ee6d1ab594202da418110cdbc8 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Wed, 24 Dec 2014 17:00:03 +0100 Subject: [PATCH] trigger methods should work with namespaces --- .../gwt/query/client/plugins/Events.java | 52 +++++++++++++------ .../client/plugins/events/EventsListener.java | 13 +++-- .../query/client/plugins/events/GqEvent.java | 3 ++ .../gwt/query/client/GQueryEventsTestGwt.java | 12 +++++ 4 files changed, 58 insertions(+), 22 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Events.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Events.java index 7f2127d1..d7f7e7df 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Events.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Events.java @@ -21,6 +21,7 @@ import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.query.client.plugins.events.EventsListener; +import com.google.gwt.query.client.plugins.events.EventsListener.EvPart; import com.google.gwt.user.client.Event; /** @@ -264,8 +265,20 @@ public class Events extends GQuery { * @param nativeEvent the browser native event. * @functions a set of function to run if the event is not canceled. */ - public Events trigger(NativeEvent nativeEvent, Function... functions) { - dispatchEvent(nativeEvent, null, functions); + public Events trigger(NativeEvent nativeEvent, Function... fcns) { + dispatchEvent(nativeEvent, null, fcns); + return this; + } + + /** + * Trigger a native event in all matched elements. + * + * @param nativeEvent the browser native event. + * @param datas a set of object passed as data when executed the handlers + * @param functions a set of function to run if the event is not canceled. + */ + public Events trigger(NativeEvent nativeEvent, Object[] datas, Function... functions) { + dispatchEvent(nativeEvent, datas, functions); return this; } @@ -273,7 +286,7 @@ public class Events extends GQuery { * Trigger a html event in all matched elements. * * @param htmlEvent A string representing the desired html event. - * @functions a set of function to run if the event is not canceled. + * @param functions a set of function to run. */ public Events triggerHtmlEvent(String htmlEvent, Function... functions) { return triggerHtmlEvent(htmlEvent, null, functions); @@ -283,24 +296,29 @@ public class Events extends GQuery { * Trigger a html event in all matched elements. * * @param htmlEvent An string representing the desired html event. - * @functions a set of function to run if the event is not canceled. + * @param datas a set of object passed as data when executed the handlers + * @param functions a set of function to run. */ public Events triggerHtmlEvent(String htmlEvent, Object[] datas, final Function... functions) { - NativeEvent e = document.createHtmlEvent(htmlEvent, true, true); - if ("submit".equals(htmlEvent)){ - Function submitFunction = new Function() { - public void f(Element e) { - // first submit the form then call the others functions - if (FormElement.is(e)) { - e.cast().submit(); + for (EvPart part : EvPart.split(htmlEvent)) { + NativeEvent e = document.createHtmlEvent(part.eventName, true, true); + JsUtils.prop(e, "namespace", part.nameSpace); + if ("submit".equals(part.eventName)){ + Function submitFunction = new Function() { + public void f(Element e) { + // first submit the form then call the others functions + if (FormElement.is(e)) { + e.cast().submit(); + } + callHandlers(e, getEvent(), functions); } - callHandlers(e, getEvent(), functions); - } - }; - dispatchEvent(e, datas, submitFunction); - } else { - dispatchEvent(e, datas, functions); + }; + dispatchEvent(e, datas, submitFunction); + } else { + dispatchEvent(e, datas, functions); + } } + return this; } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java index d8b80e55..a6e5912e 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java @@ -73,15 +73,15 @@ public class EventsListener implements EventListener { /** * Utility class to split a list of events with or without namespaces */ - private static class EvPart { - String nameSpace; - String eventName; + public static class EvPart { + public final String nameSpace; + public final String eventName; public EvPart(String n, String e) { nameSpace = n; eventName = e; } - static List split(String events) { + public static List split(String events) { List ret = new ArrayList(); String[] parts = events.split("[\\s,]+"); for (String event : parts) { @@ -561,7 +561,10 @@ public class EventsListener implements EventListener { Object[] handlerData = $(element).data(EVENT_DATA); for (int i = 0, l = elementEvents.length(); i < l; i++) { BindFunction listener = elementEvents.get(i); - if (listener != null && (listener.hasEventType(etype) || listener.isTypeOf(eventName))) { + String namespace = JsUtils.prop(event, "namespace"); + boolean matchEV = listener != null && (listener.hasEventType(etype) || listener.isTypeOf(eventName)); + boolean matchNS = matchEV && (isNullOrEmpty(namespace) || listener.nameSpace.equals(namespace)); + if (matchEV && matchNS) { if (!listener.fire(event, handlerData)) { event.stopPropagation(); event.preventDefault(); diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/GqEvent.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/GqEvent.java index e98a5f4c..cd78bde1 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/GqEvent.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/GqEvent.java @@ -96,4 +96,7 @@ public class GqEvent extends Event { } } + public static final GqEvent as(Event e) { + return e.cast(); + } } diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTestGwt.java index 08777f32..0ad8ae40 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTestGwt.java @@ -1904,4 +1904,16 @@ public class GQueryEventsTestGwt extends GWTTestCase { assertEquals(2, mouseEnterFunction.invokationCounter); } + public void testBindAndTriggerWithNameSpace() { + $(e).html("
blop
"); + CounterFunction counter = new CounterFunction(); + $("#mainDiv", e).on("click.mynamespace;foo", counter); + $("#mainDiv").trigger("click"); + assertEquals(1, counter.invokationCounter); + $("#mainDiv").trigger("click.mynamespace;bar"); + assertEquals(1, counter.invokationCounter); + $("#mainDiv").trigger("click.mynamespace;foo"); + assertEquals(2, counter.invokationCounter); + } + } -- 2.39.5