From 3174d5ba98027c8d106d252747f5c1e1205f8fe2 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Tue, 13 Jan 2015 08:14:49 +0100 Subject: [PATCH] Remove most of $ constructors in favour of $(Object) to avoid java8 disambiguation. Fixes issue 335 --- .../com/google/gwt/query/client/GQuery.java | 107 ++++++------------ .../google/gwt/query/client/js/JsUtils.java | 2 +- 2 files changed, 33 insertions(+), 76 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 92641bf8..367d956a 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 @@ -206,59 +206,6 @@ public class GQuery implements Lazy { return new GQuery(JsNodeArray.create()); } - /** - * Wrap a GQuery around an existing element. - */ - public static GQuery $(Element element) { - return new GQuery(element); - } - - /** - * Wrap a GQuery around an event's target element. - */ - public static GQuery $(Event event) { - return event == null ? $() : $((Element) event.getCurrentEventTarget().cast()); - } - - /** - * Wrap a GQuery around the element of a Function callback. - */ - public static GQuery $(Function f) { - return $(f.getElement()); - } - - /** - * Wrap a GQuery around an existing javascript element, event, node, nodelist, function or array. - */ - 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 $(elms); - } - - return JsUtils.isWindow(jso) ? $(jso. cast()) : - JsUtils.isElement(jso) ? $(jso. cast()) : - JsUtils.isEvent(jso) ? $(jso. cast()) : - JsUtils.isNodeList(jso) ? $(jso.> cast()) : - $(jso. cast()); - } - /** * Wrap a GQuery around any object, supported objects are: * String, GQuery, Function, Widget, JavaScriptObject. @@ -268,7 +215,7 @@ public class GQuery implements Lazy { * 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. + * Element, Event, Node, Nodelist, Function, and native functions or arrays. * * If the case of a native function, we execute it and return empty. */ @@ -281,20 +228,44 @@ public class GQuery implements Lazy { return (GQuery) o; } if (o instanceof Function) { - return $((Function) o); - } - if (JsUtils.isElement(o)) { - return $(JsUtils. cast(o)); + return new GQuery(((Function) o).getElement()); } if (o instanceof JsonBuilder) { return new GQuery(((JsonBuilder) o).getDataImpl()); } - if (o instanceof JavaScriptObject) { - return $((JavaScriptObject) o); - } if (o instanceof IsWidget) { return $(Arrays.asList(o)); } + if (o instanceof JavaScriptObject) { + JavaScriptObject jso = (JavaScriptObject) o; + // 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 new GQuery(elms); + } + // Wraps a native NodeList + if (JsUtils.isNodeList(jso)) { + return new GQuery(jso.> cast()); + } + // Wraps an event + if (JsUtils.isEvent(jso)) { + jso = jso.cast().getCurrentEventTarget(); + } + // Otherwise we wrap it as an element + return new GQuery(jso. cast()); + } console .log("Error: GQuery.$(Object o) could not wrap the type : ", o.getClass().getName(), o); } @@ -319,20 +290,6 @@ public class GQuery implements Lazy { return new GQuery(elms); } - /** - * Wrap a GQuery around an existing node. - */ - public static GQuery $(Node n) { - return $((Element) n); - } - - /** - * Wrap a GQuery around existing Elements. - */ - public static GQuery $(NodeList elms) { - return new GQuery(elms); - } - /** * This function accepts a string containing a CSS selector which is then used to match a set of * elements, or it accepts raw HTML creating a GQuery element containing those elements. Xpath diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java index 1012a8dc..e4acf8ee 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java @@ -410,7 +410,7 @@ public class JsUtils { public static native boolean isNodeList(JavaScriptObject o) /*-{ var r = Object.prototype.toString.call(o); return r == '[object HTMLCollection]' || r == '[object NodeList]' - || (typeof o == 'object' && o.length && o[0].tagName) ? true : false; + || (typeof o == 'object' && o.length && o[0] && o[0].tagName) ? true : false; }-*/; /** -- 2.39.5