]> source.dussan.org Git - gwtquery.git/commitdiff
re-factor SpecialEffects in order to make easier new specials.
authorManolo Carrasco <manolo@apache.org>
Sun, 21 Dec 2014 11:20:01 +0000 (12:20 +0100)
committerManolo Carrasco <manolo@apache.org>
Sun, 21 Dec 2014 11:20:01 +0000 (12:20 +0100)
- Implementing specials focusin focusout. Fixes #11

gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java
gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryEventsTestGwt.java

index a7a65b43a0475a0e99cba4ff30473e4be42a6291..923d6b57905747fec92748343e5cc70e4f28288e 100644 (file)
@@ -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;
     }
 
@@ -146,6 +112,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
    */
@@ -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();
index 6ec17595b5d793475978d47e2342e5d3494b0472..08777f32bd6e9d6721fcbbf71c64dcd2c6a5c105 100644 (file)
@@ -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));