171 lines
5.2 KiB
TypeScript
171 lines
5.2 KiB
TypeScript
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
|
|
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 {SideBarService} from "../library/sharedComponents/sidebar/sideBar.service";
|
|
import {StakeholderService} from "../services/stakeholder.service";
|
|
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
|
|
import {AlertModal} from "../openaireLibrary/utils/modal/alert";
|
|
|
|
declare var UIkit;
|
|
|
|
@Component({
|
|
selector: 'home',
|
|
templateUrl: './home.component.html',
|
|
})
|
|
export class HomeComponent implements OnInit, OnDestroy {
|
|
public status: number;
|
|
public loading: boolean = true;
|
|
public errorCodes: ErrorCodes;
|
|
public stakeholder: Stakeholder;
|
|
public analysisOpen: boolean = true;
|
|
private errorMessages: ErrorMessagesComponent;
|
|
public copyTopic: Topic;
|
|
public valid = true;
|
|
public element: any;
|
|
public index: number;
|
|
properties: EnvProperties;
|
|
|
|
@ViewChild('deleteTopicModal') deleteTopicModal: AlertModal;
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private title: Title,
|
|
private sideBarService: SideBarService,
|
|
private stakeholderService: StakeholderService) {
|
|
this.errorCodes = new ErrorCodes();
|
|
this.errorMessages = new ErrorMessagesComponent();
|
|
this.status = this.errorCodes.LOADING;
|
|
}
|
|
|
|
public ngOnInit() {
|
|
this.route.data
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
this.properties = data.envSpecific;
|
|
this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => {
|
|
if (stakeholder) {
|
|
this.stakeholder = HelperFunctions.copy(stakeholder);
|
|
this.copyTopic = null;
|
|
this.title.setTitle(stakeholder.index_name);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
public ngOnDestroy() {
|
|
}
|
|
|
|
public show(element) {
|
|
UIkit.drop(element).show();
|
|
}
|
|
|
|
public hide(element) {
|
|
UIkit.drop(element).hide();
|
|
}
|
|
|
|
get open(): boolean {
|
|
return this.sideBarService.open;
|
|
}
|
|
|
|
public toggleOpen(event = null) {
|
|
if (!event) {
|
|
this.sideBarService.setOpen(!this.open);
|
|
} else if (event && event['value'] === true) {
|
|
this.sideBarService.setOpen(false);
|
|
}
|
|
}
|
|
|
|
public saveTopicOpen(element, index = -1) {
|
|
if(index === -1) {
|
|
this.copyTopic = new Topic(null, null, null,true, true);
|
|
} else {
|
|
this.copyTopic = HelperFunctions.copy(this.stakeholder.topics[index]);
|
|
}
|
|
this.show(element);
|
|
this.valid = true;
|
|
}
|
|
|
|
public saveTopic(element, index = -1) {
|
|
if(this.copyTopic.name && this.copyTopic.name !== ''
|
|
&& this.copyTopic.description && this.copyTopic.description !== '') {
|
|
if(!this.copyTopic.alias) {
|
|
this.copyTopic.alias = this.copyTopic.name.toLowerCase().trim();
|
|
}
|
|
if(index === -1) {
|
|
this.stakeholder.topics.push(this.copyTopic);
|
|
this.save('Topic has been successfully created', element);
|
|
} else {
|
|
this.stakeholder.topics[index] = HelperFunctions.copy(this.copyTopic);
|
|
this.save('Topic has been successfully saved', element);
|
|
}
|
|
} else {
|
|
this.valid = false;
|
|
}
|
|
}
|
|
|
|
public deleteTopicOpen(element, index: number) {
|
|
this.element = element;
|
|
this.index = index;
|
|
this.deleteTopicModal.alertHeader = true;
|
|
this.deleteTopicModal.alertMessage = true;
|
|
this.deleteTopicModal.cancelButton = true;
|
|
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();
|
|
}
|
|
|
|
public navigateTo(alias: string) {
|
|
this.router.navigate(['./' + alias], {
|
|
relativeTo: this.route
|
|
});
|
|
}
|
|
|
|
private save(message: string, element) {
|
|
this.stakeholderService.saveStakeholder(this.properties.monitorServiceAPIURL, this.stakeholder).subscribe(stakeholder => {
|
|
this.stakeholderService.setStakeholder(stakeholder);
|
|
UIkit.notification(message, {
|
|
status: 'success',
|
|
timeout: 3000,
|
|
pos: 'top-left'
|
|
});
|
|
this.hide(element);
|
|
}, error => {
|
|
UIkit.notification(error.error.message, {
|
|
status: 'danger',
|
|
timeout: 3000,
|
|
pos: 'top-left'
|
|
});
|
|
this.hide(element);
|
|
});
|
|
}
|
|
|
|
deleteTopic() {
|
|
let path = [
|
|
this.stakeholder.alias,
|
|
this.stakeholder.topics[this.index].alias
|
|
];
|
|
this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe(stakeholder => {
|
|
this.stakeholderService.setStakeholder(stakeholder);
|
|
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);
|
|
});
|
|
}
|
|
}
|