AnimationHelper.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Storytelling
  5. * (C) Copyright IBM Corp. 2017, 2018
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['baglass/core-client/js/core-client/ui/core/Class', 'baglass/core-client/js/core-client/utils/BrowserUtils'], function (Class, BrowserUtils) {
  9. var AnimationHelper = Class.extend({
  10. // Not zero for IE on Windows 10. Bug reporeted to Microsoft.
  11. SCALE_ZERO: BrowserUtils.isIE() ? 'scale(0.00001)' : 'scale(0)',
  12. /**
  13. * Allows the browser to animate the properties in animationCSS using the 'transition' property.
  14. *
  15. * the caller must provide the transition values in the css parameter/
  16. *
  17. *@param {jquery} $el - the element to animate.
  18. *@param {object} css - CSS properties.
  19. * ex:
  20. * {
  21. * opacity: 1,
  22. * transition: "opacity 1s"
  23. * }
  24. *@returns promise that is resolved when the animation is done.
  25. */
  26. Animate: function Animate($el, css) {
  27. var isResolved = false;
  28. if (!$el || !$el.length || !css) {
  29. return Promise.resolve();
  30. }
  31. return new Promise(function (resolve) {
  32. var transitionend = function transitionend(event) {
  33. isResolved = true;
  34. resolve();
  35. event.target.removeEventListener('transitionend', transitionend);
  36. };
  37. // Set a timeout so that if the animation doesn't complete for some reason, all of the
  38. // code waiting for this promise to resolve isn't held up.
  39. setTimeout(function () {
  40. if (!isResolved) {
  41. resolve();
  42. }
  43. }, 1100);
  44. $el[0].addEventListener('transitionend', transitionend);
  45. // we are setting multiple conflicting css properties in a row and we want them to be all executed.
  46. // the browser does not always do this so we add the read to force a "repaint".
  47. //see: http://stackoverflow.com/questions/7974761/in-jquery-is-it-possible-to-get-callback-function-after-setting-new-css-rule
  48. $el.css('transform');
  49. $el.css(css);
  50. });
  51. }
  52. });
  53. return new AnimationHelper();
  54. });
  55. //# sourceMappingURL=AnimationHelper.js.map