1919 */
2020package gwt .material .design .client .ui .animate ;
2121
22+ import com .google .gwt .user .client .Timer ;
2223import com .google .gwt .user .client .ui .Widget ;
24+ import gwt .material .design .client .constants .CssName ;
25+ import gwt .material .design .client .js .JsMaterialElement ;
26+ import gwt .material .design .client .ui .html .ListItem ;
27+ import gwt .material .design .client .ui .html .UnorderedList ;
2328import gwt .material .design .jquery .client .api .Functions ;
2429
30+ import static gwt .material .design .client .js .JsMaterialElement .$ ;
31+
2532/**
2633 * Stateful object holding animation details.
2734 * Default behaviour is a bounce transition for 800ms.
@@ -34,9 +41,15 @@ public class MaterialAnimation {
3441 private int durationMillis = 800 ;
3542 private boolean infinite ;
3643
44+ private Timer startTimer , endTimer ;
45+
3746 public MaterialAnimation () {
3847 }
3948
49+ public MaterialAnimation (Widget widget ) {
50+ this .widget = widget ;
51+ }
52+
4053 public MaterialAnimation transition (Transition transition ) {
4154 this .transition = transition ;
4255 return this ;
@@ -58,14 +71,115 @@ public MaterialAnimation infinite(boolean infinite) {
5871 return this ;
5972 }
6073
74+ public void animate () {
75+ animate (widget );
76+ }
77+
6178 public void animate (Widget widget ) {
62- this .widget = widget ;
6379 animate (widget , null );
6480 }
6581
82+ public void animate (Functions .Func callback ) {
83+ animate (widget , callback );
84+ }
85+
6686 public void animate (Widget widget , Functions .Func callback ) {
67- this .widget = widget ;
68- MaterialAnimator .animate (transition , widget , delayMillis , durationMillis , callback , infinite );
87+ if (widget != null ) {
88+ this .widget = widget ;
89+ } else {
90+ throw new NullPointerException ("Cannot animate on a null widget." );
91+ }
92+
93+ if (startTimer != null ) {
94+ // Exit early since we are already animating.
95+ return ;
96+ }
97+
98+ final JsMaterialElement element = $ (widget .getElement ());
99+
100+ element .css ("animation-duration" , durationMillis + "ms" );
101+ element .css ("-webkit-animation-duration" , durationMillis + "ms" );
102+
103+ switch (transition ) {
104+ case SHOW_STAGGERED_LIST :
105+ if (widget instanceof UnorderedList ) {
106+ UnorderedList ul = (UnorderedList ) widget ;
107+
108+ for (Widget li : ul ) {
109+ if (li instanceof ListItem ) {
110+ li .getElement ().getStyle ().setOpacity (0 );
111+ }
112+ }
113+ }
114+ break ;
115+ case SHOW_GRID :
116+ widget .getElement ().getStyle ().setOpacity (0 );
117+ break ;
118+ default :
119+ break ;
120+ }
121+
122+ startTimer = new Timer () {
123+ @ Override
124+ public void run () {
125+ switch (transition ) {
126+ case SHOW_STAGGERED_LIST :
127+ JsMaterialElement .showStaggeredList (element );
128+ break ;
129+ case FADE_IN_IMAGE :
130+ JsMaterialElement .fadeInImage (element );
131+ break ;
132+ case SHOW_GRID :
133+ widget .addStyleName (CssName .DISPLAY_ANIMATION );
134+ JsMaterialElement .closeGrid (element );
135+ break ;
136+ case CLOSE_GRID :
137+ widget .addStyleName (CssName .DISPLAY_ANIMATION );
138+ JsMaterialElement .closeGrid (element );
139+ break ;
140+ default :
141+ // For core animation components
142+ if (infinite ) {
143+ widget .addStyleName (CssName .INFINITE );
144+ }
145+ widget .addStyleName ("animated " + transition .getCssName ());
146+
147+ // Only start the end timer if its not already active.
148+ if (endTimer == null ) {
149+ endTimer = new Timer () {
150+ @ Override
151+ public void run () {
152+ if (callback != null ) {
153+ callback .call ();
154+ }
155+ if (!infinite ) {
156+ $ (element ).removeClass ("animated " + transition .getCssName ());
157+ }
158+
159+ endTimer = null ;
160+ startTimer = null ;
161+ }
162+ };
163+ endTimer .schedule (durationMillis );
164+ }
165+ break ;
166+ }
167+ }
168+ };
169+ startTimer .schedule (delayMillis );
170+
171+ widget .removeStyleName (CssName .MATERIALIZE_CSS );
172+ }
173+
174+ /**
175+ * Stop an animation.
176+ */
177+ public void stopAnimation () {
178+ if (widget != null ) {
179+ widget .removeStyleName ("animated" );
180+ widget .removeStyleName (transition .getCssName ());
181+ widget .removeStyleName (CssName .INFINITE );
182+ }
69183 }
70184
71185 public Widget getWidget () {
0 commit comments