|
| 1 | +import { AnimationBuilder, AnimationPlayer, AnimationReferenceMetadata, useAnimation } from '@angular/animations'; |
| 2 | +import { Directive, ElementRef, EventEmitter, OnDestroy } from '@angular/core'; |
| 3 | +import { noop, Subject } from 'rxjs'; |
| 4 | +import { growVerIn, growVerOut } from '../animations/grow'; |
| 5 | + |
| 6 | +/**@hidden @internal */ |
| 7 | +export interface ToggleAnimationSettings { |
| 8 | + openAnimation: AnimationReferenceMetadata; |
| 9 | + closeAnimation: AnimationReferenceMetadata; |
| 10 | +} |
| 11 | + |
| 12 | +export interface ToggleAnimationOwner { |
| 13 | + animationSettings: ToggleAnimationSettings; |
| 14 | + openAnimationStart: EventEmitter<void>; |
| 15 | + openAnimationDone: EventEmitter<void>; |
| 16 | + closeAnimationStart: EventEmitter<void>; |
| 17 | + closeAnimationDone: EventEmitter<void>; |
| 18 | + openAnimationPlayer: AnimationPlayer; |
| 19 | + closeAnimationPlayer: AnimationPlayer; |
| 20 | + playOpenAnimation(element: ElementRef, onDone: () => void): void; |
| 21 | + playCloseAnimation(element: ElementRef, onDone: () => void): void; |
| 22 | +} |
| 23 | + |
| 24 | +enum ANIMATION_TYPE { |
| 25 | + OPEN = 'open', |
| 26 | + CLOSE = 'close', |
| 27 | +} |
| 28 | + |
| 29 | +/**@hidden @internal */ |
| 30 | +@Directive() |
| 31 | +// eslint-disable-next-line @angular-eslint/directive-class-suffix |
| 32 | +export abstract class ToggleAnimationPlayer implements ToggleAnimationOwner, OnDestroy { |
| 33 | + |
| 34 | + |
| 35 | + /** @hidden @internal */ |
| 36 | + public openAnimationDone: EventEmitter<void> = new EventEmitter(); |
| 37 | + /** @hidden @internal */ |
| 38 | + public closeAnimationDone: EventEmitter<void> = new EventEmitter(); |
| 39 | + /** @hidden @internal */ |
| 40 | + public openAnimationStart: EventEmitter<void> = new EventEmitter(); |
| 41 | + /** @hidden @internal */ |
| 42 | + public closeAnimationStart: EventEmitter<void> = new EventEmitter(); |
| 43 | + |
| 44 | + public get animationSettings(): ToggleAnimationSettings { |
| 45 | + return this._animationSettings; |
| 46 | + } |
| 47 | + public set animationSettings(value: ToggleAnimationSettings) { |
| 48 | + this._animationSettings = value; |
| 49 | + } |
| 50 | + |
| 51 | + /** @hidden @internal */ |
| 52 | + public openAnimationPlayer: AnimationPlayer = null; |
| 53 | + |
| 54 | + /** @hidden @internal */ |
| 55 | + public closeAnimationPlayer: AnimationPlayer = null; |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + protected destroy$: Subject<void> = new Subject(); |
| 60 | + protected players: Map<string, AnimationPlayer> = new Map(); |
| 61 | + protected _animationSettings: ToggleAnimationSettings = { |
| 62 | + openAnimation: growVerIn, |
| 63 | + closeAnimation: growVerOut |
| 64 | + }; |
| 65 | + |
| 66 | + private _defaultClosedCallback = noop; |
| 67 | + private _defaultOpenedCallback = noop; |
| 68 | + private onClosedCallback: () => any = this._defaultClosedCallback; |
| 69 | + private onOpenedCallback: () => any = this._defaultOpenedCallback; |
| 70 | + |
| 71 | + constructor(protected builder: AnimationBuilder) { |
| 72 | + } |
| 73 | + |
| 74 | + /** @hidden @internal */ |
| 75 | + public playOpenAnimation(targetElement: ElementRef, onDone?: () => void): void { |
| 76 | + this.startPlayer(ANIMATION_TYPE.OPEN, targetElement, onDone || this._defaultOpenedCallback); |
| 77 | + } |
| 78 | + |
| 79 | + /** @hidden @internal */ |
| 80 | + public playCloseAnimation(targetElement: ElementRef, onDone?: () => void): void { |
| 81 | + this.startPlayer(ANIMATION_TYPE.CLOSE, targetElement, onDone || this._defaultClosedCallback); |
| 82 | + } |
| 83 | + public ngOnDestroy() { |
| 84 | + this.destroy$.next(); |
| 85 | + this.destroy$.complete(); |
| 86 | + } |
| 87 | + |
| 88 | + private startPlayer(type: ANIMATION_TYPE, targetElement: ElementRef, callback: () => void): void { |
| 89 | + if (!targetElement) { // if no element is passed, there is nothing to animate |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + let target = this.getPlayer(type); |
| 94 | + if (!target) { |
| 95 | + target = this.initializePlayer(type, targetElement, callback); |
| 96 | + } |
| 97 | + |
| 98 | + if (target.hasStarted()) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + const targetEmitter = type === ANIMATION_TYPE.OPEN ? this.openAnimationStart : this.closeAnimationStart; |
| 103 | + targetEmitter.emit(); |
| 104 | + target.play(); |
| 105 | + } |
| 106 | + |
| 107 | + private initializePlayer(type: ANIMATION_TYPE, targetElement: ElementRef, callback: () => void): AnimationPlayer { |
| 108 | + const oppositeType = type === ANIMATION_TYPE.OPEN ? ANIMATION_TYPE.CLOSE : ANIMATION_TYPE.OPEN; |
| 109 | + const animationSettings = type === ANIMATION_TYPE.OPEN ? |
| 110 | + this.animationSettings.openAnimation : this.animationSettings.closeAnimation; |
| 111 | + const animation = useAnimation(animationSettings); |
| 112 | + const animationBuilder = this.builder.build(animation); |
| 113 | + const opposite = this.getPlayer(oppositeType); |
| 114 | + let oppositePosition = 1; |
| 115 | + if (opposite) { |
| 116 | + if (opposite.hasStarted()) { |
| 117 | + // .getPosition() still returns 0 sometimes, regardless of the fix for https://github.com/angular/angular/issues/18891; |
| 118 | + oppositePosition = (opposite as any)._renderer.engine.players[0].getPosition(); |
| 119 | + } |
| 120 | + this.cleanUpPlayer(oppositeType); |
| 121 | + } |
| 122 | + if (type === ANIMATION_TYPE.OPEN) { |
| 123 | + this.openAnimationPlayer = animationBuilder.create(targetElement.nativeElement); |
| 124 | + } else if (type === ANIMATION_TYPE.CLOSE) { |
| 125 | + this.closeAnimationPlayer = animationBuilder.create(targetElement.nativeElement); |
| 126 | + } |
| 127 | + const target = this.getPlayer(type); |
| 128 | + target.init(); |
| 129 | + this.getPlayer(type).setPosition(1 - oppositePosition); |
| 130 | + if (type === ANIMATION_TYPE.OPEN) { |
| 131 | + this.onOpenedCallback = callback; |
| 132 | + } else if (type === ANIMATION_TYPE.CLOSE) { |
| 133 | + this.onClosedCallback = callback; |
| 134 | + } |
| 135 | + const targetCallback = type === ANIMATION_TYPE.OPEN ? this.onOpenedCallback : this.onClosedCallback; |
| 136 | + const targetEmitter = type === ANIMATION_TYPE.OPEN ? this.openAnimationDone : this.closeAnimationDone; |
| 137 | + target.onDone(() => { |
| 138 | + targetCallback(); |
| 139 | + targetEmitter.emit(); |
| 140 | + this.cleanUpPlayer(type); |
| 141 | + }); |
| 142 | + return target; |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + private cleanUpPlayer(target: ANIMATION_TYPE) { |
| 147 | + switch (target) { |
| 148 | + case ANIMATION_TYPE.CLOSE: |
| 149 | + if (this.closeAnimationPlayer != null) { |
| 150 | + this.closeAnimationPlayer.reset(); |
| 151 | + this.closeAnimationPlayer.destroy(); |
| 152 | + this.closeAnimationPlayer = null; |
| 153 | + } |
| 154 | + this.onClosedCallback = this._defaultClosedCallback; |
| 155 | + break; |
| 156 | + case ANIMATION_TYPE.OPEN: |
| 157 | + if (this.openAnimationPlayer != null) { |
| 158 | + this.openAnimationPlayer.reset(); |
| 159 | + this.openAnimationPlayer.destroy(); |
| 160 | + this.openAnimationPlayer = null; |
| 161 | + } |
| 162 | + this.onOpenedCallback = this._defaultOpenedCallback; |
| 163 | + break; |
| 164 | + default: |
| 165 | + break; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private getPlayer(type: ANIMATION_TYPE): AnimationPlayer { |
| 170 | + switch (type) { |
| 171 | + case ANIMATION_TYPE.OPEN: |
| 172 | + return this.openAnimationPlayer; |
| 173 | + case ANIMATION_TYPE.CLOSE: |
| 174 | + return this.closeAnimationPlayer; |
| 175 | + default: |
| 176 | + return null; |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments