From: Julien Dramaix Date: Sun, 10 Apr 2011 07:10:31 +0000 (+0000) Subject: - correct live implementation to accept an array of Function X-Git-Tag: release-1.3.2~421 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4e04e209edc63690d942d79bada0725eea2d37b2;p=gwtquery.git - correct live implementation to accept an array of Function - method live(String, Object, Function) loop on itself --- 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 78796336..f16abc8d 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 @@ -2105,8 +2105,8 @@ public class GQuery implements Lazy { * *

*/ - public GQuery live(String eventName, Object data, Function func) { - return as(Events).live(eventName, data, func); + public GQuery live(String eventName, Object data, Function... funcs) { + return as(Events).live(eventName, data, funcs); } /** 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 edf79fbb..820013ea 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 @@ -351,6 +351,19 @@ public interface LazyGQuery extends LazyBase{ */ LazyGQuery bind(int eventbits, Object data, Function... funcs); + /** + * Binds a set of handlers to a particular Event for each matched element. + * + * The event handlers are passed as Functions that you can use to prevent + * default behavior. To stop both default action and event bubbling, the + * function event handler has to return false. + * + * You can pass an additional Object data to your Function as the second + * parameter + * + */ + LazyGQuery bind(String eventType, Object data, Function... funcs); + /** * Bind a set of functions to the blur event of each matched element. Or * trigger the event if no functions are provided. @@ -1124,7 +1137,7 @@ public interface LazyGQuery extends LazyBase{ * *

*/ - LazyGQuery live(String eventName, Object data, Function func); + LazyGQuery live(String eventName, Object data, Function... funcs); /** * Bind a function to the load event of each matched element. 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 501d2cb5..07dc9c8f 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 @@ -26,6 +26,7 @@ import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.EventListener; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -90,19 +91,25 @@ public class EventsListener implements EventListener { private static class LiveBindFunction extends BindFunction { // TODO can be a list of BindFunction - Map bindFunctionBySelector; + Map> bindFunctionBySelector; LiveBindFunction(int type, String namespace) { super(type, namespace, null, null, -1); - bindFunctionBySelector = new HashMap(); + bindFunctionBySelector = new HashMap>(); } /** * Add a {@link BindFunction} for a specific css selector */ public void addBindFunctionForSelector(String cssSelector, BindFunction f) { - bindFunctionBySelector.put(cssSelector, f); + List bindFunctions = bindFunctionBySelector.get(cssSelector); + if (bindFunctions == null) { + bindFunctions = new ArrayList(); + bindFunctionBySelector.put(cssSelector, bindFunctions); + } + + bindFunctions.add(f); } @Override @@ -137,14 +144,21 @@ public class EventsListener implements EventListener { com.google.gwt.query.client.plugins.events.Event gqEvent = com.google.gwt.query.client.plugins.events.Event.create(event); for (String cssSelector : realCurrentTargetBySelector.keySet()) { - BindFunction f = bindFunctionBySelector.get(cssSelector); - for (Element element : realCurrentTargetBySelector.get(cssSelector)) { - gqEvent.setCurrentElementTarget(element); - boolean subResult = f.fire(gqEvent); - result &= subResult; - if (!subResult) { - // Event should not continue to be bubbled, break the second for - break; + List bindFunctions = bindFunctionBySelector.get(cssSelector); + + if (bindFunctions == null){ + continue; + } + + for (BindFunction f : bindFunctions) { + for (Element element : realCurrentTargetBySelector.get(cssSelector)) { + gqEvent.setCurrentElementTarget(element); + boolean subResult = f.fire(gqEvent); + result &= subResult; + if (!subResult) { + // Event should not continue to be bubbled, break the second for + break; + } } } } @@ -159,8 +173,8 @@ public class EventsListener implements EventListener { /** * Remove the BindFunction associated to this cssSelector */ - public BindFunction removeBindFunctionForSelector(String cssSelector) { - return bindFunctionBySelector.remove(cssSelector); + public void removeBindFunctionForSelector(String cssSelector) { + bindFunctionBySelector.remove(cssSelector); } /** @@ -328,7 +342,7 @@ public class EventsListener implements EventListener { bind(b, nameSpace, data, function, -1); } } - + public void die(String eventName, String cssSelector) { int eventType = "submit".equals(eventName) ? eventType = ONSUBMIT : Event.getTypeInt(eventName); @@ -370,13 +384,15 @@ public class EventsListener implements EventListener { return getGwtEventListener(element); } - public void live(String eventName, String cssSelector, Object data, Function... f) { + public void live(String eventName, String cssSelector, Object data, + Function... f) { int eventType = "submit".equals(eventName) ? eventType = ONSUBMIT : Event.getTypeInt(eventName); live(eventType, cssSelector, data, f); } - - public void live(int eventbits, String cssSelector, Object data, Function... funcs) { + + public void live(int eventbits, String cssSelector, Object data, + Function... funcs) { // is a LiveBindFunction already attached for this kind of event LiveBindFunction liveBindFunction = liveBindFunctionByEventType.get(eventbits); @@ -388,9 +404,9 @@ public class EventsListener implements EventListener { liveBindFunctionByEventType.put(eventbits, liveBindFunction); } - for (Function f: funcs) { - liveBindFunction.addBindFunctionForSelector(cssSelector, new BindFunction( - eventbits, "live", f, data)); + for (Function f : funcs) { + liveBindFunction.addBindFunctionForSelector(cssSelector, + new BindFunction(eventbits, "live", f, data)); } } @@ -398,8 +414,7 @@ public class EventsListener implements EventListener { public void onBrowserEvent(Event event) { double now = Duration.currentTimeMillis(); // Workaround for Issue_20 - if (lastType == event.getTypeInt() - && now - lastEvnt < 10 + if (lastType == event.getTypeInt() && now - lastEvnt < 10 && "body".equalsIgnoreCase(element.getTagName())) { return; } 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 99515ec3..af3f2465 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 @@ -363,6 +363,54 @@ public class GQueryEventsTest extends GWTTestCase { } + +public void testLiveWithMultipleFunction() { + + $(e).html("
Content 1 blop
"); + + $(".clickable", e).live("click", new Function(){ + public void f(Element e) { + $(e).css(CSS.COLOR.with(RGBColor.RED)); + } + }, new Function(){ + public void f(Element e) { + $(e).css(CSS.BACKGROUND_COLOR.with(RGBColor.YELLOW)); + } + }); + + $("#div1", e).addClass("clickable"); + + $("#span1", e).click(); + + assertEquals("red", $("#div1", e).css(CSS.COLOR)); + assertNotSame("yellow", $("#div1", e).css(CSS.BACKGROUND_COLOR)); + + + } + +public void testLiveWithMultipleEvent() { + + $(e).html("
Content 1 blop
"); + + $(".clickable", e).live(Event.ONCLICK | Event.ONMOUSEMOVE, new Function(){ + public void f(Element e) { + $(e).css(CSS.COLOR.with(RGBColor.RED)); + } + }); + + $("#div1", e).addClass("clickable"); + + $("#span1", e).click(); + + assertEquals("red", $("#div1", e).css(CSS.COLOR)); + + //reset + $("#div1", e).css(CSS.COLOR.with(RGBColor.BLACK)); + + + +} + public void testNamedBinding() { $(e).html("

Content

"); @@ -475,30 +523,7 @@ public class GQueryEventsTest extends GWTTestCase { assertEquals($("#test").attr("tabIndex"), "2"); } - public void testUnbindMultipleEvents() { - String content = "

content

"; - $(e).html(content); - $(document).bind(Event.ONMOUSEMOVE, null, new Function() { - public void f(Element e){ - $("p").css(CSS.COLOR.with(RGBColor.RED)); - } - }); - $(document).bind(Event.ONMOUSEUP, null, new Function(){ - public void f(Element e){ - $("p").css(CSS.COLOR.with(RGBColor.YELLOW)); - } - }); - $(document).trigger(Event.ONMOUSEMOVE); - assertEquals("red", $("p").css("color")); - $(document).trigger(Event.ONMOUSEUP); - assertEquals("yellow", $("p").css("color")); - $("p").css(CSS.COLOR.with(RGBColor.BLACK)); - $(document).unbind(Event.ONMOUSEUP|Event.ONMOUSEMOVE); - $(document).trigger(Event.ONMOUSEMOVE); - assertEquals("black", $("p").css("color")); - $(document).trigger(Event.ONMOUSEUP); - assertEquals("black", $("p").css("color")); - } + public void testWidgetEvents() { final Button b = new Button("click-me");