animation.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { p as printIonError } from './index4.js';
  5. import { w as win } from './index6.js';
  6. let animationPrefix;
  7. const getAnimationPrefix = (el) => {
  8. if (animationPrefix === undefined) {
  9. const supportsUnprefixed = el.style.animationName !== undefined;
  10. const supportsWebkitPrefix = el.style.webkitAnimationName !== undefined;
  11. animationPrefix = !supportsUnprefixed && supportsWebkitPrefix ? '-webkit-' : '';
  12. }
  13. return animationPrefix;
  14. };
  15. const setStyleProperty = (element, propertyName, value) => {
  16. const prefix = propertyName.startsWith('animation') ? getAnimationPrefix(element) : '';
  17. element.style.setProperty(prefix + propertyName, value);
  18. };
  19. const addClassToArray = (classes = [], className) => {
  20. if (className !== undefined) {
  21. const classNameToAppend = Array.isArray(className) ? className : [className];
  22. return [...classes, ...classNameToAppend];
  23. }
  24. return classes;
  25. };
  26. const createAnimation = (animationId) => {
  27. let _delay;
  28. let _duration;
  29. let _easing;
  30. let _iterations;
  31. let _fill;
  32. let _direction;
  33. let _keyframes = [];
  34. let beforeAddClasses = [];
  35. let beforeRemoveClasses = [];
  36. let initialized = false;
  37. let parentAnimation;
  38. let beforeStylesValue = {};
  39. let afterAddClasses = [];
  40. let afterRemoveClasses = [];
  41. let afterStylesValue = {};
  42. let numAnimationsRunning = 0;
  43. let shouldForceLinearEasing = false;
  44. let shouldForceSyncPlayback = false;
  45. let forceDirectionValue;
  46. let forceDurationValue;
  47. let forceDelayValue;
  48. let willComplete = true;
  49. let finished = false;
  50. let shouldCalculateNumAnimations = true;
  51. let ani;
  52. let paused = false;
  53. const id = animationId;
  54. const onFinishCallbacks = [];
  55. const onFinishOneTimeCallbacks = [];
  56. const onStopOneTimeCallbacks = [];
  57. const elements = [];
  58. const childAnimations = [];
  59. const stylesheets = [];
  60. const _beforeAddReadFunctions = [];
  61. const _beforeAddWriteFunctions = [];
  62. const _afterAddReadFunctions = [];
  63. const _afterAddWriteFunctions = [];
  64. const webAnimations = [];
  65. const supportsAnimationEffect = typeof AnimationEffect === 'function' ||
  66. (win !== undefined && typeof win.AnimationEffect === 'function');
  67. /**
  68. * This is a feature detection for Web Animations.
  69. *
  70. * Certain environments such as emulated browser environments for testing,
  71. * do not support Web Animations. As a result, we need to check for support
  72. * and provide a fallback to test certain functionality related to Web Animations.
  73. */
  74. const supportsWebAnimations = typeof Element === 'function' &&
  75. typeof Element.prototype.animate === 'function' &&
  76. supportsAnimationEffect;
  77. const getWebAnimations = () => {
  78. return webAnimations;
  79. };
  80. const destroy = (clearStyleSheets) => {
  81. childAnimations.forEach((childAnimation) => {
  82. childAnimation.destroy(clearStyleSheets);
  83. });
  84. cleanUp(clearStyleSheets);
  85. elements.length = 0;
  86. childAnimations.length = 0;
  87. _keyframes.length = 0;
  88. clearOnFinish();
  89. initialized = false;
  90. shouldCalculateNumAnimations = true;
  91. return ani;
  92. };
  93. /**
  94. * Cancels any Web Animations, removes
  95. * any animation properties from the
  96. * animation's elements, and removes the
  97. * animation's stylesheets from the DOM.
  98. */
  99. const cleanUp = (clearStyleSheets) => {
  100. cleanUpElements();
  101. if (clearStyleSheets) {
  102. cleanUpStyleSheets();
  103. }
  104. };
  105. const resetFlags = () => {
  106. shouldForceLinearEasing = false;
  107. shouldForceSyncPlayback = false;
  108. shouldCalculateNumAnimations = true;
  109. forceDirectionValue = undefined;
  110. forceDurationValue = undefined;
  111. forceDelayValue = undefined;
  112. numAnimationsRunning = 0;
  113. finished = false;
  114. willComplete = true;
  115. paused = false;
  116. };
  117. const isRunning = () => {
  118. return numAnimationsRunning !== 0 && !paused;
  119. };
  120. /**
  121. * @internal
  122. * Remove a callback from a chosen callback array
  123. * @param callbackToRemove: A reference to the callback that should be removed
  124. * @param callbackObjects: An array of callbacks that callbackToRemove should be removed from.
  125. */
  126. const clearCallback = (callbackToRemove, callbackObjects) => {
  127. const index = callbackObjects.findIndex((callbackObject) => callbackObject.c === callbackToRemove);
  128. if (index > -1) {
  129. callbackObjects.splice(index, 1);
  130. }
  131. };
  132. /**
  133. * @internal
  134. * Add a callback to be fired when an animation is stopped/cancelled.
  135. * @param callback: A reference to the callback that should be fired
  136. * @param opts: Any options associated with this particular callback
  137. */
  138. const onStop = (callback, opts) => {
  139. onStopOneTimeCallbacks.push({ c: callback, o: opts });
  140. return ani;
  141. };
  142. const onFinish = (callback, opts) => {
  143. const callbacks = (opts === null || opts === void 0 ? void 0 : opts.oneTimeCallback) ? onFinishOneTimeCallbacks : onFinishCallbacks;
  144. callbacks.push({ c: callback, o: opts });
  145. return ani;
  146. };
  147. const clearOnFinish = () => {
  148. onFinishCallbacks.length = 0;
  149. onFinishOneTimeCallbacks.length = 0;
  150. return ani;
  151. };
  152. /**
  153. * Cancels any Web Animations and removes
  154. * any animation properties from the
  155. * the animation's elements.
  156. */
  157. const cleanUpElements = () => {
  158. if (supportsWebAnimations) {
  159. webAnimations.forEach((animation) => {
  160. animation.cancel();
  161. });
  162. webAnimations.length = 0;
  163. }
  164. };
  165. /**
  166. * Removes the animation's stylesheets
  167. * from the DOM.
  168. */
  169. const cleanUpStyleSheets = () => {
  170. stylesheets.forEach((stylesheet) => {
  171. /**
  172. * When sharing stylesheets, it's possible
  173. * for another animation to have already
  174. * cleaned up a particular stylesheet
  175. */
  176. if (stylesheet === null || stylesheet === void 0 ? void 0 : stylesheet.parentNode) {
  177. stylesheet.parentNode.removeChild(stylesheet);
  178. }
  179. });
  180. stylesheets.length = 0;
  181. };
  182. const beforeAddRead = (readFn) => {
  183. _beforeAddReadFunctions.push(readFn);
  184. return ani;
  185. };
  186. const beforeAddWrite = (writeFn) => {
  187. _beforeAddWriteFunctions.push(writeFn);
  188. return ani;
  189. };
  190. const afterAddRead = (readFn) => {
  191. _afterAddReadFunctions.push(readFn);
  192. return ani;
  193. };
  194. const afterAddWrite = (writeFn) => {
  195. _afterAddWriteFunctions.push(writeFn);
  196. return ani;
  197. };
  198. const beforeAddClass = (className) => {
  199. beforeAddClasses = addClassToArray(beforeAddClasses, className);
  200. return ani;
  201. };
  202. const beforeRemoveClass = (className) => {
  203. beforeRemoveClasses = addClassToArray(beforeRemoveClasses, className);
  204. return ani;
  205. };
  206. /**
  207. * Set CSS inline styles to the animation's
  208. * elements before the animation begins.
  209. */
  210. const beforeStyles = (styles = {}) => {
  211. beforeStylesValue = styles;
  212. return ani;
  213. };
  214. /**
  215. * Clear CSS inline styles from the animation's
  216. * elements before the animation begins.
  217. */
  218. const beforeClearStyles = (propertyNames = []) => {
  219. for (const property of propertyNames) {
  220. beforeStylesValue[property] = '';
  221. }
  222. return ani;
  223. };
  224. const afterAddClass = (className) => {
  225. afterAddClasses = addClassToArray(afterAddClasses, className);
  226. return ani;
  227. };
  228. const afterRemoveClass = (className) => {
  229. afterRemoveClasses = addClassToArray(afterRemoveClasses, className);
  230. return ani;
  231. };
  232. const afterStyles = (styles = {}) => {
  233. afterStylesValue = styles;
  234. return ani;
  235. };
  236. const afterClearStyles = (propertyNames = []) => {
  237. for (const property of propertyNames) {
  238. afterStylesValue[property] = '';
  239. }
  240. return ani;
  241. };
  242. const getFill = () => {
  243. if (_fill !== undefined) {
  244. return _fill;
  245. }
  246. if (parentAnimation) {
  247. return parentAnimation.getFill();
  248. }
  249. return 'both';
  250. };
  251. const getDirection = () => {
  252. if (forceDirectionValue !== undefined) {
  253. return forceDirectionValue;
  254. }
  255. if (_direction !== undefined) {
  256. return _direction;
  257. }
  258. if (parentAnimation) {
  259. return parentAnimation.getDirection();
  260. }
  261. return 'normal';
  262. };
  263. const getEasing = () => {
  264. if (shouldForceLinearEasing) {
  265. return 'linear';
  266. }
  267. if (_easing !== undefined) {
  268. return _easing;
  269. }
  270. if (parentAnimation) {
  271. return parentAnimation.getEasing();
  272. }
  273. return 'linear';
  274. };
  275. const getDuration = () => {
  276. if (shouldForceSyncPlayback) {
  277. return 0;
  278. }
  279. if (forceDurationValue !== undefined) {
  280. return forceDurationValue;
  281. }
  282. if (_duration !== undefined) {
  283. return _duration;
  284. }
  285. if (parentAnimation) {
  286. return parentAnimation.getDuration();
  287. }
  288. return 0;
  289. };
  290. const getIterations = () => {
  291. if (_iterations !== undefined) {
  292. return _iterations;
  293. }
  294. if (parentAnimation) {
  295. return parentAnimation.getIterations();
  296. }
  297. return 1;
  298. };
  299. const getDelay = () => {
  300. if (forceDelayValue !== undefined) {
  301. return forceDelayValue;
  302. }
  303. if (_delay !== undefined) {
  304. return _delay;
  305. }
  306. if (parentAnimation) {
  307. return parentAnimation.getDelay();
  308. }
  309. return 0;
  310. };
  311. const getKeyframes = () => {
  312. return _keyframes;
  313. };
  314. const direction = (animationDirection) => {
  315. _direction = animationDirection;
  316. update(true);
  317. return ani;
  318. };
  319. const fill = (animationFill) => {
  320. _fill = animationFill;
  321. update(true);
  322. return ani;
  323. };
  324. const delay = (animationDelay) => {
  325. _delay = animationDelay;
  326. update(true);
  327. return ani;
  328. };
  329. const easing = (animationEasing) => {
  330. _easing = animationEasing;
  331. update(true);
  332. return ani;
  333. };
  334. const duration = (animationDuration) => {
  335. /**
  336. * CSS Animation Durations of 0ms work fine on Chrome
  337. * but do not run on Safari, so force it to 1ms to
  338. * get it to run on both platforms.
  339. */
  340. if (!supportsWebAnimations && animationDuration === 0) {
  341. animationDuration = 1;
  342. }
  343. _duration = animationDuration;
  344. update(true);
  345. return ani;
  346. };
  347. const iterations = (animationIterations) => {
  348. _iterations = animationIterations;
  349. update(true);
  350. return ani;
  351. };
  352. const parent = (animation) => {
  353. parentAnimation = animation;
  354. return ani;
  355. };
  356. const addElement = (el) => {
  357. if (el != null) {
  358. if (el.nodeType === 1) {
  359. elements.push(el);
  360. }
  361. else if (el.length >= 0) {
  362. for (let i = 0; i < el.length; i++) {
  363. elements.push(el[i]);
  364. }
  365. }
  366. else {
  367. printIonError('createAnimation - Invalid addElement value.');
  368. }
  369. }
  370. return ani;
  371. };
  372. const addAnimation = (animationToAdd) => {
  373. if (animationToAdd != null) {
  374. if (Array.isArray(animationToAdd)) {
  375. for (const animation of animationToAdd) {
  376. animation.parent(ani);
  377. childAnimations.push(animation);
  378. }
  379. }
  380. else {
  381. animationToAdd.parent(ani);
  382. childAnimations.push(animationToAdd);
  383. }
  384. }
  385. return ani;
  386. };
  387. const keyframes = (keyframeValues) => {
  388. const different = _keyframes !== keyframeValues;
  389. _keyframes = keyframeValues;
  390. if (different) {
  391. updateKeyframes(_keyframes);
  392. }
  393. return ani;
  394. };
  395. const updateKeyframes = (keyframeValues) => {
  396. if (supportsWebAnimations) {
  397. getWebAnimations().forEach((animation) => {
  398. /**
  399. * animation.effect's type is AnimationEffect.
  400. * However, in this case we have a more specific
  401. * type of AnimationEffect called KeyframeEffect which
  402. * inherits from AnimationEffect. As a result,
  403. * we cast animation.effect to KeyframeEffect.
  404. */
  405. const keyframeEffect = animation.effect;
  406. /**
  407. * setKeyframes is not supported in all browser
  408. * versions that Ionic supports, so we need to
  409. * check for support before using it.
  410. */
  411. // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
  412. if (keyframeEffect.setKeyframes) {
  413. keyframeEffect.setKeyframes(keyframeValues);
  414. }
  415. else {
  416. const newEffect = new KeyframeEffect(keyframeEffect.target, keyframeValues, keyframeEffect.getTiming());
  417. animation.effect = newEffect;
  418. }
  419. });
  420. }
  421. };
  422. /**
  423. * Run all "before" animation hooks.
  424. */
  425. const beforeAnimation = () => {
  426. // Runs all before read callbacks
  427. _beforeAddReadFunctions.forEach((callback) => callback());
  428. // Runs all before write callbacks
  429. _beforeAddWriteFunctions.forEach((callback) => callback());
  430. // Updates styles and classes before animation runs
  431. const addClasses = beforeAddClasses;
  432. const removeClasses = beforeRemoveClasses;
  433. const styles = beforeStylesValue;
  434. elements.forEach((el) => {
  435. const elementClassList = el.classList;
  436. addClasses.forEach((c) => elementClassList.add(c));
  437. removeClasses.forEach((c) => elementClassList.remove(c));
  438. for (const property in styles) {
  439. // eslint-disable-next-line no-prototype-builtins
  440. if (styles.hasOwnProperty(property)) {
  441. setStyleProperty(el, property, styles[property]);
  442. }
  443. }
  444. });
  445. };
  446. /**
  447. * Run all "after" animation hooks.
  448. */
  449. const afterAnimation = () => {
  450. // Runs all after read callbacks
  451. _afterAddReadFunctions.forEach((callback) => callback());
  452. // Runs all after write callbacks
  453. _afterAddWriteFunctions.forEach((callback) => callback());
  454. // Updates styles and classes before animation ends
  455. const currentStep = willComplete ? 1 : 0;
  456. const addClasses = afterAddClasses;
  457. const removeClasses = afterRemoveClasses;
  458. const styles = afterStylesValue;
  459. elements.forEach((el) => {
  460. const elementClassList = el.classList;
  461. addClasses.forEach((c) => elementClassList.add(c));
  462. removeClasses.forEach((c) => elementClassList.remove(c));
  463. for (const property in styles) {
  464. // eslint-disable-next-line no-prototype-builtins
  465. if (styles.hasOwnProperty(property)) {
  466. setStyleProperty(el, property, styles[property]);
  467. }
  468. }
  469. });
  470. /**
  471. * Clean up any value coercion before
  472. * the user callbacks fire otherwise
  473. * they may get stale values. For example,
  474. * if someone calls progressStart(0) the
  475. * animation may still be reversed.
  476. */
  477. forceDurationValue = undefined;
  478. forceDirectionValue = undefined;
  479. forceDelayValue = undefined;
  480. onFinishCallbacks.forEach((onFinishCallback) => {
  481. return onFinishCallback.c(currentStep, ani);
  482. });
  483. onFinishOneTimeCallbacks.forEach((onFinishCallback) => {
  484. return onFinishCallback.c(currentStep, ani);
  485. });
  486. onFinishOneTimeCallbacks.length = 0;
  487. shouldCalculateNumAnimations = true;
  488. if (willComplete) {
  489. finished = true;
  490. }
  491. willComplete = true;
  492. };
  493. const animationFinish = () => {
  494. if (numAnimationsRunning === 0) {
  495. return;
  496. }
  497. numAnimationsRunning--;
  498. if (numAnimationsRunning === 0) {
  499. afterAnimation();
  500. if (parentAnimation) {
  501. parentAnimation.animationFinish();
  502. }
  503. }
  504. };
  505. const initializeWebAnimation = () => {
  506. elements.forEach((element) => {
  507. const animation = element.animate(_keyframes, {
  508. id,
  509. delay: getDelay(),
  510. duration: getDuration(),
  511. easing: getEasing(),
  512. iterations: getIterations(),
  513. fill: getFill(),
  514. direction: getDirection(),
  515. });
  516. animation.pause();
  517. webAnimations.push(animation);
  518. });
  519. if (webAnimations.length > 0) {
  520. webAnimations[0].onfinish = () => {
  521. animationFinish();
  522. };
  523. }
  524. };
  525. const initializeAnimation = () => {
  526. beforeAnimation();
  527. if (_keyframes.length > 0) {
  528. if (supportsWebAnimations) {
  529. initializeWebAnimation();
  530. }
  531. }
  532. initialized = true;
  533. };
  534. const setAnimationStep = (step) => {
  535. step = Math.min(Math.max(step, 0), 0.9999);
  536. if (supportsWebAnimations) {
  537. webAnimations.forEach((animation) => {
  538. // When creating the animation the delay is guaranteed to be set to a number.
  539. animation.currentTime = animation.effect.getComputedTiming().delay + getDuration() * step;
  540. animation.pause();
  541. });
  542. }
  543. };
  544. const updateWebAnimation = (step) => {
  545. webAnimations.forEach((animation) => {
  546. animation.effect.updateTiming({
  547. delay: getDelay(),
  548. duration: getDuration(),
  549. easing: getEasing(),
  550. iterations: getIterations(),
  551. fill: getFill(),
  552. direction: getDirection(),
  553. });
  554. });
  555. if (step !== undefined) {
  556. setAnimationStep(step);
  557. }
  558. };
  559. const update = (deep = false, toggleAnimationName = true, step) => {
  560. if (deep) {
  561. childAnimations.forEach((animation) => {
  562. animation.update(deep, toggleAnimationName, step);
  563. });
  564. }
  565. if (supportsWebAnimations) {
  566. updateWebAnimation(step);
  567. }
  568. return ani;
  569. };
  570. const progressStart = (forceLinearEasing = false, step) => {
  571. childAnimations.forEach((animation) => {
  572. animation.progressStart(forceLinearEasing, step);
  573. });
  574. pauseAnimation();
  575. shouldForceLinearEasing = forceLinearEasing;
  576. if (!initialized) {
  577. initializeAnimation();
  578. }
  579. update(false, true, step);
  580. return ani;
  581. };
  582. const progressStep = (step) => {
  583. childAnimations.forEach((animation) => {
  584. animation.progressStep(step);
  585. });
  586. setAnimationStep(step);
  587. return ani;
  588. };
  589. const progressEnd = (playTo, step, dur) => {
  590. shouldForceLinearEasing = false;
  591. childAnimations.forEach((animation) => {
  592. animation.progressEnd(playTo, step, dur);
  593. });
  594. if (dur !== undefined) {
  595. forceDurationValue = dur;
  596. }
  597. finished = false;
  598. willComplete = true;
  599. if (playTo === 0) {
  600. forceDirectionValue = getDirection() === 'reverse' ? 'normal' : 'reverse';
  601. if (forceDirectionValue === 'reverse') {
  602. willComplete = false;
  603. }
  604. if (supportsWebAnimations) {
  605. update();
  606. setAnimationStep(1 - step);
  607. }
  608. else {
  609. forceDelayValue = (1 - step) * getDuration() * -1;
  610. update(false, false);
  611. }
  612. }
  613. else if (playTo === 1) {
  614. if (supportsWebAnimations) {
  615. update();
  616. setAnimationStep(step);
  617. }
  618. else {
  619. forceDelayValue = step * getDuration() * -1;
  620. update(false, false);
  621. }
  622. }
  623. if (playTo !== undefined && !parentAnimation) {
  624. play();
  625. }
  626. return ani;
  627. };
  628. const pauseAnimation = () => {
  629. if (initialized) {
  630. if (supportsWebAnimations) {
  631. webAnimations.forEach((animation) => {
  632. animation.pause();
  633. });
  634. }
  635. else {
  636. elements.forEach((element) => {
  637. setStyleProperty(element, 'animation-play-state', 'paused');
  638. });
  639. }
  640. paused = true;
  641. }
  642. };
  643. const pause = () => {
  644. childAnimations.forEach((animation) => {
  645. animation.pause();
  646. });
  647. pauseAnimation();
  648. return ani;
  649. };
  650. const playCSSAnimations = () => {
  651. animationFinish();
  652. };
  653. const playWebAnimations = () => {
  654. webAnimations.forEach((animation) => {
  655. animation.play();
  656. });
  657. if (_keyframes.length === 0 || elements.length === 0) {
  658. animationFinish();
  659. }
  660. };
  661. const resetAnimation = () => {
  662. if (supportsWebAnimations) {
  663. setAnimationStep(0);
  664. updateWebAnimation();
  665. }
  666. };
  667. const play = (opts) => {
  668. return new Promise((resolve) => {
  669. if (opts === null || opts === void 0 ? void 0 : opts.sync) {
  670. shouldForceSyncPlayback = true;
  671. onFinish(() => (shouldForceSyncPlayback = false), { oneTimeCallback: true });
  672. }
  673. if (!initialized) {
  674. initializeAnimation();
  675. }
  676. if (finished) {
  677. resetAnimation();
  678. finished = false;
  679. }
  680. if (shouldCalculateNumAnimations) {
  681. numAnimationsRunning = childAnimations.length + 1;
  682. shouldCalculateNumAnimations = false;
  683. }
  684. /**
  685. * When one of these callbacks fires we
  686. * need to clear the other's callback otherwise
  687. * you can potentially get these callbacks
  688. * firing multiple times if the play method
  689. * is subsequently called.
  690. * Example:
  691. * animation.play() (onStop and onFinish callbacks are registered)
  692. * animation.stop() (onStop callback is fired, onFinish is not)
  693. * animation.play() (onStop and onFinish callbacks are registered)
  694. * Total onStop callbacks: 1
  695. * Total onFinish callbacks: 2
  696. */
  697. const onStopCallback = () => {
  698. clearCallback(onFinishCallback, onFinishOneTimeCallbacks);
  699. resolve();
  700. };
  701. const onFinishCallback = () => {
  702. clearCallback(onStopCallback, onStopOneTimeCallbacks);
  703. resolve();
  704. };
  705. /**
  706. * The play method resolves when an animation
  707. * run either finishes or is cancelled.
  708. */
  709. onFinish(onFinishCallback, { oneTimeCallback: true });
  710. onStop(onStopCallback, { oneTimeCallback: true });
  711. childAnimations.forEach((animation) => {
  712. animation.play();
  713. });
  714. if (supportsWebAnimations) {
  715. playWebAnimations();
  716. }
  717. else {
  718. playCSSAnimations();
  719. }
  720. paused = false;
  721. });
  722. };
  723. /**
  724. * Stops an animation and resets it state to the
  725. * beginning. This does not fire any onFinish
  726. * callbacks because the animation did not finish.
  727. * However, since the animation was not destroyed
  728. * (i.e. the animation could run again) we do not
  729. * clear the onFinish callbacks.
  730. */
  731. const stop = () => {
  732. childAnimations.forEach((animation) => {
  733. animation.stop();
  734. });
  735. if (initialized) {
  736. cleanUpElements();
  737. initialized = false;
  738. }
  739. resetFlags();
  740. onStopOneTimeCallbacks.forEach((onStopCallback) => onStopCallback.c(0, ani));
  741. onStopOneTimeCallbacks.length = 0;
  742. };
  743. const from = (property, value) => {
  744. const firstFrame = _keyframes[0];
  745. if (firstFrame !== undefined && (firstFrame.offset === undefined || firstFrame.offset === 0)) {
  746. firstFrame[property] = value;
  747. }
  748. else {
  749. _keyframes = [{ offset: 0, [property]: value }, ..._keyframes];
  750. }
  751. return ani;
  752. };
  753. const to = (property, value) => {
  754. const lastFrame = _keyframes[_keyframes.length - 1];
  755. if (lastFrame !== undefined && (lastFrame.offset === undefined || lastFrame.offset === 1)) {
  756. lastFrame[property] = value;
  757. }
  758. else {
  759. _keyframes = [..._keyframes, { offset: 1, [property]: value }];
  760. }
  761. return ani;
  762. };
  763. const fromTo = (property, fromValue, toValue) => {
  764. return from(property, fromValue).to(property, toValue);
  765. };
  766. return (ani = {
  767. parentAnimation,
  768. elements,
  769. childAnimations,
  770. id,
  771. animationFinish,
  772. from,
  773. to,
  774. fromTo,
  775. parent,
  776. play,
  777. pause,
  778. stop,
  779. destroy,
  780. keyframes,
  781. addAnimation,
  782. addElement,
  783. update,
  784. fill,
  785. direction,
  786. iterations,
  787. duration,
  788. easing,
  789. delay,
  790. getWebAnimations,
  791. getKeyframes,
  792. getFill,
  793. getDirection,
  794. getDelay,
  795. getIterations,
  796. getEasing,
  797. getDuration,
  798. afterAddRead,
  799. afterAddWrite,
  800. afterClearStyles,
  801. afterStyles,
  802. afterRemoveClass,
  803. afterAddClass,
  804. beforeAddRead,
  805. beforeAddWrite,
  806. beforeClearStyles,
  807. beforeStyles,
  808. beforeRemoveClass,
  809. beforeAddClass,
  810. onFinish,
  811. isRunning,
  812. progressStart,
  813. progressStep,
  814. progressEnd,
  815. });
  816. };
  817. export { createAnimation as c };