animations.mjs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @license Angular v19.2.13
  3. * (c) 2010-2025 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. import { DOCUMENT } from '@angular/common';
  7. import * as i0 from '@angular/core';
  8. import { inject, ANIMATION_MODULE_TYPE, ViewEncapsulation, ɵRuntimeError as _RuntimeError, Injectable, Inject } from '@angular/core';
  9. import { sequence } from './private_export-faY_wCkZ.mjs';
  10. export { AUTO_STYLE, AnimationMetadataType, NoopAnimationPlayer, animate, animateChild, animation, group, keyframes, query, stagger, state, style, transition, trigger, useAnimation, AnimationGroupPlayer as ɵAnimationGroupPlayer, ɵPRE_STYLE } from './private_export-faY_wCkZ.mjs';
  11. /**
  12. * An injectable service that produces an animation sequence programmatically within an
  13. * Angular component or directive.
  14. * Provided by the `BrowserAnimationsModule` or `NoopAnimationsModule`.
  15. *
  16. * @usageNotes
  17. *
  18. * To use this service, add it to your component or directive as a dependency.
  19. * The service is instantiated along with your component.
  20. *
  21. * Apps do not typically need to create their own animation players, but if you
  22. * do need to, follow these steps:
  23. *
  24. * 1. Use the <code>[AnimationBuilder.build](api/animations/AnimationBuilder#build)()</code> method
  25. * to create a programmatic animation. The method returns an `AnimationFactory` instance.
  26. *
  27. * 2. Use the factory object to create an `AnimationPlayer` and attach it to a DOM element.
  28. *
  29. * 3. Use the player object to control the animation programmatically.
  30. *
  31. * For example:
  32. *
  33. * ```ts
  34. * // import the service from BrowserAnimationsModule
  35. * import {AnimationBuilder} from '@angular/animations';
  36. * // require the service as a dependency
  37. * class MyCmp {
  38. * constructor(private _builder: AnimationBuilder) {}
  39. *
  40. * makeAnimation(element: any) {
  41. * // first define a reusable animation
  42. * const myAnimation = this._builder.build([
  43. * style({ width: 0 }),
  44. * animate(1000, style({ width: '100px' }))
  45. * ]);
  46. *
  47. * // use the returned factory object to create a player
  48. * const player = myAnimation.create(element);
  49. *
  50. * player.play();
  51. * }
  52. * }
  53. * ```
  54. *
  55. * @publicApi
  56. */
  57. class AnimationBuilder {
  58. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: AnimationBuilder, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  59. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: AnimationBuilder, providedIn: 'root', useFactory: () => inject(BrowserAnimationBuilder) });
  60. }
  61. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: AnimationBuilder, decorators: [{
  62. type: Injectable,
  63. args: [{ providedIn: 'root', useFactory: () => inject(BrowserAnimationBuilder) }]
  64. }] });
  65. /**
  66. * A factory object returned from the
  67. * <code>[AnimationBuilder.build](api/animations/AnimationBuilder#build)()</code>
  68. * method.
  69. *
  70. * @publicApi
  71. */
  72. class AnimationFactory {
  73. }
  74. class BrowserAnimationBuilder extends AnimationBuilder {
  75. animationModuleType = inject(ANIMATION_MODULE_TYPE, { optional: true });
  76. _nextAnimationId = 0;
  77. _renderer;
  78. constructor(rootRenderer, doc) {
  79. super();
  80. const typeData = {
  81. id: '0',
  82. encapsulation: ViewEncapsulation.None,
  83. styles: [],
  84. data: { animation: [] },
  85. };
  86. this._renderer = rootRenderer.createRenderer(doc.body, typeData);
  87. if (this.animationModuleType === null && !isAnimationRenderer(this._renderer)) {
  88. // We only support AnimationRenderer & DynamicDelegationRenderer for this AnimationBuilder
  89. throw new _RuntimeError(3600 /* RuntimeErrorCode.BROWSER_ANIMATION_BUILDER_INJECTED_WITHOUT_ANIMATIONS */, (typeof ngDevMode === 'undefined' || ngDevMode) &&
  90. 'Angular detected that the `AnimationBuilder` was injected, but animation support was not enabled. ' +
  91. 'Please make sure that you enable animations in your application by calling `provideAnimations()` or `provideAnimationsAsync()` function.');
  92. }
  93. }
  94. build(animation) {
  95. const id = this._nextAnimationId;
  96. this._nextAnimationId++;
  97. const entry = Array.isArray(animation) ? sequence(animation) : animation;
  98. issueAnimationCommand(this._renderer, null, id, 'register', [entry]);
  99. return new BrowserAnimationFactory(id, this._renderer);
  100. }
  101. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: BrowserAnimationBuilder, deps: [{ token: i0.RendererFactory2 }, { token: DOCUMENT }], target: i0.ɵɵFactoryTarget.Injectable });
  102. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: BrowserAnimationBuilder, providedIn: 'root' });
  103. }
  104. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: BrowserAnimationBuilder, decorators: [{
  105. type: Injectable,
  106. args: [{ providedIn: 'root' }]
  107. }], ctorParameters: () => [{ type: i0.RendererFactory2 }, { type: Document, decorators: [{
  108. type: Inject,
  109. args: [DOCUMENT]
  110. }] }] });
  111. class BrowserAnimationFactory extends AnimationFactory {
  112. _id;
  113. _renderer;
  114. constructor(_id, _renderer) {
  115. super();
  116. this._id = _id;
  117. this._renderer = _renderer;
  118. }
  119. create(element, options) {
  120. return new RendererAnimationPlayer(this._id, element, options || {}, this._renderer);
  121. }
  122. }
  123. class RendererAnimationPlayer {
  124. id;
  125. element;
  126. _renderer;
  127. parentPlayer = null;
  128. _started = false;
  129. constructor(id, element, options, _renderer) {
  130. this.id = id;
  131. this.element = element;
  132. this._renderer = _renderer;
  133. this._command('create', options);
  134. }
  135. _listen(eventName, callback) {
  136. return this._renderer.listen(this.element, `@@${this.id}:${eventName}`, callback);
  137. }
  138. _command(command, ...args) {
  139. issueAnimationCommand(this._renderer, this.element, this.id, command, args);
  140. }
  141. onDone(fn) {
  142. this._listen('done', fn);
  143. }
  144. onStart(fn) {
  145. this._listen('start', fn);
  146. }
  147. onDestroy(fn) {
  148. this._listen('destroy', fn);
  149. }
  150. init() {
  151. this._command('init');
  152. }
  153. hasStarted() {
  154. return this._started;
  155. }
  156. play() {
  157. this._command('play');
  158. this._started = true;
  159. }
  160. pause() {
  161. this._command('pause');
  162. }
  163. restart() {
  164. this._command('restart');
  165. }
  166. finish() {
  167. this._command('finish');
  168. }
  169. destroy() {
  170. this._command('destroy');
  171. }
  172. reset() {
  173. this._command('reset');
  174. this._started = false;
  175. }
  176. setPosition(p) {
  177. this._command('setPosition', p);
  178. }
  179. getPosition() {
  180. return unwrapAnimationRenderer(this._renderer)?.engine?.players[this.id]?.getPosition() ?? 0;
  181. }
  182. totalTime = 0;
  183. }
  184. function issueAnimationCommand(renderer, element, id, command, args) {
  185. renderer.setProperty(element, `@@${id}:${command}`, args);
  186. }
  187. /**
  188. * The following 2 methods cannot reference their correct types (AnimationRenderer &
  189. * DynamicDelegationRenderer) since this would introduce a import cycle.
  190. */
  191. function unwrapAnimationRenderer(renderer) {
  192. const type = renderer.ɵtype;
  193. if (type === 0 /* AnimationRendererType.Regular */) {
  194. return renderer;
  195. }
  196. else if (type === 1 /* AnimationRendererType.Delegated */) {
  197. return renderer.animationRenderer;
  198. }
  199. return null;
  200. }
  201. function isAnimationRenderer(renderer) {
  202. const type = renderer.ɵtype;
  203. return type === 0 /* AnimationRendererType.Regular */ || type === 1 /* AnimationRendererType.Delegated */;
  204. }
  205. export { AnimationBuilder, AnimationFactory, sequence, BrowserAnimationBuilder as ɵBrowserAnimationBuilder };
  206. //# sourceMappingURL=animations.mjs.map