Project

General

Profile

1
import { Component, EventEmitter, OnInit, Output } from '@angular/core';
2
import { FormArray, FormBuilder, FormGroup } from '@angular/forms';
3

    
4
@Component ({
5
  selector: 'compatibility-validate-step3-cris',
6
  templateUrl: 'compatibility-validate-step3-cris.component.html'
7
})
8

    
9
export class CompatibilityValidateStep3CrisComponent implements OnInit {
10

    
11
  entitiesList: string[] = [];
12
  selectedAllEntities: boolean;
13

    
14
  group: FormGroup;
15

    
16
  @Output() emitObject: EventEmitter<any> = new EventEmitter();
17

    
18
  constructor(private fb: FormBuilder) {}
19

    
20
  ngOnInit () {
21
    this.selectedAllEntities = true;
22
    this.getEntitiesList();
23

    
24
    if (this.entitiesList && this.entitiesList.length) {
25
      this.group = this.fb.group({
26
        entities: this.fb.array([this.initEntities()]),
27
        refIntegrity: ''
28
      });
29
      this.setEntitiesControls();
30
    }
31
  }
32

    
33
  getEntitiesList() {
34
    this.entitiesList = ['Funding','Organisation','Person','Project','Publication','Product','Service'];
35
  }
36

    
37
  initEntities() {
38
    return this.fb.group({
39
      entity : [true]
40
    });
41
  }
42

    
43
  /* inputs the entities List into the FormArray */
44
  setEntitiesControls() {
45
    let entities = <FormArray>this.group.controls['entities'];
46
    for ( let i = 0; i<this.entitiesList.length-1; i++ ) {
47
      entities.push(this.initEntities());
48
    }
49
  }
50

    
51
  /* selects/deselects all entities */
52
  toggleSelectAllContentRules() {
53
    let entities = <FormArray>this.group.controls['entities'];
54
    if (this.selectedAllEntities) {
55
      this.selectedAllEntities = false;
56
      entities.controls.map(x => x.get('entity').setValue(false));
57
    } else {
58
      this.selectedAllEntities = true;
59
      entities.controls.map(x => x.get('entity').setValue(true));
60
    }
61
  }
62

    
63
  onToggleCheckEntity(event:any) {
64
    if ( !event.target.checked ) {
65
      this.selectedAllEntities = false;
66
    }
67
  }
68

    
69
  saveChanges() {
70
    let emitted: any[] = [];
71
    let chosenEntities: string[] = [];
72
    console.log(`saving the selected entities`);
73
    let entities = <FormArray>this.group.controls['entities'];
74
    for (let i=0; i<this.entitiesList.length; i++ ) {
75
      if (entities.at(i).get('entity').value) {
76
        chosenEntities.push(this.entitiesList[i]);
77
      }
78
    }
79
    emitted.push(chosenEntities);
80
    if (this.group.get('refIntegrity').value){
81
      emitted.push(true);
82
    } else {
83
      emitted.push(false);
84
    }
85
    this.emitObject.emit(emitted);
86
  }
87
}
(6-6/8)