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;
}
/**
- * 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) {
}
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
+}
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 {
elem.__gqueryevent = gqevent;
}-*/;
- private JsObjectArray<EventsListener.BindFunction> elementEvents
- = JsObjectArray.createArray().cast();
+ private JsObjectArray<EventsListener.BindFunction> elementEvents = JsObjectArray
+ .createArray().cast();
private Element element;
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) {
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);
}
}
}
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
* 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
}\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
}\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
}\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
}\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
}\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
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
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
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
}\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
}\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
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
}\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
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;
}
}
- // FIXME: this test is broken in IE, and in chrome ONKEYPRESS does not work
public void testEventsPlugin() {
$(e).html("<p>Content</p>");
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
$(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
$(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)
$(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"));
}
-
}