From 0e47700e812c8e5f582b74de4b96d49834a041b0 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Sat, 9 Apr 2011 00:56:13 +0000 Subject: [PATCH] live methods should follow the same api that gquery uses for bind, using Event.* constants to be more type-safe. --- .../com/google/gwt/query/client/GQuery.java | 46 +++++++++++++++++-- .../google/gwt/query/client/LazyGQuery.java | 22 ++++++++- .../gwt/query/client/plugins/Events.java | 22 +++++++-- .../gwt/query/client/plugins/LazyEvents.java | 6 ++- .../client/plugins/events/EventsListener.java | 45 ++++++++++-------- 5 files changed, 112 insertions(+), 29 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java index c8c95539..78796336 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java @@ -879,6 +879,21 @@ public class GQuery implements Lazy { return as(Events).bind(eventbits, data, funcs); } + /** + * Binds a set of handlers to a particular Event for each matched element. + * + * The event handlers are passed as Functions that you can use to prevent + * default behavior. To stop both default action and event bubbling, the + * function event handler has to return false. + * + * You can pass an additional Object data to your Function as the second + * parameter + * + */ + public GQuery bind(String eventType, final Object data, final Function... funcs) { + return as(Events).bind(eventType, data, funcs); + } + /** * Bind a set of functions to the blur event of each matched element. Or * trigger the event if no functions are provided. @@ -1427,7 +1442,7 @@ public class GQuery implements Lazy { * initially used with {@link #live(String, Function)} */ public GQuery die() { - return as(Events).die(null); + return die(0); } /** @@ -1439,6 +1454,16 @@ public class GQuery implements Lazy { public GQuery die(String eventName) { return as(Events).die(eventName); } + + /** + * Remove an event handlers previously attached using + * {@link #live(int, Function)} In order for this method to function + * correctly, the selector used with it must match exactly the selector + * initially used with {@link #live(int, Function)} + */ + public GQuery die(int eventbits) { + return as(Events).die(eventbits); + } /** * Run one or more Functions over each element of the GQuery. You have to @@ -2008,10 +2033,25 @@ public class GQuery implements Lazy { * *

*/ - public GQuery live(String eventName, Function func) { - return as(Events).live(eventName, null, func); + public GQuery live(String eventName, Function... funcs) { + return as(Events).live(eventName, null, funcs); } + /** + * Attach a handler for this event to all elements which match the current + * selector, now and in the future. + */ + public GQuery live(int eventbits, Function...funcs){ + return as(Events).live(eventbits, null, funcs); + } + + /** + * Attach a handler for this event to all elements which match the current + * selector, now and in the future. + */ + public GQuery live(int eventbits, Object data, Function...funcs){ + return as(Events).live(eventbits, data, funcs); + } /** *

diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java index 2a5b32ce..edf79fbb 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java @@ -680,6 +680,14 @@ public interface LazyGQuery extends LazyBase{ */ LazyGQuery die(String eventName); + /** + * Remove an event handlers previously attached using + * {@link #live(int, Function)} In order for this method to function + * correctly, the selector used with it must match exactly the selector + * initially used with {@link #live(int, Function)} + */ + LazyGQuery die(int eventbits); + /** * Run one or more Functions over each element of the GQuery. You have to * override one of these funcions: public void f(Element e) public String @@ -1050,7 +1058,19 @@ public interface LazyGQuery extends LazyBase{ * *

*/ - LazyGQuery live(String eventName, Function func); + LazyGQuery live(String eventName, Function... funcs); + + /** + * Attach a handler for this event to all elements which match the current + * selector, now and in the future. + */ + LazyGQuery live(int eventbits, Function...funcs); + + /** + * Attach a handler for this event to all elements which match the current + * selector, now and in the future. + */ + LazyGQuery live(int eventbits, Object data, Function...funcs); /** *

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 fc58ce0f..bdd3802e 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 @@ -109,21 +109,33 @@ public class Events extends GQuery { public GQuery die(String eventName) { EventsListener.getInstance( Element.is(currentContext) ? (Element) currentContext : body).die( - eventName, currentSelector); + eventName, currentSelector); return this; } - public GQuery live(String eventName, final Object data, Function func) { + public GQuery die(int eventbits) { + EventsListener.getInstance( + Element.is(currentContext) ? (Element) currentContext : body).die( + eventbits, currentSelector); + return this; + } - // bind live delegating event to the current context + public GQuery live(String eventName, final Object data, Function... funcs) { EventsListener.getInstance( Element.is(currentContext) ? (Element) currentContext : body).live( - eventName, currentSelector, data, func); - + eventName, currentSelector, data, funcs); + return this; + } + + public GQuery live(int eventbits, final Object data, Function... funcs) { + EventsListener.getInstance( + Element.is(currentContext) ? (Element) currentContext : body).live( + eventbits, currentSelector, data, funcs); return this; } + /** * Binds a handler to a particular Event (like Event.ONCLICK) for each matched * element. The handler is executed only once for each element. diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEvents.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEvents.java index 126f6b60..5ce73011 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEvents.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEvents.java @@ -75,7 +75,11 @@ public interface LazyEvents extends LazyBase{ */ GQuery die(String eventName); - GQuery live(String eventName, Object data, Function func); + GQuery die(int eventbits); + + GQuery live(String eventName, Object data, Function... funcs); + + GQuery live(int eventbits, Object data, Function... funcs); /** * Binds a handler to a particular Event (like Event.ONCLICK) for each matched 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 e77540ff..bc7c807f 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 @@ -281,7 +281,7 @@ public class EventsListener implements EventListener { private Element element; private JsObjectArray elementEvents = JsObjectArray.createArray().cast(); - private Map liveBindFunctionByEventType = new HashMap(); + private Map liveBindFunctionByEventType = new HashMap(); private EventsListener(Element element) { this.element = element; @@ -328,21 +328,26 @@ public class EventsListener implements EventListener { bind(b, nameSpace, data, function, -1); } } - + public void die(String eventName, String cssSelector) { - if (eventName == null) { + int eventType = "submit".equals(eventName) ? eventType = ONSUBMIT + : Event.getTypeInt(eventName); + die(eventType, cssSelector); + } + + public void die(int eventbits, String cssSelector) { + if (eventbits == 0) { for (LiveBindFunction liveBindFunction : liveBindFunctionByEventType.values()) { liveBindFunction.removeBindFunctionForSelector(cssSelector); } } else { - LiveBindFunction liveBindFunction = liveBindFunctionByEventType.get(eventName); + LiveBindFunction liveBindFunction = liveBindFunctionByEventType.get(eventbits); liveBindFunction.removeBindFunctionForSelector(cssSelector); } } public void dispatchEvent(Event event) { - int etype = "submit".equalsIgnoreCase(event.getType()) ? ONSUBMIT : DOM.eventGetType(event); for (int i = 0; i < elementEvents.length(); i++) { @@ -364,26 +369,28 @@ public class EventsListener implements EventListener { return getGwtEventListener(element); } - public void live(String eventName, String cssSelector, Object data, Function f) { - int eventType = 0; - if ("submit".equals(eventName)) { - eventType = ONSUBMIT; - } else { - eventType = Event.getTypeInt(eventName); - } + public void live(String eventName, String cssSelector, Object data, Function... f) { + int eventType = "submit".equals(eventName) ? eventType = ONSUBMIT + : Event.getTypeInt(eventName); + live(eventType, cssSelector, data, f); + } + + public void live(int eventbits, String cssSelector, Object data, Function... funcs) { // is a LiveBindFunction already attached for this kind of event - LiveBindFunction liveBindFunction = liveBindFunctionByEventType.get(eventName); + LiveBindFunction liveBindFunction = liveBindFunctionByEventType.get(eventbits); if (liveBindFunction == null) { - liveBindFunction = new LiveBindFunction(eventType, "live"); - eventBits |= eventType; + liveBindFunction = new LiveBindFunction(eventbits, "live"); + eventBits |= eventbits; sink(); elementEvents.add(liveBindFunction); - liveBindFunctionByEventType.put(eventName, liveBindFunction); + liveBindFunctionByEventType.put(eventbits, liveBindFunction); } - liveBindFunction.addBindFunctionForSelector(cssSelector, new BindFunction( - eventType, "live", f, data)); + for (Function f: funcs) { + liveBindFunction.addBindFunctionForSelector(cssSelector, new BindFunction( + eventbits, "live", f, data)); + } } @@ -439,7 +446,7 @@ public class EventsListener implements EventListener { private void clean() { cleanGQListeners(element); elementEvents = JsObjectArray.createArray().cast(); - liveBindFunctionByEventType = new HashMap(); + liveBindFunctionByEventType = new HashMap(); } private void sink() { -- 2.39.5