From 557d0059c4bb2f03a53cc5eec5746be6b30b63c3 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Wed, 14 Mar 2012 13:04:35 +0000 Subject: [PATCH] Allow unbinding of certain functions like jquery does --- .../com/google/gwt/query/client/GQuery.java | 17 +++++++++- .../gwt/query/client/plugins/Events.java | 22 ++++++++++--- .../gwt/query/client/plugins/MousePlugin.java | 4 +-- .../gwt/query/client/plugins/ajax/Ajax.java | 32 +++++++++++-------- .../client/plugins/events/EventsListener.java | 19 +++++++---- .../gwt/query/client/GQueryEventsTestGwt.java | 21 +++++++++++- 6 files changed, 87 insertions(+), 28 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 03b50d65..9680fdd9 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 @@ -4092,11 +4092,26 @@ public class GQuery implements Lazy { return as(Events).unbind(eventbits); } + /** + * Removes the function passed from the set of events which match + * the eventbits. + */ + public GQuery unbind(int eventbits, Function f) { + return as(Events).unbind(eventbits, null, f); + } + /** * Removes all events that match the eventList. */ public GQuery unbind(String eventList) { - return as(Events).unbind(eventList); + return unbind(eventList, null); + } + + /** + * Removes all events that match the eventList. + */ + public GQuery unbind(String eventList, Function f) { + return as(Events).unbind(eventList, f); } /** 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 1294feb3..dd677ef3 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 @@ -273,25 +273,37 @@ public class Events extends GQuery { * * Example: unbind(Event.ONCLICK | Event.ONMOUSEOVER, "my.namespace") */ - public Events unbind(int eventbits, String name) { + public Events unbind(int eventbits, String name, Function f) { for (Element e : elements()) { if (isEventCapable(e)){ - EventsListener.getInstance(e).unbind(eventbits, name); + EventsListener.getInstance(e).unbind(eventbits, name, f); } } return this; } /** - * Removes all handlers, that matches event name passed. This name could - * contain a namespace. + * Removes all handlers, that matches the event name passed. + * + * This name could contain a namespace. * * Example: unbind("click.my.namespace") */ public Events unbind(String name) { + return unbind(name, null); + } + + /** + * Removes the function passed as parameter from the event list matching + * the event name passed. + * This name could contain a namespace. + * + * Example: unbind("click.my.namespace", myFunction) + */ + public Events unbind(String name, Function f) { for (Element e : elements()) { if (isEventCapable(e)){ - EventsListener.getInstance(e).unbind(name); + EventsListener.getInstance(e).unbind(name, f); } } return this; diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/MousePlugin.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/MousePlugin.java index 14f6aa9c..f5573fba 100755 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/MousePlugin.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/MousePlugin.java @@ -41,7 +41,7 @@ public abstract class MousePlugin extends UiPlugin { protected void destroyMouseHandler() { as(Events) - .unbind(Event.ONMOUSEDOWN | Event.ONCLICK, getPluginName()); + .unbind(Event.ONMOUSEDOWN | Event.ONCLICK, getPluginName(), null); } /** @@ -282,7 +282,7 @@ public abstract class MousePlugin extends UiPlugin { private void unbindOtherMouseEvent() { $(document).as(Events).unbind((Event.ONMOUSEUP | Event.ONMOUSEMOVE), - getPluginName()); + getPluginName(), null); } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java index 5386951a..fe6c41f4 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java @@ -342,19 +342,25 @@ public class Ajax extends GQuery { s.setData(data); s.setSuccess(new Function() { public void f() { - // We clean up the returned string to smoothly append it to our document - String s = getData()[0].toString().replaceAll("]+>\\s*", "") - .replaceAll("(?si)]*>\\s*", "") - .replaceFirst("(?si)]*>.*\\s*", "") - .replaceFirst("(?si)]*>.*\\s*", "") - .replaceAll("]*>\\s*", ""); - // We wrap the results in a div - s = "
" + s + "
"; - - Ajax.this.empty().append(filter.isEmpty() ? $(s) : $(s).find(filter)); - if (onSuccess != null) { - onSuccess.setElement(Ajax.this.get(0)); - onSuccess.f(); + try { + // We clean up the returned string to smoothly append it to our document + String s = getData()[0].toString().replaceAll("]+>\\s*", "") + .replaceAll("(?si)]*>\\s*", "") + .replaceFirst("(?si)]*>.*\\s*", "") + .replaceFirst("(?si)]*>.*\\s*", "") + .replaceAll("]*>\\s*", ""); + // We wrap the results in a div + s = "
" + s + "
"; + + Ajax.this.empty().append(filter.isEmpty() ? $(s) : $(s).find(filter)); + if (onSuccess != null) { + onSuccess.setElement(Ajax.this.get(0)); + onSuccess.f(); + } + } catch (Exception e) { + if (GWT.getUncaughtExceptionHandler() != null) { + GWT.getUncaughtExceptionHandler().onUncaughtException(e); + } } } }); 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 7699c476..8cbad2cc 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 @@ -84,6 +84,10 @@ public class EventsListener implements EventListener { public String toString() { return "bind function for event type " + type; } + + public boolean isEquals(Function f) { + return function.equals(f); + } } /** @@ -329,7 +333,7 @@ public class EventsListener implements EventListener { public void bind(int eventbits, String namespace, final Object data, final Function function, int times) { if (function == null) { - unbind(eventbits, namespace); + unbind(eventbits, namespace, null); return; } eventBits |= eventbits; @@ -435,30 +439,32 @@ public class EventsListener implements EventListener { } public void unbind(int eventbits) { - unbind(eventbits, null); + unbind(eventbits, null, null); } - public void unbind(int eventbits, String namespace) { + public void unbind(int eventbits, String namespace, Function f) { JsObjectArray newList = JsObjectArray.createArray().cast(); for (int i = 0; i < elementEvents.length(); i++) { BindFunction listener = elementEvents.get(i); boolean matchNS = namespace == null || namespace.isEmpty() || listener.nameSpace.equals(namespace); boolean matchEV = eventbits <= 0 || listener.hasEventType(eventbits); - if (matchNS && matchEV) { + boolean matchFC = f == null || listener.isEquals(f); + if (matchNS && matchEV && matchFC) { continue; } newList.add(listener); } elementEvents = newList; + } - public void unbind(String event) { + public void unbind(String event, Function f) { // TODO: nameSpaces in event lists String nameSpace = event.replaceFirst("^[^\\.]+\\.*(.*)$", "$1"); String eventName = event.replaceFirst("^([^\\.]+).*$", "$1"); int b = getEventBits(eventName); - unbind(b, nameSpace); + unbind(b, nameSpace, f); } private void clean() { @@ -515,4 +521,5 @@ public class EventsListener implements EventListener { function.clean(); } } + } 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 23eb49cd..da077622 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 @@ -215,6 +215,26 @@ public class GQueryEventsTestGwt extends GWTTestCase { $("p", e).unbind(Event.ONCLICK); $("p", e).click(); assertEquals("white", $("p", e).css("color", false)); + + Function f1 = new Function() { + public void f(){ + $(this).css(CSS.COLOR.with(RGBColor.RED)); + } + }; + Function f2 = new Function() { + public void f(){ + $(this).css(CSS.COLOR.with(RGBColor.GREEN)); + } + }; + $("p", e).click(f1, f2); + $("p", e).css(CSS.COLOR.with(RGBColor.WHITE)); + $("p", e, Events.Events).trigger(Event.ONCLICK); + assertEquals("green", $("p", e).css("color", false)); + $("p", e).unbind(Event.ONCLICK, f2); + $("p", e, Events.Events).trigger(Event.ONCLICK); + assertEquals("red", $("p", e).css("color", false)); + + // toggle $("p", e).unbind(Event.ONCLICK); @@ -266,7 +286,6 @@ public class GQueryEventsTestGwt extends GWTTestCase { public boolean f(Event evnt) { GQuery gq = $(evnt); int c = evnt.getCharCode() > 0 ? evnt.getCharCode() : evnt.getKeyCode(); - System.out.println(evnt.getCharCode() + " " + evnt.getKeyCode()); gq.val(gq.val() + Character.toString((char)c)); return false; } -- 2.39.5