Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AnimationUtil.java 6.9KB

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