From 55d2475759d798769a740ba866a9bf3ec4c4ae58 Mon Sep 17 00:00:00 2001 From: jdramaix Date: Thu, 21 Nov 2013 16:16:48 +0100 Subject: [PATCH] add bitless event support + custom event. --- .../com/google/gwt/query/client/Function.java | 8 +- .../com/google/gwt/query/client/GQuery.java | 34 ++- .../google/gwt/query/client/LazyGQuery.java | 36 --- .../gwt/query/client/plugins/Events.java | 99 ++++--- .../gwt/query/client/plugins/LazyEffects.java | 10 +- .../gwt/query/client/plugins/LazyWidgets.java | 18 +- .../client/plugins/events/EventsListener.java | 256 +++++++++++------- .../gwt/query/client/GQueryCoreTestGwt.java | 4 +- .../gwt/query/client/GQueryEventsTestGwt.java | 180 ++++++++++-- 9 files changed, 427 insertions(+), 218 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java index 00b41def..e90ad93e 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Function.java @@ -422,7 +422,7 @@ public abstract class Function { * Override this method for bound event handlers if you wish to deal with * per-handler user data. */ - public boolean f(Event e, Object arg) { + public boolean f(Event e, Object... arg) { setArguments(arg); setEvent(e); return f(e); @@ -527,16 +527,16 @@ public abstract class Function { * catch the exception and send it to the GWT UncaughtExceptionHandler * They are intentionally final to avoid override them */ - public final boolean fe(Event ev, Object arg) { + public final boolean fe(Event ev, Object... args) { if (GWT.getUncaughtExceptionHandler() != null) { try { - return f(ev, arg); + return f(ev, args); } catch (Exception e) { GWT.getUncaughtExceptionHandler().onUncaughtException(e); } return true; } - return f(ev, arg); + return f(ev, args); } /** diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java index 270ad1cc..fac71b39 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java @@ -1308,6 +1308,18 @@ public class GQuery implements Lazy { } } + /** + * Bind Handlers or fire Events for each matched element. + */ + private GQuery bindOrFire(String eventname, final Object data, final Function... funcs) { + if (funcs.length == 0) { + return as(Events).triggerHtmlEvent(eventname); + } else { + return bind(eventname, data, funcs); + } + } + + /** * Bind a set of functions to the blur event of each matched element. Or trigger the blur event if * no functions are provided. @@ -1453,7 +1465,6 @@ public class GQuery implements Lazy { * DOM. This method allows retrieving the list of ancestors matching many selectors by traversing * the DOM only one time. * - * @param selector * @return */ public JsNamedArray> closest(String[] selectors) { @@ -1466,7 +1477,6 @@ public class GQuery implements Lazy { * DOM until reach the context node.. This method allows retrieving the list of * ancestors matching many selectors by traversing the DOM only one time. * - * @param selector * @return */ public JsNamedArray> closest(String[] selectors, Node context) { @@ -3032,7 +3042,6 @@ public class GQuery implements Lazy { * Get all following siblings of each element up to but not including the element matched by the * DOM node, filtered by a selector. * - * @param selector * @return */ public GQuery nextUntil(Element until, String filter) { @@ -3043,7 +3052,6 @@ public class GQuery implements Lazy { * Get all following siblings of each element up to but not including the element matched by the * GQuery object. * - * @param selector * @return */ public GQuery nextUntil(GQuery until) { @@ -3054,7 +3062,6 @@ public class GQuery implements Lazy { * Get all following siblings of each element up to but not including the element matched by the * GQuery object, filtered by a selector * - * @param selector * @return */ public GQuery nextUntil(GQuery until, String filter) { @@ -3904,14 +3911,14 @@ public class GQuery implements Lazy { * */ public GQuery resize(Function... f) { - return bindOrFire(EventsListener.ONRESIZE, null, f); + return bindOrFire("resize", null, f); } /** * Bind an event handler to the "resize" JavaScript event, or trigger that event on an element. */ public GQuery resize(final Function f) { - return bindOrFire(EventsListener.ONRESIZE, null, f); + return bindOrFire("resize", null, f); } /** @@ -4257,7 +4264,7 @@ public class GQuery implements Lazy { * functions are provided. */ public GQuery submit(Function... funcs) { - return bindOrFire(EventsListener.ONSUBMIT, null, funcs); + return bindOrFire("submit", null, funcs); } /** @@ -4394,6 +4401,17 @@ public class GQuery implements Lazy { return as(Events).trigger(eventbits, keys); } + + /** + * Trigger a event in all matched elements. + * + * @param eventName An string representing the type of the event desired + * @param datas Additional parameters to pass along to the event handlers. + */ + public GQuery trigger(String eventName, Object... datas) { + return as(Events).triggerHtmlEvent(eventName, datas); + } + /** * Removes all events that match the eventbits. */ diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java index 14cd6b44..da0de164 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java @@ -485,7 +485,6 @@ public interface LazyGQuery extends LazyBase{ * DOM. This method allows retrieving the list of ancestors matching many selectors by traversing * the DOM only one time. * - * @param selector * @return */ JsNamedArray> closest(String[] selectors); @@ -496,7 +495,6 @@ public interface LazyGQuery extends LazyBase{ * DOM until reach the context node.. This method allows retrieving the list of * ancestors matching many selectors by traversing the DOM only one time. * - * @param selector * @return */ JsNamedArray> closest(String[] selectors, Node context); @@ -1522,7 +1520,6 @@ public interface LazyGQuery extends LazyBase{ * Get all following siblings of each element up to but not including the element matched by the * DOM node, filtered by a selector. * - * @param selector * @return */ LazyGQuery nextUntil(Element until, String filter); @@ -1531,7 +1528,6 @@ public interface LazyGQuery extends LazyBase{ * Get all following siblings of each element up to but not including the element matched by the * GQuery object. * - * @param selector * @return */ LazyGQuery nextUntil(GQuery until); @@ -1540,7 +1536,6 @@ public interface LazyGQuery extends LazyBase{ * Get all following siblings of each element up to but not including the element matched by the * GQuery object, filtered by a selector * - * @param selector * @return */ LazyGQuery nextUntil(GQuery until, String filter); @@ -2217,37 +2212,6 @@ public interface LazyGQuery extends LazyBase{ */ String toString(); - /** - * The transition() method allows you to create animation effects on any numeric HTML Attribute, - * CSS property, or color using CSS3 transformations and transitions. - * - * It works similar to animate(), and support chainning and queueing. - * - * Example: - * $("#foo").transition("{ opacity: 0.1, scale: 2, x: 50, y: 50 }", 5000, EasingCurve.easeInBack); - * - * $("#bar") - * .transition("{ opacity: 0.1, scale: 2, x: 50, y: 50 }", 5000, EasingCurve.easeInBack) - * .transition("{x: +100, width: +40px}", 2000, EasingCurve.easeOut); - * - */ - LazyGQuery transition(Object stringOrProperties, int duration, Easing easing, Function... funcs); - - /** - * The transition() method allows you to create animation effects on any numeric HTML Attribute, - * CSS property, or color using CSS3 transformations and transitions. - * - * It works similar to animate() but has an extra parameter for delaying the animation, so as - * we dont have to use GQuery queue system for delaying executions, nor callbacks for firing more - * animations - * - * Example animate an element within 2 seconds: - * $("#foo") - * .transition("{ opacity: 0.1, scale: 2, x: 50, y: 50 }", 5000, 2000, EasingCurve.easeInBack); - * - */ - LazyGQuery transition(Object stringOrProperties, int duration, int delay, Easing easing, Function... funcs); - /** * Produces a string representation of the matched elements. */ 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 f9ee5519..79b1d12c 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 @@ -14,6 +14,7 @@ package com.google.gwt.query.client.plugins; import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.FormElement; import com.google.gwt.dom.client.NativeEvent; import com.google.gwt.dom.client.Node; import com.google.gwt.query.client.Function; @@ -109,7 +110,7 @@ public class Events extends GQuery { public GQuery die(int eventbits, String nameSpace) { EventsListener.getInstance(Element.is(currentContext) ? (Element) currentContext : body).die( - eventbits, nameSpace, null, currentSelector); + eventbits, nameSpace, null, null, currentSelector); return this; } @@ -136,7 +137,7 @@ public class Events extends GQuery { public GQuery live(int eventbits, String nameSpace, final Object data, Function... funcs) { EventsListener.getInstance(Element.is(currentContext) ? (Element) currentContext : body).live( - eventbits, nameSpace, null, currentSelector, data, funcs); + eventbits, nameSpace, null, null, currentSelector, data, funcs); return this; } @@ -166,8 +167,6 @@ public class Events extends GQuery { return bind("mouseenter", null, f); } - // TODO handle unbind !! - /** * Bind an event handler to be fired when the mouse leaves an element, or trigger that handler on * an element if no functions are provided. @@ -219,55 +218,45 @@ public class Events extends GQuery { */ public Events trigger(int eventbits, int... keys) { if ((eventbits | Event.ONBLUR) == Event.ONBLUR) - dispatchEvent(document.createBlurEvent()); + dispatchEvent(document.createBlurEvent(), null); if ((eventbits | Event.ONCHANGE) == Event.ONCHANGE) - dispatchEvent(document.createChangeEvent()); + dispatchEvent(document.createChangeEvent(), null); if ((eventbits | Event.ONCLICK) == Event.ONCLICK) - dispatchEvent(document.createClickEvent(0, 0, 0, 0, 0, false, false, false, false)); + dispatchEvent(document.createClickEvent(0, 0, 0, 0, 0, false, false, false, false), null); if ((eventbits | Event.ONDBLCLICK) == Event.ONDBLCLICK) - dispatchEvent(document.createDblClickEvent(0, 0, 0, 0, 0, false, false, false, false)); + dispatchEvent(document.createDblClickEvent(0, 0, 0, 0, 0, false, false, false, false), null); if ((eventbits | Event.ONFOCUS) == Event.ONFOCUS) - dispatchEvent(document.createFocusEvent()); + dispatchEvent(document.createFocusEvent(), null); if ((eventbits | Event.ONKEYDOWN) == Event.ONKEYDOWN) - dispatchEvent(document.createKeyDownEvent(false, false, false, false, keys[0])); + dispatchEvent(document.createKeyDownEvent(false, false, false, false, keys[0]), null); if ((eventbits | Event.ONKEYPRESS) == Event.ONKEYPRESS) - dispatchEvent(document.createKeyPressEvent(false, false, false, false, keys[0], 0)); + dispatchEvent(document.createKeyPressEvent(false, false, false, false, keys[0], 0), null); if ((eventbits | Event.ONKEYUP) == Event.ONKEYUP) - dispatchEvent(document.createKeyUpEvent(false, false, false, false, keys[0])); + dispatchEvent(document.createKeyUpEvent(false, false, false, false, keys[0]), null); 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)); + NativeEvent.BUTTON_LEFT), null); if ((eventbits | Event.ONMOUSEMOVE) == Event.ONMOUSEMOVE) dispatchEvent(document.createMouseMoveEvent(0, 0, 0, 0, 0, false, false, false, false, - NativeEvent.BUTTON_LEFT)); + NativeEvent.BUTTON_LEFT), null); if ((eventbits | Event.ONMOUSEOUT) == Event.ONMOUSEOUT) dispatchEvent(document.createMouseOutEvent(0, 0, 0, 0, 0, false, false, false, false, - NativeEvent.BUTTON_LEFT, null)); + NativeEvent.BUTTON_LEFT, null), null); if ((eventbits | Event.ONMOUSEOVER) == Event.ONMOUSEOVER) dispatchEvent(document.createMouseOverEvent(0, 0, 0, 0, 0, false, false, false, false, - NativeEvent.BUTTON_LEFT, null)); + NativeEvent.BUTTON_LEFT, null), null); if ((eventbits | Event.ONMOUSEUP) == Event.ONMOUSEUP) dispatchEvent(document.createMouseUpEvent(0, 0, 0, 0, 0, false, false, false, false, - NativeEvent.BUTTON_LEFT)); + NativeEvent.BUTTON_LEFT), null); if ((eventbits | Event.ONSCROLL) == Event.ONSCROLL) - dispatchEvent(document.createScrollEvent()); + dispatchEvent(document.createScrollEvent(), null); if ((eventbits | Event.ONERROR) == Event.ONERROR) - dispatchEvent(document.createErrorEvent()); + dispatchEvent(document.createErrorEvent(), null); 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)); - if (eventbits == EventsListener.ONSUBMIT) { - Event evt = document.createHtmlEvent("submit", true, true).cast(); - dispatchEvent(evt, new Function() { - public native void f(Element e) /*-{ - e.submit(); - }-*/; - }); - } - if (eventbits == EventsListener.ONRESIZE) - triggerHtmlEvent("resize"); + false, false, false, NativeEvent.BUTTON_LEFT, null), null); return this; } @@ -277,7 +266,17 @@ public class Events extends GQuery { * @param htmlEvent An string representing the html event desired * @functions a set of function to run if the event is not canceled. */ - public Events triggerHtmlEvent(String htmlEvent, Function... functions) { + public Events triggerHtmlEvent(String htmlEvent, final Function... functions) { + return triggerHtmlEvent(htmlEvent, functions, null); + } + + /** + * Trigger a html event in all matched elements. + * + * @param htmlEvent An string representing the html event desired + * @functions a set of function to run if the event is not canceled. + */ + public Events triggerHtmlEvent(String htmlEvent, Object[] datas, final Function... functions) { SpecialEvent specialEvent = EventsListener.special.get(htmlEvent); boolean isSpecialEvent = specialEvent != null; @@ -289,7 +288,22 @@ public class Events extends GQuery { if (isSpecialEvent) { GqEvent.setOriginalEventType(e, originalEventName); } - dispatchEvent(e, functions); + + if ("submit".equals(htmlEvent)){ + Function submitFonction = new Function() { + @Override + public void f(Element e) { + // first submit the form then call the others functions + if (FormElement.is(e)) { + e.cast().submit(); + } + callHandlers(e, getEvent(), functions); + } + }; + dispatchEvent(e, datas, submitFonction); + } else { + dispatchEvent(e, datas, functions); + } return this; } @@ -315,7 +329,7 @@ public class Events extends GQuery { public Events unbind(int eventbits, String name, Function f) { for (Element e : elements()) { if (isEventCapable(e)) { - EventsListener.getInstance(e).unbind(eventbits, name, null, f); + EventsListener.getInstance(e).unbind(eventbits, name, null, null, f); } } return this; @@ -356,18 +370,27 @@ public class Events extends GQuery { return this; } - private void dispatchEvent(NativeEvent evt, Function... funcs) { + private void dispatchEvent(NativeEvent evt, Object[] datas, Function... funcs) { for (Element e : elements()) { if (isEventCapable(e)) { + $(e).data("___event_datas", datas); e.dispatchEvent(evt); if (!JsUtils.isDefaultPrevented(evt)) { - for (Function f : funcs) { - f.setEvent(Event.as(evt)); - f.f(e); - } + callHandlers(e, evt, funcs); } + $(e).removeData("___event_datas"); } } } + private void callHandlers(Element e, NativeEvent evt, Function... functions){ + if (functions == null) { + return; + } + for (Function f : functions) { + f.setEvent(Event.as(evt)); + f.f(e); + } + } + } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEffects.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEffects.java index cdcc61d2..a07112df 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEffects.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEffects.java @@ -26,6 +26,8 @@ import com.google.gwt.query.client.plugins.effects.Fx; import com.google.gwt.query.client.plugins.effects.PropertiesAnimation; import com.google.gwt.query.client.plugins.effects.PropertiesAnimation.Easing; import com.google.gwt.query.client.plugins.effects.PropertiesAnimation.EasingCurve; +import com.google.gwt.query.client.plugins.effects.TransitionsAnimation; +import com.google.gwt.query.client.plugins.effects.TransitionsAnimation.TransitionsClipAnimation; import com.google.gwt.query.client.LazyBase; public interface LazyEffects extends LazyBase{ @@ -414,14 +416,6 @@ public interface LazyEffects extends LazyBase{ */ LazyEffects slideUp(int millisecs, Function... f); - /** - * Toggle displaying each of the set of matched elements. - * - * @param showOrHide A Boolean indicating whether to show or hide the - * elements. - */ - LazyEffects toggle(boolean showOrHide); - /** * Toggle displaying each of the set of matched elements by animating the * width, height, and opacity of the matched elements simultaneously. When diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyWidgets.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyWidgets.java index eb2baf68..5fe7de8b 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyWidgets.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyWidgets.java @@ -45,10 +45,7 @@ public interface LazyWidgets extends LazyBase{ LazyWidgets widgets(WidgetFactory factory, WidgetInitializer initializers); /** - * Create a {@link Button} widget for each selected element. The - * initializers will be called on each new {@link Button} created - * by passing them in parameter. - * + * Create a {@link Button} widget for each selected element. */ LazyWidgets button(); @@ -60,8 +57,21 @@ public interface LazyWidgets extends LazyBase{ */ LazyWidgets button(WidgetInitializer