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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright 2000-2014 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.dom.client.Element;
  18. import com.google.gwt.dom.client.NativeEvent;
  19. import com.google.gwt.dom.client.Style;
  20. /**
  21. * Utility methods for working with CSS transitions and animations.
  22. *
  23. * @author Vaadin Ltd
  24. */
  25. public class AnimationUtil {
  26. /**
  27. * For internal use only. May be removed or replaced in the future.
  28. *
  29. * Set the animation-duration CSS property.
  30. *
  31. * @param elem
  32. * the element whose animation-duration to set
  33. * @param duration
  34. * the duration as a valid CSS value
  35. */
  36. public static void setAnimationDuration(Element elem, String duration) {
  37. Style style = elem.getStyle();
  38. style.setProperty(ANIMATION_PROPERTY_NAME + "Duration", duration);
  39. }
  40. /**
  41. * For internal use only. May be removed or replaced in the future.
  42. *
  43. * Set the animation-delay CSS property.
  44. *
  45. * @param elem
  46. * the element whose animation-delay to set
  47. * @param delay
  48. * the delay as a valid CSS value
  49. */
  50. public static void setAnimationDelay(Element elem, String delay) {
  51. Style style = elem.getStyle();
  52. style.setProperty(ANIMATION_PROPERTY_NAME + "Delay", delay);
  53. }
  54. /** For internal use only. May be removed or replaced in the future. */
  55. public static native void registerAnimationEndEventListener(Element elem,
  56. AnimationEndListener listener)
  57. /*-{
  58. var callbackFunc = $entry(function(e) {
  59. listener.@com.vaadin.client.AnimationUtil.AnimationEndListener::onAnimationEnd(Lcom/google/gwt/dom/client/NativeEvent;)(e);
  60. });
  61. elem.addEventListener(@com.vaadin.client.AnimationUtil::ANIMATION_END_EVENT_NAME, callbackFunc, false);
  62. // Store function reference for later removal
  63. if(!elem._vaadin_animationend_callbacks) {
  64. elem._vaadin_animationend_callbacks = [];
  65. }
  66. elem._vaadin_animationend_callbacks.push(callbackFunc);
  67. }-*/;
  68. /** For internal use only. May be removed or replaced in the future. */
  69. public static native void unregisterAnimationEndEventListeners(Element elem)
  70. /*-{
  71. if(elem._vaadin_animationend_callbacks) {
  72. var callbacks = elem._vaadin_animationend_callbacks;
  73. for(var i=0; i < callbacks.length; i++) {
  74. elem.removeEventListener(@com.vaadin.client.AnimationUtil::ANIMATION_END_EVENT_NAME, callbacks[i], false);
  75. }
  76. }
  77. }-*/;
  78. /** For internal use only. May be removed or replaced in the future. */
  79. public interface AnimationEndListener {
  80. public void onAnimationEnd(NativeEvent event);
  81. }
  82. /** For internal use only. May be removed or replaced in the future. */
  83. public static native String getAnimationName(NativeEvent event)
  84. /*-{
  85. if(event.webkitAnimationName)
  86. return event.webkitAnimationName;
  87. else if(event.animationName)
  88. return event.animationName;
  89. else if(event.mozAnimationName)
  90. return event.mozAnimationName;
  91. else if(event.oAnimationName)
  92. return event.oAnimationName;
  93. return "";
  94. }-*/;
  95. /** For internal use only. May be removed or replaced in the future. */
  96. public static native String getAnimationName(ComputedStyle cstyle)
  97. /*-{
  98. var cs = cstyle.@com.vaadin.client.ComputedStyle::computedStyle;
  99. if(!cs.getPropertyValue)
  100. return "";
  101. if(cs.getPropertyValue("-webkit-animation-name"))
  102. return cs.getPropertyValue("-webkit-animation-name");
  103. else if(cs.getPropertyValue("animation-name"))
  104. return cs.getPropertyValue("animation-name");
  105. else if(cs.getPropertyValue("-moz-animation-name"))
  106. return cs.getPropertyValue("-moz-animation-name");
  107. else if(cs.getPropertyValue("-o-animation-name"))
  108. return cs.getPropertyValue("-o-animation-name");
  109. return "";
  110. }-*/;
  111. private static final String ANIMATION_END_EVENT_NAME = whichAnimationEndEvent();
  112. private static native String whichAnimationEndEvent()
  113. /*-{
  114. var el = document.createElement('fakeelement');
  115. var anims = {
  116. 'animationName': 'animationend',
  117. 'OAnimationName': 'oAnimationEnd',
  118. 'MozAnimation': 'animationend',
  119. 'WebkitAnimation': 'webkitAnimationEnd'
  120. }
  121. for(var a in anims){
  122. if( el.style[a] !== undefined ){
  123. return anims[a];
  124. }
  125. }
  126. }-*/;
  127. private static final String ANIMATION_PROPERTY_NAME = whichAnimationProperty();
  128. private static native String whichAnimationProperty()
  129. /*-{
  130. var el = document.createElement('fakeelement');
  131. var anims = [
  132. 'animation',
  133. 'oAnimation',
  134. 'mozAnimation',
  135. 'webkitAnimation'
  136. ]
  137. for(var i=0; i < anims.length; i++) {
  138. if( el.style[anims[i]] !== undefined ){
  139. return anims[i];
  140. }
  141. }
  142. }-*/;
  143. }