2019-11-12 16:55:37 +01:00
|
|
|
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
|
2020-06-25 10:10:42 +02:00
|
|
|
import {Location} from '@angular/common';
|
2019-11-01 19:30:11 +01:00
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
2020-06-12 12:46:11 +02:00
|
|
|
import {DomSanitizer, Title} from '@angular/platform-browser';
|
2019-11-01 19:30:11 +01:00
|
|
|
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
|
|
|
|
|
|
|
|
import {ErrorCodes} from '../openaireLibrary/utils/properties/errorCodes';
|
|
|
|
import {ErrorMessagesComponent} from '../openaireLibrary/utils/errorMessages.component';
|
2020-06-03 15:44:03 +02:00
|
|
|
import {Stakeholder, Topic} from "../openaireLibrary/monitor/entities/stakeholder";
|
|
|
|
import {StakeholderService} from "../openaireLibrary/monitor/services/stakeholder.service";
|
2019-11-11 14:28:36 +01:00
|
|
|
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
|
2019-11-12 16:55:37 +01:00
|
|
|
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
|
2019-11-13 15:19:04 +01:00
|
|
|
import {Subscriber} from "rxjs";
|
2019-11-26 20:11:24 +01:00
|
|
|
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
|
2019-12-23 14:54:37 +01:00
|
|
|
import {LayoutService} from "../openaireLibrary/dashboard/sharedComponents/sidebar/layout.service";
|
2019-12-19 12:17:32 +01:00
|
|
|
import {StakeholderUtils} from "../utils/indicator-utils";
|
2020-01-08 12:00:50 +01:00
|
|
|
import {IDeactivateComponent} from "../openaireLibrary/utils/can-exit.guard";
|
2019-11-01 19:30:11 +01:00
|
|
|
|
|
|
|
declare var UIkit;
|
|
|
|
|
|
|
|
@Component({
|
2019-12-19 12:17:32 +01:00
|
|
|
selector: 'stakeholder',
|
|
|
|
templateUrl: './stakeholder.component.html',
|
2019-11-01 19:30:11 +01:00
|
|
|
})
|
2020-01-08 12:00:50 +01:00
|
|
|
export class StakeholderComponent implements OnInit, OnDestroy, IDeactivateComponent {
|
2019-12-19 12:17:32 +01:00
|
|
|
public subscriptions: any[] = [];
|
|
|
|
public loading: boolean = true;
|
|
|
|
public errorCodes: ErrorCodes;
|
|
|
|
public stakeholder: Stakeholder;
|
|
|
|
public analysisOpen: boolean = true;
|
|
|
|
private errorMessages: ErrorMessagesComponent;
|
|
|
|
public form: FormGroup;
|
|
|
|
public stakeholderUtils: StakeholderUtils = new StakeholderUtils();
|
|
|
|
public index: number = -1;
|
|
|
|
properties: EnvProperties;
|
|
|
|
|
|
|
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
|
|
|
@ViewChild('editModal') editModal: AlertModal;
|
2020-06-23 10:33:13 +02:00
|
|
|
public topicChildrenActionOnDelete: string;
|
|
|
|
|
2019-12-19 12:17:32 +01:00
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
|
|
|
private title: Title,
|
|
|
|
private layoutService: LayoutService,
|
|
|
|
private fb: FormBuilder,
|
2020-06-25 10:10:42 +02:00
|
|
|
private stakeholderService: StakeholderService, private sanitizer: DomSanitizer, private _location: Location) {
|
2019-12-19 12:17:32 +01:00
|
|
|
this.errorCodes = new ErrorCodes();
|
|
|
|
this.errorMessages = new ErrorMessagesComponent();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnInit() {
|
|
|
|
this.route.data
|
|
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
|
|
|
this.subscriptions.push(this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
|
|
|
|
if (stakeholder) {
|
|
|
|
this.stakeholder = HelperFunctions.copy(stakeholder);
|
|
|
|
this.form = null;
|
2020-06-25 10:10:42 +02:00
|
|
|
this.title.setTitle(stakeholder.name);
|
2019-11-01 19:30:11 +01:00
|
|
|
}
|
2020-06-10 16:03:28 +02:00
|
|
|
}));
|
2019-12-19 12:17:32 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnDestroy() {
|
2020-01-08 12:00:50 +01:00
|
|
|
}
|
2020-06-25 10:10:42 +02:00
|
|
|
|
2020-01-08 12:00:50 +01:00
|
|
|
canExit():boolean {
|
2019-12-19 12:17:32 +01:00
|
|
|
this.subscriptions.forEach(value => {
|
|
|
|
if (value instanceof Subscriber) {
|
|
|
|
value.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
2020-01-08 12:00:50 +01:00
|
|
|
this.stakeholderService.setStakeholder(this.stakeholder);
|
|
|
|
return true;
|
2019-12-19 12:17:32 +01:00
|
|
|
}
|
|
|
|
|
2020-01-10 15:20:42 +01:00
|
|
|
hide(element: any) {
|
|
|
|
UIkit.dropdown(element).hide();
|
|
|
|
}
|
|
|
|
|
2019-12-19 12:17:32 +01:00
|
|
|
get open(): boolean {
|
|
|
|
return this.layoutService.open;
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildTopic(topic: Topic) {
|
|
|
|
let topics = this.stakeholder.topics.filter(element => element._id !== topic._id);
|
|
|
|
this.form = this.fb.group({
|
|
|
|
_id: this.fb.control(topic._id),
|
|
|
|
name: this.fb.control(topic.name, Validators.required),
|
|
|
|
description: this.fb.control(topic.description),
|
|
|
|
alias: this.fb.control(topic.alias, [
|
|
|
|
Validators.required,
|
|
|
|
this.stakeholderUtils.aliasValidator(topics)
|
|
|
|
]
|
|
|
|
),
|
|
|
|
isActive: this.fb.control(topic.isActive),
|
|
|
|
isPublic: this.fb.control(topic.isPublic),
|
2020-01-08 12:00:50 +01:00
|
|
|
defaultId: this.fb.control(topic.defaultId),
|
2020-06-11 12:12:30 +02:00
|
|
|
categories: this.fb.control(topic.categories),
|
2020-06-12 12:46:11 +02:00
|
|
|
icon: this.fb.control(topic.icon)
|
2019-12-19 12:17:32 +01:00
|
|
|
});
|
|
|
|
this.subscriptions.push(this.form.get('name').valueChanges.subscribe(value => {
|
|
|
|
let i = 1;
|
|
|
|
value = this.stakeholderUtils.generateAlias(value);
|
|
|
|
this.form.controls['alias'].setValue(value);
|
|
|
|
while (this.form.get('alias').invalid) {
|
|
|
|
this.form.controls['alias'].setValue(value + i);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
public editTopicOpen(index = -1) {
|
|
|
|
this.index = index;
|
|
|
|
if (index === -1) {
|
2020-01-08 12:00:50 +01:00
|
|
|
this.buildTopic(new Topic(null, null, null, true, true));
|
2019-12-19 12:17:32 +01:00
|
|
|
} else {
|
|
|
|
this.buildTopic(this.stakeholder.topics[index]);
|
2019-12-06 14:52:26 +01:00
|
|
|
}
|
2019-12-19 12:17:32 +01:00
|
|
|
this.editModal.cancelButtonText = 'Cancel';
|
|
|
|
this.editModal.okButtonLeft = false;
|
|
|
|
this.editModal.alertMessage = false;
|
|
|
|
if (index === -1) {
|
|
|
|
this.editModal.alertTitle = 'Create a new topic';
|
|
|
|
this.editModal.okButtonText = 'Create';
|
|
|
|
} else {
|
|
|
|
this.editModal.alertTitle = 'Edit topic\'s information ';
|
|
|
|
this.editModal.okButtonText = 'Save';
|
2019-11-12 16:55:37 +01:00
|
|
|
}
|
2019-12-19 12:17:32 +01:00
|
|
|
this.editModal.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
public saveTopic(index = -1) {
|
|
|
|
if (!this.form.invalid) {
|
|
|
|
if (index === -1) {
|
|
|
|
this.save('Topic has been successfully created');
|
|
|
|
} else {
|
|
|
|
this.save('Topic has been successfully saved', index);
|
|
|
|
}
|
2019-11-01 19:30:11 +01:00
|
|
|
}
|
2019-12-19 12:17:32 +01:00
|
|
|
}
|
|
|
|
|
2020-06-23 10:33:13 +02:00
|
|
|
public deleteTopicOpen(index: number, childrenAction: string = null) {
|
|
|
|
this.topicChildrenActionOnDelete = null;
|
|
|
|
if(childrenAction == "delete") {
|
|
|
|
this.topicChildrenActionOnDelete = childrenAction;
|
|
|
|
} else if(childrenAction == "disconnect") {
|
|
|
|
this.topicChildrenActionOnDelete = childrenAction;
|
|
|
|
}
|
|
|
|
|
2019-12-19 12:17:32 +01:00
|
|
|
this.index = index;
|
|
|
|
this.deleteModal.alertTitle = 'Delete topic';
|
|
|
|
this.deleteModal.cancelButtonText = 'No';
|
|
|
|
this.deleteModal.okButtonText = 'Yes';
|
|
|
|
this.deleteModal.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
private save(message: string, index: number = -1) {
|
|
|
|
let path = [this.stakeholder._id];
|
|
|
|
this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, this.form.value, path).subscribe(topic => {
|
|
|
|
if (index === -1) {
|
|
|
|
this.stakeholder.topics.push(topic);
|
|
|
|
} else {
|
|
|
|
this.stakeholder.topics[index] = topic;
|
|
|
|
}
|
|
|
|
UIkit.notification(message, {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
}, error => {
|
|
|
|
UIkit.notification(error.error.message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteTopic() {
|
|
|
|
let path = [
|
|
|
|
this.stakeholder._id,
|
|
|
|
this.stakeholder.topics[this.index]._id
|
|
|
|
];
|
2020-06-23 10:33:13 +02:00
|
|
|
this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path, this.topicChildrenActionOnDelete).subscribe(() => {
|
2019-12-19 12:17:32 +01:00
|
|
|
this.stakeholder.topics.splice(this.index, 1);
|
2020-05-13 10:46:22 +02:00
|
|
|
this.index = -1;
|
2019-12-19 12:17:32 +01:00
|
|
|
UIkit.notification('Topic has been successfully deleted', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
}, error => {
|
|
|
|
UIkit.notification(error.error.message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-12-20 12:13:34 +01:00
|
|
|
|
|
|
|
toggleTopicStatus(topic: Topic) {
|
|
|
|
let path = [
|
|
|
|
this.stakeholder._id,
|
|
|
|
topic._id
|
|
|
|
];
|
|
|
|
this.stakeholderService.toggleStatus(this.properties.monitorServiceAPIURL, path).subscribe(isActive => {
|
|
|
|
topic.isActive = isActive;
|
2020-05-13 17:10:44 +02:00
|
|
|
UIkit.notification('Topic has been successfully changed to ' + (isActive ? 'active' : 'inactive'), {
|
2019-12-20 12:13:34 +01:00
|
|
|
status: 'success',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
}, error => {
|
|
|
|
UIkit.notification(error.error.message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleTopicAccess(topic: Topic) {
|
|
|
|
let path = [
|
|
|
|
this.stakeholder._id,
|
|
|
|
topic._id
|
|
|
|
];
|
|
|
|
this.stakeholderService.toggleAccess(this.properties.monitorServiceAPIURL, path).subscribe(isPublic => {
|
|
|
|
topic.isPublic = isPublic;
|
|
|
|
UIkit.notification('Topic has been successfully changed to ' + (isPublic?'public':'private'), {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
}, error => {
|
|
|
|
UIkit.notification(error.error.message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-06-12 12:46:11 +02:00
|
|
|
satinizeHTML(html){
|
|
|
|
return this.sanitizer.bypassSecurityTrustHtml(html);
|
|
|
|
}
|
2019-11-01 19:30:11 +01:00
|
|
|
}
|