]> source.dussan.org Git - gwtquery.git/commitdiff
Accept an array of optional functions in the delay method to be run after the delayed...
authorManolo Carrasco <manolo@apache.org>
Thu, 14 Jul 2011 10:58:17 +0000 (10:58 +0000)
committerManolo Carrasco <manolo@apache.org>
Thu, 14 Jul 2011 10:58:17 +0000 (10:58 +0000)
gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java
gwtquery-core/src/main/java/com/google/gwt/query/client/LazyGQuery.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEffects.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyEvents.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/QueuePlugin.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/SimpleNamedQueue.java

index cb7117dccc9f72c33449a42ffde36498f3048696..8c34f23758cd954e5c4d207758d4818d7f5f2599 100644 (file)
@@ -1399,40 +1399,55 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   }\r
 \r
   /**\r
-   * Insert a delay (in ms) in the Effects queue.\r
+   * Insert a delay (in ms) in the GQuery queue, and optionally execute one o\r
+   * more functions if provided when the delay finishes.\r
+   * It uses the effects queue namespace, so you can stack any of the methods in the effects\r
+   * plugin.\r
    * \r
    * Example:\r
    * \r
    * <pre class="code">\r
-   * $('#foo').slideUp(300).delay(800).fadeIn(400); \r
+   * $("#foo").slideUp(300)\r
+   *          .delay(800)\r
+   *          .fadeIn(400); \r
    * </pre>\r
    * \r
    * When this statement is executed, the element slides up for 300 milliseconds\r
    * and then pauses for 800 milliseconds before fading in for 400 milliseconds.\r
+   * Aditionally after those 800 milliseconds the element color is set to red.\r
    * \r
-   * Please note that this methods affects only the Effects queue. So the\r
-   * following example is wrong:\r
+   * NOTE that this methods affects only methods which uses the queue like effects.\r
+   * So the following example is wrong:\r
    * \r
    * <pre>\r
-   * $('#foo').css(CSS.COLOR.with(RGBColor.RED)).delay(800).css(CSS.COLOR.with(RGBColor.BLACK)); \r
+   * $("#foo").css(CSS.COLOR.with(RGBColor.RED)).delay(800).css(CSS.COLOR.with(RGBColor.BLACK)); \r
    * </pre>\r
    * \r
    * The code above will not insert a delay of 800 ms between the css() calls !\r
-   * For this kind of behavior, please check {@link #delay(int, String)}\r
+   * For this kind of behavior, you should execute these methods puting them in inline \r
+   * functions passed as argument to the delay() method, or adding them to the queue.\r
+   * \r
+   * <pre>\r
+   * $("#foo").css(CSS.COLOR.with(RGBColor.RED)).delay(800, lazy().css(CSS.COLOR.with(RGBColor.BLACK)).done()); \r
+   * $("#foo").css(CSS.COLOR.with(RGBColor.RED)).delay(800).queue(lazy().css(CSS.COLOR.with(RGBColor.BLACK)).dequeue().done()); \r
+   * </pre>\r
    */\r
