From: jdramaix Date: Mon, 7 Jan 2013 12:27:38 +0000 (+0100) Subject: Add support for namespace/ multiple events in live and die methods X-Git-Tag: release-1.3.2~22^2~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1b23bbaa5a114f96afaa636b5355b3b827975d96;p=gwtquery.git Add support for namespace/ multiple events in live and die methods --- 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 4b558fe8..ce9d0272 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 @@ -106,12 +106,18 @@ public class Events extends GQuery { return this; } - public GQuery die(int eventbits) { + + public GQuery die(int eventbits, String nameSpace) { EventsListener.getInstance(Element.is(currentContext) ? (Element) currentContext : body).die( - eventbits, currentSelector); + eventbits, nameSpace, currentSelector); return this; } + + public GQuery die(int eventbits) { + return die(eventbits, null); + } + /** * Remove an event handlers previously attached using live() The selector used with it must match * exactly the selector initially used with live(). if eventName is null, all event @@ -124,8 +130,13 @@ public class Events extends GQuery { } public GQuery live(int eventbits, final Object data, Function... funcs) { + return live(eventbits, null, data, funcs); + + } + + public GQuery live(int eventbits, String nameSpace, final Object data, Function... funcs) { EventsListener.getInstance(Element.is(currentContext) ? (Element) currentContext : body).live( - eventbits, currentSelector, data, funcs); + eventbits, nameSpace, currentSelector, data, funcs); return this; } 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 991ab086..7064ae35 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 @@ -280,8 +280,30 @@ public class EventsListener implements EventListener { /** * Remove the BindFunction associated to this cssSelector */ - public void removeBindFunctionForSelector(String cssSelector) { - bindFunctionBySelector.delete(cssSelector); + public void removeBindFunctionForSelector(String cssSelector, String nameSpace) { + if (nameSpace == null || nameSpace.length() == 0) { + bindFunctionBySelector.delete(cssSelector); + } else { + JsObjectArray functions = bindFunctionBySelector.get(cssSelector); + + if (functions == null || functions.length() == 0) { + return; + } + JsObjectArray newFunctions = JsObjectArray.create(); + + for (int i = 0; i < functions.length(); i++) { + BindFunction f = functions.get(i); + if (!nameSpace.equals(f.nameSpace)) { + newFunctions.add(f); + } + } + + bindFunctionBySelector.delete(cssSelector); + if (newFunctions.length() > 0) { + bindFunctionBySelector.put(cssSelector, newFunctions); + } + + } } /** @@ -472,19 +494,39 @@ public class EventsListener implements EventListener { } public void die(String eventNames, String cssSelector) { - die(getEventBits(eventNames), cssSelector); + String[] parts = eventNames.split("[\\s,]+"); + + for (String event : parts) { + String nameSpace = null; + String eventName = event; + + //seperate possible namespace + //jDramaix: I removed old regex ^([^.]*)\.?(.*$) because it didn't work on IE8... + String[] subparts = event.split("\\.", 2); + + if (subparts.length == 2) { + nameSpace = subparts[1]; + eventName = subparts[0]; + } + + int b = getTypeInt(eventName); + + die(b, nameSpace, cssSelector); + } + + } - public void die(int eventbits, String cssSelector) { - if (eventbits == 0) { + public void die(int eventbits, String nameSpace, String cssSelector) { + if (eventbits <= 0) { for (String k : liveBindFunctionByEventType.keys()) { LiveBindFunction liveBindFunction = liveBindFunctionByEventType. cast().get(k); - liveBindFunction.removeBindFunctionForSelector(cssSelector); + liveBindFunction.removeBindFunctionForSelector(cssSelector, nameSpace); } } else { LiveBindFunction liveBindFunction = liveBindFunctionByEventType.get(eventbits); if (liveBindFunction != null) { - liveBindFunction.removeBindFunctionForSelector(cssSelector); + liveBindFunction.removeBindFunctionForSelector(cssSelector, nameSpace); } } } @@ -514,11 +556,32 @@ public class EventsListener implements EventListener { return getGwtEventListener(element); } - public void live(String eventNames, String cssSelector, Object data, Function... f) { - live(getEventBits(eventNames), cssSelector, data, f); + public void live(String events, String cssSelector, Object data, Function... funcs) { + + String[] parts = events.split("[\\s,]+"); + + for (String event : parts) { + + String nameSpace = null; + String eventName = event; + + + String[] subparts = event.split("\\.", 2); + + if (subparts.length == 2) { + nameSpace = subparts[1]; + eventName = subparts[0]; + } + + int b = getTypeInt(eventName); + for (Function function : funcs) { + //Function handler = hook != null ? hook.createDelegateHandler(function) : function; + live(b, nameSpace, cssSelector, data, function); + } + } } - public void live(int eventbits, String cssSelector, Object data, Function... funcs) { + public void live(int eventbits, String nameSpace, String cssSelector, Object data, Function... funcs) { for (int i = 0; i < 28; i++) { int event = (int) Math.pow(2, i); if ((eventbits & event) == event) { @@ -535,7 +598,7 @@ public class EventsListener implements EventListener { for (Function f : funcs) { // TODO handle special event by passing original event name - liveBindFunction.addBindFunctionForSelector(cssSelector, new BindFunction(event, "live", + liveBindFunction.addBindFunctionForSelector(cssSelector, new BindFunction(event, nameSpace, null, f, data)); } } 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 c5bdc5ac..c833c130 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 @@ -619,6 +619,78 @@ public class GQueryEventsTestGwt extends GWTTestCase { assertEquals("yellow", $("#div1", e).css(CSS.BACKGROUND_COLOR, false)); } + public void testLiveWithNameSpace() { + String content = ""; + $(e).html(content); + + $(".live", e) + .live( + "keydown.keyevents keypress.keyevents keyup.keyevents " + + "mousedown.mouseevents mouseup.mouseevents mousemove.mouseevents mouseover.mouseevents " + + "mouseout.mouseevents", new Function() { + @Override + public void f() { + $("#test", e).val("event fired"); + } + }); + + $("#test", e).addClass("live"); + + + int allEventbits[] = + new int[]{ + ONKEYDOWN, ONKEYPRESS, ONKEYUP, ONMOUSEDOWN, ONMOUSEUP, ONMOUSEMOVE, + ONMOUSEOVER, ONMOUSEOUT}; + + for (int eventbits : allEventbits) { + $("#test", e).trigger(eventbits, 'c'); + + assertEquals("event fired", $("#test", e).val()); + $("#test", e).val(""); + + + } + + // test die without namespace + $(".live", e).die("keydown"); + + for (int eventbits : allEventbits) { + $("#test", e).trigger(eventbits, 'c'); + + if (eventbits != ONKEYDOWN) { + assertEquals("event fired", $("#test", e).val()); + $("#test", e).val(""); + } else { + assertEquals("", $("#test", e).val()); + } + + } + + // test die event name + namespace + $(".live", e).die("keypress.keyevents keyup.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()); + } + } + + // test die only on namespace + $(".live", e).die(".mouseevents"); + + for (int eventbits : allEventbits) { + $("#test", e).trigger(eventbits, 'c'); + + assertEquals("", $("#test", e).val()); + + } + + } + public void testMouseenterEvent() { String content = "
blop
"; $(e).html(content);