From: Julien Dramaix Date: Mon, 11 Apr 2011 20:34:40 +0000 (+0000) Subject: implement undelegate methods X-Git-Tag: release-1.3.2~412 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a7f735f9cae67a2255365d89d5e0bad6e1a12027;p=gwtquery.git implement undelegate methods --- 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 506cc262..4698bcae 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 @@ -3355,6 +3355,50 @@ public class GQuery implements Lazy { public GQuery unbind(int eventbits) { return as(Events).unbind(eventbits); } + + /** + * Remove all event delegation that have been bound using + * {@link #delegate(String, int, Function...)} {@link #live(int, Function...)} methods + */ + public GQuery undelegate() { + return as(Events).undelegate(); + } + + /** + * Undelegate is a way of removing event handlers that have been bound using + * {@link #delegate(String, int, Function...)} method + */ + public GQuery undelegate(String selector) { + for (Element e : elements()){ + $(selector, e).die(); + } + + return this; + } + + /** + * Undelegate is a way of removing event handlers that have been bound using + * {@link #delegate(String, int, Function...)} method + */ + public GQuery undelegate(String selector, String eventName) { + for (Element e : elements()){ + $(selector, e).die(eventName); + } + + return this; + } + + /** + * Undelegate is a way of removing event handlers that have been bound using + * {@link #delegate(String, int, Function...)} method + */ + public GQuery undelegate(String selector, int eventBit) { + for (Element e : elements()){ + $(selector, e).die(eventBit); + } + + return this; + } /** * Remove all duplicate elements from an array of elements. Note that this 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 680e2c35..6fe42c1a 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 @@ -1880,6 +1880,30 @@ public interface LazyGQuery extends LazyBase{ */ LazyGQuery unbind(int eventbits); + /** + * Remove all event delegation that have been bound using + * {@link #delegate(String, int, Function...)} {@link #live(int, Function...)} methods + */ + LazyGQuery undelegate(); + + /** + * Undelegate is a way of removing event handlers that have been bound using + * {@link #delegate(String, int, Function...)} method + */ + LazyGQuery undelegate(String selector); + + /** + * Undelegate is a way of removing event handlers that have been bound using + * {@link #delegate(String, int, Function...)} method + */ + LazyGQuery undelegate(String selector, String eventName); + + /** + * Undelegate is a way of removing event handlers that have been bound using + * {@link #delegate(String, int, Function...)} method + */ + LazyGQuery undelegate(String selector, int eventBit); + /** * Remove all duplicate elements from an array of elements. Note that this * only works on arrays of DOM elements, not strings or numbers. 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 bdd3802e..5bebe339 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,5 +273,12 @@ public class Events extends GQuery { e.dispatchEvent(evt); } } + + public Events undelegate(){ + for (Element e : elements()) { + EventsListener.getInstance(e).cleanEventDelegation(); + } + return this; + } } 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 5ce73011..0ef62afe 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 @@ -140,4 +140,6 @@ public interface LazyEvents extends LazyBase{ */ LazyEvents unbind(String name); + LazyEvents undelegate(); + } 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 efd53405..d673025a 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 @@ -110,6 +110,10 @@ public class EventsListener implements EventListener { bindFunctions.add(f); } + + public void clean(){ + bindFunctionBySelector = new HashMap>(); + } @Override public boolean fire(Event event) { @@ -493,4 +497,10 @@ public class EventsListener implements EventListener { private int getTypeInt(String eventName) { return "submit".equals(eventName) ? ONSUBMIT : Event.getTypeInt(eventName); } + + public void cleanEventDelegation() { + for (LiveBindFunction function : liveBindFunctionByEventType.values()){ + function.clean(); + } + } } diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTest.java index 075788d7..4acc0146 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTest.java @@ -443,6 +443,158 @@ public class GQueryEventsTest extends GWTTestCase { } + public void testUnDelegate(){ + + $(e).html("
Content 0blop
Content 0blop
"); + + $(".mainDiv",e).delegate(".subDiv", "click", new Function(){ + @Override + public void f(Element e) { + $(e).css(CSS.COLOR.with(RGBColor.RED)); + } + }); + + $(".mainDiv",e).delegate(".subDiv", Event.ONMOUSEOVER, new Function(){ + @Override + public void f(Element e) { + $(e).css(CSS.BACKGROUND_COLOR.with(RGBColor.YELLOW)); + } + }); + + for (Element mainDiv : $(".mainDiv",e).elements()){ + for (int i = 0; i < 3 ; i++){ + String html = "
Content "+i+"blop
"; + $(mainDiv).append(html); + } + } + + assertEquals(8, $(".subDiv",e).length()); + + $("span",e).click().trigger(Event.ONMOUSEOVER); + + for (Element el : $(".subDiv",e).elements()){ + assertEquals("red", $(el).css(CSS.COLOR)); + assertEquals("yellow", $(el).css(CSS.BACKGROUND_COLOR)); + //reset + $(el).css(CSS.COLOR.with(RGBColor.BLACK), CSS.BACKGROUND_COLOR.with(RGBColor.WHITE)); + } + + $(".mainDiv", e).undelegate(".subDiv",Event.ONCLICK); + + $("span",e).click().trigger(Event.ONMOUSEOVER); + + for (Element el : $(".subDiv",e).elements()){ + assertEquals("black", $(el).css(CSS.COLOR)); + assertEquals("yellow", $(el).css(CSS.BACKGROUND_COLOR)); + //reset + $(el).css(CSS.COLOR.with(RGBColor.BLACK), CSS.BACKGROUND_COLOR.with(RGBColor.WHITE)); + } + + $(".mainDiv", e).undelegate(".subDiv","mouseover"); + + $("span",e).click().trigger(Event.ONMOUSEOVER); + + for (Element el : $(".subDiv",e).elements()){ + assertEquals("black", $(el).css(CSS.COLOR)); + assertEquals("white", $(el).css(CSS.BACKGROUND_COLOR)); + } + } + + public void testUnDelegateAll(){ + + $(e).html("
Content 0blop
Content 0blop
"); + + $(".mainDiv",e).delegate(".subDiv", "click", new Function(){ + @Override + public void f(Element e) { + $(e).css(CSS.COLOR.with(RGBColor.RED)); + } + }); + + $(".mainDiv",e).delegate(".subDiv", Event.ONMOUSEOVER, new Function(){ + @Override + public void f(Element e) { + $(e).css(CSS.BACKGROUND_COLOR.with(RGBColor.YELLOW)); + } + }); + + for (Element mainDiv : $(".mainDiv",e).elements()){ + for (int i = 0; i < 3 ; i++){ + String html = "
Content "+i+"blop
"; + $(mainDiv).append(html); + } + } + + assertEquals(8, $(".subDiv",e).length()); + + $("span",e).click().trigger(Event.ONMOUSEOVER); + + for (Element el : $(".subDiv",e).elements()){ + assertEquals("red", $(el).css(CSS.COLOR)); + assertEquals("yellow", $(el).css(CSS.BACKGROUND_COLOR)); + //reset + $(el).css(CSS.COLOR.with(RGBColor.BLACK), CSS.BACKGROUND_COLOR.with(RGBColor.WHITE)); + } + + $(".mainDiv", e).undelegate(".subDiv"); + + $("span",e).click().trigger(Event.ONMOUSEOVER); + + for (Element el : $(".subDiv",e).elements()){ + assertEquals("black", $(el).css(CSS.COLOR)); + assertEquals("white", $(el).css(CSS.BACKGROUND_COLOR)); + } + } + +public void testUnDelegateAll2(){ + + $(e).html("
Content 0blop
Content 0blop
"); + + $(".mainDiv",e).delegate(".subDiv", "click", new Function(){ + @Override + public void f(Element e) { + $(e).css(CSS.COLOR.with(RGBColor.RED)); + } + }); + + $(".mainDiv",e).delegate(".subDiv", Event.ONMOUSEOVER, new Function(){ + @Override + public void f(Element e) { + $(e).css(CSS.BACKGROUND_COLOR.with(RGBColor.YELLOW)); + } + }); + + for (Element mainDiv : $(".mainDiv",e).elements()){ + for (int i = 0; i < 3 ; i++){ + String html = "
Content "+i+"blop
"; + $(mainDiv).append(html); + } + } + + assertEquals(8, $(".subDiv",e).length()); + + $("span",e).click().trigger(Event.ONMOUSEOVER); + + for (Element el : $(".subDiv",e).elements()){ + assertEquals("red", $(el).css(CSS.COLOR)); + assertEquals("yellow", $(el).css(CSS.BACKGROUND_COLOR)); + //reset + $(el).css(CSS.COLOR.with(RGBColor.BLACK), CSS.BACKGROUND_COLOR.with(RGBColor.WHITE)); + } + + $(".mainDiv", e).undelegate(); + + $("span",e).click().trigger(Event.ONMOUSEOVER); + + for (Element el : $(".subDiv",e).elements()){ + assertEquals("black", $(el).css(CSS.COLOR)); + assertEquals("white", $(el).css(CSS.BACKGROUND_COLOR)); + } + } + + + + public void testLiveWithMultipleEvent() { $(e).html("
Content 1 blop
");