2017-09-22 12:26:08 +02:00
|
|
|
/**
|
|
|
|
* Created by stefania on 7/13/17.
|
|
|
|
*/
|
|
|
|
import {Component, OnInit, Input} from '@angular/core';
|
2018-11-13 16:26:49 +01:00
|
|
|
import { ActivatedRoute, Router } from "@angular/router";
|
2017-12-13 12:15:19 +01:00
|
|
|
import {FormGroup, FormArray, FormBuilder, Validators} from "@angular/forms";
|
|
|
|
import {Entity} from '../../domain/entity';
|
|
|
|
import { HelpContentService } from "../../services/help-content.service";
|
2018-03-06 16:36:33 +01:00
|
|
|
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
|
2017-09-22 12:26:08 +02:00
|
|
|
|
2018-11-13 16:26:49 +01:00
|
|
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
|
|
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
|
|
|
|
2017-09-22 12:26:08 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'page-form',
|
|
|
|
templateUrl: './page-form.component.html',
|
|
|
|
})
|
|
|
|
|
|
|
|
export class PageFormComponent implements OnInit{
|
|
|
|
|
|
|
|
@Input('group')
|
|
|
|
myForm: FormGroup;
|
2017-12-20 15:26:30 +01:00
|
|
|
@Input('type')
|
|
|
|
public type: string;
|
2017-12-13 12:15:19 +01:00
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
//public errorMessage: string;
|
2017-12-13 12:15:19 +01:00
|
|
|
|
|
|
|
public allEntities: Map<Entity, boolean> = new Map<Entity, boolean>();
|
|
|
|
private gotEntities: boolean = false;
|
2018-03-06 16:36:33 +01:00
|
|
|
public properties:EnvProperties = null;
|
2017-12-13 12:15:19 +01:00
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
public showLoading: boolean = false;
|
|
|
|
public errorMessage: string = '';
|
|
|
|
|
2018-11-13 16:26:49 +01:00
|
|
|
constructor(private route: ActivatedRoute, private _router: Router, public _fb: FormBuilder, private _helpContentService: HelpContentService){}
|
2017-12-13 12:15:19 +01:00
|
|
|
|
2018-03-06 16:36:33 +01:00
|
|
|
ngOnInit(): void {
|
|
|
|
this.route.data
|
|
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
|
|
|
});
|
|
|
|
}
|
2017-12-13 12:15:19 +01:00
|
|
|
|
|
|
|
public toggle() {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
|
|
|
this.myForm.value.isCollapsed = !this.myForm.value.isCollapsed;
|
|
|
|
|
|
|
|
if(!this.myForm.value.isCollapsed) {
|
|
|
|
let includedEntities: Set<String> = new Set<String>();
|
|
|
|
for(let entityName of this.myForm.value.entities) {
|
|
|
|
includedEntities.add(entityName._id);
|
|
|
|
}
|
2017-12-13 12:15:19 +01:00
|
|
|
|
2018-11-13 16:26:49 +01:00
|
|
|
let allEntities = this.allEntities;
|
2017-12-13 12:15:19 +01:00
|
|
|
|
2018-11-13 16:26:49 +01:00
|
|
|
let self = this;
|
|
|
|
allEntities.forEach(function (status, entity, map) {
|
|
|
|
if(includedEntities.has(entity._id)) {
|
|
|
|
self.allEntities.set(entity, true);
|
|
|
|
} else {
|
|
|
|
self.allEntities.set(entity, false);
|
|
|
|
}
|
|
|
|
});
|
2017-12-13 12:15:19 +01:00
|
|
|
|
|
|
|
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!this.gotEntities) {
|
|
|
|
this.gotEntities = true;
|
|
|
|
this.getEntities(includedEntities);
|
|
|
|
}
|
2017-12-13 12:15:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-22 12:26:08 +02:00
|
|
|
|
2017-12-13 12:15:19 +01:00
|
|
|
public getEntities(includedEntities: Set<String>) {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.errorMessage = "";
|
|
|
|
|
|
|
|
this._helpContentService.getEntities(this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
entities => {
|
|
|
|
for(let entity of entities) {
|
|
|
|
if(includedEntities.has(entity._id)) {
|
|
|
|
this.allEntities.set(entity, true);
|
|
|
|
} else {
|
|
|
|
this.allEntities.set(entity, false);
|
|
|
|
}
|
2017-12-13 12:15:19 +01:00
|
|
|
}
|
2018-11-13 16:26:49 +01:00
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving community entities', error));
|
|
|
|
}
|
2017-12-13 12:15:19 +01:00
|
|
|
}
|
2017-09-22 12:26:08 +02:00
|
|
|
|
2017-12-13 12:15:19 +01:00
|
|
|
public getKeys( map) {
|
|
|
|
return Array.from(map.keys());
|
2017-09-22 12:26:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public get form() {
|
|
|
|
return this._fb.group({
|
|
|
|
route : ['', Validators.required],
|
|
|
|
name : ['', Validators.required],
|
2017-12-20 15:26:30 +01:00
|
|
|
isEnabled: '',
|
2018-10-30 16:31:16 +01:00
|
|
|
openaire: true,
|
2019-06-24 12:34:19 +02:00
|
|
|
connect: false,
|
|
|
|
communities: true,
|
2017-12-20 15:26:30 +01:00
|
|
|
type: ['', Validators.required],
|
2017-12-13 12:15:19 +01:00
|
|
|
entities: this._fb.array([]),
|
|
|
|
_id : '',
|
|
|
|
isCollapsed: [true]
|
2017-09-22 12:26:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public reset() {
|
|
|
|
this.myForm.patchValue({
|
|
|
|
route : '',
|
|
|
|
name : '',
|
2017-12-20 15:26:30 +01:00
|
|
|
type: '',
|
|
|
|
isEnabled: '',
|
2018-10-30 16:31:16 +01:00
|
|
|
openaire: true,
|
2019-06-24 12:34:19 +02:00
|
|
|
connect: false,
|
|
|
|
communities: true,
|
2017-12-13 12:15:19 +01:00
|
|
|
_id : '',
|
|
|
|
isCollapsed: [true]
|
2017-09-22 12:26:08 +02:00
|
|
|
});
|
2017-12-13 12:15:19 +01:00
|
|
|
|
|
|
|
this.setEntities([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public get entities(): FormArray {
|
|
|
|
return this.myForm.get('entities') as FormArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
setEntities(entities: Entity[]) {
|
|
|
|
const entityFGs = entities.map(entity => this._fb.group(entity));
|
|
|
|
const entityFormArray = this._fb.array(entityFGs);
|
|
|
|
this.myForm.setControl('entities', entityFormArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
public toggleEntity(status : boolean, id : string, entity: Entity) {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
|
|
|
let index: number = -1;
|
|
|
|
for(let i=0; i<this.myForm.get('entities').value.length; i++) {
|
|
|
|
if(this.myForm.get('entities').value[i]._id == entity._id) {
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
2017-12-13 12:15:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-13 16:26:49 +01:00
|
|
|
this.allEntities.set(entity, status);
|
2017-12-13 12:15:19 +01:00
|
|
|
|
2018-11-13 16:26:49 +01:00
|
|
|
if(status && index<0) {
|
|
|
|
this.myForm.value.entities.push(entity);
|
|
|
|
} else if(!status){
|
|
|
|
if(index >= 0) {
|
|
|
|
this.myForm.value.entities.splice(index, 1);
|
|
|
|
}
|
2017-12-13 12:15:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(message: string, error) {
|
2018-10-30 16:31:16 +01:00
|
|
|
this.errorMessage = message;
|
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
this.showLoading = false;
|
2017-12-13 12:15:19 +01:00
|
|
|
}
|
|
|
|
}
|