This repository has been archived by the owner on Jun 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation-builder.component.ts
124 lines (105 loc) · 3.62 KB
/
animation-builder.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import {
AfterViewInit,
Component,
ElementRef,
OnDestroy,
ViewChild
} from '@angular/core';
import { AnimationBuilder, AnimationPlayer } from '@angular/animations';
import {
slideAnimations,
slideBackAnimation,
slideForwardAnimation
} from '../../animations/slide.animation';
@Component({
selector: 'nx-workspace-animation-builder',
templateUrl: './animation-builder.component.html',
styleUrls: ['./animation-builder.component.scss']
})
export class AnimationBuilderComponent implements AfterViewInit, OnDestroy {
@ViewChild('photos') photosContainer: ElementRef;
@ViewChild('simpleBlock') simpleBlock: ElementRef;
private photos: HTMLImageElement[];
private players: Map<string, AnimationPlayer>;
private static getHtmlElement<TElement>(elementRef: ElementRef): TElement {
const el = elementRef.nativeElement as TElement;
if (!el) {
console.warn('the expected element is not here');
return;
}
return el;
}
private static getHtmlElements(collection: HTMLCollection): Element[] {
const children = Array.from(collection);
if (!children) {
console.warn('the expected element children are not here');
return;
}
if (!children.length) {
console.warn('the expected number of element children is not here');
return;
}
return children;
}
constructor(private animationBuilder: AnimationBuilder) {}
ngAfterViewInit(): void {
const div = AnimationBuilderComponent.getHtmlElement<HTMLDivElement>(
this.photosContainer
);
const children = AnimationBuilderComponent.getHtmlElements(
div.children
);
this.photos = children
.filter(el => el.localName === 'img')
.map(el => el as HTMLImageElement);
this.players = new Map();
}
ngOnDestroy(): void {
console.log('ngOnDestroy() called');
this.players.forEach(player => player.destroy());
}
slideBack(): void {
this.photos.forEach((img, i) => {
const player = this.getPlayer(slideBackAnimation.id, img, i);
player.play();
});
}
slideForward(): void {
this.photos.forEach((img, i) => {
const player = this.getPlayer(slideForwardAnimation.id, img, i);
player.play();
});
}
slideDivBack(): void {
const div = AnimationBuilderComponent.getHtmlElement<HTMLDivElement>(
this.simpleBlock
);
const player = this.getPlayer(slideBackAnimation.id, div);
player.play();
}
slideDivForward(): void {
const div = AnimationBuilderComponent.getHtmlElement<HTMLDivElement>(
this.simpleBlock
);
const player = this.getPlayer(slideForwardAnimation.id, div);
player.play();
}
private getPlayer(
animationId: string,
el: Element,
elIndex: number = 0
): AnimationPlayer {
const uniqueId = `${animationId}-${el.localName}${elIndex}`;
if (this.players.has(uniqueId)) {
this.players.get(uniqueId).destroy();
}
const animation = slideAnimations.get(animationId);
const factory = this.animationBuilder.build(animation);
const x = el.clientWidth / 2;
const player = factory.create(el, { params: { x: x } });
player.onDestroy(() => console.log(`player ${uniqueId} destroyed`));
player.onDone(() => console.log(`player ${uniqueId} done`));
this.players.set(uniqueId, player);
return player;
}
}