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
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
47
      } else {
48
        this.myForm.value.isCollapsed = !this.myForm.value.isCollapsed;
49

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

    
56
          let allEntities = this.allEntities;
57

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

    
67

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

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

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

    
98
    public getKeys( map) {
99
      return Array.from(map.keys());
100
    }
101

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

    
119
    public reset() {
120
        this.myForm.patchValue({
121
            route : '',
122
            name : '',
123
            type: '',
124
            isEnabled: '',
125
            portalType: '',
126
            top: true,
127
            bottom: true,
128
            left: true,
129
            right: true,
130
            _id : '',
131
            isCollapsed: [true]
132
        });
133

    
134
        this.setEntities([]);
135
    }
136

    
137
    public get entities(): FormArray {
138
      return this.myForm.get('entities') as FormArray;
139
    };
140

    
141
    setEntities(entities: Entity[]) {
142
      const entityFGs = entities.map(entity => this._fb.group(entity));
143
      const entityFormArray = this._fb.array(entityFGs);
144
      this.myForm.setControl('entities', entityFormArray);
145
    }
146

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

    
159
        this.allEntities.set(entity, status);
160

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

    
171
    handleError(message: string, error) {
172
        this.errorMessage = message;
173
        console.log('Server responded: ' + error);
174
        this.showLoading = false;
175
    }
176

    
177
}
(2-2/6)