diff options
3 files changed, 70 insertions, 39 deletions
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 ec453c4d..e5e86de4 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 @@ -15,6 +15,11 @@ package com.google.gwt.query.client; import static com.google.gwt.query.client.plugins.QueuePlugin.Queue;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
@@ -61,11 +66,6 @@ import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.GqUi;
import com.google.gwt.user.client.ui.Widget;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-
/**
* GwtQuery is a GWT clone of the popular jQuery library.
*/
@@ -213,14 +213,71 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { }
/**
- * Wrap a GQuery around an existing element, event, node or nodelist.
+ * Wrap a GQuery around an existing javascript element, event, node, nodelist, function or array.
*/
- public static GQuery $(JavaScriptObject e) {
- return JsUtils.isWindow(e) ? GQuery.$(e.<Element> cast()) : JsUtils.isElement(e) ? GQuery.$(e
- .<Element> cast()) : JsUtils.isEvent(e) ? GQuery.$(e.<Event> cast()) : JsUtils
- .isNodeList(e) ? GQuery.$(e.<NodeList<Element>> cast()) : $();
- }
+ public static GQuery $(JavaScriptObject jso) {
+ if (jso == null) {
+ return $();
+ }
+ // Execute a native javascript function like jquery does
+ if (JsUtils.isFunction(jso)) {
+ new JsUtils.JsFunction(jso).fe();
+ return $();
+ }
+ // Wraps a native array like jquery does
+ if (!JsUtils.isWindow(jso) && !JsUtils.isElement(jso) && JsUtils.isArray(jso)) {
+ JsArrayMixed c = jso.cast();
+ JsNodeArray elms = JsNodeArray.create();
+ for (int i = 0; i < c.length(); i++) {
+ Object obj = c.getObject(i);
+ if (obj instanceof Node) {
+ elms.addNode((Node) obj);
+ }
+ }
+ return $((NodeList<Element>)elms);
+ }
+ return JsUtils.isWindow(jso) ? $(jso.<Element> cast()) :
+ JsUtils.isElement(jso) ? $(jso.<Element> cast()) :
+ JsUtils.isEvent(jso) ? $(jso.<Event> cast()) :
+ JsUtils.isNodeList(jso) ? $(jso.<NodeList<Element>> cast()) : $();
+ }
+
+ /**
+ * Wrap a GQuery around any object, supported objects are:
+ * String, GQuery, Function, Widget, JavaScriptObject
+ *
+ * In the case of string, we accept a CSS selector which is then used to match a set of
+ * elements, or a raw HTML to create a GQuery element containing those elements. Xpath
+ * selector is supported in browsers with native xpath engine.
+ *
+ * In the case of a JavaScriptObject we handle:
+ * Element, Event, Node, Nodelist and native functions or arrays.
+ *
+ * If the case of a native function, we execute it and return empty.
+ */
+ public static GQuery $(Object o) {
+ if (o != null) {
+ if (o instanceof String) {
+ return $((String)o);
+ }
+ if (o instanceof GQuery) {
+ return $((GQuery)o);
+ }
+ if (o instanceof Function) {
+ return $((Function)o);
+ }
+ if (o instanceof JavaScriptObject) {
+ return $((JavaScriptObject)o);
+ }
+ if (o instanceof Widget) {
+ return $(new Widget[]{(Widget)o});
+ }
+ System.err.println("GQuery.$(Object o) could not wrap the type : " + o.getClass());
+ }
+ return $();
+ }
+
/**
* Create a new GQuery given a list of nodes, elements or widgets
*/
diff --git a/jsquery/src/main/java/com/google/gwt/query/jsquery/client/GQueryOverlay.java b/jsquery/src/main/java/com/google/gwt/query/jsquery/client/GQueryOverlay.java index e407a89e..a48266c5 100644 --- a/jsquery/src/main/java/com/google/gwt/query/jsquery/client/GQueryOverlay.java +++ b/jsquery/src/main/java/com/google/gwt/query/jsquery/client/GQueryOverlay.java @@ -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.$") diff --git a/jsquery/src/main/java/com/google/gwt/query/jsquery/client/JsQueryUtils.java b/jsquery/src/main/java/com/google/gwt/query/jsquery/client/JsQueryUtils.java index d5ca8bbc..b84d86b4 100644 --- a/jsquery/src/main/java/com/google/gwt/query/jsquery/client/JsQueryUtils.java +++ b/jsquery/src/main/java/com/google/gwt/query/jsquery/client/JsQueryUtils.java @@ -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); |