1
|
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
|
2
|
import { Rule, RuleSet } from '../../../domain/typeScriptClasses';
|
3
|
import { FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
|
4
|
|
5
|
@Component ({
|
6
|
selector: 'compatibility-validate-step2',
|
7
|
templateUrl: 'compatibility-validate-step2.component.html'
|
8
|
})
|
9
|
|
10
|
export class CompatibilityValidateStep2Component implements OnInit {
|
11
|
|
12
|
showRules: boolean;
|
13
|
currentContentRules: Rule[] = [];
|
14
|
currentUsageRules: Rule[] = [];
|
15
|
selectedAllContentRules: boolean;
|
16
|
selectedAllUsageRules: boolean;
|
17
|
|
18
|
group: FormGroup;
|
19
|
|
20
|
@Input() ruleSets: RuleSet[];
|
21
|
@Output() emitObject: EventEmitter<any> = new EventEmitter();
|
22
|
|
23
|
constructor(private fb: FormBuilder) {}
|
24
|
|
25
|
ngOnInit() {
|
26
|
this.showRules = false;
|
27
|
this.selectedAllContentRules = true;
|
28
|
this.selectedAllUsageRules = true;
|
29
|
|
30
|
if ( this.ruleSets.length ) {
|
31
|
this.group = this.fb.group({
|
32
|
ruleSet: ['', Validators.required],
|
33
|
contentRules: this.fb.array([this.initRules()]),
|
34
|
usageRules: this.fb.array([this.initRules()])
|
35
|
});
|
36
|
this.group.get('ruleSet').setValue(this.ruleSets[0].id);
|
37
|
this.getCurrentRuleSets();
|
38
|
this.getRulesLists();
|
39
|
}
|
40
|
}
|
41
|
|
42
|
/* creates a formGroup of the Rules FormArray */
|
43
|
initRules() {
|
44
|
return this.fb.group({
|
45
|
rule : [true]
|
46
|
});
|
47
|
}
|
48
|
|
49
|
/* returns Rules for selected RuleSet */
|
50
|
getCurrentRuleSets () {
|
51
|
console.log(this.group.get('ruleSet').value);
|
52
|
let id = this.group.get('ruleSet').value;
|
53
|
let index: number;
|
54
|
this.currentContentRules = [];
|
55
|
this.currentUsageRules = [];
|
56
|
for (let i=0; i< this.ruleSets.length; i++ ) {
|
57
|
if (this.ruleSets[i].id == id) {
|
58
|
index = i;
|
59
|
break;
|
60
|
}
|
61
|
}
|
62
|
this.currentContentRules = this.ruleSets[index].contentRules;
|
63
|
this.currentUsageRules = this.ruleSets[index].usageRules;
|
64
|
console.log(`contentRules length: ${this.currentContentRules.length}`);
|
65
|
console.log(`usageRules length: ${this.currentUsageRules.length}`);
|
66
|
}
|
67
|
|
68
|
/* inputs the Rule Lists into the FormArrays */
|
69
|
getRulesLists() {
|
70
|
let contentRules = <FormArray>this.group.controls['contentRules'];
|
71
|
for ( let i = 0; i<this.currentContentRules.length-1; i++ ) {
|
72
|
contentRules.push(this.initRules());
|
73
|
}
|
74
|
let usageRules = <FormArray>this.group.controls['usageRules'];
|
75
|
for ( let i = 0; i<this.currentUsageRules.length-1; i++ ) {
|
76
|
usageRules.push(this.initRules());
|
77
|
}
|
78
|
}
|
79
|
|
80
|
/* refreshes the Rule Lists according to the selected RuleSet and reinitializes the form controls */
|
81
|
refreshLists(){
|
82
|
this.removeRulesControls();
|
83
|
this.getCurrentRuleSets();
|
84
|
this.getRulesLists();
|
85
|
}
|
86
|
|
87
|
/* removes form controls in order to reinitialize contentRules formArrays */
|
88
|
removeRulesControls() {
|
89
|
let contentRules = <FormArray>this.group.controls['contentRules'];
|
90
|
// contentRules.reset();
|
91
|
contentRules.controls = [];
|
92
|
contentRules.push(this.initRules());
|
93
|
|
94
|
let usageRules = <FormArray>this.group.controls['usageRules'];
|
95
|
// usageRules.reset();
|
96
|
usageRules.controls = [];
|
97
|
usageRules.push(this.initRules());
|
98
|
}
|
99
|
|
100
|
/* selects/deselects all content rules */
|
101
|
toggleSelectAllContentRules() {
|
102
|
let contentRules = <FormArray>this.group.controls['contentRules'];
|
103
|
if (this.selectedAllContentRules) {
|
104
|
this.selectedAllContentRules = false;
|
105
|
contentRules.controls.map(x => x.get('rule').setValue(false));
|
106
|
} else {
|
107
|
this.selectedAllContentRules = true;
|
108
|
contentRules.controls.map(x => x.get('rule').setValue(true));
|
109
|
}
|
110
|
}
|
111
|
|
112
|
/* selects/deselects all usage rules */
|
113
|
toggleSelectAllUsageRules() {
|
114
|
let usageRules = <FormArray>this.group.controls['usageRules'];
|
115
|
if (this.selectedAllUsageRules) {
|
116
|
this.selectedAllUsageRules = false;
|
117
|
usageRules.controls.map(x => x.get('rule').setValue(false))
|
118
|
} else {
|
119
|
this.selectedAllUsageRules = true;
|
120
|
usageRules.controls.map(x => x.get('rule').setValue(true))
|
121
|
}
|
122
|
|
123
|
}
|
124
|
|
125
|
onToggleCheckRule(event:any, contentOrUsage: string) {
|
126
|
if ( !event.target.checked ) {
|
127
|
if (contentOrUsage == 'content') {
|
128
|
this.selectedAllContentRules = false;
|
129
|
} else if (contentOrUsage == 'usage') {
|
130
|
this.selectedAllUsageRules = false;
|
131
|
}
|
132
|
}
|
133
|
}
|
134
|
|
135
|
toggleShowRules() {
|
136
|
this.showRules = !this.showRules;
|
137
|
return this.showRules;
|
138
|
}
|
139
|
|
140
|
saveChanges() {
|
141
|
let emitted: any[] = [];
|
142
|
let index: number;
|
143
|
|
144
|
for (let i=0; i< this.ruleSets.length; i++ ) {
|
145
|
if (this.ruleSets[i].id == this.group.get('ruleSet').value) {
|
146
|
index = i;
|
147
|
break;
|
148
|
}
|
149
|
}
|
150
|
|
151
|
emitted.push(this.ruleSets[index].guidelinesAcronym);
|
152
|
|
153
|
console.log(`saving the selected rules`);
|
154
|
let contentRules = <FormArray>this.group.controls['contentRules'];
|
155
|
let selectedContent: number[] = [];
|
156
|
for (let i=0; i< this.ruleSets[index].contentRules.length; i++ ) {
|
157
|
if (contentRules.at(i).get('rule').value) {
|
158
|
selectedContent.push(this.ruleSets[index].contentRules[i].id);
|
159
|
}
|
160
|
}
|
161
|
emitted.push(selectedContent);
|
162
|
let selectedUsage: number[] = [];
|
163
|
let usageRules = <FormArray>this.group.controls['usageRules'];
|
164
|
for (let i=0; i< this.ruleSets[index].usageRules.length; i++ ) {
|
165
|
if (usageRules.at(i).get('rule').value) {
|
166
|
selectedUsage.push(this.ruleSets[index].usageRules[i].id);
|
167
|
}
|
168
|
}
|
169
|
emitted.push(selectedUsage);
|
170
|
this.emitObject.emit(emitted);
|
171
|
}
|
172
|
|
173
|
}
|