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 {Category, Stakeholder, SubCategory, 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: 'topic', templateUrl: './topic.component.html', }) export class TopicComponent implements OnInit, OnDestroy { public properties: EnvProperties; public loading: boolean = true; public stakeholder: Stakeholder; public topicIndex: number = -1; public topic: Topic = null; public categoryIndex: number = -1; public selectedCategoryIndex: number = -1; public copyCategory: Category = null; public subCategoryIndex: number = -1; public copySubCategory: SubCategory = null; public valid = true; public edit: boolean = false; public toggle: boolean = false; public element: any; public index: number; @ViewChild('deleteTopicModal') deleteTopicModal: AlertModal; @ViewChild('deleteCategoryModal') deleteCategoryModal: AlertModal; @ViewChild('deleteSubcategoryModal') deleteSubcategoryModal: AlertModal; constructor( private route: ActivatedRoute, private router: Router, private title: Title, private sideBarService: SideBarService, private stakeholderService: StakeholderService) { } public ngOnInit() { this.route.data .subscribe((data: { envSpecific: EnvProperties }) => { this.properties = data.envSpecific; this.route.params.subscribe( params => { this.stakeholderService.getStakeholderAsObservable().subscribe(stakeholder => { if (stakeholder) { this.stakeholder = HelperFunctions.copy(stakeholder); this.topicIndex = this.stakeholder.topics.findIndex(topic => topic.alias === params['topic']); if(this.topicIndex === -1) { this.navigateToError(); } else { this.title.setTitle(stakeholder.index_name); this.categoryIndex = 0; this.selectedCategoryIndex = 0; this.subCategoryIndex = 0; this.toggle = true; } } }); }); }); } public ngOnDestroy() { } public hide(element) { this.edit = false; UIkit.drop(element).hide(); } public show(element) { this.edit = true; UIkit.drop(element).show(); } public toggleCategory(index: number) { if(this.selectedCategoryIndex !== index) { this.selectedCategoryIndex = index; this.toggle = true; } else { this.toggle = !this.toggle; } } public editCategoryOpen(index:number = -1, element = null) { if(index === -1) { this.copyCategory = new Category(null, null, null, true, true); } else { if(element.className.indexOf('uk-open') !== -1) { this.hide(element); } else { this.copyCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex].categories[index]); this.show(element); this.valid = true; } } } public saveCategory(element, index = -1) { if(this.copyCategory.name && this.copyCategory.name !== '') { if(!this.copyCategory.alias) { this.copyCategory.alias = this.copyCategory.name.toLowerCase(); } if(index === -1) { this.stakeholder.topics[this.topicIndex].categories.push(this.copyCategory); this.save('Category has been successfully created', element); } else { this.stakeholder.topics[this.topicIndex].categories[index] = HelperFunctions.copy(this.copyCategory); this.save('Category has been successfully saved', element); } } else { this.valid = false; } } public deleteCategoryOpen(element, index) { this.deleteOpen('category', this.deleteCategoryModal, element, index); } public deleteCategory() { this.stakeholder.topics[this.topicIndex].categories.splice(this.index, 1); this.stakeholderService.saveStakeholder(this.properties.monitorServiceAPIURL, this.stakeholder).subscribe(stakeholder => { this.stakeholderService.setStakeholder(stakeholder); UIkit.notification('Category 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); }); } public editSubCategoryOpen(index:number = -1, element = null) { if(index === -1) { this.copySubCategory = new SubCategory(null, null, null, true, true); } else { if(element.className.indexOf('uk-open') !== -1) { this.hide(element); } else { this.copySubCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex]. categories[this.categoryIndex].subCategories[index]); this.show(element); this.valid = true; } } } public saveSubCategory(element, index = -1) { if(this.copySubCategory.name && this.copySubCategory.name !== '') { if(!this.copySubCategory.alias) { this.copySubCategory.alias = this.copySubCategory.name.toLowerCase(); } if(index === -1) { this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]. subCategories.push(this.copySubCategory); this.save('Subcategory has been successfully created', element); } else { this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]. subCategories[index] = HelperFunctions.copy(this.copySubCategory); this.save('Subcategory has been successfully saved', element); } this.hide(element); } else { this.valid = false; } } public deleteSubcategoryOpen(element, index) { this.deleteOpen('subcategory', this.deleteSubcategoryModal, element, index); } public deleteSubcategory() { this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories. splice(this.index, 1); this.stakeholderService.saveStakeholder(this.properties.monitorServiceAPIURL, this.stakeholder).subscribe(stakeholder => { this.stakeholderService.setStakeholder(stakeholder); UIkit.notification('Subcategory 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); }); } public editTopicOpen(element) { if(element.className.indexOf('uk-open') !== -1) { this.hide(element); } else { this.topic = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex]); this.valid = true; this.show(element); } } public saveTopic(element) { if(this.topic.name && this.topic.name !== '' && this.topic.description && this.topic.description !== '') { if(!this.topic.alias) { this.topic.alias = this.topic.name.toLowerCase().trim(); } this.stakeholder.topics[this.topicIndex] = HelperFunctions.copy(this.topic); this.save('Topic has been successfully saved', element, true); } else { this.valid = false; } } public deleteTopicOpen(element) { this.deleteOpen('topic', this.deleteTopicModal, element, this.topicIndex); } public deleteTopic() { this.stakeholder.topics.splice(this.index, 1); this.stakeholderService.saveStakeholder(this.properties.monitorServiceAPIURL, this.stakeholder).subscribe(stakeholder => { this.stakeholderService.setStakeholder(stakeholder); UIkit.notification('Topic has been successfully deleted', { status: 'success', timeout: 3000, pos: 'top-left' }); this.hide(this.element); this.back(); }, error => { UIkit.notification(error.error.message, { status: 'danger', timeout: 3000, pos: 'top-left' }); this.hide(this.element); }); } private navigateToError() { this.router.navigate(['/error'], {queryParams: {'page': this.router.url}}); } private deleteOpen(type: string, modal: AlertModal, element, index) { this.element = element; this.index = index; modal.alertHeader = true; modal.alertMessage = true; modal.cancelButton = true; modal.cancelButtonText = 'No'; modal.okButtonText = 'Yes'; modal.message = 'This ' + type + ' will permanently be deleted. Are you sure you want to proceed?'; modal.open(); } private save(message: string, element, redirect = false) { this.stakeholderService.saveStakeholder(this.properties.monitorServiceAPIURL, this.stakeholder).subscribe(stakeholder => { this.stakeholderService.setStakeholder(stakeholder); UIkit.notification(message, { status: 'success', timeout: 3000, pos: 'top-left' }); if(redirect) { this.router.navigate(['../' + this.topic.alias], { relativeTo: this.route }); } this.hide(element); }, error => { UIkit.notification(error.error.message, { status: 'danger', timeout: 3000, pos: 'top-left' }); this.hide(element); }); } back() { this.router.navigate(['../'], { relativeTo: this.route }); } chooseSubcategory(categoryIndex: number, subcategoryIndex: number) { this.categoryIndex = categoryIndex; this.subCategoryIndex = subcategoryIndex; } }