From c7b36710c45a144db235b805a0738aea07ee2c2b Mon Sep 17 00:00:00 2001 From: Julien Dramaix Date: Fri, 18 May 2012 15:46:40 +0000 Subject: [PATCH] best handling of binding/unbinding string events with or whithout namespace --- .../client/plugins/events/EventsListener.java | 54 +++--- .../gwt/query/client/GQueryEventsTestGwt.java | 159 +++++++++++++++--- 2 files changed, 172 insertions(+), 41 deletions(-) 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 9f469618..52567199 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 @@ -29,6 +29,8 @@ import com.google.gwt.query.client.js.JsCache; import com.google.gwt.query.client.js.JsMap; import com.google.gwt.query.client.js.JsNamedArray; import com.google.gwt.query.client.js.JsObjectArray; +import com.google.gwt.regexp.shared.RegExp; +import com.google.gwt.regexp.shared.SplitResult; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.EventListener; @@ -252,6 +254,8 @@ public class EventsListener implements EventListener { public static int ONSUBMIT = GqEvent.ONSUBMIT; public static int ONRESIZE = GqEvent.ONRESIZE; + + private static RegExp eventStringPattern = RegExp.compile("^([^\\.]*)\\.?(.*$)"); public static void clean(Element e) { EventsListener ret = getGQueryEventListener(e); @@ -357,13 +361,18 @@ public class EventsListener implements EventListener { times)); } - public void bind(String event, final Object data, Function... funcs) { - // TODO: nameSpaces in event lists - String nameSpace = event.replaceFirst("^[^\\.]+\\.*(.*)$", "$1"); - String eventName = event.replaceFirst("^([^\\.]+).*$", "$1"); - int b = getEventBits(eventName); - for (Function function : funcs) { - bind(b, nameSpace, data, function, -1); + public void bind(String events, final Object data, Function... funcs) { + String[] parts = events.split("[\\s,]+"); + + for (String event: parts){ + SplitResult subParts = eventStringPattern.split(event); + + String nameSpace = subParts.get(2); + String eventName = subParts.get(1); + int b = getTypeInt(eventName); + for (Function function : funcs) { + bind(b, nameSpace, data, function, -1); + } } } @@ -482,12 +491,20 @@ public class EventsListener implements EventListener { } - 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, f); + public void unbind(String events, Function f) { + + String[] parts = events.split("[\\s,]+"); + + for (String event: parts){ + SplitResult subParts = eventStringPattern.split(event); + + String nameSpace = subParts.get(2); + String eventName = subParts.get(1); + + int b = getTypeInt(eventName); + + unbind(b, nameSpace, f); + } } private void clean() { @@ -519,18 +536,13 @@ public class EventsListener implements EventListener { for (String e: events) { String[] parts = e.split("[\\s,]+"); for (String s : parts) { - if ("submit".equals(s)) { - ret |= ONSUBMIT; - } else if ("resize".equals(s)) { - ret |= ONRESIZE; - } else { - int event = Event.getTypeInt(s); + int event = getTypeInt(s); if (event > 0) { ret |= event; } - } } - } + } + return ret; } 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 dcd6c2af..5e405678 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 @@ -18,6 +18,8 @@ import static com.google.gwt.query.client.GQuery.document; import static com.google.gwt.query.client.GQuery.lazy; import static com.google.gwt.user.client.Event.*; +import java.util.Arrays; + import com.google.gwt.dom.client.Element; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; @@ -905,7 +907,7 @@ public class GQueryEventsTestGwt extends GWTTestCase { String content = ""; $(e).html(content); - $("#test", e).bind(FOCUSEVENTS | KEYEVENTS | MOUSEEVENTS, null, new Function() { + $("#test", e).bind(FOCUSEVENTS | KEYEVENTS | MOUSEEVENTS, null, new Function() { @Override public void f() { $("#test", e).val("event fired"); @@ -914,8 +916,8 @@ public class GQueryEventsTestGwt extends GWTTestCase { int allEventbits[] = new int[] { - ONFOCUS, ONBLUR, ONKEYDOWN, ONKEYPRESS, ONKEYUP, ONMOUSEDOWN, ONMOUSEUP, - ONMOUSEMOVE, ONMOUSEOVER, ONMOUSEOUT}; + ONFOCUS, ONBLUR, ONKEYDOWN, ONKEYPRESS, ONKEYUP, ONMOUSEDOWN, ONMOUSEUP, ONMOUSEMOVE, + ONMOUSEOVER, ONMOUSEOUT}; for (int eventbits : allEventbits) { $("#test", e).trigger(eventbits, 'c'); @@ -924,31 +926,147 @@ public class GQueryEventsTestGwt extends GWTTestCase { $("#test", e).val(""); } - - - //unbind focus event + + // unbind focus event $("#test", e).unbind(FOCUSEVENTS); - + for (int eventbits : allEventbits) { $("#test", e).trigger(eventbits, 'c'); - if (eventbits == ONBLUR || eventbits == ONFOCUS){ + if (eventbits == ONBLUR || eventbits == ONFOCUS) { assertEquals("", $("#test", e).val()); - }else{ + } else { assertEquals("event fired", $("#test", e).val()); $("#test", e).val(""); } } - - - //unbind focus event + + // unbind focus event $("#test", e).unbind(KEYEVENTS); + + for (int eventbits : allEventbits) { + $("#test", e).trigger(eventbits, 'c'); + + if ((eventbits & MOUSEEVENTS) == eventbits) { + assertEquals("event fired", $("#test", e).val()); + $("#test", e).val(""); + } else { + assertEquals("", $("#test", e).val()); + } + + } + + // unbind some mouse events + $("#test", e).unbind(ONMOUSEDOWN | ONMOUSEUP | ONMOUSEMOVE | ONMOUSEOVER); + + for (int eventbits : allEventbits) { + $("#test", e).trigger(eventbits, 'c'); + + if (eventbits == ONMOUSEOUT) { + assertEquals("event fired", $("#test", e).val()); + $("#test", e).val(""); + } else { + assertEquals("", $("#test", e).val()); + } + + } + + // unbind one event + $("#test", e).unbind(ONMOUSEOUT); + + for (int eventbits : allEventbits) { + $("#test", e).trigger(eventbits, 'c'); + assertEquals("", $("#test", e).val()); + } + + } + + public void testMultipleEventsString() { + String content = ""; + $(e).html(content); + + int allEventbits[] = + new int[] { + ONFOCUS, ONBLUR, ONKEYDOWN, ONKEYPRESS, ONKEYUP, ONMOUSEDOWN, ONMOUSEUP, ONMOUSEMOVE, + ONMOUSEOVER, ONMOUSEOUT}; + + $("#test", e).bind( + "focus blur keydown keypress keyup mousedown mouseup mousemove mouseover mouseout", null, + new Function() { + @Override + public void f() { + $("#test", e).val("event fired"); + } + }); + + for (int eventbits : allEventbits) { + $("#test", e).trigger(eventbits, 'c'); + + assertEquals("event fired", $("#test", e).val()); + $("#test", e).val(""); + + } + + $("#test", e).unbind("focus blur keydown keypress keyup"); + + for (int eventbits : allEventbits) { + $("#test", e).trigger(eventbits, 'c'); + + if ((eventbits & MOUSEEVENTS) == eventbits) { + assertEquals("event fired", $("#test", e).val()); + $("#test", e).val(""); + } else { + assertEquals("", $("#test", e).val()); + } + } + + $("#test", e).unbind("mousedown mouseup mousemove mouseover").unbind("mouseout"); + + for (int eventbits : allEventbits) { + $("#test", e).trigger(eventbits, 'c'); + + assertEquals("", $("#test", e).val()); + + } + + } + + public void testBindingWithNameSpace() { + String content = ""; + $(e).html(content); + + $("#test", e) + .bind( + "focus.focusevents blur.focusevents keydown.keyevents keypress.keyevents keyup.keyevents " + + "mousedown.mouseevents mouseup.mouseevents mousemove.mouseevents mouseover.mouseevents " + + "mouseout.mouseevents", null, new Function() { + @Override + public void f() { + $("#test", e).val("event fired"); + } + }); + int allEventbits[] = + new int[] { + ONFOCUS, ONBLUR, ONKEYDOWN, ONKEYPRESS, ONKEYUP, ONMOUSEDOWN, ONMOUSEUP, ONMOUSEMOVE, + ONMOUSEOVER, ONMOUSEOUT}; + for (int eventbits : allEventbits) { $("#test", e).trigger(eventbits, 'c'); - if ((eventbits & MOUSEEVENTS) == eventbits){ + assertEquals("event fired", $("#test", e).val()); + $("#test", e).val(""); + + } + + //test unbind without namespace + $("#test", e).unbind("focus blur"); + + for (int eventbits : allEventbits) { + $("#test", e).trigger(eventbits, 'c'); + + if (eventbits != ONFOCUS && eventbits != ONBLUR){ assertEquals("event fired", $("#test", e).val()); $("#test", e).val(""); }else{ @@ -957,27 +1075,28 @@ public class GQueryEventsTestGwt extends GWTTestCase { } - //unbind some mouse events - $("#test", e).unbind(ONMOUSEDOWN | ONMOUSEUP| ONMOUSEMOVE| ONMOUSEOVER); + //test unbind event name + namespace + $("#test", e).unbind("keydown.keyevents keypress.keyevents keyup.keyevents"); for (int eventbits : allEventbits) { $("#test", e).trigger(eventbits, 'c'); - if (eventbits == ONMOUSEOUT){ + if ((eventbits & MOUSEEVENTS) == eventbits) { assertEquals("event fired", $("#test", e).val()); $("#test", e).val(""); - }else{ + } else { assertEquals("", $("#test", e).val()); } - } - //unbind one event - $("#test", e).unbind(ONMOUSEOUT); + //test unbind only on namespace + $("#test", e).unbind(".mouseevents"); for (int eventbits : allEventbits) { $("#test", e).trigger(eventbits, 'c'); + assertEquals("", $("#test", e).val()); + } } -- 2.39.5