diff options
author | Manolo Carrasco <manolo@apache.org> | 2012-03-14 16:52:10 +0000 |
---|---|---|
committer | Manolo Carrasco <manolo@apache.org> | 2012-03-14 16:52:10 +0000 |
commit | 5db327df2c7312a6872e7b0bc63ba1b4a66cffec (patch) | |
tree | 33979b888550c2e39be5fe6e5d470b9d04c9d988 /jsquery | |
parent | 7822e013f689e39acbe6f37f05f3ced842af209d (diff) | |
download | gwtquery-5db327df2c7312a6872e7b0bc63ba1b4a66cffec.tar.gz gwtquery-5db327df2c7312a6872e7b0bc63ba1b4a66cffec.zip |
jsquery re-factoring
Diffstat (limited to 'jsquery')
-rw-r--r-- | jsquery/src/main/java/gwtquery/jsquery/JsQuery.gwt.xml | 5 | ||||
-rw-r--r-- | jsquery/src/main/java/gwtquery/jsquery/JsQueryXs.gwt.xml | 3 | ||||
-rw-r--r-- | jsquery/src/main/java/gwtquery/jsquery/client/JsMenu.java | 6 | ||||
-rw-r--r-- | jsquery/src/main/java/gwtquery/jsquery/client/JsQuery.java | 25 | ||||
-rw-r--r-- | jsquery/src/main/java/gwtquery/jsquery/client/OverlayGQuery.java (renamed from jsquery/src/main/java/gwtquery/jsquery/client/JQ.java) | 278 | ||||
-rw-r--r-- | jsquery/src/main/java/gwtquery/jsquery/client/utils/JsQAux.java | 129 |
6 files changed, 250 insertions, 196 deletions
diff --git a/jsquery/src/main/java/gwtquery/jsquery/JsQuery.gwt.xml b/jsquery/src/main/java/gwtquery/jsquery/JsQuery.gwt.xml index 2dc6ab94..ace3971e 100644 --- a/jsquery/src/main/java/gwtquery/jsquery/JsQuery.gwt.xml +++ b/jsquery/src/main/java/gwtquery/jsquery/JsQuery.gwt.xml @@ -5,7 +5,8 @@ <set-property name="export" value="yes" /> <entry-point class="gwtquery.jsquery.client.JsQuery" /> - - <add-linker name="xsiframe"/> + + <!-- cross-site --> + <add-linker name="xsiframe"/> </module> diff --git a/jsquery/src/main/java/gwtquery/jsquery/JsQueryXs.gwt.xml b/jsquery/src/main/java/gwtquery/jsquery/JsQueryXs.gwt.xml index 5cb9179f..80ce12a6 100644 --- a/jsquery/src/main/java/gwtquery/jsquery/JsQueryXs.gwt.xml +++ b/jsquery/src/main/java/gwtquery/jsquery/JsQueryXs.gwt.xml @@ -26,9 +26,6 @@ ]]> </property-provider> - <!-- Cross Site --> - <add-linker name="xslinker"/> - <!-- Minimize JS --> <set-property name="compiler.stackMode" value="strip"/> </module> diff --git a/jsquery/src/main/java/gwtquery/jsquery/client/JsMenu.java b/jsquery/src/main/java/gwtquery/jsquery/client/JsMenu.java index 6c30de6a..9c1cf373 100644 --- a/jsquery/src/main/java/gwtquery/jsquery/client/JsMenu.java +++ b/jsquery/src/main/java/gwtquery/jsquery/client/JsMenu.java @@ -2,15 +2,11 @@ package gwtquery.jsquery.client; public abstract class JsMenu { - static void log(Object l) { - System.out.println(l); - } - public static native void loadPlugin() /*-{ (function($) { - var l = @gwtquery.jsquery.client.JsMenu::log(Ljava/lang/Object;); + var l = @gwtquery.jsquery.client.utils.JsQAux::log(Ljava/lang/Object;); var window = $wnd; var document = $doc; diff --git a/jsquery/src/main/java/gwtquery/jsquery/client/JsQuery.java b/jsquery/src/main/java/gwtquery/jsquery/client/JsQuery.java index 98b9cca3..bf9559ee 100644 --- a/jsquery/src/main/java/gwtquery/jsquery/client/JsQuery.java +++ b/jsquery/src/main/java/gwtquery/jsquery/client/JsQuery.java @@ -1,10 +1,5 @@ package gwtquery.jsquery.client; -import gwtquery.jsquery.client.JQ.Dollar; -import gwtquery.jsquery.client.JQ.JEasing; -import gwtquery.jsquery.client.JQ.JFunction; -import gwtquery.jsquery.client.JQ.JPredicate; - import java.util.logging.Logger; import com.google.gwt.core.client.EntryPoint; @@ -24,15 +19,23 @@ public class JsQuery implements EntryPoint { } }); - GWT.create(JFunction.class); - GWT.create(JPredicate.class); - GWT.create(JEasing.class); - GWT.create(JQ.class); - GWT.create(Dollar.class); - JsMenu.loadPlugin(); + OverlayGQuery.export(); + onJsQueryLoad(); + + testJs(); } + private native static void testJs() /*-{ + var l = @gwtquery.jsquery.client.utils.JsQAux::log(Ljava/lang/Object;); + $ = $wnd.$; + l($.each); + $.each(["a","b"], function(a, b){ + l("kk " + " " + a + " " + b); + } + ); + }-*/; + private native static void onJsQueryLoad() /*-{ $wnd.onJsQueryLoad && $wnd.onJsQueryLoad(); $wnd.JsQuery && $wnd.JsQuery.onLoad && $wnd.JsQuery.onLoad(); diff --git a/jsquery/src/main/java/gwtquery/jsquery/client/JQ.java b/jsquery/src/main/java/gwtquery/jsquery/client/OverlayGQuery.java index f34b223e..20b7139b 100644 --- a/jsquery/src/main/java/gwtquery/jsquery/client/JQ.java +++ b/jsquery/src/main/java/gwtquery/jsquery/client/OverlayGQuery.java @@ -1,6 +1,6 @@ package gwtquery.jsquery.client; -import static com.google.gwt.user.client.Window.alert; +import gwtquery.jsquery.client.utils.JsQAux; import org.timepedia.exporter.client.Export; import org.timepedia.exporter.client.ExportAfterCreateMethod; @@ -9,11 +9,12 @@ import org.timepedia.exporter.client.ExportInstanceMethod; import org.timepedia.exporter.client.ExportJsInitMethod; import org.timepedia.exporter.client.ExportOverlay; import org.timepedia.exporter.client.ExportPackage; -import org.timepedia.exporter.client.Exportable; +import org.timepedia.exporter.client.ExportStaticMethod; import org.timepedia.exporter.client.ExporterUtil; +import org.timepedia.exporter.client.NoExport; +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.dom.client.Element; import com.google.gwt.dom.client.Node; import com.google.gwt.dom.client.NodeList; @@ -22,26 +23,37 @@ import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.GQuery.Offset; import com.google.gwt.query.client.Predicate; import com.google.gwt.query.client.Properties; -import com.google.gwt.query.client.js.JsCache; -import com.google.gwt.query.client.js.JsNodeArray; -import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.query.client.plugins.Effects; import com.google.gwt.query.client.plugins.effects.PropertiesAnimation; -import com.google.gwt.query.client.plugins.effects.PropertiesAnimation.Easing; import com.google.gwt.user.client.Event; - +/** + * Class used to expose GQuery methods and object to Javascript using + * gwt-exporter annotations. + * + * We prefer to overlay the original GQuery object instead of adding + * the gwt-exporter dependency to the project. + * + * Because of the differences between java and js apis, we need to + * override some methods in order to deal with complex cases. + * + */ @ExportPackage("jsQuery") @Export(value="fn", all=false) -public class JQ implements ExportOverlay<GQuery> { +public class OverlayGQuery implements ExportOverlay<GQuery> { + + @NoExport + public static void export() { + GWT.create(OverlayFunction.class); + GWT.create(OverlayPredicate.class); + GWT.create(OverlayGQuery.class); + } @ExportPackage("jsQuery") @Export("jFunction") @ExportClosure() - public interface JFunction extends ExportOverlay<Function> { + protected interface OverlayFunction extends ExportOverlay<Function> { public void f(); -// Element and Event are JavascriptObjects so we export one. -// public void f(Element e); public boolean f(Event e); public Object f(Element e, int i); } @@ -49,130 +61,87 @@ public class JQ implements ExportOverlay<GQuery> { @ExportPackage("jsQuery") @Export("jPredicate") @ExportClosure() - public interface JPredicate extends ExportOverlay<Predicate> { + protected interface OverlayPredicate extends ExportOverlay<Predicate> { public boolean f(Element e, int i); } - @ExportPackage("jsQuery") - @Export("jEasing") - @ExportClosure() - public interface JEasing extends ExportOverlay<Easing> { - public double interpolate(double progress); + private OverlayGQuery(){} + + /** + * In js a GQuery object represents a Nodelist. + * gwt-exporter will use the object returned by get() to wrap + * the GQuery object + */ + @ExportJsInitMethod + public NodeList<Element> get() {return null;} + + /** + * Customized JS code to execute after GQuery has been exported. + */ + @ExportAfterCreateMethod + public static native void afterCreate() /*-{ + }-*/; + + @ExportStaticMethod("$wnd.$") + public static GQuery $(Object o) { + return JsQAux.dollar(o); + } + + @ExportStaticMethod("$wnd.$") + public static GQuery $(String s, Element ctx) { + return GQuery.$(s, ctx); } - @ExportPackage("") - @Export("jsQuery") - // Used to export $ method - public static class Dollar implements Exportable { - // jQuery.fn.init.prototype = jQuery.fn - native static String dumpObject(JavaScriptObject o) /*-{ - var s = "" ; for (k in o) s += " " + k; return s; - }-*/; - - private static native boolean isFunction(Object o) /*-{ - return typeof(o) == 'function'; - }-*/; - - private static native void runFunction(Object f) /*-{ - f(); - }-*/; + @ExportStaticMethod("$wnd.$.extend") + public static JavaScriptObject extend(Object...objs) { + return JsQAux.extend(objs); + } + + @ExportStaticMethod("$wnd.$.each") + public static Object[] each(Object[] objs, Function f) { + return JsQAux.each(objs, f); + } + + @ExportInstanceMethod + public static GQuery ready(GQuery g, Function f) { + f.fe(); + return g; + } - @Export("$wnd.$") - public static GQuery staticDollar(Object o) { - if (o instanceof String) { - return GQuery.$((String)o); - } else if (isFunction(o)) { - runFunction(o); - } else if (o instanceof JavaScriptObject) { - JavaScriptObject jso = (JavaScriptObject)o; - GQuery r = GQuery.$(jso); - if (JsUtils.isArray(jso)) { - JsCache c = jso.cast(); - JsNodeArray elms = JsNodeArray.create(); - for (int i = 0 ; i < c.length(); i++) { - elms.addNode(c.getJavaScriptObject(i).<Node>cast()); - } - r = GQuery.$(elms); -// System.out.println(c.length() + " " + elms.getLength() + " " + r.length()); - } - - return r; - } else { - System.out.println("Bad!!!! " + o); - } - return GQuery.$(); - } - - @Export("$wnd.$") - public static GQuery staticDollar(String s, Element ctx) { - return GQuery.$(s, ctx); - } - - - @Export("$wnd.$.ready") - public static void ready(Function f) { - alert("ready"); - } - - @Export("$wnd.$.inArray") - public static int inArray(Object o, Object a) { - if (o instanceof JavaScriptObject && JsUtils.isElement((JavaScriptObject)o)) { - return staticDollar(a).index((Element)o); - } else if (a instanceof JavaScriptObject && JsUtils.isArray((JavaScriptObject)a)) { - return ((JsCache)a).indexOf(o); - } - return -1; + @ExportInstanceMethod + // TODO: normally plugins adds new easing functions to jquery.easing array + public static GQuery animate(GQuery g, Object stringOrProperties, int duration, String easing, Function... funcs) { + return g.animate(stringOrProperties, duration, + "linear".equalsIgnoreCase(easing) + ? PropertiesAnimation.Easing.LINEAR + : PropertiesAnimation.Easing.SWING, funcs); + } + + @ExportInstanceMethod + public static Object css(GQuery g, Object o) { + if (o instanceof String) { + return g.css((String)o, false); + } else { + return ExporterUtil.wrap(g.css((Properties)o)); } + } - @Export("$wnd.$.extend") - public static JavaScriptObject extend(Object...objs) { - int i = 0, l = objs.length; - boolean deep = false; - JavaScriptObject ctx = null; - Object target = objs[i]; - if (target instanceof Boolean) { - deep = (Boolean) target; - if (l == 1) return ctx; - target = objs[i++]; - } - if (l - i == 1) { - i--; - } else { - ctx = (JavaScriptObject)target; - } - - for (++i; i < l; i++) { - if (objs[i] != null) { - ctx = extendImpl(deep, ctx, objs[i]); - } - } - return ctx; - } - - private static native JavaScriptObject getDefaultPrototype() /*-{ - return $wnd.jsQuery && $wnd.jsQuery.fn ? $wnd.jsQuery.fn.prototype : null; - }-*/; - - private static native JavaScriptObject extendImpl(boolean deep, JavaScriptObject ctx, Object s) /*-{ - var d = ctx ? ctx : $wnd.jsQuery.fn.prototype || {}; - for (k in s) { - d[k] = s[k]; - if (!ctx) $wnd.$[k] = s[k]; - } - return d; - }-*/; - - @ExportAfterCreateMethod - public static native void afterCreate() /*-{ - }-*/; + @ExportInstanceMethod + public static GQuery css(GQuery g, String k, Object v) { + return g.css(k, String.valueOf(v)); + } + + @ExportInstanceMethod + public static JavaScriptObject offset(GQuery instance) { + Offset o = instance.offset(); + return Properties.create("left: " + o.left + ", top:" + o.top); + } + + @ExportInstanceMethod + public static GQuery unbind(GQuery g, String s, Function o) { + return g.unbind(s); } - // We have to stub all the method we want to export here. - private JQ(){} - - @ExportJsInitMethod - public NodeList<Element> get() {return null;} - public String toString() {return null;} public GQuery add(GQuery previousObject) {return null;} public GQuery add(String selector) {return null;} @@ -182,18 +151,14 @@ public class JQ implements ExportOverlay<GQuery> { public GQuery after(String html) {return null;} public GQuery andSelf() {return null;} - @ExportInstanceMethod - public static GQuery animate(GQuery g, Object stringOrProperties, int duration, String easing, Function... funcs) { - return g.animate(stringOrProperties, duration, "linear".equalsIgnoreCase(easing)? PropertiesAnimation.Easing.LINEAR : PropertiesAnimation.Easing.SWING, funcs); - } -// public GQuery animate(Object stringOrProperties, int duration, Easing easing, Function... funcs) {return null;} +// public GQuery animate(Object stringOrProperties, int duration, Easing easing, Function... funcs) {return null;} public GQuery animate(Object stringOrProperties, Function... funcs) {return null;} public GQuery animate(Object stringOrProperties, int duration, Function... funcs) {return null;} -//public GQuery attr(Properties properties) {return null;} -public String attr(String name) {return null;} -//public GQuery attr(String key, Function closure) {return null;} -public GQuery attr(String key, Object value) {return null;} -public int size() {return 0;} +// public GQuery attr(Properties properties) {return null;} + public String attr(String name) {return null;} +// public GQuery attr(String key, Function closure) {return null;} + public GQuery attr(String key, Object value) {return null;} + public int size() {return 0;} public GQuery append(GQuery query) {return null;} public GQuery append(Node n) {return null;} @@ -206,8 +171,8 @@ public int size() {return 0;} public GQuery before(GQuery query) {return null;} public GQuery before(Node n) {return null;} public GQuery before(String html) {return null;} -// public GQuery bind(int eventbits, Object data, Function... funcs) {return null;} -// public GQuery bind(String eventType, Object data, Function... funcs) {return null;} +// public GQuery bind(int eventbits, Object data, Function... funcs) {return null;} +// public GQuery bind(String eventType, Object data, Function... funcs) {return null;} @ExportInstanceMethod public static GQuery bind(GQuery g, String events, Function func) { return g.bind(events, null, func); @@ -226,18 +191,6 @@ public int size() {return 0;} public GQuery closest(String selector, Node context) {return null;} public GQuery contains(String text) {return null;} public GQuery contents() {return null;} - @ExportInstanceMethod - public static Object css(GQuery g, Object o) { - if (o instanceof String) { - return g.css((String)o, false); - } else { - return ExporterUtil.wrap(g.css((Properties)o)); - } - } - @ExportInstanceMethod - public static GQuery css(GQuery g, String k, Object v) { - return g.css(k, String.valueOf(v)); - } // public GQuery css(Properties properties) {return null;} // public String css(String name) {return null;} // public String css(String name, boolean force) {return null;} @@ -317,7 +270,6 @@ public int size() {return 0;} // public GQuery live(int eventbits, Object data, Function... funcs) {return null;} public GQuery live(String eventName, Object data, Function... funcs) {return null;} // public GQuery load(Function f) {return null;} - public GQuery lt(int pos) {return null;} // public <W> List<W> map(Function f) {return null;} public GQuery mousedown(Function... f) {return null;} @@ -332,13 +284,6 @@ public int size() {return 0;} public GQuery not(Element elem) {return null;} public GQuery not(GQuery gq) {return null;} public GQuery not(String... filters) {return null;} - - @ExportInstanceMethod - public static JavaScriptObject offset(GQuery instance) { - Offset o = instance.offset(); - return Properties.create("left: " + o.left + ", top:" + o.top); - } - public GQuery offsetParent() {return null;} public GQuery one(int eventbits, Object data, Function f) {return null;} public int outerHeight() {return 0;} @@ -418,10 +363,6 @@ public int size() {return 0;} public String toString(boolean pretty) {return null;} // public GQuery trigger(int eventbits, int... keys) {return null;} // public GQuery unbind(int eventbits) {return null;} - @ExportInstanceMethod - public static GQuery unbind(GQuery g, String s, Function o) { - return g.unbind(s); - } public GQuery undelegate() {return null;} public GQuery undelegate(String selector) {return null;} public GQuery undelegate(String selector, String eventName) {return null;} @@ -443,17 +384,4 @@ public int size() {return 0;} public GQuery wrapInner(Element elem) {return null;} public GQuery wrapInner(GQuery query) {return null;} public GQuery wrapInner(String html) {return null;} - - @ExportInstanceMethod - public static GQuery ready(GQuery g, Function f) { - f.fe(); - return g; - } - - @ExportInstanceMethod - public static String imprime(GQuery g, Function f) { - System.out.println("Imprime " + g.size() + " " + g.toString(true)); - return g.toString(); - } - } diff --git a/jsquery/src/main/java/gwtquery/jsquery/client/utils/JsQAux.java b/jsquery/src/main/java/gwtquery/jsquery/client/utils/JsQAux.java new file mode 100644 index 00000000..72cc100c --- /dev/null +++ b/jsquery/src/main/java/gwtquery/jsquery/client/utils/JsQAux.java @@ -0,0 +1,129 @@ +package gwtquery.jsquery.client.utils; + +import java.util.ArrayList; + +import com.google.gwt.core.client.JavaScriptObject; +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.Node; +import com.google.gwt.query.client.Function; +import com.google.gwt.query.client.GQuery; +import com.google.gwt.query.client.js.JsCache; +import com.google.gwt.query.client.js.JsNodeArray; +import com.google.gwt.query.client.js.JsUtils; + +public abstract class JsQAux { + + private native static String dumpObject(JavaScriptObject o) /*-{ + var s = ""; + for (k in o) + s += " " + k; + return s; + }-*/; + + private static native void runJsFunction(JavaScriptObject f) /*-{ + f(); + }-*/; + + 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)) { + runJsFunction(jso); + } else { + GQuery r = GQuery.$(jso); + if (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); + } + + public static void ready(Function f) { + f.f(); + } + + public static int inArray(Object o, Object a) { + if (o instanceof JavaScriptObject) { + JavaScriptObject jso = (JavaScriptObject) o; + if (JsUtils.isElement(jso)) { + return dollar(a).index((Element) o); + } else if (JsUtils.isArray(jso)) { + return ((JsCache) a).indexOf(o); + } + } + return -1; + } + + public static JavaScriptObject extend(Object... objs) { + int i = 0, l = objs.length; + boolean deep = false; + JavaScriptObject ctx = null; + Object target = objs[i]; + if (target instanceof Boolean) { + deep = (Boolean) target; + if (l == 1) + return ctx; + target = objs[i++]; + } + if (l - i == 1) { + i--; + } else { + ctx = (JavaScriptObject) target; + } + + for (++i; i < l; i++) { + if (objs[i] != null) { + ctx = extendImpl(deep, ctx, objs[i]); + } + } + return ctx; + } + + private static native JavaScriptObject getDefaultPrototype() /*-{ + return $wnd.jsQuery && $wnd.jsQuery.fn ? $wnd.jsQuery.fn.prototype + : null; + }-*/; + + private static native JavaScriptObject extendImpl(boolean deep, + JavaScriptObject ctx, Object s) /*-{ + var d = ctx ? ctx : $wnd.jsQuery.fn.prototype || {}; + for (k in s) { + d[k] = s[k]; + if (!ctx) + $wnd.$[k] = s[k]; + } + return d; + }-*/; + + public static Object[] each(Object[] objs, Function f) { + ArrayList<Object> ret = new ArrayList<Object>(); + for (Object o : objs) { + f.setDataObject(o); + if (f.fe(null, o)) { + ret.add(o); + } + } + return ret.toArray(new Object[0]); + } + + public static void log(Object l) { + System.out.println(l); + } + +} |