import {Component, OnDestroy, OnInit} 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"; 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; 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.sideBarService.setHasSidebar(true); 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 !== '') { this.copyCategory.alias = this.copyCategory.name.toLowerCase(); if(index === -1) { this.stakeholder.topics[this.topicIndex].categories.push(this.copyCategory); } else { this.stakeholder.topics[this.topicIndex].categories[index] = HelperFunctions.copy(this.copyCategory); } this.hide(element); } else { this.valid = false; } } 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 !== '') { this.copySubCategory.alias = this.copySubCategory.name.toLowerCase(); if(index === -1) { this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]. subCategories.push(this.copySubCategory); } else { this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex]. subCategories[index] = HelperFunctions.copy(this.copySubCategory); } this.hide(element); } else { this.valid = false; } } 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.alias = this.topic.name.toLowerCase(); this.stakeholder.topics[this.topicIndex] = HelperFunctions.copy(this.topic); this.hide(element); } else { this.valid = false; } } public deleteTopic(element) { /*this.stakeholder.topics.splice(this.topicIndex, 1); this.hide(element); this.back();*/ } private navigateToError() { this.router.navigate(['/error'], {queryParams: {'page': this.router.url}}); } public getDefaultSubcategoryIndex(categoryIndex: number) { return this.stakeholder.topics[this.topicIndex].categories[categoryIndex]. subCategories.findIndex(subcategory => subcategory.alias === null); } back() { this.router.navigate(['../'], { relativeTo: this.route }); } chooseSubcategory(categoryIndex: number, subcategoryIndex: number) { /*let topic: Topic = this.stakeholder.topics[this.topicIndex]; let category: Category = topic.categories[this.categoryIndex]; let subCatetegory: SubCategory = category.subCategories[index];*/ this.categoryIndex = categoryIndex; this.subCategoryIndex = subcategoryIndex; } }