]> source.dussan.org Git - gwtquery.git/commitdiff
Fixed event handler with IE and Chrome. Re-factor of Events. Increased coverage.
authorManolo Carrasco <manolo@apache.org>
Wed, 5 May 2010 08:48:34 +0000 (08:48 +0000)
committerManolo Carrasco <manolo@apache.org>
Wed, 5 May 2010 08:48:34 +0000 (08:48 +0000)
gwtquery-core/src/main/java/com/google/gwt/query/client/Events.java
gwtquery-core/src/main/java/com/google/gwt/query/client/EventsListener.java
gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java
gwtquery-core/src/test/java/com/google/gwt/query/client/GwtEventsTest.java

index 8c6d136461b429b3d565e18605dd586f03f3c370..30120d6a9078a464a7c2e6b0641f5e1f1d1f9afc 100644 (file)
@@ -16,6 +16,7 @@
 package com.google.gwt.query.client;
 
 import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.NativeEvent;
 import com.google.gwt.dom.client.NodeList;
 import com.google.gwt.user.client.Event;
 
@@ -47,38 +48,81 @@ public class Events extends GQuery {
   }
 
   /**
-   * Binds a handler to a particular Event for each matched element.
-   *
-   * The event handler is passed as a Function that you can use to prevent
-   * default behaviour. To stop both default action and event bubbling, the
+   * 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
+   * 
    */
