2019-11-12 16:55:37 +01:00
|
|
|
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
|
2019-11-01 19:30:11 +01:00
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {Title} from '@angular/platform-browser';
|
|
|
|
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
|
|
|
|
|
|
|
|
import {ErrorCodes} from '../openaireLibrary/utils/properties/errorCodes';
|
|
|
|
import {ErrorMessagesComponent} from '../openaireLibrary/utils/errorMessages.component';
|
|
|
|
import {Stakeholder, Topic} from "../utils/entities/stakeholder";
|
|
|
|
import {StakeholderService} from "../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-11-27 11:09:14 +01:00
|
|
|
import {LayoutService} from "../library/sharedComponents/sidebar/layout.service";
|
2019-11-27 13:14:14 +01:00
|
|
|
import {IndicatorUtils} from "../utils/indicator-utils";
|
2019-11-01 19:30:11 +01:00
|
|
|
|
|
|
|
declare var UIkit;
|
|
|
|
|
|
|
|
@Component({
|
2019-11-27 13:14:14 +01:00
|
|
|
selector: 'stakeholder',
|
|
|
|
templateUrl: './stakeholder.component.html',
|
2019-11-01 19:30:11 +01:00
|
|
|
})
|
2019-11-27 13:14:14 +01:00
|
|
|
export class StakeholderComponent implements OnInit, OnDestroy {
|
2019-11-13 15:19:04 +01:00
|
|
|
public subscriptions: any[] = [];
|
2019-11-01 19:30:11 +01:00
|
|
|
public loading: boolean = true;
|
|
|
|
public errorCodes: ErrorCodes;
|
|
|
|
public stakeholder: Stakeholder;
|
|
|
|
public analysisOpen: boolean = true;
|
|
|
|
private errorMessages: ErrorMessagesComponent;
|
2019-11-26 20:11:24 +01:00
|
|
|
public topicFb: FormGroup;
|
2019-11-27 13:14:14 +01:00
|
|
|
public indicatorUtils: IndicatorUtils = new IndicatorUtils();
|
2019-11-12 16:55:37 +01:00
|
|
|
public element: any;
|
|
|
|
public index: number;
|
2019-11-01 19:30:11 +01:00
|
|
|
properties: EnvProperties;
|
|
|
|
|
2019-11-12 16:55:37 +01:00
|
|
|
@ViewChild('deleteTopicModal') deleteTopicModal: AlertModal;
|
|
|
|
|
2019-11-01 19:30:11 +01:00
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
|
|
|
private title: Title,
|
2019-11-27 11:09:14 +01:00
|
|
|
private layoutService: LayoutService,
|
2019-11-26 20:11:24 +01:00
|
|
|
private fb: FormBuilder,
|
2019-11-01 19:30:11 +01:00
|
|
|
private stakeholderService: StakeholderService) {
|
|
|
|
this.errorCodes = new ErrorCodes();
|
|
|
|
this.errorMessages = new ErrorMessagesComponent();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnInit() {
|
|
|
|
this.route.data
|
|
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
2019-11-13 15:19:04 +01:00
|
|
|
this.subscriptions.push(this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
|
2019-11-01 19:30:11 +01:00
|
|
|
if (stakeholder) {
|
2019-11-11 14:28:36 +01:00
|
|
|
this.stakeholder = HelperFunctions.copy(stakeholder);
|
2019-11-26 20:11:24 +01:00
|
|
|
this.topicFb = null;
|
2019-11-04 12:50:25 +01:00
|
|
|
this.title.setTitle(stakeholder.index_name);
|
2019-11-01 19:30:11 +01:00
|
|
|
}
|
2019-11-13 15:19:04 +01:00
|
|
|
}));
|
2019-11-01 19:30:11 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnDestroy() {
|
2019-11-24 17:30:04 +01:00
|
|
|
this.subscriptions.forEach(value => {
|
|
|
|
if (value instanceof Subscriber) {
|
2019-11-13 15:19:04 +01:00
|
|
|
value.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
2019-11-01 19:30:11 +01:00
|
|
|
}
|
|
|
|
|
2019-11-12 16:55:37 +01:00
|
|
|
public show(element) {
|
|
|
|
UIkit.drop(element).show();
|
2019-11-01 19:30:11 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 16:47:47 +01:00
|
|
|
public hide(element) {
|
2019-11-01 19:30:11 +01:00
|
|
|
UIkit.drop(element).hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
get open(): boolean {
|
2019-11-27 11:09:14 +01:00
|
|
|
return this.layoutService.open;
|
2019-11-01 19:30:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public toggleOpen(event = null) {
|
|
|
|
if (!event) {
|
2019-11-27 11:09:14 +01:00
|
|
|
this.layoutService.setOpen(!this.open);
|
2019-11-01 19:30:11 +01:00
|
|
|
} else if (event && event['value'] === true) {
|
2019-11-27 11:09:14 +01:00
|
|
|
this.layoutService.setOpen(false);
|
2019-11-01 19:30:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 20:11:24 +01:00
|
|
|
private buildTopic(topic: Topic) {
|
|
|
|
this.topicFb = 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),
|
|
|
|
isActive: this.fb.control(topic.isActive),
|
|
|
|
isPublic: this.fb.control(topic.isPublic),
|
|
|
|
isDefault: this.fb.control(topic.isDefault),
|
|
|
|
categories: this.fb.control(topic.categories)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-12 16:55:37 +01:00
|
|
|
public saveTopicOpen(element, index = -1) {
|
2019-11-24 17:30:04 +01:00
|
|
|
if (element.className.indexOf('uk-open') !== -1) {
|
2019-11-13 15:19:04 +01:00
|
|
|
this.hide(element);
|
2019-11-12 16:55:37 +01:00
|
|
|
} else {
|
2019-11-13 15:19:04 +01:00
|
|
|
if (index === -1) {
|
2019-11-26 20:11:24 +01:00
|
|
|
this.buildTopic(new Topic(null, null, null, true, true, false));
|
2019-11-13 15:19:04 +01:00
|
|
|
} else {
|
2019-11-26 20:11:24 +01:00
|
|
|
this.buildTopic(this.stakeholder.topics[index]);
|
2019-11-13 15:19:04 +01:00
|
|
|
}
|
|
|
|
this.show(element);
|
2019-11-12 16:55:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public saveTopic(element, index = -1) {
|
2019-11-26 20:11:24 +01:00
|
|
|
if (!this.topicFb.invalid) {
|
|
|
|
if (!this.topicFb.value.alias) {
|
|
|
|
this.topicFb.value.alias = this.topicFb.value.name.toLowerCase().trim();
|
2019-11-12 16:55:37 +01:00
|
|
|
}
|
2019-11-24 17:30:04 +01:00
|
|
|
if (index === -1) {
|
2019-11-12 16:55:37 +01:00
|
|
|
this.save('Topic has been successfully created', element);
|
|
|
|
} else {
|
2019-11-24 17:30:04 +01:00
|
|
|
this.save('Topic has been successfully saved', element, index);
|
2019-11-12 16:55:37 +01:00
|
|
|
}
|
2019-11-01 19:30:11 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-12 16:55:37 +01:00
|
|
|
|
2019-11-19 15:25:19 +01:00
|
|
|
public deleteTopicOpen(name: string, element, index: number) {
|
2019-11-12 16:55:37 +01:00
|
|
|
this.element = element;
|
|
|
|
this.index = index;
|
2019-11-19 15:25:19 +01:00
|
|
|
this.deleteTopicModal.alertTitle = 'Delete ' + name;
|
2019-11-12 16:55:37 +01:00
|
|
|
this.deleteTopicModal.cancelButtonText = 'No';
|
|
|
|
this.deleteTopicModal.okButtonText = 'Yes';
|
|
|
|
this.deleteTopicModal.message = 'This topic will permanently be deleted. Are you sure you want to proceed?';
|
|
|
|
this.deleteTopicModal.open();
|
|
|
|
}
|
|
|
|
|
2019-11-24 17:30:04 +01:00
|
|
|
private save(message: string, element, index: number = -1) {
|
|
|
|
let path = [this.stakeholder._id];
|
2019-11-26 20:11:24 +01:00
|
|
|
this.stakeholderService.saveElement(this.properties.monitorServiceAPIURL, this.topicFb.value, path).subscribe(topic => {
|
2019-11-24 17:30:04 +01:00
|
|
|
if (index === -1) {
|
|
|
|
this.stakeholder.topics.push(topic);
|
|
|
|
} else {
|
|
|
|
this.stakeholder.topics[index] = topic;
|
|
|
|
}
|
|
|
|
this.stakeholderService.setStakeholder(this.stakeholder);
|
2019-11-12 16:55:37 +01:00
|
|
|
UIkit.notification(message, {
|
|
|
|
status: 'success',
|
2019-11-26 20:11:24 +01:00
|
|
|
timeout: 3000000,
|
2019-11-12 16:55:37 +01:00
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
this.hide(element);
|
|
|
|
}, error => {
|
|
|
|
UIkit.notification(error.error.message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
this.hide(element);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteTopic() {
|
2019-11-13 11:26:50 +01:00
|
|
|
let path = [
|
2019-11-24 17:30:04 +01:00
|
|
|
this.stakeholder._id,
|
|
|
|
this.stakeholder.topics[this.index]._id
|
2019-11-13 11:26:50 +01:00
|
|
|
];
|
2019-11-24 17:30:04 +01:00
|
|
|
this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe(() => {
|
|
|
|
this.stakeholder.topics.splice(this.index, 1);
|
|
|
|
this.stakeholderService.setStakeholder(this.stakeholder);
|
2019-11-13 11:26:50 +01:00
|
|
|
UIkit.notification('Topic has been successfully deleted', {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
this.hide(this.element);
|
|
|
|
}, error => {
|
|
|
|
UIkit.notification(error.error.message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
this.hide(this.element);
|
|
|
|
});
|
2019-11-12 16:55:37 +01:00
|
|
|
}
|
2019-11-01 19:30:11 +01:00
|
|
|
}
|