Project

General

Profile

1
/**
2
 * Created by stefania on 7/13/17.
3
 */
4
import {Component, OnInit, Input} from '@angular/core';
5
import { ActivatedRoute, Router } from "@angular/router";
6
import {FormGroup, FormArray, FormBuilder, Validators} from "@angular/forms";
7
import {Entity} from '../../domain/entity';
8
import { HelpContentService } from "../../services/help-content.service";
9
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
10

    
11
import {Session} from '../../openaireLibrary/login/utils/helper.class';
12
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
13

    
14
@Component({
15
    selector: 'page-form',
16
    templateUrl: './page-form.component.html',
17
})
18

    
19
export class PageFormComponent implements OnInit{
20

    
21
    @Input('group')
22
    myForm: FormGroup;
23
    @Input('type')
24
    public type: string;
25

    
26
    //public errorMessage: string;
27

    
28
    public allEntities: Map<Entity, boolean> = new Map<Entity, boolean>();
29
    private gotEntities: boolean = false;
30
    public properties:EnvProperties = null;
31

    
32
    public showLoading: boolean = false;
33
    public errorMessage: string = '';
34

    
35
    constructor(private route: ActivatedRoute, private _router: Router, public _fb: FormBuilder, private _helpContentService: HelpContentService){}
36

    
37
    ngOnInit(): void {
38
      this.route.data
39
        .subscribe((data: { envSpecific: EnvProperties }) => {
40
           this.properties = data.envSpecific;
41
      });
42
    }
43

    
44
    public toggle() {
45
      if(!Session.isLoggedIn()){
46
        console.info(this._router.url);
47
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
48
      } else {
49
        this.myForm.value.isCollapsed = !this.myForm.value.isCollapsed;
50

    
51
        if(!this.myForm.value.isCollapsed) {
52
          let includedEntities: Set<String> = new Set<String>();
53
          for(let entityName of this.myForm.value.entities) {
54
            includedEntities.add(entityName._id);
55
          }
56

    
57
          let allEntities = this.allEntities;
58

    
59
          let self = this;
60
          allEntities.forEach(function (status, entity, map) {
61
            if(includedEntities.has(entity._id)) {
62
              self.allEntities.set(entity, true);
63
            } else {
64
              self.allEntities.set(entity, false);
65
            }
66
          });
67

    
68

    
69
          if(!this.gotEntities) {
70
            this.gotEntities = true;
71
            this.getEntities(includedEntities);
72
          }
73
        }
74
      }
75
    }
76

    
77
    public getEntities(includedEntities: Set<String>) {
78
      if(!Session.isLoggedIn()){
79
        console.info(this._router.url);
80
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
81
      } else {
82
        this.showLoading = true;
83
        this.errorMessage = "";
84

    
85
        this._helpContentService.getEntities(this.properties.adminToolsAPIURL).subscribe(
86
          entities => {
87
              for(let entity of entities) {
88
                if(includedEntities.has(entity._id)) {
89
                  this.allEntities.set(entity, true);
90
                } else {
91
                  this.allEntities.set(entity, false);
92
                }
93
              }
94
              this.showLoading = false;
95
        },
96
        error => this.handleError('System error retrieving community entities', error));
97
      }
98
    }
99

    
100
    public getKeys( map) {
101
      return Array.from(map.keys());
102
    }
103

    
104
    public get form() {
105
        return this._fb.group({
106
            route : ['', Validators.required],
107
            name : ['', Validators.required],
108
            isEnabled: '',
109
            openaire: true,
110
            connect: true,
111
            type: ['', Validators.required],
112
            entities: this._fb.array([]),
113
            _id : '',
114
            isCollapsed: [true]
115
        });
116
    }
117

    
118
    public reset() {
119
        this.myForm.patchValue({
120
            route : '',
121
            name : '',
122
            type: '',
123
            isEnabled: '',
124
            openaire: true,
125
            connect: true,
126
            _id : '',
127
            isCollapsed: [true]
128
        });
129

    
130
        this.setEntities([]);
131
    }
132

    
133
    public get entities(): FormArray {
134
      return this.myForm.get('entities') as FormArray;
135
    };
136

    
137
    setEntities(entities: Entity[]) {
138
      const entityFGs = entities.map(entity => this._fb.group(entity));
139
      const entityFormArray = this._fb.array(entityFGs);
140
      this.myForm.setControl('entities', entityFormArray);
141
    }
142

    
143
    public toggleEntity(status : boolean, id : string, entity: Entity) {
144
      if(!Session.isLoggedIn()){
145
        console.info(this._router.url);
146
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
147
      } else {
148
        let index: number = -1;
149
        for(let i=0; i<this.myForm.get('entities').value.length; i++) {
150
          if(this.myForm.get('entities').value[i]._id == entity._id) {
151
            index = i;
152
            break;
153
          }
154
        }
155

    
156
        this.allEntities.set(entity, status);
157

    
158
        if(status && index<0) {
159
          this.myForm.value.entities.push(entity);
160
        } else  if(!status){
161
          if(index >= 0) {
162
            this.myForm.value.entities.splice(index, 1);
163
          }
164
        }
165
      }
166
    }
167

    
168
    handleError(message: string, error) {
169
        this.errorMessage = message;
170
        console.log('Server responded: ' + error);
171
        this.showLoading = false;
172
    }
173
}
(2-2/4)