Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AnimationUtil.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright 2000-2018 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. callbackFunc.listener = listener;
  64. elem.addEventListener(@com.vaadin.client.AnimationUtil::ANIMATION_END_EVENT_NAME, callbackFunc, false);
  65. // Store function reference for later removal
  66. if (!elem._vaadin_animationend_callbacks) {
  67. elem._vaadin_animationend_callbacks = [];
  68. }
  69. elem._vaadin_animationend_callbacks.push(callbackFunc);
  70. return callbackFunc;
  71. }-*/;
  72. /** For internal use only. May be removed or replaced in the future. */
  73. public static native void removeAnimationEndListener(Element elem,
  74. JavaScriptObject listener)
  75. /*-{
  76. elem.removeEventListener(@com.vaadin.client.AnimationUtil::ANIMATION_END_EVENT_NAME, listener, false);
  77. }-*/;
  78. /**
  79. * Removes the given animation listener.
  80. *
  81. * @param elem
  82. * the element which has the listener
  83. * @param animationEndListener
  84. * the listener to remove
  85. * @return <code>true</code> if the listener was removed, <code>false</code>
  86. * if the listener was not registered to the given element
  87. */
  88. public static native boolean removeAnimationEndListener(Element elem,
  89. AnimationEndListener animationEndListener)
  90. /*-{
  91. if (elem._vaadin_animationend_callbacks) {
  92. var callbacks = elem._vaadin_animationend_callbacks;
  93. for (var i=0; i < callbacks.length; i++) {
  94. if (callbacks[i].listener == animationEndListener) {
  95. elem.removeEventListener(@com.vaadin.client.AnimationUtil::ANIMATION_END_EVENT_NAME, callbacks[i], false);
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. }-*/;
  102. /** For internal use only. May be removed or replaced in the future. */
  103. public static native void removeAllAnimationEndListeners(Element elem)
  104. /*-{
  105. if (elem._vaadin_animationend_callbacks) {
  106. var callbacks = elem._vaadin_animationend_callbacks;
  107. for (var i=0; i < callbacks.length; i++) {
  108. elem.removeEventListener(@com.vaadin.client.AnimationUtil::ANIMATION_END_EVENT_NAME, callbacks[i], false);
  109. }
  110. }
  111. }-*/;
  112. /** For internal use only. May be removed or replaced in the future. */
  113. public interface AnimationEndListener {
  114. public void onAnimationEnd(NativeEvent event);
  115. }
  116. /** For internal use only. May be removed or replaced in the future. */
  117. public static native String getAnimationName(NativeEvent event)
  118. /*-{
  119. if (event.webkitAnimationName)
  120. return event.webkitAnimationName;
  121. if (event.animationName)
  122. return event.animationName;
  123. if (event.mozAnimationName)
  124. return event.mozAnimationName;
  125. if (event.oAnimationName)
  126. return event.oAnimationName;
  127. return "";
  128. }-*/;
  129. /** For internal use only. May be removed or replaced in the future. */
  130. public static native String getAnimationName(ComputedStyle cstyle)
  131. /*-{
  132. var cs = cstyle.@com.vaadin.client.ComputedStyle::computedStyle;
  133. if (!cs.getPropertyValue)
  134. return "";
  135. if (cs.getPropertyValue("-webkit-animation-name"))
  136. return cs.getPropertyValue("-webkit-animation-name");
  137. if (cs.getPropertyValue("animation-name"))
  138. return cs.getPropertyValue("animation-name");
  139. if (cs.getPropertyValue("-moz-animation-name"))
  140. return cs.getPropertyValue("-moz-animation-name");
  141. if (cs.getPropertyValue("-o-animation-name"))
  142. return cs.getPropertyValue("-o-animation-name");
  143. return "";
  144. }-*/;
  145. private static final String ANIMATION_END_EVENT_NAME = whichAnimationEndEvent();
  146. private static native String whichAnimationEndEvent()
  147. /*-{
  148. var el = document.createElement('fakeelement');
  149. var anims = {
  150. 'animationName': 'animationend',
  151. 'OAnimationName': 'oAnimationEnd',
  152. 'MozAnimation': 'animationend',
  153. 'WebkitAnimation': 'webkitAnimationEnd'
  154. }
  155. for (var a in anims) {
  156. if ( el.style[a] !== undefined ) {
  157. return anims[a];
  158. }
  159. }
  160. }-*/;
  161. private static final String ANIMATION_PROPERTY_NAME = whichAnimationProperty();
  162. private static native String whichAnimationProperty()
  163. /*-{
  164. var el = document.createElement('fakeelement');
  165. var anims = [
  166. 'animation',
  167. 'oAnimation',
  168. 'mozAnimation',
  169. 'webkitAnimation'
  170. ]
  171. for (var i=0; i < anims.length; i++) {
  172. if ( el.style[anims[i]] !== undefined ) {
  173. return anims[i];
  174. }
  175. }
  176. }-*/;
  177. }