From 434ef5c7940bd735b71160c8c20333c7b2e24637 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Sun, 17 Jul 2011 10:49:25 +0000 Subject: [PATCH] Now the user can disable all effects using Fx.off = true like jquery does --- .../com/google/gwt/query/client/GQuery.java | 4 +- .../gwt/query/client/plugins/Effects.java | 79 +++++++++++-------- .../gwt/query/client/plugins/QueuePlugin.java | 23 +++--- .../client/plugins/effects/ClipAnimation.java | 35 ++++---- .../gwt/query/client/plugins/effects/Fx.java | 5 ++ .../plugins/effects/PropertiesAnimation.java | 43 +++++----- 6 files changed, 104 insertions(+), 85 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 38b4472a..a11e20a6 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 @@ -120,7 +120,7 @@ public class GQuery implements Lazy { * A static reference to the GQuery class. */ public static Class GQUERY = GQuery.class; - + /** * Static references to GQuery core plugins */ @@ -355,6 +355,7 @@ public class GQuery implements Lazy { if (plugins == null) { plugins = JsMap.createObject().cast(); } + plugins.put(plugin, pluginFactory); return plugin; } @@ -867,6 +868,7 @@ public class GQuery implements Lazy { if (plugin == GQUERY) { return (T) $(this); } else if (plugins != null) { + Plugin p = plugins.get(plugin); if (p != null) { return (T) p.init(this); 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 5eb34b7c..6afe2f89 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 @@ -30,7 +30,19 @@ import com.google.gwt.query.client.plugins.effects.PropertiesAnimation.Easing; * Effects plugin for Gwt Query. */ public class Effects extends QueuePlugin { - + + /** + * Class to access protected methods in Animation. + */ + public static abstract class GQAnimation extends Animation { + protected void onStart() { + super.onStart(); + } + protected void onComplete() { + super.onComplete(); + } + } + /** * Just a class to store predefined speed constant values. */ @@ -40,8 +52,8 @@ public class Effects extends QueuePlugin { public static final int SLOW = 600; } - private static final String EFFECTS_RUNNNING = "EffectsRunnning"; - + private static final String ACTUAL_ANIMATION = "EffectsRunnning"; + public static final Class Effects = GQuery.registerPlugin( Effects.class, new Plugin() { public Effects init(GQuery gq) { @@ -110,23 +122,36 @@ public class Effects extends QueuePlugin { final Properties p = (stringOrProperties instanceof String) ? $$((String) stringOrProperties) : (Properties) stringOrProperties; - - queue(new Function() { - public void cancel(Element e) { - Animation anim = (Animation) data(e, EFFECTS_RUNNNING, null); - if (anim != null) { - anim.cancel(); - } - } - - public void f(Element e) { - Animation anim = new PropertiesAnimation(easing, e, p, funcs); - anim.run(duration); - data(e, EFFECTS_RUNNNING, anim); - } - }); + + for (Element e: elements()) { + queueAnimation(e, new PropertiesAnimation(easing, e, p, funcs), duration); + } return this; } + + private void queueAnimation(final Element e, final GQAnimation anim, final int duration) { + if (isOff()) { + anim.onStart(); + anim.onComplete(); + } else { + queue(e, new Function() { + public void cancel(Element e) { + Animation anim = (Animation) data(e, ACTUAL_ANIMATION, null); + if (anim != null) { + anim.cancel(); + } + } + public void f(Element e) { + anim.run(duration); + data(e, ACTUAL_ANIMATION, anim); + } + }); + } + } + + protected boolean isOff() { + return true; + } /** * @@ -268,20 +293,10 @@ public class Effects extends QueuePlugin { public Effects clip(final ClipAnimation.Action a, final ClipAnimation.Corner c, final ClipAnimation.Direction d, final int duration, final Function... f) { - queue(new Function() { - public void cancel(Element e) { - Animation anim = (Animation) data(e, EFFECTS_RUNNNING, null); - if (anim != null) { - anim.cancel(); - } - } - - public void f(Element e) { - Animation anim = new ClipAnimation(e, a, c, d, f); - anim.run(duration); - data(e, EFFECTS_RUNNNING, anim); - } - }); + + for (Element e : elements()) { + queueAnimation(e, new ClipAnimation(e, a, c, d, f), duration); + } return this; } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/QueuePlugin.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/QueuePlugin.java index a74848b5..bf090305 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/QueuePlugin.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/QueuePlugin.java @@ -177,7 +177,7 @@ public abstract class QueuePlugin> extends GQuery { } @SuppressWarnings("unchecked") - private Queue queue(Element elem, S func) { + protected Queue queue(Element elem, S func) { if (elem != null) { Queue q = (Queue) data(elem, getQueueType(), null); if (q == null) { @@ -196,13 +196,17 @@ public abstract class QueuePlugin> extends GQuery { return null; } - public void dequeueIfNotDoneYet(Element elem, Object o) { - if (o.equals(queue(elem, null).peek())) { + /** + * Dequeue the object and run the next if it is the first + * in the queue. + */ + public void dequeueIfNotDoneYet(Element elem, Object object) { + if (object.equals(queue(elem, null).peek())) { dequeue(); } } - private void replacequeue(Element elem, Queue queue) { + protected void replacequeue(Element elem, Queue queue) { if (elem != null) { data(elem, getQueueType(), queue); } @@ -212,18 +216,17 @@ public abstract class QueuePlugin> extends GQuery { Queue q = queue(elem, null); if (q != null) { Object f = q.peek(); + if (clear) { + q.clear(); + } if (f != null) { if (f instanceof Function) { - // pass jumpToEnd to Annimation.onCancel() via the element's data object + // pass jumpToEnd to Animation.onCancel() via the element's data object $(elem).data(JUMP_TO_END, new Boolean(jumpToEnd)); ((Function) f).cancel(elem); $(elem).removeData(JUMP_TO_END); } - } - if (clear) { - q.clear(); - } else { - dequeueCurrentAndRunNext(elem); + dequeueIfNotDoneYet(elem, f); } } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/effects/ClipAnimation.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/effects/ClipAnimation.java index 9ca86fb6..c748a691 100755 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/effects/ClipAnimation.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/effects/ClipAnimation.java @@ -15,16 +15,16 @@ */ package com.google.gwt.query.client.plugins.effects; -import com.google.gwt.animation.client.Animation; import com.google.gwt.dom.client.Element; import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.plugins.Effects; +import com.google.gwt.query.client.plugins.Effects.GQAnimation; /** * Animation wich uses the css clip property to show/hide an element. */ -public class ClipAnimation extends Animation { +public class ClipAnimation extends GQAnimation { /** * Type of the effect action. @@ -75,16 +75,25 @@ public class ClipAnimation extends Animation { @Override public void onCancel() { Boolean jumpToEnd = Effects.$(e).data(Effects.JUMP_TO_END, Boolean.class); - if (jumpToEnd != null && jumpToEnd) { - onCompleteImpl(); + if (jumpToEnd != null && jumpToEnd){ + onComplete(); + } else { + g.dequeueIfNotDoneYet(e, this); } - //Do not dequeue here, stop() will do } @Override public void onComplete() { - onCompleteImpl(); - g.dequeue(); + super.onComplete(); + if (action == Action.HIDE) { + g.hide(); + } + g.restoreCssAttrs(attrsToSave); + back.remove(); + back = Effects.$(); + g.css("clip", ""); + g.each(funcs); + g.dequeueIfNotDoneYet(e, this); } @Override @@ -135,16 +144,4 @@ public class ClipAnimation extends Animation { String rect = top + "px " + right + "px " + bottom + "px " + left + "px"; g.css("clip", "rect(" + rect + ")"); } - - private void onCompleteImpl(){ - super.onComplete(); - if (action == Action.HIDE) { - g.hide(); - } - g.restoreCssAttrs(attrsToSave); - back.remove(); - back = Effects.$(); - g.css("clip", ""); - g.each(funcs); - } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/effects/Fx.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/effects/Fx.java index e719f499..70464e18 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/effects/Fx.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/effects/Fx.java @@ -14,6 +14,11 @@ import com.google.gwt.query.client.js.JsRegexp; */ public class Fx { + /** + * Public variable to enable/disable effects + */ + public static boolean off = false; + /** * A pojo to store color effect values. */ diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/effects/PropertiesAnimation.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/effects/PropertiesAnimation.java index 48514d18..c751fdb5 100755 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/effects/PropertiesAnimation.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/effects/PropertiesAnimation.java @@ -15,7 +15,6 @@ */ package com.google.gwt.query.client.plugins.effects; -import com.google.gwt.animation.client.Animation; import com.google.gwt.dom.client.Element; import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQuery; @@ -23,13 +22,14 @@ import com.google.gwt.query.client.Properties; import com.google.gwt.query.client.js.JsObjectArray; import com.google.gwt.query.client.js.JsRegexp; import com.google.gwt.query.client.plugins.Effects; +import com.google.gwt.query.client.plugins.Effects.GQAnimation; import com.google.gwt.query.client.plugins.effects.Fx.ColorFx; import com.google.gwt.query.client.plugins.effects.Fx.ColorFx.BorderColorFx; /** * Animation effects on any numeric CSS property. */ -public class PropertiesAnimation extends Animation { +public class PropertiesAnimation extends GQAnimation { /** * Easing method to use. @@ -207,20 +207,33 @@ public class PropertiesAnimation extends Animation { this.prps = p; g = Effects.$(e).as(Effects.Effects); } - + @Override public void onCancel() { Boolean jumpToEnd = Effects.$(e).data(Effects.JUMP_TO_END, Boolean.class); if (jumpToEnd != null && jumpToEnd){ - onCompleteImpl(); + onComplete(); + } else { + g.dequeueIfNotDoneYet(e, this); } - //Do not dequeue here, stop() will do } @Override public void onComplete() { - onCompleteImpl(); - g.dequeue(); + super.onComplete(); + for (int i = 0; i < effects.length(); i++) { + Fx fx = effects.get(i); + if ("hide".equals(fx.value)) { + g.hide(); + g.restoreCssAttrs(fx.cssprop); + } else if ("show".equals(fx.value)) { + g.show(); + g.restoreCssAttrs(fx.cssprop); + } + } + g.restoreCssAttrs(ATTRS_TO_SAVE); + g.each(funcs); + g.dequeueIfNotDoneYet(e, this); } @Override @@ -264,21 +277,5 @@ public class PropertiesAnimation extends Animation { // maybe return super.interpolate() instead ? return progress; } - - private void onCompleteImpl(){ - super.onComplete(); - for (int i = 0; i < effects.length(); i++) { - Fx fx = effects.get(i); - if ("hide".equals(fx.value)) { - g.hide(); - g.restoreCssAttrs(fx.cssprop); - } else if ("show".equals(fx.value)) { - g.show(); - g.restoreCssAttrs(fx.cssprop); - } - } - g.restoreCssAttrs(ATTRS_TO_SAVE); - g.each(funcs); - } } -- 2.39.5