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"; import {Subscriber} from "rxjs"; declare var UIkit; @Component({ selector: 'home', templateUrl: './home.component.html', }) export class HomeComponent implements OnInit, OnDestroy { public subscriptions: any[] = []; 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(); } 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.copyTopic = null; this.title.setTitle(stakeholder.index_name); } })); }); } public ngOnDestroy() { this.subscriptions.forEach( value => { if(value instanceof Subscriber) { value.unsubscribe(); } }); } 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(element.className.indexOf('uk-open') !== -1) { this.hide(element); } else { 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 !== '') { 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(); } 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); }); } }