From 8b42588c4d8a1674dba7c226d30add108280590d Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Thu, 15 Mar 2012 10:04:08 +0000 Subject: [PATCH] Format source, and generate lazy interfaces --- .../google/gwt/query/client/LazyGQuery.java | 11 + .../google/gwt/query/client/js/JsUtils.java | 320 +++++++++--------- .../gwt/query/client/plugins/LazyEvents.java | 16 +- 3 files changed, 187 insertions(+), 160 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java index 1ae86eec..687f3d59 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java @@ -2135,11 +2135,22 @@ public interface LazyGQuery extends LazyBase{ */ LazyGQuery unbind(int eventbits); + /** + * Removes the function passed from the set of events which match + * the eventbits. + */ + LazyGQuery unbind(int eventbits, Function f); + /** * Removes all events that match the eventList. */ LazyGQuery unbind(String eventList); + /** + * Removes all events that match the eventList. + */ + LazyGQuery unbind(String eventList, Function f); + /** * Remove all event delegation that have been bound using * {@link #delegate(String, int, Function...)} {@link #live(int, Function...)} methods 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 488e693b..e7ca0871 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 @@ -31,43 +31,43 @@ import com.google.gwt.user.client.DOM; * A bunch of utility methods for GQuery. */ public class JsUtils { - - private static JsUtilsImpl utilsImpl = GWT.create(JsUtilsImpl.class); - + + /** + * A Function which wraps a javascript function. + */ public static class JsFunction extends Function { private JavaScriptObject jso = null; + public JsFunction(JavaScriptObject f) { if (JsUtils.isFunction(f)) { jso = f; } } + private native void exec(JavaScriptObject f, Object data) /*-{ - f(data); + f(data); }-*/; + public void f() { if (jso != null) { exec(jso, getData()[0]); } } } - + public static class JsUtilsImpl { - public native Element parseXML(String xml) /*-{ - return new DOMParser().parseFromString(xml, "text/xml").documentElement; - }-*/; - public native Properties parseJSON(String json) /*-{ - return $wnd.JSON.parse(json); + return $wnd.JSON.parse(json); }-*/; - - public native String XML2String(JavaScriptObject o) /*-{ - return (new XMLSerializer()).serializeToString(o); + + public native Element parseXML(String xml) /*-{ + return new DOMParser().parseFromString(xml, "text/xml").documentElement; }-*/; - + public String text(Element e) { return e.getInnerText(); } - + public JsArray unique(JsArray a) { JsArray ret = JavaScriptObject.createArray().cast(); JsCache cache = JsCache.create(); @@ -78,35 +78,39 @@ public class JsUtils { cache.put(id, 1); ret.push(e); } - } + } return ret; } + + public native String XML2String(JavaScriptObject o) /*-{ + return (new XMLSerializer()).serializeToString(o); + }-*/; } - public static class JsUtilsImplIE6 extends JsUtilsImpl { - @Override - public native Element parseXML(String xml) /*-{ - var d = new ActiveXObject("Microsoft.XmlDom"); - d.loadXML(xml); - return d.documentElement; + public static class JsUtilsImplIE6 extends JsUtilsImpl { + public static final native Properties evalImpl(String properties) /*-{ + return eval(properties); }-*/; - + @Override public Properties parseJSON(String json) { // No checks to the passed string so json should be // a well-formed json string. return evalImpl("(" + json + ")"); } - - public static final native Properties evalImpl(String properties) /*-{ - return eval(properties); - }-*/; - + @Override - public native String XML2String(JavaScriptObject o) /*-{ - return o.xml; + public native Element parseXML(String xml) /*-{ + var d = new ActiveXObject("Microsoft.XmlDom"); + d.loadXML(xml); + return d.documentElement; }-*/; - + + @Override + public String text(Element e) { + return isXML(e) ? xmlText(e) : super.text(e); + } + @Override public JsArray unique(JsArray a) { // in IE6 XML elements does not support adding hashId to the object @@ -115,46 +119,49 @@ public class JsUtils { } return super.unique(a); } - + @Override - public String text(Element e) { - return isXML(e) ? xmlText(e) : super.text(e); - } - + public native String XML2String(JavaScriptObject o) /*-{ + return o.xml; + }-*/; + private native String xmlText(Element e) /*-{ - return e.text; + return e.text; }-*/; } + private static JsUtilsImpl utilsImpl = GWT.create(JsUtilsImpl.class); + /** * Camelize style property names. for instance: font-name -> fontName */ public static native String camelize(String s)/*-{ - return s.replace(/\-(\w)/g, function(all, letter) { - return letter.toUpperCase(); - }); + return s.replace(/\-(\w)/g, function(all, letter) { + return letter.toUpperCase(); + }); }-*/; /** - * Merge the oldNodes list into the newNodes one. - * If oldNodes is null, a new list will be created and returned. - * If oldNodes is not null, a new list will be created depending on - * the create flag. + * Merge the oldNodes list into the newNodes one. If oldNodes is null, a new + * list will be created and returned. If oldNodes is not null, a new list will + * be created depending on the create flag. */ - public static NodeList copyNodeList(NodeList oldNodes, NodeList newNodes, boolean create) { - NodeList ret = oldNodes == null || create ? JsNodeArray.create() : oldNodes; + public static NodeList copyNodeList(NodeList oldNodes, + NodeList newNodes, boolean create) { + NodeList ret = oldNodes == null || create ? JsNodeArray.create() + : oldNodes; JsCache idlist = JsCache.create(); for (int i = 0; oldNodes != null && i < oldNodes.getLength(); i++) { Element e = oldNodes.getItem(i); idlist.put(e.hashCode(), 1); if (create) { - ret.cast().addNode(e, i); + ret. cast().addNode(e, i); } } for (int i = 0, l = newNodes.getLength(), j = ret.getLength(); i < l; i++) { Element e = newNodes.getItem(i); if (!idlist.exists(e.hashCode())) { - ret.cast().addNode(newNodes.getItem(i), j++); + ret. cast().addNode(newNodes.getItem(i), j++); } } return ret; @@ -172,179 +179,178 @@ public class JsUtils { * Compare two numbers using javascript equality. */ public static native boolean eq(double s1, double s2) /*-{ - return s1 == s2; + return s1 == s2; }-*/; - + /** * Compare two objects using javascript equality. */ public static native boolean eq(Object s1, Object s2) /*-{ - return s1 == s2; + return s1 == s2; }-*/; /** - * Hyphenize style property names. for instance: fontName -> font-name + * Returns the owner document element of an element. */ - public static native String hyphenize(String name) /*-{ - return name.replace(/([A-Z])/g, "-$1" ).toLowerCase(); - }-*/; - - /** - * Load an external javascript library. - * The inserted script replaces the element with the - * given id in the document. - */ - public static void loadScript(String url, String id) { - GQuery gs = GQuery.$(DOM.createElement("script")); - GQuery gp = GQuery.$("#" + id).parent(); - if (gp.size() != 1) { - gp = GQuery.$(GQuery.document.getBody()); - } - GQuery.$("#" + id).remove(); - gp.append(gs.attr("src", url).attr("type", "text/javascript").attr("id", id)); + public static Document getOwnerDocument(Node n) { + return n == null ? null : n.getNodeType() == Node.DOCUMENT_NODE + ? n. cast() : n.getOwnerDocument(); } /** - * Return the element which is truth in the double scope. - */ - public static native double or(double s1, double s2) /*-{ - return s1 || s2; - }-*/; - - /** - * Return the element which is truth in the javascript scope. + * Check if an object have already a property with name name + * defined. */ - public static native T or(T s1, T s2) /*-{ - return s1 || s2; + public static native boolean hasProperty(JavaScriptObject o, String name)/*-{ + return name in o; }-*/; - + /** - * Check if a number is true in the javascript scope. + * Hyphenize style property names. for instance: fontName -> font-name */ - public static native boolean truth(double a) /*-{ - return a ? true : false; + public static native String hyphenize(String name) /*-{ + return name.replace(/([A-Z])/g, "-$1").toLowerCase(); }-*/; - /** - * Check if an object is true in the javascript scope. + * Check is a javascript object can be used as an array */ - public static native boolean truth(Object a) /*-{ - return a ? true : false; + public static native boolean isArray(JavaScriptObject o) /*-{ + return Object.prototype.toString.call(o) == '[object Array]' + || typeof o.length == 'number'; }-*/; - - + /** - * Check if an object have already a property with name name defined. + * Return whether the event was prevented */ - public static native boolean hasProperty(JavaScriptObject o, String name)/*-{ - return name in o; + public static native boolean isDefaultPrevented(JavaScriptObject e)/*-{ + return e.defaultPrevented || e.returnValue === false || e.getPreventDefault + && e.getPreventDefault() ? true : false; }-*/; - - /** - * Check is a javascript object is a Window - */ - public static boolean isWindow(JavaScriptObject o) { - return hasProperty(o, "alert"); - } + /** - * Check is a javascript object can be cast to an Element + * Check is a javascript object can be cast to an Element */ public static boolean isElement(JavaScriptObject o) { return hasProperty(o, "nodeName"); } - + /** - * Check is a javascript object can be cast to an Event + * Check is a javascript object can be cast to an Event */ public static boolean isEvent(JavaScriptObject o) { return hasProperty(o, "currentTarget"); } - + /** * Check is a javascript object is a function */ public static native boolean isFunction(JavaScriptObject o) /*-{ - return o && typeof o == 'function' + return o && typeof o == 'function' }-*/; - + /** - * Check is a javascript object can be used as an array + * Check is a javascript can be cast to a node list */ - public static native boolean isArray(JavaScriptObject o) /*-{ - return Object.prototype.toString.call(o) == '[object Array]' - || typeof o.length == 'number'; + 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; }-*/; - + /** - * Check is a javascript can be cast to a node list + * Check is a javascript object is a Window */ - 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; - }-*/; - + public static boolean isWindow(JavaScriptObject o) { + return hasProperty(o, "alert"); + } + /** - * Check if an element is a DOM or a XML node + * Check if an element is a DOM or a XML node */ public static boolean isXML(Node o) { - return o == null ? false : - !"HTML".equals(getOwnerDocument(o).getDocumentElement().getNodeName()); + return o == null + ? false + : !"HTML".equals(getOwnerDocument(o).getDocumentElement().getNodeName()); } - - public static String XML2String(JavaScriptObject o) { - return utilsImpl.XML2String(o); - } - - public static String text(Element e) { - return utilsImpl.text(e); - } - + /** - * Parses a xml string and return the xml document element which - * can then be passed to GQuery to create a typical GQuery object - * that can be traversed and manipulated. + * Load an external javascript library. The inserted script replaces the + * element with the given id in the document. */ - public static Element parseXML(String xml) { - return utilsImpl.parseXML(xml); + public static void loadScript(String url, String id) { + GQuery gs = GQuery.$(DOM.createElement("script")); + GQuery gp = GQuery.$("#" + id).parent(); + if (gp.size() != 1) { + gp = GQuery.$(GQuery.document.getBody()); + } + GQuery.$("#" + id).remove(); + gp.append(gs.attr("src", url).attr("type", "text/javascript").attr("id", id)); } - + /** - * Returns the owner document element of an element. + * Return the element which is truth in the double scope. */ - public static Document getOwnerDocument(Node n) { - return n == null ? null : - n.getNodeType() == Node.DOCUMENT_NODE ? - n.cast() : n.getOwnerDocument(); - } - + public static native double or(double s1, double s2) /*-{ + return s1 || s2; + }-*/; + /** - * Remove duplicates from an elements array + * Return the element which is truth in the javascript scope. */ - public static JsArray unique(JsArray a) { - return utilsImpl.unique(a); - } - + public static native T or(T s1, T s2) /*-{ + return s1 || s2; + }-*/; + /** - * Parses a json string returning a Object with useful method - * to get the content. + * Parses a json string returning a Object with useful method to get the + * content. */ - public static Properties parseJSON(String json) { + public static Properties parseJSON(String json) { try { return utilsImpl.parseJSON(json); } catch (Exception e) { - System.err.println("Error while parsing json: " + e.getMessage() + ".\n" + json); + System.err.println("Error while parsing json: " + e.getMessage() + ".\n" + + json); return Properties.create(); } } - + /** - * Return whether the event was prevented + * Parses a xml string and return the xml document element which can then be + * passed to GQuery to create a typical GQuery object that can be traversed + * and manipulated. */ - public static native boolean isDefaultPrevented(JavaScriptObject e)/*-{ - return e.defaultPrevented || e.returnValue === false - || e.getPreventDefault && e.getPreventDefault() ? true : false; + public static Element parseXML(String xml) { + return utilsImpl.parseXML(xml); + } + + public static String text(Element e) { + return utilsImpl.text(e); + } + + /** + * Check if a number is true in the javascript scope. + */ + public static native boolean truth(double a) /*-{ + return a ? true : false; + }-*/; + + /** + * Check if an object is true in the javascript scope. + */ + public static native boolean truth(Object a) /*-{ + return a ? true : false; }-*/; + + /** + * Remove duplicates from an elements array + */ + public static JsArray unique(JsArray a) { + return utilsImpl.unique(a); + } + + public static String XML2String(JavaScriptObject o) { + return utilsImpl.XML2String(o); + } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEvents.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEvents.java index 7584f571..96496bb9 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEvents.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEvents.java @@ -134,16 +134,26 @@ public interface LazyEvents extends LazyBase{ * * Example: unbind(Event.ONCLICK | Event.ONMOUSEOVER, "my.namespace") */ - LazyEvents unbind(int eventbits, String name); + LazyEvents unbind(int eventbits, String name, Function f); /** - * Removes all handlers, that matches event name passed. This name could - * contain a namespace. + * Removes all handlers, that matches the event name passed. + * + * This name could contain a namespace. * * Example: unbind("click.my.namespace") */ LazyEvents unbind(String name); + /** + * Removes the function passed as parameter from the event list matching + * the event name passed. + * This name could contain a namespace. + * + * Example: unbind("click.my.namespace", myFunction) + */ + LazyEvents unbind(String name, Function f); + LazyEvents undelegate(); } -- 2.39.5