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;
private static class LiveBindFunction extends BindFunction {
// TODO can be a list of BindFunction
- Map<String, BindFunction> bindFunctionBySelector;
+ Map<String, List<BindFunction>> bindFunctionBySelector;
LiveBindFunction(int type, String namespace) {
super(type, namespace, null, null, -1);
- bindFunctionBySelector = new HashMap<String, BindFunction>();
+ bindFunctionBySelector = new HashMap<String, List<BindFunction>>();
}
/**
* Add a {@link BindFunction} for a specific css selector
*/
public void addBindFunctionForSelector(String cssSelector, BindFunction f) {
- bindFunctionBySelector.put(cssSelector, f);
+ List<BindFunction> bindFunctions = bindFunctionBySelector.get(cssSelector);
+ if (bindFunctions == null) {
+ bindFunctions = new ArrayList<BindFunction>();
+ bindFunctionBySelector.put(cssSelector, bindFunctions);
+ }
+
+ bindFunctions.add(f);
}
@Override
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<BindFunction> 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;
+ }
}
}
}
/**
* Remove the BindFunction associated to this cssSelector
*/
- public BindFunction removeBindFunctionForSelector(String cssSelector) {
- return bindFunctionBySelector.remove(cssSelector);
+ public void removeBindFunctionForSelector(String cssSelector) {
+ bindFunctionBySelector.remove(cssSelector);
}
/**
bind(b, nameSpace, data, function, -1);
}
}
-
+
public void die(String eventName, String cssSelector) {
int eventType = "submit".equals(eventName) ? eventType = ONSUBMIT
: Event.getTypeInt(eventName);
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);
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));
}
}
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;
}
}
+
+public void testLiveWithMultipleFunction() {
+
+ $(e).html("<div id='div1'><div id='div2'>Content 1<span id='span1'> blop</span></div></div>");
+
+ $(".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("<div id='div1'><div id='div2'>Content 1<span id='span1'> blop</span></div></div>");
+
+ $(".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("<p>Content</p>");
assertEquals($("#test").attr("tabIndex"), "2");
}
- public void testUnbindMultipleEvents() {
- String content = "<p>content</p>";
- $(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");