Skip to content

Commit

Permalink
Implementation of on/off methods, deprecate live, die, delegate, unde…
Browse files Browse the repository at this point in the history
…legate
  • Loading branch information
manolo committed Dec 12, 2013
1 parent c58402c commit 31f2945
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1827,6 +1828,7 @@ public GQuery delay(int milliseconds, String queueName, Function... f) {
* <pre>
* $("div.main").delegate(".subMain", Event.ONCLICK | Event.ONDBLCLICK, new Function(){...});
* </pre>
* @deprecated use {@link #on(String, String, Function...)}
*/
public GQuery delegate(String selector, int eventbits, Function... handlers) {
return delegate(selector, eventbits, null, handlers);
Expand Down Expand Up @@ -1867,6 +1869,7 @@ public GQuery delegate(String selector, int eventbits, Function... handlers) {
* <pre>
* $("div.main").delegate(".subMain", Event.ONCLICK | Event.ONDBLCLICK, new Function(){...});
* </pre>
* @deprecated use {@link #on(String, String, Object, Function...)}
*/
public GQuery delegate(String selector, int eventbits, Object data, Function... handlers) {

Expand Down Expand Up @@ -1914,6 +1917,7 @@ public GQuery delegate(String selector, int eventbits, Object data, Function...
* </pre>
*
* </pre>
* @deprecated use {@link #on(String, String, Function...)}
*/
public GQuery delegate(String selector, String eventType, Function... handlers) {
return delegate(selector, eventType, null, handlers);
Expand Down Expand Up @@ -1954,6 +1958,7 @@ public GQuery delegate(String selector, String eventType, Function... handlers)
* </pre>
*
* </pre>
* @deprecated use {@link #on(String, String, Object, Function...)}
*/
public GQuery delegate(String selector, String eventType, Object data, Function... handlers) {
for (Element e : elements) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -2767,6 +2776,7 @@ public GQuery live(int eventbits, Object data, Function... funcs) {
* descendant of myElement.</li>
* </ul>
* </p>
* @deprecated use {@link #on(String, Function...)}
*/
public GQuery live(String eventName, Function... funcs) {
return as(Events).live(eventName, null, funcs);
Expand Down Expand Up @@ -2821,6 +2831,7 @@ public GQuery live(String eventName, Function... funcs) {
* descendant of myElement.</li>
* </ul>
* </p>
* @deprecated use {@link #on(String, Object, Function...)}
*/
public GQuery live(String eventName, Object data, Function... funcs) {
return as(Events).live(eventName, data, funcs);
Expand Down Expand Up @@ -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();

This comment has been minimized.

Copy link
@ibaca

ibaca Dec 14, 2014

I think that Effects is a typo, the correct line will be 'as(Events).off()'. Current line is causing an infinite loop.

This comment has been minimized.

Copy link
@jDramaix

jDramaix Dec 17, 2014

Contributor

Indeed it should be Events

This comment has been minimized.

Copy link
@jDramaix

jDramaix Dec 17, 2014

Contributor

I have a pending patch for that

}

/**
* 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.
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ public void unbind(int eventbits) {

public void unbind(int eventbits, String namespace, String eventName, String originalEventType,
Function f) {

JsObjectArray<BindFunction> newList = JsObjectArray.createArray().cast();
for (int i = 0; i < elementEvents.length(); i++) {
BindFunction listener = elementEvents.get(i);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 31f2945

Please sign in to comment.