001 package gwt.material.design.client.ui.animate; 002 003/* 004 * #%L 005 * GwtMaterialDesign 006 * %% 007 * Copyright (C) 2015 GwtMaterial 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import com.google.gwt.user.client.DOM; 024import com.google.gwt.user.client.Timer; 025import com.google.gwt.user.client.ui.Widget; 026import gwt.material.design.client.ui.html.ListItem; 027import gwt.material.design.client.ui.html.UnorderedList; 028 029 /** 030 * Provides core and meaningful animation 031 * @author kevzlou7979 032 */ 033 public class MaterialAnimator { 034 035 public static void animate(final Transition transition, final Widget w, int delayMillis, Runnable callback) { 036 animate(transition, w, delayMillis, 800, callback, false); 037 } 038 039 public static void animate(final Transition transition, final Widget w, int delayMillis, int durationMillis) { 040 animate(transition, w, delayMillis, durationMillis, null, false); 041 } 042 043 public static void animate(final Transition transition, final Widget w, int delayMillis, boolean infinite) { 044 animate(transition, w, delayMillis, 800, null, infinite); 045 } 046 047 public static void animate(final Transition transition, final Widget w, int delayMillis) { 048 animate(transition, w, delayMillis, 800, null, false); 049 } 050 051 public static void stopAnimation(Widget w) { 052 w.removeStyleName("infinite"); 053 } 054 055 public static void animate(final Transition transition, final Widget w, int delayMillis, final int durationMillis, final Runnable callback, final boolean infinite) { 056 final String name = String.valueOf(DOM.createUniqueId()); 057 w.getElement().setId(name); 058 w.getElement().getStyle().setProperty("WebkitAnimationDuration",durationMillis+"ms"); 059 w.getElement().getStyle().setProperty("animationDuration",durationMillis+"ms"); 060 switch (transition) { 061 case SHOW_STAGGERED_LIST: 062 if(w instanceof UnorderedList) { 063 UnorderedList ul = (UnorderedList) w; 064 065 for(Widget li : ul) { 066 if(li instanceof ListItem) { 067 li.getElement().getStyle().setOpacity(0); 068 } 069 } 070 } 071 break; 072 case SHOW_GRID: 073 w.getElement().getStyle().setOpacity(0); 074 break; 075 default: 076 break; 077 } 078 079 new Timer() { 080 @Override 081 public void run() { 082 switch (transition) { 083 case SHOW_STAGGERED_LIST: 084 showStaggeredList(name); 085 break; 086 case FADE_IN_IMAGE: 087 fadeInImage(name); 088 break; 089 case SHOW_GRID: 090 w.addStyleName("display-animation"); 091 showGrid(name); 092 break; 093 case CLOSE_GRID: 094 w.addStyleName("display-animation"); 095 closeGrid(name); 096 break; 097 default: 098 // For core animation components 099 if(infinite) { 100 w.addStyleName("infinite"); 101 } 102 w.addStyleName("animated " + transition.getCssName()); 103 animationFinishedCallback(name, "animated " + transition.getCssName(), durationMillis, callback); 104 break; 105 } 106 } 107 }.schedule(delayMillis); 108 109 w.removeStyleName("materialcss"); 110 } 111 112 protected static native void animationFinishedCallback(String name, String oldClass, int durationMillis, Runnable callback) /*-{ 113 //$wnd.jQuery('#' + name).css("animationDuration", + durationMillis + "ms"); 114 $wnd.jQuery('#' + name).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() { 115 if(callback != null) { 116 callback.@java.lang.Runnable::run()(); 117 } 118 $wnd.jQuery('#' + name).removeClass(oldClass); 119 }); 120 }-*/; 121 122 protected static native void closeGrid(String name) /*-{ 123 $wnd.closeGrid('#' + name); 124 }-*/; 125 126 protected static native void showGrid(String name) /*-{ 127 $wnd.showGrid('#' + name); 128 }-*/; 129 130 protected static native void fadeInImage(String name) /*-{ 131 $wnd.Materialize.fadeInImage('#' + name); 132 }-*/; 133 134 protected static native void showStaggeredList(String name) /*-{ 135 $wnd.Materialize.showStaggeredList('#' + name); 136 }-*/; 137 }