/
xmas-tree.component.ts
75 lines (65 loc) · 2.71 KB
/
xmas-tree.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
import { Component, OnInit, ViewChildren } from '@angular/core';
import { trigger, state, style, animate, transition } from '@angular/animations';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import { Subject } from 'rxjs/Subject';
import { Subscription } from 'rxjs/Subscription';
import { generate } from 'rxjs/observable/generate';
import { QueryList } from '@angular/core/src/linker/query_list';
import { ElementRef } from '@angular/core/src/linker/element_ref';
@Component({
selector: 'app-xmas-tree',
templateUrl: './xmas-tree.component.html',
styleUrls: ['./xmas-tree.component.css'],
animations: [
// trigger, wird im template mittels "@TRIGGER-NAME" verknüpft
trigger('xmas', [
// trigger-Wert, direkt z.B. @xmas='true' oder als "value"-property siehe: generateAnmationConfig()
state('false', style({ opacity: '0', visibility: 'hidden' })),
// variablen mittels "{{VARNAME}}" müssen in params-Property des Trigger-Objektes abgelegt sein, ! default Werte sind Pflicht .
state('true', style({ opacity: '1', visibility: 'visible', backgroundColor: "{{rgb}}"}), {params: {rgb: "white"}}),
// transition festlegen
transition('false <=> true', animate('0.1s linear'))
])
]
})
export class XmasTreeComponent{
public plugged: boolean;
public animationConfig:any[];
private lightIntervall: Subscription;
@ViewChildren("bulp")
private bulps:QueryList<ElementRef>;
constructor() {
this.animationConfig=[];
}
public toggleXmasLight() {
this.plugged = !this.plugged;
if (this.plugged) {
this.lightIntervall=Observable.interval(500).subscribe( n => { // ändern des Trigger-Values, um "blinken" zu erzeugen
this.animationConfig = this.generateAnmationConfig ( this.animationConfig.length>0?this.animationConfig[0].value:false );
})
} else if (this.lightIntervall){
this.lightIntervall.unsubscribe();
this.animationConfig= this.generateAnmationConfig(true);
}
}
/**
* Erzeugt eine Config-Objekt für ein DIV.
* Enthält den state "value" mit "true" oder "false"
* Entählt params-Objekt mit zusätzliche Parametern welche in der Animation oben genutzt werden können.
* @param oldValue alter state-Wert (für Toggle-Funktion)
*/
private generateAnmationConfig(oldValue:boolean){
let args=Array(this.bulps.length).fill(0).map( _ => {return {value: !oldValue, params: {rgb: this.generateRGBColors()}}});
return args;
}
/**
* Zufalls-RGB-Generator
*/
private generateRGBColors():string{
let r= Math.floor(Math.random() * (255 - 0));
let g= Math.floor(Math.random() * (255 - 0));
let b= Math.floor(Math.random() * (255 - 0));
return `rgb(${r},${g},${b}`;
}
}