-  public GQuery delay(int milliseconds) {\r
-    return as(Effects).delay(milliseconds);\r
+  public GQuery delay(int milliseconds, Function... f) {\r
+    return as(Effects).delay(milliseconds, f);\r
   }\r
 \r
   /**\r
    * Insert a delay (in ms) in the queue identified by the\r
-   * <code>queueName</code> parameter. if <code>queueName</code> is null or\r
+   * <code>queueName</code> parameter, and optionally execute one o\r
+   * more functions if provided when the delay finishes.\r
+   * \r
+   * If <code>queueName</code> is null or\r
    * equats to 'fx', the delay will be inserted to the Effects queue.\r
    * \r
    * Example :\r
    * \r
    * <pre class="code">\r
-   * $('#foo').queue("colorQueue", lazy().css(CSS.COLOR.with(RGBColor.RED)).done())\r
+   * $("#foo").queue("colorQueue", lazy().css(CSS.COLOR.with(RGBColor.RED)).dequeue("colorQueue").done())\r
    *          .delay(800, "colorQueue")\r
    *          .queue("colorQueue", lazy().css(CSS.COLOR.with(RGBColor.BLACK)).done()); \r
    * </pre>\r
@@ -1442,12 +1457,12 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * black.\r
    * \r
    */\r
-  public GQuery delay(int milliseconds, String queueName) {\r
+  public GQuery delay(int milliseconds, String queueName, Function... f) {\r
     if (queueName == null || "fx".equalsIgnoreCase(queueName)) {\r
-      return as(Effects).delay(milliseconds);\r
+      return as(Effects).delay(milliseconds, f);\r
     }\r
 \r
-    return as(SimpleNamedQueue).delay(milliseconds, queueName);\r
+    return as(SimpleNamedQueue).delay(milliseconds, queueName, f);\r
   }\r
 \r
   /**\r
@@ -1624,15 +1639,22 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * Execute the next function on the Effects queue for the matched elements.\r
    * This method is usefull to tell when a function you add in the Effects queue\r
    * is ended and so the next function in the queue can start.\r
+   * \r
+   * Note: you should be sure to call dequeue() in all functions of a queue chain,\r
+   * otherwise the queue execution will be stopped.\r
    */\r
   public GQuery dequeue() {\r
     return as(Effects).dequeue();\r
   }\r
 \r
   /**\r
-   * Execute the next function on the queue for the matched elements. This\r
-   * method is usefull to tell when a function you add in the Effects queue is\r
+   * Execute the next function on the queue named as queueName for the matched elements. \r
+   * This method is usefull to tell when a function you add in the Effects queue is\r
    * ended and so the next function in the queue can start.\r
+   * \r
+   * If you are queuing functions in a named queue (not the Effects one), \r
+   * you do not need to call dequeue(queueName) since it is preformed automatically. \r
+   * \r
    */\r
   public GQuery dequeue(String queueName) {\r
     if (queueName == null || "fx".equalsIgnoreCase(queueName)) {\r
@@ -2948,15 +2970,20 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    *             $(e).css(CSS.BACKGROUNG_COLOR.with(RGBColor.RED));\r
    *             $(e).dequeue();     \r
    *          }\r
-   *        }).animate("left:'-=500'", 400)\r
+   *        })\r
+   *       .animate("left:'-=500'", 400)\r
+   *       .queue(lazy().css("color", "yellow");\r
+   *       \r
    * </pre>\r
    * \r
    * When this statement is executed, the element move to 500 px to left for 400\r
    * ms, then its background color is changed to red and then move to 500px to\r
-   * right for 400ms.\r
+   * right for 400ms, and finally its color is set to yellow.\r
    * \r
    * Please note that {@link #dequeue()} function is needed at the end of your\r
-   * function to start the next function in the queue. {@see #dequeue()}\r
+   * function to start the next function in the queue. In lazy() methods you should\r
+   * call dequeue() just before the done() call.\r
+   * {@see #dequeue()}\r
    */\r
   public GQuery queue(Function f) {\r
     return as(Effects).queue(f);\r
@@ -2971,23 +2998,19 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    * $("#foo").queue("myQueue", new Function(){\r
    *          public void f(Element e){\r
    *             $(e).css(CSS.BACKGROUNG_COLOR.with(RGBColor.RED));\r
-   *             $(e).dequeue();     \r
-   *          }\r
-   *        }).delay(500, "myQueue")\r
-   *        .queue("myQueue", new Function(){\r
-   *          public void f(Element e){\r
-   *             $(e).css(CSS.COLOR.with(RGBColor.YELLOW));\r
-   *             $(e).dequeue();     \r
    *          }\r
-   *         });\r
+   *        })\r
+   *        .delay(500, "myQueue")\r
+   *        .queue("myQueue", lazy().css(CSS.COLOR.with(RGBColor.YELLOW)).done());\r
    * </pre>\r
    * \r
    * When this statement is executed, the background color of the element is set\r
    * to red, then wait 500ms before to set the text color of the element to\r
    * yellow. right for 400ms.\r
    * \r
-   * Please note that {@link #dequeue()} function is needed at the end of your\r
-   * function to start the next function in the queue. {@see #dequeue()}\r
+   * NOTE: {@link #dequeue()} function is not needed at the end of your\r
+   * function unless you use the Effects ('fx') namespace.\r
+   * {@see #dequeue()}\r
    */\r
   public GQuery queue(String queueName, Function f) {\r
     if (queueName == null || "fx".equalsIgnoreCase(queueName)) {\r
index dd060b0f7a00c45cd8c4474514b7a1847ef9d595..ea3302920ba8c6abd1b500503921fdec521bdeca 100644 (file)
@@ -150,16 +150,16 @@ public interface LazyGQuery<T> extends LazyBase<T>{
    * Example:
    * 
    * <pre class="code">
-   *  $("#foo").animate(Properties.create("{backgroundColor:'red', color:'#ffffff', borderColor:'rgb(129, 0, 70)'}"), 400, Easing.SWING);
+   *  $("#foo").animate("backgroundColor:'red', color:'#ffffff', borderColor:'rgb(129, 0, 70)'"), 400, Easing.SWING);
    * </pre>
    * 
-   * @param p a {@link Properties} object containing css properties to animate.
+   * @param stringOrProperties a String or a {@link Properties} object containing css properties to animate.
    * @param funcs an array of {@link Function} called once the animation is
    *          complete
    * @param duration the duration in milliseconds of the animation
    * @param easing the easing function to use for the transition
    */
-  LazyGQuery<T> animate(Properties p, int duration, Easing easing, Function... funcs);
+  LazyGQuery<T> animate(Object stringOrProperties, int duration, Easing easing, Function... funcs);
 
   /**
    * 
@@ -649,38 +649,53 @@ public interface LazyGQuery<T> extends LazyBase<T>{
   LazyGQuery<T> dblclick(Function... f);
 
   /**
-   * Insert a delay (in ms) in the Effects queue.
+   * Insert a delay (in ms) in the GQuery queue, and optionally execute one o
+   * more functions if provided when the delay finishes.
+   * It uses the effects queue namespace, so you can stack any of the methods in the effects
+   * plugin.
    * 
    * Example:
    * 
    * <pre class="code">
-   * $('#foo').slideUp(300).delay(800).fadeIn(400); 
+   * $("#foo").slideUp(300)
+   *          .delay(800)
+   *          .fadeIn(400); 
    * </pre>
    * 
    * When this statement is executed, the element slides up for 300 milliseconds
    * and then pauses for 800 milliseconds before fading in for 400 milliseconds.
+   * Aditionally after those 800 milliseconds the element color is set to red.
    * 
-   * Please note that this methods affects only the Effects queue. So the
-   * following example is wrong:
+   * NOTE that this methods affects only methods which uses the queue like effects.
+   * So the following example is wrong:
    * 
    * <pre>
-   * $('#foo').css(CSS.COLOR.with(RGBColor.RED)).delay(800).css(CSS.COLOR.with(RGBColor.BLACK)); 
+   * $("#foo").css(CSS.COLOR.with(RGBColor.RED)).delay(800).css(CSS.COLOR.with(RGBColor.BLACK)); 
    * </pre>
    * 
    * The code above will not insert a delay of 800 ms between the css() calls !
-   * For this kind of behavior, please check {@link #delay(int, String)}
+   * For this kind of behavior, you should execute these methods puting them in inline 
+   * functions passed as argument to the delay() method, or adding them to the queue.
+   * 
+   * <pre>
+   * $("#foo").css(CSS.COLOR.with(RGBColor.RED)).delay(800, lazy().css(CSS.COLOR.with(RGBColor.BLACK)).done()); 
+   * $("#foo").css(CSS.COLOR.with(RGBColor.RED)).delay(800).queue(lazy().css(CSS.COLOR.with(RGBColor.BLACK)).dequeue().done()); 
+   * </pre>
    */
-  LazyGQuery<T> delay(int milliseconds);
+  LazyGQuery<T> delay(int milliseconds, Function... f);
 
   /**
    * Insert a delay (in ms) in the queue identified by the
-   * <code>queueName</code> parameter. if <code>queueName</code> is null or
+   * <code>queueName</code> parameter, and optionally execute one o
+   * more functions if provided when the delay finishes.
+   * 
+   * If <code>queueName</code> is null or
    * equats to 'fx', the delay will be inserted to the Effects queue.
    * 
    * Example :
    * 
    * <pre class="code">
-   * $('#foo').queue("colorQueue", lazy().css(CSS.COLOR.with(RGBColor.RED)).done())
+   * $("#foo").queue("colorQueue", lazy().css(CSS.COLOR.with(RGBColor.RED)).dequeue("colorQueue").done())
    *          .delay(800, "colorQueue")
    *          .queue("colorQueue", lazy().css(CSS.COLOR.with(RGBColor.BLACK)).done()); 
    * </pre>
@@ -690,7 +705,7 @@ public interface LazyGQuery<T> extends LazyBase<T>{
    * black.
    * 
    */
-  LazyGQuery<T> delay(int milliseconds, String queueName);
+  LazyGQuery<T> delay(int milliseconds, String queueName, Function... f);
 
   /**
    * Attach <code>handlers</code> to one or more events for all elements that
@@ -846,13 +861,20 @@ public interface LazyGQuery<T> extends LazyBase<T>{
    * Execute the next function on the Effects queue for the matched elements.
    * This method is usefull to tell when a function you add in the Effects queue
    * is ended and so the next function in the queue can start.
+   * 
+   * Note: you should be sure to call dequeue() in all functions of a queue chain,
+   * otherwise the queue execution will be stopped.
    */
   LazyGQuery<T> dequeue();
 
   /**
-   * Execute the next function on the queue for the matched elements. This
-   * method is usefull to tell when a function you add in the Effects queue is
+   * Execute the next function on the queue named as queueName for the matched elements. 
+   * This method is usefull to tell when a function you add in the Effects queue is
    * ended and so the next function in the queue can start.
+   * 
+   * If you are queuing functions in a named queue (not the Effects one), 
+   * you do not need to call dequeue(queueName) since it is preformed automatically. 
+   * 
    */
   LazyGQuery<T> dequeue(String queueName);
 
@@ -1674,15 +1696,20 @@ public interface LazyGQuery<T> extends LazyBase<T>{
    *             $(e).css(CSS.BACKGROUNG_COLOR.with(RGBColor.RED));
    *             $(e).dequeue();     
    *          }
-   *        }).animate("left:'-=500'", 400)
+   *        })
+   *       .animate("left:'-=500'", 400)
+   *       .queue(lazy().css("color", "yellow");
+   *       
    * </pre>
    * 
    * When this statement is executed, the element move to 500 px to left for 400
    * ms, then its background color is changed to red and then move to 500px to
-   * right for 400ms.
+   * right for 400ms, and finally its color is set to yellow.
    * 
    * Please note that {@link #dequeue()} function is needed at the end of your
-   * function to start the next function in the queue. {@see #dequeue()}
+   * function to start the next function in the queue. In lazy() methods you should
+   * call dequeue() just before the done() call.
+   * {@see #dequeue()}
    */
   LazyGQuery<T> queue(Function f);
 
@@ -1695,23 +1722,19 @@ public interface LazyGQuery<T> extends LazyBase<T>{
    * $("#foo").queue("myQueue", new Function(){
    *          public void f(Element e){
    *             $(e).css(CSS.BACKGROUNG_COLOR.with(RGBColor.RED));
-   *             $(e).dequeue();     
-   *          }
-   *        }).delay(500, "myQueue")
-   *        .queue("myQueue", new Function(){
-   *          public void f(Element e){
-   *             $(e).css(CSS.COLOR.with(RGBColor.YELLOW));
-   *             $(e).dequeue();     
    *          }
-   *         });
+   *        })
+   *        .delay(500, "myQueue")
+   *        .queue("myQueue", lazy().css(CSS.COLOR.with(RGBColor.YELLOW)).done());
    * </pre>
    * 
    * When this statement is executed, the background color of the element is set
    * to red, then wait 500ms before to set the text color of the element to
    * yellow. right for 400ms.
    * 
-   * Please note that {@link #dequeue()} function is needed at the end of your
-   * function to start the next function in the queue. {@see #dequeue()}
+   * NOTE: {@link #dequeue()} function is not needed at the end of your
+   * function unless you use the Effects ('fx') namespace.
+   * {@see #dequeue()}
    */
   LazyGQuery<T> queue(String queueName, Function f);
 
index 0a60c32d0cfd406a0c959c04f9f4ad3d82cb4aaa..fd387cb6e292f35b8b068344a187f738b4e3ba76 100644 (file)
@@ -70,7 +70,8 @@ public interface LazyEffects<T> extends LazyBase<T>{
    * Example:
    * 
    * <pre class="code">
-   *  $("#foo").animate(Properties.create("{backgroundColor:'red', color:'#ffffff', borderColor:'rgb(129, 0, 70)'}"), 400, Easing.SWING);
+   *  $("#foo").animate("backgroundColor:'red', color:'#ffffff', borderColor:'rgb(129, 0, 70)'", 400, Easing.SWING);
+   *  $("#foo").animate($$("{backgroundColor:'red', color:'#ffffff', borderColor:'rgb(129, 0, 70)'}"), 400, Easing.SWING);
    * </pre>
    * 
    * @param p a {@link Properties} object containing css properties to animate.
@@ -79,7 +80,7 @@ public interface LazyEffects<T> extends LazyBase<T>{
    * @param duration the duration in milliseconds of the animation
    * @param easing the easing function to use for the transition
    */
-  LazyEffects<T> animate(Properties p, int duration, Easing easing, Function... funcs);
+  LazyEffects<T> animate(Object stringOrProperties, int duration, Easing easing, Function... funcs);
 
   /**
    * 
index 0ef62afeb230521644692dfaa22b83d04a1adf08..0dc01b726e234d0e56e7bcea72658a92f6c05133 100644 (file)
@@ -16,6 +16,7 @@
 package com.google.gwt.query.client.plugins;
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.dom.client.NativeEvent;
+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.plugins.events.EventsListener;
index 84d3098d9e95c6c2c077476fe232f0266ecc83ae..af9d05d619838df691d15ae14f9af5efbe5fd48b 100644 (file)
@@ -34,13 +34,18 @@ public abstract class QueuePlugin<T extends QueuePlugin<?>> extends GQuery {
       @Override
       public void run() {
         dequeue();
+        g.each(funcs);
       }
     }
 
     private int delay;
+    Function[] funcs;
+    GQuery g;
 
-    public DelayFunction(int delayInMilliseconds) {
+    public DelayFunction(GQuery gquery, int delayInMilliseconds, Function... f) {
+      this.g = gquery;
       this.delay = delayInMilliseconds;
+      this.funcs = f;
     }
 
     @Override
@@ -74,8 +79,8 @@ public abstract class QueuePlugin<T extends QueuePlugin<?>> extends GQuery {
    * Add a delay in the queue
    */
   @SuppressWarnings("unchecked")
-  public T delay(int milliseconds) {
-    queue(new DelayFunction(milliseconds));
+  public T delay(int milliseconds, Function... funcs) {
+    queue(new DelayFunction(this, milliseconds));
     return (T) this;
   }
 
@@ -181,6 +186,12 @@ public abstract class QueuePlugin<T extends QueuePlugin<?>> extends GQuery {
     }
     return null;
   }
+  
+  public void dequeueIfNotDoneYet(Element elem, Object o) {
+    if (o.equals(queue(elem, null).peek())) {
+      dequeue();
+    }
+  }
 
   private void replacequeue(Element elem, Queue<?> queue) {
     if (elem != null) {
index c7d56a9f2efbb3640f8f8b250631c224b19bd5af..5387f83bdb93b76cc89b0a954f83734fb451fdd1 100644 (file)
@@ -2,6 +2,7 @@ package com.google.gwt.query.client.plugins;
 
 import com.google.gwt.query.client.Function;
 import com.google.gwt.query.client.GQuery;
+import com.google.gwt.user.client.Element;
 
 public class SimpleNamedQueue extends QueuePlugin<SimpleNamedQueue>{
 
@@ -21,15 +22,21 @@ public class SimpleNamedQueue extends QueuePlugin<SimpleNamedQueue>{
   private String queueName;
   
   @Override
-  public SimpleNamedQueue delay(int milliseconds, String queueName) {
+  public SimpleNamedQueue delay(int milliseconds, String queueName, Function... f) {
     this.queueName = queueName;
-    return delay(milliseconds);
+    return delay(milliseconds, f);
   }
   
   @Override
-  public SimpleNamedQueue queue(String queueName, Function func) {
+  public SimpleNamedQueue queue(final String queueName, final Function func) {
     this.queueName = queueName;
-    return queue(func);
+    return queue(new Function(){
+      @Override
+      public void f(Element e) {
+        func.f(e.<com.google.gwt.dom.client.Element>cast());
+        dequeueIfNotDoneYet(e, this);
+      }
+    });
   }
   
   @Override