From: jdramaix Date: Wed, 17 Dec 2014 10:17:28 +0000 (+0100) Subject: improve off and on methods X-Git-Tag: gwtquery-project-1.4.3~12^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=69ab01e7d6bba1d8bfd0783b33ad231650ecf5d8;p=gwtquery.git improve off and on 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 09e69fe3..7f5bbf82 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 @@ -3236,6 +3236,10 @@ public class GQuery implements Lazy { * Attach an event handler function for one or more events to the selected elements. */ public GQuery on(String eventName, String selector, Function... funcs) { + if (selector == null || selector.isEmpty()) { + return on(eventName, funcs); + } + return delegate(selector, eventName, funcs); } @@ -3243,6 +3247,10 @@ public class GQuery implements Lazy { * 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) { + if (selector == null || selector.isEmpty()) { + return on(eventName, data, funcs); + } + return delegate(selector, eventName, data, funcs); } @@ -3250,7 +3258,7 @@ public class GQuery implements Lazy { * Remove all event handlers. */ public GQuery off() { - return as(Effects).off(); + return as(Events).off(); } /** @@ -3271,6 +3279,9 @@ public class GQuery implements Lazy { * Remove an event handler */ public GQuery off(String eventName, String selector) { + if (selector == null || selector.isEmpty()) { + return off(eventName); + } return undelegate(selector, eventName); } 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 31ff1695..98700235 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 @@ -13,24 +13,6 @@ */ package com.google.gwt.query.client; -import com.google.gwt.dom.client.Element; -import com.google.gwt.dom.client.NodeList; -import com.google.gwt.event.dom.client.ClickEvent; -import com.google.gwt.event.dom.client.ClickHandler; -import com.google.gwt.junit.DoNotRunWith; -import com.google.gwt.junit.Platform; -import com.google.gwt.junit.client.GWTTestCase; -import com.google.gwt.query.client.css.CSS; -import com.google.gwt.query.client.css.Length; -import com.google.gwt.query.client.css.RGBColor; -import com.google.gwt.query.client.plugins.Events; -import com.google.gwt.query.client.plugins.events.EventsListener; -import com.google.gwt.user.client.Event; -import com.google.gwt.user.client.Timer; -import com.google.gwt.user.client.ui.Button; -import com.google.gwt.user.client.ui.HTML; -import com.google.gwt.user.client.ui.RootPanel; - import static com.google.gwt.query.client.GQuery.$; import static com.google.gwt.query.client.GQuery.document; import static com.google.gwt.query.client.GQuery.lazy; @@ -48,6 +30,23 @@ import static com.google.gwt.user.client.Event.ONMOUSEOUT; import static com.google.gwt.user.client.Event.ONMOUSEOVER; import static com.google.gwt.user.client.Event.ONMOUSEUP; +import com.google.gwt.dom.client.Element; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.junit.DoNotRunWith; +import com.google.gwt.junit.Platform; +import com.google.gwt.junit.client.GWTTestCase; +import com.google.gwt.query.client.css.CSS; +import com.google.gwt.query.client.css.Length; +import com.google.gwt.query.client.css.RGBColor; +import com.google.gwt.query.client.plugins.Events; +import com.google.gwt.query.client.plugins.events.EventsListener; +import com.google.gwt.user.client.Event; +import com.google.gwt.user.client.Timer; +import com.google.gwt.user.client.ui.Button; +import com.google.gwt.user.client.ui.HTML; +import com.google.gwt.user.client.ui.RootPanel; + /** * Test class for testing gwt events plugin api. */ @@ -1186,6 +1185,134 @@ public class GQueryEventsTestGwt extends GWTTestCase { } } + public void testOnOff() { + $(e).html("
Content
"); + + $(".mainDiv", e).on("click", new Function() { + @Override + public void f(Element e) { + $(e).css("color", "red"); + } + }); + + $(".mainDiv", e).click(); + + assertEquals("red", $(".mainDiv", e).css("color", false)); + + // reset + $(".mainDiv", e).css("color", "black"); + + $(".mainDiv", e).off("click"); + + $(".mainDiv", e).click(); + + assertEquals("black", $(".mainDiv", e).css("color", false)); + + // try with other signatures by passing null to extra parameters + $(".mainDiv", e).on("click", (String) null, new Function() { + @Override + public void f(Element e) { + $(e).css("color", "red"); + } + }); + + $(".mainDiv", e).click(); + + assertEquals("red", $(".mainDiv", e).css("color", false)); + + // reset + $(".mainDiv", e).css("color", "black"); + + $(".mainDiv", e).off("click"); + + // try with other signatures by passing null to extra parameters + $(".mainDiv", e).on("click", null, new Object(), new Function() { + @Override + public void f(Element e) { + $(e).css("color", "red"); + } + }); + + $(".mainDiv", e).click(); + + assertEquals("red", $(".mainDiv", e).css("color", false)); + } + + + public void testOff() { + $(e).html("
Content
"); + + $(".mainDiv", e).on("click", new Function() { + @Override + public void f(Element e) { + $(e).css("color", "red"); + } + }); + + $(".mainDiv", e).on("mouseover", new Function() { + @Override + public void f(Element e) { + $(e).css("background-color", "yellow"); + } + }); + + $(".mainDiv", e).click().trigger(Event.ONMOUSEOVER); + + assertEquals("red", $(".mainDiv", e).css("color", false)); + assertEquals("yellow", $(".mainDiv", e).css("background-color", false)); + + // reset + $(".mainDiv", e).css("color", "black"); + $(".mainDiv", e).css("background-color", "white"); + + $(".mainDiv", e).off(); + + $(".mainDiv", e).click().trigger(Event.ONMOUSEOVER); + + assertEquals("black", $(".mainDiv", e).css("color", false)); + assertEquals("white", $(".mainDiv", e).css("background-color", false)); + } + + + public void testOnOffWithSelector() { + $(e).html("
Content " + + "0blop
" + + "Content 0blop
"); + + $(".mainDiv", e).on("click", ".subDiv", new Function() { + @Override + public void f(Element e) { + $(e).css("color", "red"); + } + }); + + + 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(); + + for (Element el : $(".subDiv", e).elements()) { + assertEquals("red", $(el).css("color", false)); + // reset + $(el).css("color", "black"); + } + + $(".mainDiv", e).off("click", ".subDiv"); + + $("span", e).click(); + + for (Element el : $(".subDiv", e).elements()) { + assertEquals("black", $(el).css(CSS.COLOR, false)); + } + } + public void testUnDelegateAll() { $(e).html( diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTestJre.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTestJre.java index 64d7ce6e..8618f10d 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTestJre.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTestJre.java @@ -107,4 +107,3 @@ public class AjaxTestJre extends AjaxTests { return server; } } -