From 31f2945ecdde3b6c9b7cc2b8ac3dac8c65c67d51 Mon Sep 17 00:00:00 2001
From: Manolo Carrasco
Date: Wed, 11 Dec 2013 17:58:15 -0800
Subject: [PATCH] Implementation of on/off methods, deprecate live, die,
delegate, undelegate
---
.../com/google/gwt/query/client/GQuery.java | 64 +++++++++++++++++++
.../gwt/query/client/plugins/Events.java | 9 +++
.../client/plugins/events/EventsListener.java | 4 +-
3 files changed, 76 insertions(+), 1 deletion(-)
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 211eb2bf..044a2500 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
@@ -26,6 +26,7 @@
import com.google.gwt.core.client.JsArrayMixed;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.core.client.ScriptInjector;
+import com.google.gwt.dev.Link;
import com.google.gwt.dom.client.BodyElement;
import com.google.gwt.dom.client.ButtonElement;
import com.google.gwt.dom.client.Document;
@@ -1827,6 +1828,7 @@ public GQuery delay(int milliseconds, String queueName, Function... f) {
*
* $("div.main").delegate(".subMain", Event.ONCLICK | Event.ONDBLCLICK, new Function(){...});
*
+ * @deprecated use {@link #on(String, String, Function...)}
*/
public GQuery delegate(String selector, int eventbits, Function... handlers) {
return delegate(selector, eventbits, null, handlers);
@@ -1867,6 +1869,7 @@ public GQuery delegate(String selector, int eventbits, Function... handlers) {
*
* $("div.main").delegate(".subMain", Event.ONCLICK | Event.ONDBLCLICK, new Function(){...});
*
+ * @deprecated use {@link #on(String, String, Object, Function...)}
*/
public GQuery delegate(String selector, int eventbits, Object data, Function... handlers) {
@@ -1914,6 +1917,7 @@ public GQuery delegate(String selector, int eventbits, Object data, Function...
*
*
*
+ * @deprecated use {@link #on(String, String, Function...)}
*/
public GQuery delegate(String selector, String eventType, Function... handlers) {
return delegate(selector, eventType, null, handlers);
@@ -1954,6 +1958,7 @@ public GQuery delegate(String selector, String eventType, Function... handlers)
*
*
*
+ * @deprecated use {@link #on(String, String, Object, Function...)}
*/
public GQuery delegate(String selector, String eventType, Object data, Function... handlers) {
for (Element e : elements) {
@@ -2007,6 +2012,7 @@ public GQuery detach(String filter) {
* Remove all event handlers previously attached using {@link #live(String, Function)}. In order
* for this method to function correctly, the selector used with it must match exactly the
* selector initially used with {@link #live(String, Function)}
+ * @deprecated use {@link #off(String, String)} instead
*/
public GQuery die() {
return die(0);
@@ -2016,6 +2022,7 @@ public GQuery die() {
* 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)}
+ * @deprecated use {@link #off(String)}
*/
public GQuery die(int eventbits) {
return as(Events).die(eventbits);
@@ -2705,6 +2712,7 @@ public int length() {
/**
* Attach a handler for this event to all elements which match the current selector, now and in
* the future.
+ * @deprecated use {@link #on(String, Function...)}
*/
public GQuery live(int eventbits, Function... funcs) {
return as(Events).live(eventbits, null, funcs);
@@ -2713,6 +2721,7 @@ public GQuery live(int eventbits, Function... funcs) {
/**
* Attach a handler for this event to all elements which match the current selector, now and in
* the future.
+ * @deprecated use {@link #on(String, Object, Function...)}
*/
public GQuery live(int eventbits, Object data, Function... funcs) {
return as(Events).live(eventbits, data, funcs);
@@ -2767,6 +2776,7 @@ public GQuery live(int eventbits, Object data, Function... funcs) {
* descendant of myElement.
*
*
+ * @deprecated use {@link #on(String, Function...)}
*/
public GQuery live(String eventName, Function... funcs) {
return as(Events).live(eventName, null, funcs);
@@ -2821,6 +2831,7 @@ public GQuery live(String eventName, Function... funcs) {
* descendant of myElement.
*
*
+ * @deprecated use {@link #on(String, Object, Function...)}
*/
public GQuery live(String eventName, Object data, Function... funcs) {
return as(Events).live(eventName, data, funcs);
@@ -3189,7 +3200,56 @@ public GQuery offsetParent() {
}
return new GQuery(offParent);
}
+
+ /**
+ * Attach an event handler function for one or more events to the selected elements.
+ */
+ public GQuery on(String eventName, Function... funcs) {
+ return bind(eventName, funcs);
+ }
+
+ /**
+ * Attach an event handler function for one or more events to the selected elements.
+ */
+ public GQuery on(String eventName, Object data, Function... funcs) {
+ return bind(eventName, data, funcs);
+ }
+ /**
+ * Attach an event handler function for one or more events to the selected elements.
+ */
+ public GQuery on(String eventName, String selector, Function... funcs) {
+ return delegate(selector, eventName, funcs);
+ }
+
+ /**
+ * Attach an event handler function for one or more events to the selected elements.
+ */
+ public GQuery on(String eventName, String selector, Object data, Function... funcs) {
+ return delegate(selector, eventName, data, funcs);
+ }
+
+ /**
+ * Remove all event handlers.
+ */
+ public GQuery off() {
+ return as(Effects).off();
+ }
+
+ /**
+ * Remove an event handler
+ */
+ public GQuery off(String eventName, Function f) {
+ return unbind(eventName, f);
+ }
+
+ /**
+ * Remove an event handler
+ */
+ public GQuery off(String eventName, String selector) {
+ return undelegate(selector, eventName);
+ }
+
/**
* Binds a handler to a particular Event (like Event.ONCLICK) for each matched element. The
* handler is executed only once for each element.
@@ -4443,6 +4503,7 @@ public GQuery unbind(String eventList, Function f) {
/**
* Remove all event delegation that have been bound using
* {@link #delegate(String, int, Function...)} {@link #live(int, Function...)} methods
+ * @deprecated use {@link #off()}
*/
public GQuery undelegate() {
return as(Events).undelegate();
@@ -4451,6 +4512,7 @@ public GQuery undelegate() {
/**
* Undelegate is a way of removing event handlers that have been bound using
* {@link #delegate(String, int, Function...)} method
+ * @deprecated use {@link #off(String)}
*/
public GQuery undelegate(String selector) {
for (Element e : elements) {
@@ -4463,6 +4525,7 @@ public GQuery undelegate(String selector) {
/**
* Undelegate is a way of removing event handlers that have been bound using
* {@link #delegate(String, int, Function...)} method
+ * @deprecated use {@link #off(String)}
*/
public GQuery undelegate(String selector, int eventBit) {
for (Element e : elements) {
@@ -4475,6 +4538,7 @@ public GQuery undelegate(String selector, int eventBit) {
/**
* Undelegate is a way of removing event handlers that have been bound using
* {@link #delegate(String, int, Function...)} method
+ * @deprecated use {@link #off(String, String)}
*/
public GQuery undelegate(String selector, String eventName) {
for (Element e : elements) {
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 26f96fc4..e3642dec 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
@@ -320,6 +320,15 @@ public Events unbind(int eventbits) {
return this;
}
+ public Events off() {
+ for (Element e : elements()) {
+ if (isEventCapable(e)) {
+ EventsListener.getInstance(e).clean();
+ }
+ }
+ return this;
+ }
+
/**
* Removes all handlers, that matches the events bits and the namespace passed, from each element.
*
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 039ce0d1..0fbf8742 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
@@ -732,6 +732,7 @@ public void unbind(int eventbits) {
public void unbind(int eventbits, String namespace, String eventName, String originalEventType,
Function f) {
+
JsObjectArray newList = JsObjectArray.createArray().cast();
for (int i = 0; i < elementEvents.length(); i++) {
BindFunction listener = elementEvents.get(i);
@@ -788,10 +789,11 @@ public void unbind(String events, Function f) {
}
}
- private void clean() {
+ public void clean() {
cleanGQListeners(element);
elementEvents = JsObjectArray.createArray().cast();
liveBindFunctionByEventType = JsMap.create();
+ eventBits = 0;
}
private void sink(int eventbits, String eventName) {