diff options
author | Manuel Carrasco Moñino <manuel.carrasco.m@gmail.com> | 2013-02-08 11:26:37 +0100 |
---|---|---|
committer | Manuel Carrasco Moñino <manuel.carrasco.m@gmail.com> | 2013-02-08 11:26:37 +0100 |
commit | 10063828575e9af82e19991f355ba554ce914e16 (patch) | |
tree | 95b04a5e776b5866f41dcace75034717bd5896de /gwtquery-core | |
parent | a4da20b42d64e0c3f6f65b17979d4b0791f3034c (diff) | |
download | gwtquery-10063828575e9af82e19991f355ba554ce914e16.tar.gz gwtquery-10063828575e9af82e19991f355ba554ce914e16.zip |
Adding a utility function to call external functions from gwt without writing jsni
Diffstat (limited to 'gwtquery-core')
3 files changed, 50 insertions, 5 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsObjectArray.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsObjectArray.java index 1e8cf6a6..9eac0d79 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsObjectArray.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsObjectArray.java @@ -33,14 +33,22 @@ public final class JsObjectArray<T> extends JavaScriptObject { return cast(); } - public void add(T...vals) { + public JsObjectArray<T> add(T...vals) { for (T t: vals) { - c().put(length(), t); + if (t instanceof Number) { + c().putNumber(length(), (((Number)t).doubleValue())); + } else if (t instanceof Boolean) { + c().putBoolean(length(), ((Boolean)t).booleanValue()); + } else { + c().put(length(), t); + } } + return this; } - public void add(int i, T val) { + public JsObjectArray<T> add(int i, T val) { c().put(i, val); + return this; } @SuppressWarnings("unchecked") 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 b24fe2eb..974462db 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 @@ -18,6 +18,7 @@ package com.google.gwt.query.client.js; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArray; +import com.google.gwt.core.client.JsArrayMixed; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.Node; @@ -316,8 +317,7 @@ public class JsUtils { * Check is a javascript object is a function */ public static native boolean isFunction(JavaScriptObject o) /*-{ - var r = Object.prototype.toString.call(o); - return r == '[object Function]'; + return Object.prototype.toString.call(o) == '[object Function]'; }-*/; /** @@ -399,6 +399,33 @@ public class JsUtils { public static String text(Element e) { return utilsImpl.text(e); } + + /** + * Call via jsni any arbitrary function present in a Javascript object. + * + * It's thought for avoiding to create jsni methods to call external functions and + * facilitate the writing of js wrappers. + * + * Example + * <pre> + * // Create a svg node in our document. + * Element svg = runJavascriptFunction(document, "createElementNS", "http://www.w3.org/2000/svg", "svg"); + * // Append it to the dom + * $(svg).appendTo(document); + * </pre> + * + * @param o the javascript object where the function is. + * @param meth the literal name of the function to call. + * @param args an array with the arguments to pass to the function. + * @return the javascript object returned by the jsni method or null. + */ + public static <T> T runJavascriptFunction(JavaScriptObject o, String meth, Object... args) { + return runJavascriptFunctionImpl(o, meth, JsObjectArray.create().add(args).<JsArrayMixed>cast()); + } + + private static native <T> T runJavascriptFunctionImpl(JavaScriptObject o, String meth, JsArrayMixed args) /*-{ + return (f = o && o[meth]) && @com.google.gwt.query.client.js.JsUtils::isFunction(*)(f) && f.apply(o, args) || null; + }-*/; /** * Check if a number is true in the javascript scope. diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java index 1180f5cc..eb61169f 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java @@ -28,6 +28,7 @@ import java.util.List; import com.google.gwt.query.client.GQuery.Offset; import junit.framework.Assert; +import com.google.gwt.dev.jjs.impl.AssertionNormalizer; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.InputElement; @@ -1002,6 +1003,15 @@ public class GQueryCoreTestGwt extends GWTTestCase { assertFalse(JsUtils.truth(null)); assertFalse(JsUtils.truth("")); } + + public void testUtilsCallFunc() { + Element e = JsUtils.runJavascriptFunction(document, "createElement", "div"); + assertNotNull(e); + assertEquals(e.getTagName().toLowerCase(), "div"); + + e = JsUtils.runJavascriptFunction(document, "foo", "bar", 2, true); + assertNull(e); + } public void testVal_issue98() { $(e).html("" |