aboutsummaryrefslogtreecommitdiffstats
path: root/gwtquery-core
diff options
context:
space:
mode:
authorJulien Dramaix <julien.dramaix@gmail.com>2013-01-21 13:52:36 -0800
committerJulien Dramaix <julien.dramaix@gmail.com>2013-01-21 13:52:36 -0800
commitf3c350dc31c04029f02a4cdee8b97d00fed472e2 (patch)
tree3c30253aa27383a1cdc12908ce9fc6e2957263fd /gwtquery-core
parent3969bc8418ace02065bcf1800f32bea70fe3ac09 (diff)
parent4848cd15db0fd04f42255b7904a4f314b84e4936 (diff)
downloadgwtquery-f3c350dc31c04029f02a4cdee8b97d00fed472e2.tar.gz
gwtquery-f3c350dc31c04029f02a4cdee8b97d00fed472e2.zip
Merge pull request #7 from gwtquery/jd_event_delegation
Add support for namespace/ multiple events in live and die methods
Diffstat (limited to 'gwtquery-core')
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Events.java17
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java102
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTestGwt.java72
3 files changed, 175 insertions, 16 deletions
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..f9ee5519 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, null, 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 <code>eventName</code> 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, null, 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..6ba03bae 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,33 @@ 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, String originalEventName) {
+ if (nameSpace == null && originalEventName == null) {
+ bindFunctionBySelector.delete(cssSelector);
+ } else {
+ JsObjectArray<BindFunction> functions = bindFunctionBySelector.get(cssSelector);
+
+ if (functions == null || functions.length() == 0) {
+ return;
+ }
+ JsObjectArray<BindFunction> newFunctions = JsObjectArray.create();
+
+ for (int i = 0; i < functions.length(); i++) {
+ BindFunction f = functions.get(i);
+ boolean matchNamespace = nameSpace == null || nameSpace.equals(f.nameSpace);
+ boolean matchOriginalEventName = originalEventName == null || originalEventName.equals(f.originalEventType);
+
+ if (!matchNamespace || !matchOriginalEventName) {
+ newFunctions.add(f);
+ }
+ }
+
+ bindFunctionBySelector.delete(cssSelector);
+ if (newFunctions.length() > 0) {
+ bindFunctionBySelector.put(cssSelector, newFunctions);
+ }
+
+ }
}
/**
@@ -472,19 +497,45 @@ 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];
+ }
+
+
+ //handle special event like mouseenter or mouseleave
+ SpecialEvent hook = special.get(eventName);
+ eventName = hook != null ? hook.getDelegateType() : eventName;
+ String originalEventName = hook != null ? hook.getOriginalType() : null;
+
+ int b = getTypeInt(eventName);
+
+ die(b, nameSpace, originalEventName, cssSelector);
+ }
+
+
}
- public void die(int eventbits, String cssSelector) {
- if (eventbits == 0) {
+ public void die(int eventbits, String nameSpace, String originalEventName,String cssSelector) {
+ if (eventbits <= 0) {
for (String k : liveBindFunctionByEventType.keys()) {
LiveBindFunction liveBindFunction = liveBindFunctionByEventType.<JsCache> cast().get(k);
- liveBindFunction.removeBindFunctionForSelector(cssSelector);
+ liveBindFunction.removeBindFunctionForSelector(cssSelector, nameSpace, null);
}
} else {
LiveBindFunction liveBindFunction = liveBindFunctionByEventType.get(eventbits);
if (liveBindFunction != null) {
- liveBindFunction.removeBindFunctionForSelector(cssSelector);
+ liveBindFunction.removeBindFunctionForSelector(cssSelector, nameSpace, originalEventName);
}
}
}
@@ -514,11 +565,37 @@ 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];
+ }
+
+ //handle special event like mouseenter or mouseleave
+ SpecialEvent hook = special.get(eventName);
+ eventName = hook != null ? hook.getDelegateType() : eventName;
+ String originalEventName = hook != null ? hook.getOriginalType() : null;
+
+ int b = getTypeInt(eventName);
+ for (Function function : funcs) {
+ Function handler = hook != null ? hook.createDelegateHandler(function) : function;
+ live(b, nameSpace, originalEventName, cssSelector, data, handler);
+ }
+ }
}
- public void live(int eventbits, String cssSelector, Object data, Function... funcs) {
+ public void live(int eventbits, String nameSpace, String originalEventName, String cssSelector, Object data, Function... funcs) {
for (int i = 0; i < 28; i++) {
int event = (int) Math.pow(2, i);
if ((eventbits & event) == event) {
@@ -534,9 +611,8 @@ 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",
- null, f, data));
+ liveBindFunction.addBindFunctionForSelector(cssSelector, new BindFunction(event, nameSpace,
+ originalEventName, 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 = "<input type='text' id='test'></div>";
+ $(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 = "<div id='test'>blop</div>";
$(e).html(content);