diff options
author | Manolo Carrasco <manolo@apache.org> | 2014-12-21 12:20:01 +0100 |
---|---|---|
committer | Manolo Carrasco <manolo@apache.org> | 2014-12-21 12:20:01 +0100 |
commit | 38513d2716258a80605938ea165d4664bf03966b (patch) | |
tree | cd9ef9fe04708c5c2fefd9ee64b93efdd7ea5949 | |
parent | a8511be680e954b370123bd4710dad29e220348d (diff) | |
download | gwtquery-38513d2716258a80605938ea165d4664bf03966b.tar.gz gwtquery-38513d2716258a80605938ea165d4664bf03966b.zip |
re-factor SpecialEffects in order to make easier new specials.
- Implementing specials focusin focusout. Fixes #11
-rw-r--r-- | gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java | 105 | ||||
-rw-r--r-- | gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTestGwt.java | 23 |
2 files changed, 84 insertions, 44 deletions
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 a7a65b43..923d6b57 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 @@ -74,41 +74,12 @@ public class EventsListener implements EventListener { boolean hasHandlers(EventsListener l); } - /** - * Used for simulating mouseenter and mouseleave events - */ - public static class MouseSpecialEvent implements SpecialEvent { - private String type; - private String delegateType; - - HashMap<EventListener, MouseSpecialFunction> registeredHandlers = new HashMap<EventListener, MouseSpecialFunction>(); - - private class MouseSpecialFunction extends Function { - final EventsListener listener; - public MouseSpecialFunction(EventsListener l) { - listener = l; - } - - public boolean f(Event e, Object... arg) { - EventTarget eventTarget = e.getCurrentEventTarget(); - Element target = eventTarget != null ? eventTarget.<Element> cast() : null; - - EventTarget relatedEventTarget = e.getRelatedEventTarget(); - Element related = relatedEventTarget != null ? relatedEventTarget.<Element> cast() : null; + public static abstract class AbstractSpecialEvent implements SpecialEvent { + protected String type; + protected String delegateType; + protected Function handler = null; - if (related == null || (related != target && !GQuery.contains(target, related))) { - for (int i = 0, l = listener.elementEvents.length(); i < l ; i ++) { - BindFunction function = listener.elementEvents.get(i); - if (function.isTypeOf(type) && !function.fire(e, arg)) { - return false; - } - } - } - return true; - }; - } - - public MouseSpecialEvent(String type, String delegateType) { + public AbstractSpecialEvent(String type, String delegateType) { this.type = type; this.delegateType = delegateType; } @@ -125,18 +96,13 @@ public class EventsListener implements EventListener { @Override public boolean setup(EventsListener l) { - MouseSpecialFunction handler = new MouseSpecialFunction(l); - registeredHandlers.put(l, handler); l.bind(Event.getTypeInt(delegateType), null, delegateType, null, handler, -1); return false; } @Override public boolean tearDown(EventsListener l) { - MouseSpecialFunction handler = registeredHandlers.remove(l); - if (handler != null) { - l.unbind(Event.getTypeInt(delegateType), null, delegateType, handler); - } + l.unbind(Event.getTypeInt(delegateType), null, delegateType, handler); return false; } @@ -147,6 +113,45 @@ public class EventsListener implements EventListener { } /** + * Used for simulating mouseenter and mouseleave events + */ + public static class MouseSpecialEvent extends AbstractSpecialEvent { + public MouseSpecialEvent(final String type, String delegateType) { + super(type, delegateType); + handler = new Function() { + public boolean f(Event e, Object... arg) { + EventTarget eventTarget = e.getCurrentEventTarget(); + Element target = eventTarget != null ? eventTarget.<Element> cast() : null; + + EventTarget relatedEventTarget = e.getRelatedEventTarget(); + Element related = relatedEventTarget != null ? relatedEventTarget.<Element> cast() : null; + + if (related == null || (related != target && !GQuery.contains(target, related))) { + getInstance(target).dispatchEvent(e, type); + } + return true; + }; + }; + } + } + + /** + * Used for simulating mouseenter and mouseleave events + */ + public static class FocusSpecialEvent extends AbstractSpecialEvent { + public FocusSpecialEvent(final String type, String delegateType) { + super(type, delegateType); + handler = new Function() { + public boolean f(Event e, Object... arg) { + setEvent(e); + getInstance(getElement()).dispatchEvent(e, type); + return true; + }; + }; + } + } + + /** * Utility class to split a list of events with or without namespaces */ private static class EvPart { @@ -427,6 +432,8 @@ public class EventsListener implements EventListener { public static String MOUSEENTER = "mouseenter"; public static String MOUSELEAVE = "mouseleave"; + public static String FOCUSIN = "focusin"; + public static String FOCUSOUT = "focusout"; public static HashMap<String, SpecialEvent> special; @@ -434,6 +441,8 @@ public class EventsListener implements EventListener { special = new HashMap<String, SpecialEvent>(); special.put(MOUSEENTER, new MouseSpecialEvent(MOUSEENTER, "mouseover")); special.put(MOUSELEAVE, new MouseSpecialEvent(MOUSELEAVE, "mouseout")); + special.put(FOCUSIN, new MouseSpecialEvent(FOCUSIN, "focus")); + special.put(FOCUSOUT, new MouseSpecialEvent(FOCUSOUT, "blur")); } public static void clean(Element e) { @@ -614,13 +623,23 @@ public class EventsListener implements EventListener { } } + /** + * Dispatch an event in this element. + */ public void dispatchEvent(Event event) { - String ename = event.getType(); - int etype = Event.getTypeInt(ename); + dispatchEvent(event, event.getType()); + } + + /** + * Dispatch an event in this element but changing the type, + * it's useful for special events. + */ + public void dispatchEvent(Event event, String eventName) { + int etype = Event.getTypeInt(eventName); Object[] handlerData = $(element).data(EVENT_DATA); for (int i = 0, l = elementEvents.length(); i < l; i++) { BindFunction listener = elementEvents.get(i); - if (listener != null && (listener.hasEventType(etype) || listener.isTypeOf(ename))) { + if (listener != null && (listener.hasEventType(etype) || listener.isTypeOf(eventName))) { if (!listener.fire(event, handlerData)) { event.stopPropagation(); event.preventDefault(); 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 6ec17595..08777f32 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 @@ -43,7 +43,6 @@ import com.google.gwt.query.client.plugins.Events; import com.google.gwt.query.client.plugins.events.EventsListener; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.Timer; -import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.RootPanel; @@ -456,6 +455,28 @@ public class GQueryEventsTestGwt extends GWTTestCase { assertEquals("", $("p", e).css("border", false)); } + @DoNotRunWith({Platform.HtmlUnitLayout}) + public void testSpecialFocusInOut() { + $(e).html("<p>Content</p>"); + $("p", e).on(EventsListener.FOCUSIN, new Function() { + public void f(Element elem) { + GQuery.console.log("focus"); + $(elem).css("background-color", "red"); + } + }); + $("p", e).focus(); + assertEquals("red", $("p", e).css("background-color", false)); + + // blur + $("p", e).on(EventsListener.FOCUSOUT, new Function() { + public void f(Element elem) { + $(elem).css("background-color", "white"); + } + }); + $("p", e).blur(); + assertEquals("white", $("p", e).css("background-color", false)); + } + public void testLazyMethods() { $(e).css(CSS.COLOR.with(RGBColor.WHITE)); assertEquals("white", $(e).css("color", false)); |