diff options
author | Manuel Carrasco <manolo@apache.org> | 2013-02-28 03:40:46 -0800 |
---|---|---|
committer | Manuel Carrasco <manolo@apache.org> | 2013-02-28 03:40:46 -0800 |
commit | 88cab57c857460457760c83815b9fe3e93fb554c (patch) | |
tree | ad992eaef60e69e7265a0e6ff2f9981a631f31d6 | |
parent | 9e7a629e65eb9d5210538db5eb3cbd809a3ff323 (diff) | |
parent | 7bd17cc570088b25941d3862dfd95906a120d822 (diff) | |
download | gwtquery-88cab57c857460457760c83815b9fe3e93fb554c.tar.gz gwtquery-88cab57c857460457760c83815b9fe3e93fb554c.zip |
Merge pull request #17 from manolo/master
Changes in jsquery to support more methods. Implementation of Callbacks.
14 files changed, 550 insertions, 101 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 16b10b92..d130b50e 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 @@ -2104,6 +2104,26 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { public GQuery fadeIn(int millisecs, Function... f) { return as(Effects).fadeIn(millisecs, f); } + + /** + * Fade the opacity of all matched elements to a specified opacity and firing + * an optional callback after completion. Only the opacity is adjusted for + * this animation, meaning that all of the matched elements should already + * have some form of height and width associated with them. + */ + public GQuery fadeTo(int millisecs, double opacity, Function... f) { + return as(Effects).fadeTo(millisecs, opacity, f); + } + + /** + * Fade the opacity of all matched elements to a specified opacity and firing + * an optional callback after completion. Only the opacity is adjusted for + * this animation, meaning that all of the matched elements should already + * have some form of height and width associated with them. + */ + public GQuery fadeTo(double opacity, Function... f) { + return as(Effects).fadeTo(opacity, f); + } /** * Fade out all matched elements by adjusting their opacity. The effect will take 1000 @@ -3667,6 +3687,16 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> { } /** + * Remove a property for the set of matched elements. + */ + public GQuery removeProp(String name) { + for (Element e : elements) { + e.<JsCache>cast().delete(name); + } + return this; + } + + /** * Replaces the element <code>elem</code> by the specified selector with the matched elements. * This function is the complement to replaceWith() which does the same task with the parameters * reversed. 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 2b7c996f..01492ede 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 @@ -12,56 +12,23 @@ * the License. */ 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; -import com.google.gwt.core.client.JsArrayMixed; -import com.google.gwt.core.client.JsArrayString; -import com.google.gwt.dom.client.BodyElement; -import com.google.gwt.dom.client.ButtonElement; -import com.google.gwt.dom.client.Document; + import com.google.gwt.dom.client.Element; -import com.google.gwt.dom.client.InputElement; import com.google.gwt.dom.client.Node; import com.google.gwt.dom.client.NodeList; -import com.google.gwt.dom.client.OptionElement; -import com.google.gwt.dom.client.SelectElement; -import com.google.gwt.dom.client.Style.Display; import com.google.gwt.dom.client.Style.HasCssName; -import com.google.gwt.dom.client.TextAreaElement; +import com.google.gwt.query.client.GQuery.Offset; import com.google.gwt.query.client.css.CSS; import com.google.gwt.query.client.css.HasCssValue; import com.google.gwt.query.client.css.TakesCssValue; import com.google.gwt.query.client.css.TakesCssValue.CssSetter; -import com.google.gwt.query.client.impl.AttributeImpl; -import com.google.gwt.query.client.impl.DocumentStyleImpl; -import com.google.gwt.query.client.impl.SelectorEngine; -import com.google.gwt.query.client.js.JsCache; -import com.google.gwt.query.client.js.JsMap; import com.google.gwt.query.client.js.JsNamedArray; import com.google.gwt.query.client.js.JsNodeArray; -import com.google.gwt.query.client.js.JsObjectArray; -import com.google.gwt.query.client.js.JsRegexp; -import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.query.client.plugins.Effects; -import com.google.gwt.query.client.plugins.Events; -import com.google.gwt.query.client.plugins.Plugin; -import com.google.gwt.query.client.plugins.Widgets; -import com.google.gwt.query.client.plugins.ajax.Ajax; -import com.google.gwt.query.client.plugins.ajax.Ajax.Settings; import com.google.gwt.query.client.plugins.effects.PropertiesAnimation.Easing; -import com.google.gwt.query.client.plugins.events.EventsListener; -import com.google.gwt.user.client.DOM; -import com.google.gwt.user.client.Event; -import com.google.gwt.user.client.EventListener; -import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Widget; -import com.google.gwt.query.client.LazyBase; public interface LazyGQuery<T> extends LazyBase<T>{ @@ -110,22 +77,27 @@ public interface LazyGQuery<T> extends LazyBase<T>{ /** * - * The animate() method allows you to create animation effects on any numeric Attribute, CSS - * property, or color CSS property. + * The animate() method allows you to create animation effects on any numeric HTML Attribute, + * CSS property, or color CSS property. * * Concerning to numeric properties, values are treated as a number of pixels unless otherwise * specified. The units em and % can be specified where applicable. * * By default animate considers css properties, if you wanted to animate element attributes you - * should to prepend the symbol dollar to the attribute name. + * should to prepend the symbol dollar to the attribute name. It's useful to animate svg elements. + * + * NOTE: The ability of animating attribute values is only available in gquery but not jquery + * * * Example: * * <pre class="code"> - * //move the element from its original position to left:500px for 500ms + * //move the element from its original position to left:500px * $("#foo").animate("left:'500'"); - * // Change the width attribute of a table - * $("table").animate("$width:'500'"), 400, Easing.LINEAR); + * + * // Change the width html attribute of a table, note the symbol '$' to + * // tell gquery which it is an html-attribute instead of a css-property. + * $("table").animate("$width:'500'"); * </pre> * * In addition to numeric values, each property can take the strings 'show', 'hide', and 'toggle'. @@ -139,16 +111,15 @@ public interface LazyGQuery<T> extends LazyBase<T>{ * <pre class="code"> * //move the element from its original position to 500px to the left for 500ms and * // change the background color of the element at the end of the animation + * * $("#foo").animate("left:'+=500'", new Function(){ - * * public void f(Element e){ * $(e).css(CSS.BACKGROUND_COLOR.with(RGBColor.RED); * } - * * }); * </pre> * - * The duration of the animation is 500ms. + * The default duration of the animation is 500ms. * * For color css properties, values can be specified via hexadecimal or rgb or literal values. * @@ -158,28 +129,34 @@ public interface LazyGQuery<T> extends LazyBase<T>{ * $("#foo").animate("backgroundColor:'red', color:'#ffffff', borderColor:'rgb(129, 0, 70)'"); * </pre> * - * @param prop the property to animate : "cssName:'value'" + * @param stringOrProperties the property to animate : "cssName:'value'" * @param funcs an array of {@link Function} called once the animation is complete */ LazyGQuery<T> animate(Object stringOrProperties, Function... funcs); /** - * The animate() method allows you to create animation effects on any numeric Attribute, CSS - * property, or color CSS property. + * + * The animate() method allows you to create animation effects on any numeric HTML Attribute, + * CSS property, or color CSS property. * * Concerning to numeric properties, values are treated as a number of pixels unless otherwise * specified. The units em and % can be specified where applicable. * * By default animate considers css properties, if you wanted to animate element attributes you - * should to prepend the symbol dollar to the attribute name. + * should to prepend the symbol dollar to the attribute name. It's useful to animate svg elements. + * + * NOTE: The ability of animating attribute values is only available in gquery but not jquery + * * * Example: * * <pre class="code"> - * //move the element from its original position to the position top:500px and left:500px for 400ms. - * //use a swing easing function for the transition - * $("#foo").animate(Properties.create("{top:'500px',left:'500px'}"), 400, Easing.SWING); - * // Change the width and border attributes of a table + * //move the element from its original position to left:500px for 500ms using a swing easing + * $("#foo").animate("left:'500'", 500, Easing.SWING); + * + * // Change the width html attribute of a table, note the symbol '$' to + * // tell gquery which it is an html-attribute instead of a css-property. + * // the animation will last 400ms, and we use the LINEAR easing algorithm * $("table").animate(Properties.create("{$width: '500', $border: '10'}"), 400, Easing.LINEAR); * </pre> * @@ -214,21 +191,27 @@ public interface LazyGQuery<T> extends LazyBase<T>{ LazyGQuery<T> animate(Object stringOrProperties, int duration, Easing easing, Function... funcs); /** - * The animate() method allows you to create animation effects on any numeric Attribute, CSS - * properties, or color CSS property. + * The animate() method allows you to create animation effects on any numeric HTML Attribute, + * CSS property, or color CSS property. * - * Concerning to numeric property, values are treated as a number of pixels unless otherwise + * Concerning to numeric properties, values are treated as a number of pixels unless otherwise * specified. The units em and % can be specified where applicable. * * By default animate considers css properties, if you wanted to animate element attributes you - * should to prepend the symbol dollar to the attribute name. + * should to prepend the symbol dollar to the attribute name. It's useful to animate svg elements. + * + * NOTE: The ability of animating attribute values is only available in gquery but not jquery + * * * Example: * * <pre class="code"> - * //move the element from its original position to left:500px for 2s - * $("#foo").animate("left:'500px'", 2000); - * // Change the width attribute of a table + * //move the element from its original position to left:500px for 500ms + * $("#foo").animate("left:'500'", 500); + * + * // Change the width html attribute of a table, note the symbol '$' to + * // tell gquery which it is an html-attribute instead of a css-property. + * // the animation will last 400ms * $("table").animate("$width:'500'"), 400); * </pre> * @@ -955,6 +938,22 @@ public interface LazyGQuery<T> extends LazyBase<T>{ LazyGQuery<T> fadeIn(int millisecs, Function... f); /** + * Fade the opacity of all matched elements to a specified opacity and firing + * an optional callback after completion. Only the opacity is adjusted for + * this animation, meaning that all of the matched elements should already + * have some form of height and width associated with them. + */ + LazyGQuery<T> fadeTo(int millisecs, double opacity, Function... f); + + /** + * Fade the opacity of all matched elements to a specified opacity and firing + * an optional callback after completion. Only the opacity is adjusted for + * this animation, meaning that all of the matched elements should already + * have some form of height and width associated with them. + */ + LazyGQuery<T> fadeTo(double opacity, Function... f); + + /** * Fade out all matched elements by adjusting their opacity. The effect will take 1000 * milliseconds to complete */ @@ -1538,7 +1537,17 @@ public interface LazyGQuery<T> extends LazyBase<T>{ * returned object contains two integer properties, top and left. The method works only with * visible elements. */ - com.google.gwt.query.client.GQuery.Offset offset(); + Offset offset(); + + /** + * Set the current coordinates of every element in the set of matched elements, relative to the document. + */ + LazyGQuery<T> offset(Offset offset); + + /** + * Set the current coordinates of every element in the set of matched elements, relative to the document. + */ + LazyGQuery<T> offset(int top, int left); /** * Returns a GQuery collection with the positioned parent of the first matched element. This is @@ -1618,7 +1627,7 @@ public interface LazyGQuery<T> extends LazyBase<T>{ * contains two Integer properties, top and left. For accurate calculations make sure to use pixel * values for margins, borders and padding. This method only works with visible elements. */ - com.google.gwt.query.client.GQuery.Offset position(); + Offset position(); /** * Prepend content to the inside of every matched element. This operation is the best way to @@ -1865,6 +1874,11 @@ public interface LazyGQuery<T> extends LazyBase<T>{ LazyGQuery<T> removeData(String name); /** + * Remove a property for the set of matched elements. + */ + LazyGQuery<T> removeProp(String name); + + /** * Replaces the element <code>elem</code> by the specified selector with the matched elements. * This function is the complement to replaceWith() which does the same task with the parameters * reversed. diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java index cc8d6b3f..090089f5 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java @@ -48,7 +48,7 @@ public class Properties extends JavaScriptObject { .replaceAll("\\s+([:\\)\\(,;}{'\"])" , "$1") // Remove spaces .replaceFirst("^[\\(]+(.*)[\\)]+$", "$1") // Remove () .replaceAll("\\([\"']([^\\)]+)[\"']\\)" , "($1)") // Remove quotes - .replaceAll("[;,]+([\\w-\\$]+):", ";$1:") // Change comma by semicolon + .replaceAll("[;,]+([\\w-\\$]+:|$)", ";$1") // Change comma by semicolon .replaceAll("([^,;])([\\]}])", "$1;$2") // Put control semicolon used below .replaceAll(":\\s*[\"']?([^;\\{\\}\\[\\]\"']*)[\"']?\\s*([;,]+|$)", ":\"$1\";") // put quotes to all values (even empty) .replaceAll("[;,]+([\\w-]+):", ";$1:") // Change semicolon by comma diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java index ac3b3102..e0d48c26 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java @@ -41,7 +41,9 @@ public abstract class JsonBuilderBase<J extends JsonBuilderBase<?>> implements J if (prp != null && prp instanceof String) { return parse((String)prp); } - p = (Properties)prp; + if (prp != null) { + p = (Properties)prp; + } return (J)this; } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java index 0220dca6..f8dca659 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java @@ -70,8 +70,7 @@ public class JsCache extends JavaScriptObject { } public final native <T> boolean getBoolean(T id) /*-{ - var r = this[id], t = typeof r; - return 'boolean' == r ? r : 'true' == String(r); + return /true|1/.test(this[id]); }-*/; public final <T> float getFloat(T id) { @@ -95,7 +94,7 @@ public class JsCache extends JavaScriptObject { public final native <T> JsArrayMixed getArray(T id) /*-{ var r = this[id]; - if (r && Object.prototype.toString.call(r) == '[object Array]') { + if (r && @com.google.gwt.query.client.js.JsUtils::isArray(*)(r)) { return r; } return null; @@ -110,21 +109,34 @@ public class JsCache extends JavaScriptObject { for (k in this) return false; return true; }-*/; + + public final native boolean contains(Object o)/*-{ + return this.indexOf(o) >= 0; + }-*/; + + public final native void remove(Object o) /*-{ + var i = this.indexOf(o); + if (i >= 0) this.splice(i, 1); + }-*/; public final native int indexOf(Object o) /*-{ + // HtmlUnit fails when this returns 0 return this.indexOf(o); }-*/; - public final native <T> void putBoolean(T id, boolean b) /*-{ + public final native <T> JsCache putBoolean(T id, boolean b) /*-{ this[id] = b; + return this; }-*/; - public final native <T> void putNumber(T id, double n) /*-{ + public final native <T> JsCache putNumber(T id, double n) /*-{ this[id] = n; + return this; }-*/; - public final native <T, O> void put(T id, O obj) /*-{ + public final native <T, O> JsCache put(T id, O obj) /*-{ this[id] = obj; + return this; }-*/; public final native int length() /*-{ 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 9eac0d79..d8d5e312 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 @@ -71,5 +71,19 @@ public final class JsObjectArray<T> extends JavaScriptObject { public void pushAll(JavaScriptObject prevElem) { c().pushAll(prevElem); } + + public boolean contains(Object o) { + return c().contains(o); + } + + public void remove(Object... objects) { + for (Object o : objects) { + c().remove(o); + } + } + + public Object[] elements() { + return c().elements(); + } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Effects.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Effects.java index ff8eac49..7112f4c6 100755 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Effects.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Effects.java @@ -459,7 +459,17 @@ public class Effects extends QueuePlugin<Effects> { public Effects fadeOut(int millisecs, Function... f) { return animate("opacity: 'hide'", millisecs, f); }; - + + /** + * Fade the opacity of all matched elements to a specified opacity and firing + * an optional callback after completion. Only the opacity is adjusted for + * this animation, meaning that all of the matched elements should already + * have some form of height and width associated with them. + */ + public Effects fadeTo(double opacity, Function... f) { + return fadeTo(Speed.DEFAULT, opacity, f); + } + /** * Fade the opacity of all matched elements to a specified opacity and firing * an optional callback after completion. Only the opacity is adjusted for diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEffects.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEffects.java index 77e113cc..b7686302 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEffects.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEffects.java @@ -331,6 +331,14 @@ public interface LazyEffects<T> extends LazyBase<T>{ * this animation, meaning that all of the matched elements should already * have some form of height and width associated with them. */ + LazyEffects<T> fadeTo(double opacity, Function... f); + + /** + * Fade the opacity of all matched elements to a specified opacity and firing + * an optional callback after completion. Only the opacity is adjusted for + * this animation, meaning that all of the matched elements should already + * have some form of height and width associated with them. + */ LazyEffects<T> fadeTo(int millisecs, double opacity, Function... f); /** 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 08b96e7b..741cfb85 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 @@ -65,6 +65,8 @@ public interface LazyEvents<T> extends LazyBase<T>{ */ LazyEvents<T> bind(String event, Object data, Function... funcs); + GQuery die(int eventbits, String nameSpace); + GQuery die(int eventbits); /** @@ -76,6 +78,8 @@ public interface LazyEvents<T> extends LazyBase<T>{ GQuery live(int eventbits, Object data, Function... funcs); + GQuery live(int eventbits, String nameSpace, Object data, Function... funcs); + GQuery live(String eventName, Object data, Function... funcs); /** diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/callbacks/Callbacks.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/callbacks/Callbacks.java new file mode 100644 index 00000000..0a34b5d5 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/callbacks/Callbacks.java @@ -0,0 +1,178 @@ +/* + * Copyright 2013, The gwtquery team. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.query.client.plugins.callbacks; + +import com.google.gwt.core.shared.GWT; +import com.google.gwt.query.client.Function; +import com.google.gwt.query.client.Properties; +import com.google.gwt.query.client.builders.JsonBuilder; +import com.google.gwt.query.client.js.JsObjectArray; + +/** + * Implementation of jQuery.Callbacks for gwtquery. + */ +public class Callbacks { + + /** + * Iterface used for callbacks which could cancel the execution + * when returning false; + * + */ + public static interface Callback { + /** + * Return false to avoid executing the rest of functions + */ + boolean f(Object ...objects); + } + + /** + * Interface representing the options of a Callbacks collection. + * + * To create an implementation of this interface just call: Callbacks.createOptions() + */ + public static interface CallbackOptions extends JsonBuilder { + boolean getMemory(); + boolean getOnce(); + boolean getStopOnFalse(); + boolean getUnique(); + CallbackOptions setMemory(); + CallbackOptions setOnce(); + CallbackOptions setStopOnFalse(); + CallbackOptions setUnique(); + } + + public static CallbackOptions createOptions() { + return GWT.create(CallbackOptions.class); + } + + private JsObjectArray<Object> callbacks = JsObjectArray.create(); + + private boolean done = false; + + private JsObjectArray<Object> memory = null; + + public final CallbackOptions opts; + + /** + * Create a new Callbacks object with default options + */ + public Callbacks() { + opts = createOptions(); + } + + /** + * Create a new Callbacks object with given options + */ + public Callbacks(CallbackOptions options) { + opts = options; + } + + /** + * Create a new Callbacks object with options given as a space delimited string. + * + * Valid options are: + * + * once, memory, unique, stopOnFalse + */ + public Callbacks(String options) { + this(); + opts.load(Properties.create(options.replaceAll("[^\\S]+|$", ":1,"))); + } + + /** + * Add a Callback or a collection of callbacks to a callback list. + * + */ + public Callbacks add(Callback... c) { + addAll((Object[])c); + return this; + } + + /** + * Add a Callback or a collection of callbacks to a callback list. + */ + public Callbacks add(com.google.gwt.core.client.Callback<?, ?>... c) { + addAll((Object[])c); + return this; + } + + /** + * Add a Function or a collection of Function to a callback list. + */ + public Callbacks add(Function... f) { + addAll((Object[])f); + return this; + } + + /** + * Disable a callback list from doing anything more. + */ + public Callbacks disable() { + callbacks = memory = null; + done = true; + return this; + } + + /** + * Call all of the callbacks with the given arguments. + */ + public Callbacks fire(Object... o) { + if (!done) { + done = opts.getOnce(); + for (Object c : callbacks.elements()) { + if (!run(c, o) && opts.getStopOnFalse()) { + break; + } + } + if (opts.getMemory()) { + memory = JsObjectArray.create().add(o); + } + } + return this; + } + + /** + * Remove a callback or a collection of callbacks from a callback list. + */ + public Callbacks remove(Object... o) { + callbacks.remove(o); + return this; + } + + private void addAll(Object...o) { + if (callbacks != null) { + for (Object c : o) { + if (!opts.getUnique() || !callbacks.contains(c)) { + callbacks.add(c); + } + // In jQuery add always is run when memory is true even when unique is set + if (opts.getMemory() && memory != null) { + run(c, memory.elements()); + } + } + } + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + private boolean run(Object c, Object...o) { + if (c instanceof Callback) { + return ((Callback)c).f(o); + } else if (c instanceof Function) { + ((Function)c).f(o); + } else if (c instanceof com.google.gwt.core.client.Callback) { + ((com.google.gwt.core.client.Callback)c).onSuccess(o); + } + return true; + } +} diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryDeferredTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryDeferredTestGwt.java new file mode 100644 index 00000000..9bcc4884 --- /dev/null +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryDeferredTestGwt.java @@ -0,0 +1,145 @@ +/* + * Copyright 2011, The gwtquery team. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.query.client; + + +import com.google.gwt.junit.client.GWTTestCase; +import com.google.gwt.query.client.plugins.callbacks.Callbacks; +import com.google.gwt.query.client.plugins.callbacks.Callbacks.Callback; + +/** + * Test class for testing deferred and callbacks stuff. + */ +public class GQueryDeferredTestGwt extends GWTTestCase { + + public String getModuleName() { + return "com.google.gwt.query.Query"; + } + + private String result = ""; + + public void testCallbacks() { + Function fn1 = new Function() { + public void f() { + String s = " f1:"; + for (Object o: getData()){ + s += " " + o; + } + result += s; + } + }; + + Callback fn2 = new Callback() { + public boolean f(Object... objects) { + String s = " f2:"; + for (Object o: objects){ + s += " " + o; + } + result += s; + return false; + } + }; + + com.google.gwt.core.client.Callback<Object, Object> fn3 = new com.google.gwt.core.client.Callback<Object, Object>() { + public void onFailure(Object reason) { + String s = " f3_fail: " + reason; + System.out.println(s); + } + public void onSuccess(Object objects) { + String s = " f3_success:"; + for (Object o: (Object[])objects){ + s += " " + o; + } + result += s; + } + }; + + result = ""; + Callbacks callbacks = new Callbacks(); + callbacks.add( fn1 ); + callbacks.fire( "foo" ); + assertEquals(" f1: foo", result); + + result = ""; + callbacks.add( fn2 ); + callbacks.fire( "bar" ); + assertEquals(" f1: bar f2: bar", result); + + result = ""; + callbacks.remove( fn2 ); + callbacks.fire( "foobar" ); + assertEquals(" f1: foobar", result); + + result = ""; + callbacks.add( fn1 ); + callbacks.fire( "foo" ); + assertEquals(" f1: foo f1: foo", result); + + result = ""; + callbacks = new Callbacks("unique"); + callbacks.add( fn1 ); + callbacks.add( fn1 ); + callbacks.fire( "foo" ); + assertEquals(" f1: foo", result); + + result = ""; + callbacks.add( fn3 ); + callbacks.fire( "bar" ); + assertEquals(" f1: bar f3_success: bar", result); + + result = ""; + callbacks = new Callbacks("memory"); + callbacks.add( fn1 ); + callbacks.fire( "foo" ); + callbacks.add( fn2 ); + callbacks.fire( "bar" ); + callbacks.remove(fn2); + callbacks.fire( "foobar" ); + assertEquals(" f1: foo f2: foo f1: bar f2: bar f1: foobar", result); + + result = ""; + callbacks = new Callbacks("stopOnFalse"); + callbacks.add( fn2 ); + callbacks.add( fn1 ); + callbacks.fire( "bar" ); + assertEquals(" f2: bar", result); + + result = ""; + callbacks.disable(); + callbacks.fire( "bar" ); + assertEquals("", result); + + result = ""; + callbacks = new Callbacks("memory once unique"); + callbacks.add( fn1 ); + callbacks.add( fn1 ); + callbacks.fire( "bar" ); + assertEquals(" f1: bar", result); + callbacks.fire( "foo" ); + assertEquals(" f1: bar", result); + callbacks.add( fn2 ); + callbacks.add( fn2 ); + assertEquals(" f1: bar f2: bar f2: bar", result); + callbacks.remove( fn1 ); + callbacks.add( fn1 ); + assertEquals(" f1: bar f2: bar f2: bar f1: bar", result); + callbacks.remove( fn1 ); + callbacks.disable(); + callbacks.add( fn1 ); + assertEquals(" f1: bar f2: bar f2: bar f1: bar", result); + } + +} diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryGwtSuiteTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryGwtSuiteTest.java index 370b5cc7..4093efae 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryGwtSuiteTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryGwtSuiteTest.java @@ -15,6 +15,7 @@ public class GQueryGwtSuiteTest extends GWTTestSuite { GWTTestSuite suite = new GWTTestSuite( "GQuery Suite" ); suite.addTestSuite(GQueryAjaxTestGwt.class); + suite.addTestSuite(GQueryDeferredTestGwt.class); suite.addTestSuite(GQuerySelectorsTestGwt.class); suite.addTestSuite(GQueryCoreTestGwt.class); suite.addTestSuite(GQueryCssTestGwt.class); diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/JreQueryCoreTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/JreQueryCoreTest.java index af755243..e92654f7 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/JreQueryCoreTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/JreQueryCoreTest.java @@ -47,7 +47,7 @@ public class JreQueryCoreTest extends GWTTestCase { assertEquals("{\"width\":\"\",\"top\":\"\"}", Properties .wrapPropertiesString("width: '' ; top:'' ;")); assertEquals("{\"border-left\":\"solid\"}", Properties - .wrapPropertiesString("border-left: solid")); + .wrapPropertiesString("border-left: solid,")); assertEquals("[{\"a\":1,\"b\":{\"a\":2,\"b\":{\"a\":3}},\"u\":\"url\",\"d\":2,\"t\":[\"hola\",\"adios\"],\"z\":true}]", Properties .wrapPropertiesString("[{a:1, b:{a:2,b:{a:3}},u:url, d:'2','t':['hola','adios'], 'z': true}]")); assertEquals("{\"$x\":22.60,\"$y\":\".0\",\"h\":\"#y\"}", Properties 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 cad0a1d2..1a315587 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 @@ -22,6 +22,7 @@ import com.google.gwt.query.client.Properties; import com.google.gwt.query.client.js.JsCache; import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.query.client.plugins.Effects; +import com.google.gwt.query.client.plugins.Events; import com.google.gwt.query.client.plugins.effects.PropertiesAnimation; import com.google.gwt.user.client.Event; @@ -55,7 +56,7 @@ public abstract class GQueryOverlay implements ExportOverlay<GQuery> { public interface PredicateOverlay extends ExportOverlay<Predicate> { public boolean f(Element e, int i); } - + private GQueryOverlay(){} /** @@ -150,6 +151,22 @@ public abstract class GQueryOverlay implements ExportOverlay<GQuery> { } @ExportInstanceMethod + public static GQuery offset(GQuery instance, JsCache o) { + return instance.offset(new Offset(o.getInt("left"), o.getInt("top"))); + } + + @ExportInstanceMethod + public static Element[] toArray(GQuery g) { + return g.elements(); + } + + @ExportInstanceMethod + public static GQuery trigger(GQuery g, String name, Function f) { + g.as(Events.Events).triggerHtmlEvent(name, f); + return g; + } + + @ExportInstanceMethod public static GQuery unbind(GQuery g, String s, Function o) { return g.unbind(s); } @@ -178,7 +195,7 @@ public abstract class GQueryOverlay implements ExportOverlay<GQuery> { public abstract GQuery appendTo(GQuery other); public abstract GQuery appendTo(Node n); public abstract GQuery appendTo(String html); - public abstract <T extends GQuery> T as(Class<T> plugin); +// public abstract <T extends GQuery> T as(Class<T> plugin); public abstract GQuery before(GQuery query); public abstract GQuery before(Node n); @@ -233,7 +250,7 @@ public abstract class GQueryOverlay implements ExportOverlay<GQuery> { public abstract GQuery die(String eventName); // public abstract GQuery die(int eventbits); public abstract GQuery each(Function... f); - public abstract Element[] elements(); +// public abstract Element[] elements(); public abstract GQuery empty(); // public abstract GQuery end(); public abstract GQuery eq(int pos); @@ -242,17 +259,23 @@ public abstract class GQueryOverlay implements ExportOverlay<GQuery> { public abstract GQuery fadeIn(int millisecs, Function... f); public abstract GQuery fadeOut(Function... f); public abstract GQuery fadeOut(int millisecs, Function... f); + public abstract GQuery fadeTo(double opacity, Function... f); + public abstract GQuery fadeTo(int millisecs, double opacity, Function... f); public abstract Effects fadeToggle(int millisecs, Function... f); public abstract GQuery filter(Predicate filterFn); // public abstract GQuery filter(String... filters); public abstract GQuery find(String... filters); public abstract GQuery first(); + // TODO: focusIn + // TODO: focusOut public abstract GQuery focus(Function... f); public abstract Element get(int i); public abstract Node getContext(); - public abstract GQuery getPreviousObject(); - public abstract String getSelector(); - public abstract GQuery gt(int pos); +// public abstract GQuery getPreviousObject(); +// public abstract String getSelector(); +// public abstract GQuery gt(int pos); + public abstract GQuery has(String selector); + public abstract GQuery has(Element elem); public abstract boolean hasClass(String... classes); public abstract int height(); public abstract GQuery height(int height); @@ -261,9 +284,10 @@ public abstract class GQueryOverlay implements ExportOverlay<GQuery> { public abstract GQuery hover(Function fover, Function fout); public abstract String html(); public abstract GQuery html(String html); - public abstract String id(); - public abstract GQuery id(String id); +// public abstract String id(); +// public abstract GQuery id(String id); public abstract int index(Element element); + // TODO: init public abstract int innerHeight(); public abstract int innerWidth(); public abstract GQuery insertAfter(Element elem); @@ -273,7 +297,7 @@ public abstract class GQueryOverlay implements ExportOverlay<GQuery> { public abstract GQuery insertBefore(GQuery query); public abstract GQuery insertBefore(String selector); public abstract boolean is(String... filters); - public abstract boolean isEmpty(); +// public abstract boolean isEmpty(); public abstract GQuery keydown(Function... f); public abstract GQuery keydown(int key); public abstract GQuery keypress(Function... f); @@ -281,16 +305,18 @@ public abstract class GQueryOverlay implements ExportOverlay<GQuery> { public abstract GQuery keyup(Function... f); public abstract GQuery keyup(int key); public abstract GQuery last(); - public abstract int left(); - public abstract int length(); +// public abstract int left(); +// public abstract int length(); public abstract GQuery live(String eventName, Function... funcs); // public abstract GQuery live(int eventbits, Function... funcs); // public abstract GQuery live(int eventbits, Object data, Function... funcs); public abstract GQuery live(String eventName, Object data, Function... funcs); -// public abstract GQuery load(Function f); - public abstract GQuery lt(int pos); -// public abstract <W> List<W> map(Function f); +// TODO: public abstract GQuery load(Function f); +// public abstract GQuery lt(int pos); +// TODO: public abstract <W> List<W> map(Function f); public abstract GQuery mousedown(Function... f); + public abstract GQuery mouseenter(Function... f); + public abstract GQuery mouseleave(Function... f); public abstract GQuery mousemove(Function... f); public abstract GQuery mouseout(Function... f); public abstract GQuery mouseover(Function... f); @@ -313,7 +339,6 @@ public abstract class GQueryOverlay implements ExportOverlay<GQuery> { public abstract GQuery parents(); public abstract GQuery parents(String... filters); public abstract GQuery parentsUntil(String selector); -// public abstract Offset position(); public abstract GQuery prepend(GQuery query); public abstract GQuery prepend(Node n); public abstract GQuery prepend(String html); @@ -324,6 +349,7 @@ public abstract class GQueryOverlay implements ExportOverlay<GQuery> { public abstract GQuery prev(String... selectors); public abstract GQuery prevAll(); public abstract GQuery prevUntil(String selector); + // TODO: pushStack public abstract boolean prop(String key); public abstract GQuery prop(String key, boolean value); public abstract GQuery prop(String key, Function closure); @@ -336,6 +362,7 @@ public abstract class GQueryOverlay implements ExportOverlay<GQuery> { public abstract GQuery removeAttr(String key); public abstract GQuery removeClass(String... classes); public abstract GQuery removeData(String name); + public abstract GQuery removeProp(String name); public abstract GQuery replaceAll(Element elem); public abstract GQuery replaceAll(GQuery target); public abstract GQuery replaceAll(String selector); @@ -343,21 +370,25 @@ public abstract class GQueryOverlay implements ExportOverlay<GQuery> { public abstract GQuery replaceWith(GQuery target); public abstract GQuery replaceWith(String html); public abstract GQuery resize(Function... f); - public abstract void restoreCssAttrs(String... cssProps); - public abstract void resize(Function f); - public abstract void saveCssAttrs(String... cssProps); +// public abstract void restoreCssAttrs(String... cssProps); +// public abstract void resize(Function f); +// public abstract void saveCssAttrs(String... cssProps); public abstract GQuery scroll(Function... f); - public abstract GQuery scrollIntoView(); - public abstract GQuery scrollIntoView(boolean ensure); +// public abstract GQuery scrollIntoView(); +// public abstract GQuery scrollIntoView(boolean ensure); public abstract int scrollLeft(); public abstract GQuery scrollLeft(int left); - public abstract GQuery scrollTo(int left, int top); +// public abstract GQuery scrollTo(int left, int top); public abstract int scrollTop(); public abstract GQuery scrollTop(int top); public abstract GQuery select(); - public abstract GQuery setArray(NodeList<Element> list); - public abstract void setPreviousObject(GQuery previousObject); - public abstract GQuery setSelector(String selector); + // TODO: selector + // TODO: selector + // TODO: serialize + // TODO: serializeArray + // public abstract GQuery setArray(NodeList<Element> list); + // public abstract void setPreviousObject(GQuery previousObject); + // public abstract GQuery setSelector(String selector); public abstract GQuery show(); public abstract GQuery siblings(); public abstract GQuery siblings(String... selectors); @@ -377,20 +408,20 @@ public abstract class GQueryOverlay implements ExportOverlay<GQuery> { public abstract GQuery toggle(Function... fn); public abstract GQuery toggleClass(String... classes); public abstract GQuery toggleClass(String clz, boolean addOrRemove); - public abstract int top(); public abstract String toString(boolean pretty); -// public abstract GQuery trigger(int eventbits, int... keys); -// public abstract GQuery unbind(int eventbits); +// public abstract GQuery unbind(String eventName); +// public abstract GQuery unbind(String eventName, Function f); public abstract GQuery undelegate(); public abstract GQuery undelegate(String selector); public abstract GQuery undelegate(String selector, String eventName); - public abstract GQuery undelegate(String selector, int eventBit); +// public abstract GQuery undelegate(String selector, int eventBit); // public abstract JsNodeArray unique(NodeList<Element> result); + // TODO: unload public abstract GQuery unwrap(); public abstract String val(); public abstract GQuery val(String... values); - public abstract String[] vals(); - public abstract boolean isVisible(); +// public abstract String[] vals(); +// public abstract boolean isVisible(); public abstract int width(); public abstract GQuery width(int width); public abstract GQuery wrap(Element elem); |