diff options
author | jdramaix <julien.dramaix@gmail.com> | 2014-12-17 11:17:28 +0100 |
---|---|---|
committer | jdramaix <julien.dramaix@gmail.com> | 2014-12-17 11:17:28 +0100 |
commit | 69ab01e7d6bba1d8bfd0783b33ad231650ecf5d8 (patch) | |
tree | 5538a7700861c378a4339d4596b8dcca2b124630 /gwtquery-core | |
parent | ac85d28be7ca341fa513bccd54960dcc1204327d (diff) | |
download | gwtquery-69ab01e7d6bba1d8bfd0783b33ad231650ecf5d8.tar.gz gwtquery-69ab01e7d6bba1d8bfd0783b33ad231650ecf5d8.zip |
improve off and on methodsjd_off
Diffstat (limited to 'gwtquery-core')
3 files changed, 157 insertions, 20 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 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<GQuery, LazyGQuery> { * 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<GQuery, LazyGQuery> { * 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<GQuery, LazyGQuery> { * Remove all event handlers. */ public GQuery off() { - return as(Effects).off(); + return as(Events).off(); } /** @@ -3271,6 +3279,9 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { * 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("<div class='mainDiv'><div class='subDiv'>Content</div></div>"); + + $(".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("<div class='mainDiv'><div class='subDiv'>Content</div></div>"); + + $(".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("<div class='mainDiv'><div class='subDiv'>Content " + + "0<span>blop</span></div></div><div class='mainDiv'><div class='subDiv'>" + + "Content 0<span>blop</span></div></div>"); + + $(".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 = "<div class='subDiv'>Content " + i + "<span>blop</span></div>"; + $(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; } } - |