You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnimationUtil.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client;
  17. import com.google.gwt.core.client.JavaScriptObject;
  18. import com.google.gwt.dom.client.Element;
  19. import com.google.gwt.dom.client.NativeEvent;
  20. import com.google.gwt.dom.client.Style;
  21. /**
  22. * Utility methods for working with CSS transitions and animations.
  23. *
  24. * @author Vaadin Ltd
  25. * @since 7.3
  26. */
  27. public class AnimationUtil {
  28. /**
  29. * For internal use only. May be removed or replaced in the future.
  30. *
  31. * Set the animation-duration CSS property.
  32. *
  33. * @param elem
  34. * the element whose animation-duration to set
  35. * @param duration
  36. * the duration as a valid CSS value
  37. */
  38. public static void setAnimationDuration(Element elem, String duration) {
  39. Style style = elem.getStyle();
  40. style.setProperty(ANIMATION_PROPERTY_NAME + "Duration", duration);
  41. }
  42. /**
  43. * For internal use only. May be removed or replaced in the future.
  44. *
  45. * Set the animation-delay CSS property.
  46. *
  47. * @param elem
  48. * the element whose animation-delay to set
  49. * @param delay
  50. * the delay as a valid CSS value
  51. */
  52. public static void setAnimationDelay(Element elem, String delay) {
  53. Style style = elem.getStyle();
  54. style.setProperty(ANIMATION_PROPERTY_NAME + "Delay", delay);
  55. }
  56. /** For internal use only. May be removed or replaced in the future. */
  57. public static native JavaScriptObject addAnimationEndListener(Element elem,
  58. AnimationEndListener listener)
  59. /*-{
  60. var callbackFunc = $entry(function(e) {
  61. listener.@com.vaadin.client.AnimationUtil.AnimationEndListener::onAnimationEnd(Lcom/google/gwt/dom/client/NativeEvent;)(e);
  62. });
  63. elem.addEventListener(@com.vaadin.client.AnimationUtil::ANIMATION_END_EVENT_NAME, callbackFunc, false);
  64. // Store function reference for later removal
  65. if(!elem._vaadin_animationend_callbacks) {
  66. elem._vaadin_animationend_callbacks = [];
  67. }
  68. elem._vaadin_animationend_callbacks.push(callbackFunc);
  69. return callbackFunc;
  70. }-*/;
  71. /** For internal use only. May be removed or replaced in the future. */
  72. public static native void removeAnimationEndListener(Element elem,
  73. JavaScriptObject listener)
  74. /*-{
  75. elem.removeEventListener(@com.vaadin.client.AnimationUtil::ANIMATION_END_EVENT_NAME, listener, false);
  76. }-*/;
  77. /** For internal use only. May be removed or replaced in the future. */
  78. public static native void removeAllAnimationEndListeners(Element elem)
  79. /*-{
  80. if(elem._vaadin_animationend_callbacks) {
  81. var callbacks = elem._vaadin_animationend_callbacks;
  82. for(var i=0; i < callbacks.length; i++) {
  83. elem.removeEventListener(@com.vaadin.client.AnimationUtil::ANIMATION_END_EVENT_NAME, callbacks[i], false);
  84. }
  85. }
  86. }-*/;
  87. /** For internal use only. May be removed or replaced in the future. */
  88. public interface AnimationEndListener {
  89. public void onAnimationEnd(NativeEvent event);
  90. }
  91. /** For internal use only. May be removed or replaced in the future. */
  92. public static native String getAnimationName(NativeEvent event)
  93. /*-{
  94. if(event.webkitAnimationName)
  95. return event.webkitAnimationName;
  96. else if(event.animationName)
  97. return event.animationName;
  98. else if(event.mozAnimationName)
  99. return event.mozAnimationName;
  100. else if(event.oAnimationName)
  101. return event.oAnimationName;
  102. return "";
  103. }-*/;
  104. /** For internal use only. May be removed or replaced in the future. */
  105. public static native String getAnimationName(ComputedStyle cstyle)
  106. /*-{
  107. var cs = cstyle.@com.vaadin.client.ComputedStyle::computedStyle;
  108. if(!cs.getPropertyValue)
  109. return "";
  110. if(cs.getPropertyValue("-webkit-animation-name"))
  111. return cs.getPropertyValue("-webkit-animation-name");
  112. else if(cs.getPropertyValue("animation-name"))
  113. return cs.getPropertyValue("animation-name");
  114. else if(cs.getPropertyValue("-moz-animation-name"))
  115. return cs.getPropertyValue("-moz-animation-name");
  116. else if(cs.getPropertyValue("-o-animation-name"))
  117. return cs.getPropertyValue("-o-animation-name");
  118. return "";
  119. }-*/;
  120. private static final String ANIMATION_END_EVENT_NAME = whichAnimationEndEvent();
  121. private static native String whichAnimationEndEvent()
  122. /*-{
  123. var el = document.createElement('fakeelement');
  124. var anims = {
  125. 'animationName': 'animationend',
  126. 'OAnimationName': 'oAnimationEnd',
  127. 'MozAnimation': 'animationend',
  128. 'WebkitAnimation': 'webkitAnimationEnd'
  129. }
  130. for(var a in anims){
  131. if( el.style[a] !== undefined ){
  132. return anims[a];
  133. }
  134. }
  135. }-*/;
  136. private static final String ANIMATION_PROPERTY_NAME = whichAnimationProperty();
  137. private static native String whichAnimationProperty()
  138. /*-{
  139. var el = document.createElement('fakeelement');
  140. var anims = [
  141. 'animation',
  142. 'oAnimation',
  143. 'mozAnimation',
  144. 'webkitAnimation'
  145. ]
  146. for(var i=0; i < anims.length; i++) {
  147. if( el.style[anims[i]] !== undefined ){
  148. return anims[i];
  149. }
  150. }
  151. }-*/;
  152. }