]> source.dussan.org Git - gwtquery.git/commitdiff
Adding a dollar method to handle any object and route it to the apropriate constructo...
authorManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>
Wed, 7 Nov 2012 18:11:44 +0000 (19:11 +0100)
committerManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>
Wed, 7 Nov 2012 18:11:44 +0000 (19:11 +0100)
gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java
jsquery/src/main/java/com/google/gwt/query/jsquery/client/GQueryOverlay.java
jsquery/src/main/java/com/google/gwt/query/jsquery/client/JsQueryUtils.java

index ec453c4d280e54f83e240a389d95c052eed4bfa8..e5e86de4de2c91b4266407c04612e72e302cbc68 100644 (file)
@@ -15,6 +15,11 @@ package com.google.gwt.query.client;
 \r
 import static com.google.gwt.query.client.plugins.QueuePlugin.Queue;\r
 \r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
 import com.google.gwt.core.client.GWT;\r
 import com.google.gwt.core.client.JavaScriptObject;\r
 import com.google.gwt.core.client.JsArray;\r
@@ -61,11 +66,6 @@ import com.google.gwt.user.client.Window;
 import com.google.gwt.user.client.ui.GqUi;\r
 import com.google.gwt.user.client.ui.Widget;\r
 \r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
 /**\r
  * GwtQuery is a GWT clone of the popular jQuery library.\r
  */\r
@@ -213,14 +213,71 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   /**\r
-   * Wrap a GQuery around an existing element, event, node or nodelist.\r
+   * Wrap a GQuery around an existing javascript element, event, node, nodelist, function or array.\r
    */\r
-  public static GQuery $(JavaScriptObject e) {\r
-    return JsUtils.isWindow(e) ? GQuery.$(e.<Element> cast()) : JsUtils.isElement(e) ? GQuery.$(e\r
-        .<Element> cast()) : JsUtils.isEvent(e) ? GQuery.$(e.<Event> cast()) : JsUtils\r
-        .isNodeList(e) ? GQuery.$(e.<NodeList<Element>> cast()) : $();\r
-  }\r
+  public static GQuery $(JavaScriptObject jso) {\r
+    if (jso == null) {\r
+      return $();\r
+    }\r
+    // Execute a native javascript function like jquery does\r
+    if (JsUtils.isFunction(jso)) {\r
+      new JsUtils.JsFunction(jso).fe();\r
+      return $();\r
+    }\r
+    // Wraps a native array like jquery does \r
+    if (!JsUtils.isWindow(jso) && !JsUtils.isElement(jso) && JsUtils.isArray(jso)) {\r
+      JsArrayMixed c = jso.cast();\r
+      JsNodeArray elms = JsNodeArray.create();\r
+      for (int i = 0; i < c.length(); i++) {\r
+        Object obj = c.getObject(i);\r
+        if (obj instanceof Node) {\r
+          elms.addNode((Node) obj);\r
+        }\r
+      }\r
+      return $((NodeList<Element>)elms);\r
+    }\r
 \r
+    return JsUtils.isWindow(jso) ? $(jso.<Element> cast()) : \r
+      JsUtils.isElement(jso) ? $(jso.<Element> cast()) : \r
+      JsUtils.isEvent(jso) ? $(jso.<Event> cast()) : \r
+      JsUtils.isNodeList(jso) ? $(jso.<NodeList<Element>> cast()) : $();\r
+  }\r
+  \r
+  /**\r
+   * Wrap a GQuery around any object, supported objects are:\r
+   *   String, GQuery, Function, Widget, JavaScriptObject\r
+   *   \r
+   * In the case of string, we accept a CSS selector which is then used to match a set of\r
+   * elements, or a raw HTML to create a GQuery element containing those elements. Xpath\r
+   * selector is supported in browsers with native xpath engine.\r
+   *   \r
+   * In the case of a JavaScriptObject we handle:  \r
+   *   Element, Event, Node, Nodelist and native functions or arrays.\r
+   *   \r
+   * If the case of a native function, we execute it and return empty.   \r
+   */\r
+  public static GQuery $(Object o) {\r
+    if (o != null) {\r
+      if (o instanceof String) {\r
+        return $((String)o);\r
+      }\r
+      if (o instanceof GQuery) {\r
+        return $((GQuery)o);\r
+      }\r
+      if (o instanceof Function) {\r
+        return $((Function)o);\r
+      }\r
+      if (o instanceof JavaScriptObject) {\r
+        return $((JavaScriptObject)o);\r
+      }\r
+      if (o instanceof Widget) {\r
+        return $(new Widget[]{(Widget)o});\r
+      }\r
+      System.err.println("GQuery.$(Object o) could not wrap the type : " + o.getClass());\r
+    }\r
+    return $();\r
+  }\r
+  \r
   /**\r
    * Create a new GQuery given a list of nodes, elements or widgets\r
    */\r
index e407a89e0ce7be52df008423e9965cc6561ce1c7..a48266c58e9dca19ef999dd75a0b4cae28dba074 100644 (file)
@@ -77,7 +77,7 @@ public class GQueryOverlay implements ExportOverlay<GQuery> {
   
   @ExportStaticMethod("$wnd.$")
   public static GQuery $(Object o) {
-    return JsQueryUtils.dollar(o);
+    return GQuery.$(o);
   }
 
   @ExportStaticMethod("$wnd.$")
index d5ca8bbc5e70550c0cad977cee1fa3cc836ea96f..b84d86b4a3074eeb1d8ad8b74698f307c2cf8dcc 100644 (file)
@@ -28,32 +28,6 @@ public abstract class JsQueryUtils {
                return s;
   }-*/;
 
-  public static GQuery dollar(Object o) {
-    if (o instanceof String) {
-      return GQuery.$((String) o);
-    } else if (o instanceof JavaScriptObject) {
-      JavaScriptObject jso = (JavaScriptObject) o;
-      if (JsUtils.isFunction(jso)) {
-        new JsUtils.JsFunction(jso).fe();
-      } else {
-        GQuery r = GQuery.$(jso);
-        if (!JsUtils.isWindow(jso) && !JsUtils.isElement(jso) && JsUtils.isArray(jso)) {
-          JsCache c = jso.cast();
-          JsNodeArray elms = JsNodeArray.create();
-          for (int i = 0; i < c.length(); i++) {
-            Object obj = c.get(i);
-            if (obj instanceof Node) {
-              elms.addNode((Node) obj);
-            }
-          }
-          r = GQuery.$(elms);
-        }
-        return r;
-      }
-    }
-    return GQuery.$();
-  }
-
   public static GQuery dollar(String s, Element ctx) {
     return GQuery.$(s, ctx);
   }
@@ -67,7 +41,7 @@ public abstract class JsQueryUtils {
        return ((List<?>)array).indexOf(object);
     } else if (object instanceof JavaScriptObject
         && JsUtils.isElement((JavaScriptObject) object)) {
-      return dollar(array).index((Element) object);
+      return GQuery.$(array).index((Element) object);
     } else if (array instanceof JavaScriptObject
         && JsUtils.isArray((JavaScriptObject) array)) {
       return ((JsCache) array).indexOf(object);