-  public GQuery bind(int eventbits, final Object data, final Function f) {
+  public GQuery bind(int eventbits, Object data, Function...funcs) {
     for (Element e : elements()) {
-      EventsListener.getInstance(e).bind(eventbits, data, f);
+      EventsListener.getInstance(e).bind(eventbits, data, funcs);
     }
     return this;
   }
-
+  
   /**
-   * Fires an event on each matched element.
-   *
-   * Example: fire(Event.ONCLICK)
+   * Execute all handlers and behaviors attached to the matched elements for the given event types.
+   * 
+   * Different event types can be passed joining these using the or bit wise operator.
+   * 
+   * For keyboard events you can pass a second parameter which represents 
+   * the key-code of the pushed key. 
+   * 
+   * Example: fire(Event.ONCLICK | Event.ONFOCUS)
+   * Example: fire(Event.ONKEYDOWN. 'a');
    */
-  public GQuery fire(int eventbits, int... keys) {
-    for (Element e : elements()) {
-      EventsListener.getInstance(e).fire(eventbits, keys);
-    }
+  public GQuery trigger(int eventbits, int... keys) {
+    if ((eventbits | Event.ONBLUR) == Event.ONBLUR)
+      dispatchEvent(document.createBlurEvent());
+    if ((eventbits | Event.ONCHANGE) == Event.ONCHANGE)
+      dispatchEvent(document.createChangeEvent());
+    if ((eventbits | Event.ONCLICK) == Event.ONCLICK)
+      dispatchEvent(document.createClickEvent(0, 0, 0, 0, 0, false, false, false, false));
+    if ((eventbits | Event.ONDBLCLICK) == Event.ONDBLCLICK)
+      dispatchEvent(document.createDblClickEvent(0, 0, 0, 0, 0, false, false, false, false));
+    if ((eventbits | Event.ONFOCUS) == Event.ONFOCUS)
+      dispatchEvent(document.createFocusEvent());
+    if ((eventbits | Event.ONKEYDOWN) == Event.ONKEYDOWN)
+      dispatchEvent(document.createKeyDownEvent(false, false, false, false, keys[0], 0));
+    if ((eventbits | Event.ONKEYPRESS) == Event.ONKEYPRESS)
+      dispatchEvent(document.createKeyPressEvent(false, false, false, false, keys[0], 0));
+    if ((eventbits | Event.ONKEYUP) == Event.ONKEYUP)
+      dispatchEvent(document.createKeyUpEvent(false, false, false, false, keys[0], 0));
+    if ((eventbits | Event.ONLOSECAPTURE) == Event.ONLOSECAPTURE)
+      triggerHtmlEvent("losecapture");
+    if ((eventbits | Event.ONMOUSEDOWN) == Event.ONMOUSEDOWN)
+      dispatchEvent(document.createMouseDownEvent(0, 0, 0, 0, 0, false, false, false, false, NativeEvent.BUTTON_LEFT));
+    if ((eventbits | Event.ONMOUSEMOVE) == Event.ONMOUSEMOVE)
+      dispatchEvent(document.createMouseMoveEvent(0, 0, 0, 0, 0, false, false, false, false, NativeEvent.BUTTON_LEFT));
+    if ((eventbits | Event.ONMOUSEOUT) == Event.ONMOUSEOUT)
+      dispatchEvent(document.createMouseOutEvent(0, 0, 0, 0, 0, false, false, false, false, NativeEvent.BUTTON_LEFT, null));
+    if ((eventbits | Event.ONMOUSEOVER) == Event.ONMOUSEOVER)
+      dispatchEvent(document.createMouseOverEvent(0, 0, 0, 0, 0, false, false, false, false, NativeEvent.BUTTON_LEFT, null));
+    if ((eventbits | Event.ONMOUSEUP) == Event.ONMOUSEUP)
+      dispatchEvent(document.createMouseUpEvent(0, 0, 0, 0, 0, false, false, false, false, NativeEvent.BUTTON_LEFT));
+    if ((eventbits | Event.ONSCROLL) == Event.ONSCROLL)
+      dispatchEvent(document.createScrollEvent());
+    if ((eventbits | Event.ONERROR) == Event.ONERROR)
+      dispatchEvent(document.createErrorEvent());
+    if ((eventbits | Event.ONMOUSEWHEEL) == Event.ONMOUSEWHEEL)
+      dispatchEvent(document.createMouseEvent("mousewheel", true, true, 0, 0, 0, 0, 0, false, false, false, false, NativeEvent.BUTTON_LEFT, null));
+    return this;
+  }
+  
+  protected GQuery triggerHtmlEvent(String htmlEvent) {
+    dispatchEvent(document.createHtmlEvent(htmlEvent, false, false));
     return this;
   }
 
   /**
    * Removes all handlers, that matches the events bits passed, from each
    * element.
-   *
+   * 
    * Example: unbind(Event.ONCLICK | Event.ONMOUSEOVER)
    */
   public GQuery unbind(int eventbits) {
@@ -87,101 +131,10 @@ public class Events extends GQuery {
     }
     return this;
   }
-}
-
-/**
- * Just a class with static methods for firing element events on demand.
- */
-class FireEvents {
-
-  public static void fire(Element element, int eventbits, int... keys) {
-    Event event = null;
-
-    String type = getEventTypeStr(eventbits);
-
-    if ((eventbits & Event.MOUSEEVENTS) != 0
-        || (eventbits | Event.ONCLICK) == Event.ONCLICK) {
-      event = createMouseEventImpl(type);
-    } else if ((eventbits & Event.KEYEVENTS) != 0) {
-      event = createKeyEventImpl(type, keys[0]);
-    } else if ((eventbits & Event.FOCUSEVENTS) != 0) {
-      event = createHtmlEventImpl(type);
-    }
-
-    dispatchEvent(element, event);
-  }
-
-  private static native Event createHtmlEventImpl(String type) /*-{
-   var event = $doc.createEvent('HTMLEvents');
-   event.initEvent( type, true, true);
-   return event;
-  }-*/;
-
-  private static native Event createKeyEventImpl(String type, int keycode) /*-{
-   var event;
-     if( $wnd.KeyEvent ) {
-        event = $doc.createEvent('KeyEvents');
-       event.initKeyEvent( type, true, true, $wnd, false, false, false, false, keycode, 0 );
-     } else {
-        event = $doc.createEvent('UIEvents');
-       event.initUIEvent( type, true, true, $wnd, 1 );
-       event.keyCode = keycode;
-     }
-    return event;
-  }-*/;
-
-  private static native Event createMouseEventImpl(String type) /*-{
-   var event = $doc.createEvent('MouseEvents');
-    event.initEvent(type, true, true);
-    return event;
-  }-*/;
-
-  private static native void dispatchEvent(Element elem, Event event) /*-{
-    elem.dispatchEvent(event);
-    if (event.type == 'focus' && elem.focus)
-      elem.focus();
-    else if (event.type == 'blur' && elem.focus)  
-      elem.blur();
-  }-*/;
-
-  private static String getEventTypeStr(int type) {
-    switch (type) {
-      case Event.ONBLUR:
-        return "blur";
-      case Event.ONCHANGE:
-        return "change";
-      case Event.ONCLICK:
-        return "click";
-      case Event.ONDBLCLICK:
-        return "dblclick";
-      case Event.ONFOCUS:
-        return "focus";
-      case Event.ONKEYDOWN:
-        return "keydown";
-      case Event.ONKEYPRESS:
-        return "keypress";
-      case Event.ONKEYUP:
-        return "keyup";
-      case Event.ONLOSECAPTURE:
-        return "losecapture";
-      case Event.ONMOUSEDOWN:
-        return "mousedown";
-      case Event.ONMOUSEMOVE:
-        return "mousemove";
-      case Event.ONMOUSEOUT:
-        return "mouseout";
-      case Event.ONMOUSEOVER:
-        return "mouseover";
-      case Event.ONMOUSEUP:
-        return "mouseup";
-      case Event.ONSCROLL:
-        return "scroll";
-      case Event.ONERROR:
-        return "error";
-      case Event.ONMOUSEWHEEL:
-        return "mousewheel";
-      default:
-        return "";
+  
+  private void dispatchEvent(NativeEvent evt) {
+    for (Element e : elements()) {
+      e.dispatchEvent(evt);
     }
   }
-}
\ No newline at end of file
+}
index 5f40530b8f2c3a8dad1b01ead8e6d97ff9f379c2..aa27e0b11ada60e66e844bb402acb3f19bd2a8a7 100644 (file)
@@ -21,14 +21,14 @@ import com.google.gwt.user.client.Event;
 import com.google.gwt.user.client.EventListener;
 
 /**
- * This class implements an event queue instance for one element. This queue
+ * This class implements an event queue instance for one Element. The queue
  * instance is configured as the default event listener in GWT.
- *
- * The reference to this queue is stored as a uniq variable in the element's
+ * 
+ * The reference to this queue is stored as a unique variable in the element's
  * DOM
- *
- * The class takes care of calling the appropiate functions for each browser
- * event and also calls sinkEvents methods.
+ * 
+ * The class takes care of calling the appropriate functions for each browser
+ * event and it also calls sinkEvents method.
  */
 class EventsListener implements EventListener {
 
@@ -84,8 +84,8 @@ class EventsListener implements EventListener {
     elem.__gqueryevent = gqevent;
   }-*/;
 
-  private JsObjectArray<EventsListener.BindFunction> elementEvents
-      = JsObjectArray.createArray().cast();
+  private JsObjectArray<EventsListener.BindFunction> elementEvents = JsObjectArray
+      .createArray().cast();
 
   private Element element;
 
@@ -100,25 +100,22 @@ class EventsListener implements EventListener {
     if (function == null) {
       unbind(eventbits);
     } else {
-      DOM.sinkEvents((com.google.gwt.user.client.Element) element,
-          eventbits | DOM
-              .getEventsSunk((com.google.gwt.user.client.Element) element));
+      DOM.sinkEvents((com.google.gwt.user.client.Element) element, eventbits
+          | DOM.getEventsSunk((com.google.gwt.user.client.Element) element));
 
       if ((eventbits | Event.FOCUSEVENTS) == Event.FOCUSEVENTS) {
         setFocusable(element);
       }
 
-      elementEvents.add(
-          new EventsListener.BindFunction(eventbits, function, data, times));
+      elementEvents.add(new EventsListener.BindFunction(eventbits, function,
+          data, times));
     }
   }
 
-  public void bind(int eventbits, final Object data, final Function function) {
-    bind(eventbits, data, function, -1);
-  }
-
-  public void fire(int eventbits, int... keys) {
-    FireEvents.fire(element, eventbits, keys);
+  public void bind(int eventbits, final Object data, Function...funcs) {
+    for (Function function: funcs) {
+      bind(eventbits, data, function, -1);
+    }
   }
 
   public void onBrowserEvent(Event event) {
@@ -127,9 +124,11 @@ class EventsListener implements EventListener {
       EventsListener.BindFunction listener = elementEvents.get(i);
       if (listener.hasEventType(etype)) {
         if (!listener.fire(event)) {
-          event.cancelBubble(true);
+          System.out.println("Return: " + false);
           event.stopPropagation();
           event.preventDefault();
+        } else {
+          System.out.println("Return: " + true);
         }
       }
     }
index efe340a555f2df0de72396833f70004c942790c9..36318ad8675ea41104ce086d3b7f02abfe0a99fb 100644 (file)
@@ -27,7 +27,6 @@ import com.google.gwt.dom.client.Document;
 import com.google.gwt.dom.client.Element;\r
 import com.google.gwt.dom.client.IFrameElement;\r
 import com.google.gwt.dom.client.InputElement;\r
-import com.google.gwt.dom.client.NativeEvent;\r
 import com.google.gwt.dom.client.Node;\r
 import com.google.gwt.dom.client.NodeList;\r
 import com.google.gwt.dom.client.OptionElement;\r
@@ -298,7 +297,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * Wrap a GQuery around an event's target element.\r
    */\r
   public static GQuery $(Event event) {\r
-    return $(event.getCurrentTarget());\r
+    return $((Element)event.getCurrentEventTarget().cast());\r
   }\r
 \r
   /**\r
@@ -638,46 +637,45 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   /**\r
-   * Binds a handler to a particular Event (like Event.ONCLICK) for each matched\r
-   * element.\r
-   *\r
-   * The event handler is passed as a Function that you can use to prevent\r
-   * default behaviour. To stop both default action and event bubbling, the\r
+   * Binds a set of handlers to a particular Event for each matched element.\r
+   * \r
+   * The event handlers are passed as Functions that you can use to prevent\r
+   * default behavior. To stop both default action and event bubbling, the\r
    * function event handler has to return false.\r
-   *\r
+   * \r
    * You can pass an additional Object data to your Function as the second\r
    * parameter\r
+   * \r
    */\r
-  public GQuery bind(int eventbits, final Object data, final Function f) {\r
-    return as(Events).bind(eventbits, data, f);\r
-  }\r
-\r
-  /**\r
-   * Bind a function to the blur event of each matched element.\r
-   */\r
-  public GQuery blur(Function f) {\r
-    return bind(Event.ONBLUR, null, f);\r
+  public GQuery bind(int eventbits, final Object data, final Function...funcs) {\r
+    return as(Events).bind(eventbits, data, funcs);\r
   }\r
 \r
   /**\r
-   * Trigger a blur event.\r
+   * Bind Handlers or fire Events for each matched element. \r
    */\r
-  public GQuery blur() {\r
-    return trigger(document.createBlurEvent(), null);\r
+  private GQuery bindOrFire(int eventbits, final Object data, final Function...funcs) {\r
+    if (funcs.length == 0) {\r
+      return trigger(eventbits);\r
+    } else {\r
+      return bind(eventbits, data, funcs);\r
+    }\r
   }\r
 \r
   /**\r
-   * Bind a function to the change event of each matched element.\r
+   * Bind a set of functions to the blur event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery change(Function f) {\r
-    return bind(Event.ONCHANGE, null, f);\r
+  public GQuery blur(Function...f) {\r
+    return bindOrFire(Event.ONBLUR, null, f);\r
   }\r
 \r
   /**\r
-   * Trigger a change event.\r
+   * Bind a set of functions to the change event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery change() {\r
-    return trigger(document.createChangeEvent(), null);\r
+  public GQuery change(Function...f) {\r
+    return bindOrFire(Event.ONCHANGE, null, f);\r
   }\r
 \r
   /**\r
@@ -703,22 +701,13 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   /**\r
-   * Trigger a click event.\r
-   */\r
-  public GQuery click() {\r
-    return trigger(\r
-        document.createClickEvent(0, 0, 0, 0, 0, false, false, false,\r
-            false), null);\r
-  }\r
-\r
-  /**\r
-   * Triggers the click event of each matched element. Causes all of the\r
-   * functions that have been bound to that click event to be executed.\r
+   * Bind a set of functions to the click event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery click(final Function f) {\r
-    return bind(Event.ONCLICK, null, f);\r
+  public GQuery click(Function...f) {\r
+    return bindOrFire(Event.ONCLICK, null, f);\r
   }\r
-\r
+  \r
   /**\r
    * Clone matched DOM Elements and select the clones. This is useful for moving\r
    * copies of the elements to another location in the DOM.\r
@@ -858,21 +847,13 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     }\r
     return this;\r
   }\r
-\r
-  /**\r
-   * Trigger a double click event.\r
-   */\r
-  public GQuery dblclick() {\r
-    return trigger(\r
-        document.createDblClickEvent(0, 0, 0, 0, 0, false, false, false,\r
-            false), null);\r
-  }\r
-\r
+  \r
   /**\r
-   * Bind a function to the dblclick event of each matched element.\r
+   * Bind a set of functions to the dblclick event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery dblclick(Function f) {\r
-    return bind(Event.ONDBLCLICK, null, f);\r
+  public GQuery dblclick(Function...f) {\r
+    return bindOrFire(Event.ONDBLCLICK, null, f);\r
   }\r
 \r
   /**\r
@@ -946,19 +927,13 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   /**\r
-   * Trigger an error event.\r
+   * Bind a set of functions to the error event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery error() {\r
-    return trigger(document.createErrorEvent(), null);\r
-  }\r
-\r
-  /**\r
-   * Bind a function to the error event of each matched element.\r
-   */\r
-  public GQuery error(Function f) {\r
-    return bind(Event.ONERROR, null, f);\r
-  }\r
-\r
+  public GQuery error(Function...f) {\r
+    return bindOrFire(Event.ONERROR, null, f);\r
+  }  \r
+  \r
   /**\r
    * Fade in all matched elements by adjusting their opacity.\r
    */\r
@@ -1048,20 +1023,13 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   /**\r
-   * Trigger a focus event.\r
+   * Bind a set of functions to the focus event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery focus() {\r
-    return trigger(document.createFocusEvent(), null);\r
-  }\r
-\r
-  /**\r
-   * Bind a function to the focus event of each matched element.\r
-   */\r
-\r
-  public GQuery focus(Function f) {\r
-    return bind(Event.ONFOCUS, null, f);\r
-  }\r
-\r
+  public GQuery focus(Function...f) {\r
+    return bindOrFire(Event.ONFOCUS, null, f);\r
+  }  \r
+  \r
   /**\r
    * Return all elements matched in the GQuery as a NodeList. @see #elements()\r
    * for a method which returns them as an immutable Java array.\r
@@ -1264,54 +1232,51 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   public boolean is(String... filters) {\r
     return filter(filters).size() > 0;\r
   }\r
-\r
+  \r
   /**\r
-   * Trigger a keydown event.\r
+   * Bind a set of functions to the keydown event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery keydown() {\r
-    return trigger(\r
-        document.createKeyDownEvent(false, false, false, false, 0, 0),\r
-        null);\r
-  }\r
+  public GQuery keydown(Function...f) {\r
+    return bindOrFire(Event.ONKEYDOWN, null, f);\r
+  }  \r
 \r
   /**\r
-   * Bind a function to the keydown event of each matched element.\r
+   * Trigger a keydown event passing the key pushed\r
    */\r
-  public GQuery keydown(Function f) {\r
-    return bind(Event.ONKEYDOWN, null, f);\r
+  public GQuery keydown(int key) {\r
+    return trigger(Event.ONKEYDOWN, key);\r
   }\r
 \r
   /**\r
-   * Trigger a keypress event.\r
+   * Bind a set of functions to the keypress event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery keypress() {\r
-    return trigger(\r
-        document.createKeyPressEvent(false, false, false, false, 0, 0),\r
-        null);\r
-  }\r
+  public GQuery keypress(Function...f) {\r
+    return bindOrFire(Event.ONKEYPRESS, null, f);\r
+  }  \r
 \r
   /**\r
-   * Bind a function to the keypress event of each matched element.\r
+   * Trigger a keypress event passing the key pushed\r
    */\r
-  public GQuery keypressed(Function f) {\r
-    return bind(Event.ONKEYPRESS, null, f);\r
+  public GQuery keypress(int key) {\r
+    return trigger(Event.ONKEYPRESS, key);\r
   }\r
 \r
   /**\r
-   * Trigger a keyup event.\r
+   * Bind a set of functions to the keyup event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery keyup() {\r
-    return trigger(\r
-        document.createKeyUpEvent(false, false, false, false, 0, 0),\r
-        null);\r
-  }\r
+  public GQuery keyup(Function...f) {\r
+    return bindOrFire(Event.ONKEYUP, null, f);\r
+  }  \r
 \r
   /**\r
-   * Bind a function to the keyup event of each matched element.\r
+   * Trigger a keyup event passing the key pushed\r
    */\r
-  public GQuery keyup(Function f) {\r
-    return bind(Event.ONKEYUP, null, f);\r
-  }\r
+  public GQuery keyup(int key) {\r
+    return trigger(Event.ONKEYUP, key);\r
+  }  \r
 \r
   /**\r
    * Returns the number of elements currently matched. The size function will\r
@@ -1336,41 +1301,46 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   public GQuery lt(int pos) {\r
     return $(slice(0, pos));\r
   }\r
-\r
+  \r
   /**\r
-   * Bind a function to the mousedown event of each matched element.\r
+   * Bind a set of functions to the mousedown event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery mousedown(Function f) {\r
-    return bind(Event.ONMOUSEDOWN, null, f);\r
-  }\r
-\r
+  public GQuery mousedown(Function...f) {\r
+    return bindOrFire(Event.ONMOUSEDOWN, null, f);\r
+  }  \r
\r
   /**\r
-   * Bind a function to the mousemove event of each matched element.\r
+   * Bind a set of functions to the mousemove event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery mousemove(Function f) {\r
-    return bind(Event.ONMOUSEMOVE, null, f);\r
-  }\r
+  public GQuery mousemove(Function...f) {\r
+    return bindOrFire(Event.ONMOUSEMOVE, null, f);\r
+  }  \r
 \r
   /**\r
-   * Bind a function to the mouseout event of each matched element.\r
+   * Bind a set of functions to the mouseout event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery mouseout(Function f) {\r
-    return bind(Event.ONMOUSEOUT, null, f);\r
-  }\r
+  public GQuery mouseout(Function...f) {\r
+    return bindOrFire(Event.ONMOUSEOUT, null, f);\r
+  }  \r
 \r
   /**\r
-   * Bind a function to the mouseover event of each matched element.\r
+   * Bind a set of functions to the mouseover event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery mouseover(Function f) {\r
-    return bind(Event.ONMOUSEOVER, null, f);\r
-  }\r
-\r
+  public GQuery mouseover(Function...f) {\r
+    return bindOrFire(Event.ONMOUSEOVER, null, f);\r
+  }  \r
+  \r
   /**\r
-   * Bind a function to the mouseup event of each matched element.\r
+   * Bind a set of functions to the mouseup event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery mouseup(Function f) {\r
-    return bind(Event.ONMOUSEUP, null, f);\r
-  }\r
+  public GQuery mouseup(Function...f) {\r
+    return bindOrFire(Event.ONMOUSEUP, null, f);\r
+  }    \r
 \r
   /**\r
    * Get a set of elements containing the unique next siblings of each of the\r
@@ -1816,13 +1786,15 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     return replaceWith($(elem));\r
   }\r
 \r
+\r
   /**\r
-   * Bind a function to the scroll event of each matched element.\r
+   * Bind a set of functions to the scroll event of each matched element.\r
+   * Or trigger the event if no functions are provided.\r
    */\r
-  public GQuery scroll(Function f) {\r
-    return bind(Event.ONSCROLL, null, f);\r
-  }\r
-\r
+  public GQuery scroll(Function...f) {\r
+    return bindOrFire(Event.ONSCROLL, null, f);\r
+  }    \r
+  \r
   /**\r
    * When a value is passed in, the scroll left offset is set to that value on\r
    * all matched elements. This method works for both visible and hidden\r
@@ -1886,8 +1858,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   public GQuery select() {\r
-    return trigger(document.createHtmlEvent("select", false, false),\r
-        null);\r
+    return as(Events).triggerHtmlEvent("select");\r
   }\r
 \r
   /**\r
@@ -1973,8 +1944,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   public GQuery submit() {\r
-    return trigger(document.createHtmlEvent("submit", false, false),\r
-        null);\r
+    return as(Events).triggerHtmlEvent("submit");\r
   }\r
 \r
   /**\r
@@ -2071,11 +2041,18 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     return r;\r
   }\r
 \r
+\r
   /**\r
-   * Trigger an event of type eventbits on every matched element.\r
+   * Trigger a set of events on each matched element.\r
+   * \r
+   * For keyboard events you can pass a second parameter which represents \r
+   * the key-code of the pushed key. \r
+   * \r
+   * Example: fire(Event.ONCLICK | Event.ONFOCUS)\r
+   * Example: fire(Event.ONKEYDOWN. 'a');\r
    */\r
   public GQuery trigger(int eventbits, int... keys) {\r
-    return as(Events).fire(eventbits, keys);\r
+    return as(Events).trigger(eventbits, keys);\r
   }\r
 \r
   /**\r
@@ -2619,13 +2596,6 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     }\r
   }\r
 \r
-  private GQuery trigger(NativeEvent event, Object o) {\r
-    for (Element e : elements()) {\r
-      e.dispatchEvent(event);\r
-    }\r
-    return this;\r
-  }\r
-\r
   private static native Element window() /*-{\r
     return $wnd;\r
   }-*/;\r
index 8e8ecbc20548a265d951442e0e8efe67b2fa47b5..d6509b23263612c4d71d8ea17fa7d5adf8ad91dd 100644 (file)
@@ -18,7 +18,8 @@ package com.google.gwt.query.client;
 import static com.google.gwt.query.client.GQuery.$;
 
 import com.google.gwt.dom.client.Element;
-import com.google.gwt.dom.client.InputElement;
+import com.google.gwt.junit.DoNotRunWith;
+import com.google.gwt.junit.Platform;
 import com.google.gwt.junit.client.GWTTestCase;
 import com.google.gwt.user.client.Event;
 import com.google.gwt.user.client.ui.HTML;
@@ -48,7 +49,6 @@ public class GwtEventsTest extends GWTTestCase {
     }
   }
 
-  // FIXME: this test is broken in IE, and in chrome ONKEYPRESS does not work
   public void testEventsPlugin() {
     $(e).html("<p>Content</p>");
 
@@ -57,14 +57,19 @@ public class GwtEventsTest extends GWTTestCase {
       public void f(Element elem) {
         $(elem).css("color", "red");
       }
+    }, new Function() {
+      public void f(Element elem) {
+        $(elem).css("background", "green");
+      }
     });
-    $("p", e, Events.Events).fire(Event.ONCLICK);
+    $("p", e, Events.Events).trigger(Event.ONCLICK);
     assertEquals("red", $("p", e).css("color"));
+    assertEquals("green", $("p", e).css("background-color"));
 
     // unbind
     $("p", e).css("color", "white");
     $("p", e).unbind(Event.ONCLICK);
-    $("p", e).trigger(Event.ONCLICK);
+    $("p", e).click();
     assertEquals("white", $("p", e).css("color"));
     
     // toggle
@@ -78,9 +83,9 @@ public class GwtEventsTest extends GWTTestCase {
         $(elem).css("color", "blue");
       }
     });
-    $("p", e, Events.Events).fire(Event.ONCLICK);
+    $("p", e).click();
     assertEquals("red", $("p", e).css("color"));
-    $("p", e, Events.Events).fire(Event.ONCLICK);
+    $("p", e).click();
     assertEquals("blue", $("p", e).css("color"));
 
     // one
@@ -90,10 +95,10 @@ public class GwtEventsTest extends GWTTestCase {
         $(elem).css("color", "red");
       }
     });
-    $("p", e).trigger(Event.ONCLICK);
+    $("p", e).click();
     assertEquals("red", $("p", e).css("color"));
     $("p", e).css("color", "white");
-    $("p", e).trigger(Event.ONCLICK);
+    $("p", e).click();
     assertEquals("white", $("p", e).css("color"));
 
     // hover (mouseover, mouseout)
@@ -117,32 +122,52 @@ public class GwtEventsTest extends GWTTestCase {
         $(elem).css("border", "1px dotted black");
       }
     });
-    $("p", e).trigger(Event.ONFOCUS);
-    assertEquals("1px dotted black", $("p", e).css("border"));
-
+    $("p", e).focus();
+    assertEquals("black", $("p", e).css("border-top-color"));
+    assertEquals("dotted", $("p", e).css("border-top-style"));
+    assertEquals("1px", $("p", e).css("border-top-width"));
+    
     // blur
     $("p", e).blur(new Function() {
       public void f(Element elem) {
         $(elem).css("border", "");
       }
     });
-    $("p", e).trigger(Event.ONBLUR);
+    $("p", e).blur();
     assertEquals("", $("p", e).css("border"));
 
-    // keypressed
+    // key events
     $(e).html("<input type='text'/>");
-    $("input", e).keypressed(new Function() {
+    Function keyEventAction = new Function() {
       public boolean f(Event evnt) {
-        Element elem = evnt.getCurrentEventTarget().cast();
-        InputElement input = InputElement.as(elem);
-        input.setValue(
-            input.getValue() + Character.toString((char) evnt.getKeyCode()));
+        GQuery gq = $(evnt);
+        gq.val(gq.val() + Character.toString((char) evnt.getKeyCode()));
         return false;
       }
+    }; 
+    $("input", e).keypress(keyEventAction);
+    $("input", e).keydown(keyEventAction);
+    $("input", e).keyup(keyEventAction);
+    $("input", e).focus();
+    $("input", e).keydown('a');
+    $("input", e).keypress('b');
+    $("input", e).keyup('c');
+    assertEquals("abc", $("input", e).val());
+  }
+  
+  /**
+   * TODO: DblClick doesn't work with HtmlUnit, investigate and report.
+   */
+  @DoNotRunWith(Platform.HtmlUnit)
+  public void testEventsDblClick() {
+    $(e).html("<p>Content</p>");
+    $("p", e).css("color", "white");
+    $("p", e).dblclick(new Function() {
+      public void f(Element elem) {
+        $(elem).css("color", "yellow");
+      }
     });
-    $("input", e).trigger(Event.ONFOCUS);
-    $("input", e).trigger(Event.ONKEYPRESS, 'a');
-    assertEquals("a", InputElement.as($("input", e).get(0)).getValue());
+    $("p", e).dblclick();
+    assertEquals("yellow", $("p", e).css("color"));    
   }
-
 }