2019-11-12 16:55:37 +01:00
|
|
|
import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
|
2019-11-04 12:50:25 +01:00
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {Title} from '@angular/platform-browser';
|
|
|
|
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
|
2019-11-05 15:59:06 +01:00
|
|
|
import {Category, Stakeholder, SubCategory, Topic} from "../utils/entities/stakeholder";
|
2019-11-04 12:50:25 +01:00
|
|
|
import {SideBarService} from "../library/sharedComponents/sidebar/sideBar.service";
|
|
|
|
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-04 12:50:25 +01:00
|
|
|
|
|
|
|
declare var UIkit;
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'topic',
|
|
|
|
templateUrl: './topic.component.html',
|
|
|
|
})
|
|
|
|
export class TopicComponent implements OnInit, OnDestroy {
|
2019-11-08 16:47:47 +01:00
|
|
|
public properties: EnvProperties;
|
2019-11-04 12:50:25 +01:00
|
|
|
public loading: boolean = true;
|
|
|
|
public stakeholder: Stakeholder;
|
2019-11-13 11:26:50 +01:00
|
|
|
/**
|
|
|
|
* Current topic
|
|
|
|
**/
|
2019-11-05 15:59:06 +01:00
|
|
|
public topicIndex: number = -1;
|
|
|
|
public topic: Topic = null;
|
2019-11-13 11:26:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* categoryIndex: Current category to be edited, selectedCategoryIndex: selected on menu(opened)
|
|
|
|
*/
|
2019-11-05 15:59:06 +01:00
|
|
|
public categoryIndex: number = -1;
|
2019-11-08 16:47:47 +01:00
|
|
|
public selectedCategoryIndex: number = -1;
|
2019-11-05 15:59:06 +01:00
|
|
|
public copyCategory: Category = null;
|
2019-11-13 11:26:50 +01:00
|
|
|
/**
|
|
|
|
* Current Subcategory to be edited
|
|
|
|
*/
|
2019-11-05 15:59:06 +01:00
|
|
|
public subCategoryIndex: number = -1;
|
|
|
|
public copySubCategory: SubCategory = null;
|
2019-11-13 11:26:50 +01:00
|
|
|
/**
|
|
|
|
* Current drop element and index of topic, category or subcategory to be deleted.
|
|
|
|
*/
|
2019-11-12 16:55:37 +01:00
|
|
|
public element: any;
|
|
|
|
public index: number;
|
2019-11-13 11:26:50 +01:00
|
|
|
/**
|
|
|
|
* Check form validity
|
|
|
|
*/
|
|
|
|
public valid = true;
|
|
|
|
public toggle: boolean = false;
|
2019-11-12 16:55:37 +01:00
|
|
|
|
|
|
|
@ViewChild('deleteTopicModal') deleteTopicModal: AlertModal;
|
|
|
|
@ViewChild('deleteCategoryModal') deleteCategoryModal: AlertModal;
|
|
|
|
@ViewChild('deleteSubcategoryModal') deleteSubcategoryModal: AlertModal;
|
|
|
|
|
2019-11-04 12:50:25 +01:00
|
|
|
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) {
|
2019-11-11 14:28:36 +01:00
|
|
|
this.stakeholder = HelperFunctions.copy(stakeholder);
|
2019-11-05 15:59:06 +01:00
|
|
|
this.topicIndex = this.stakeholder.topics.findIndex(topic => topic.alias === params['topic']);
|
|
|
|
if(this.topicIndex === -1) {
|
2019-11-04 12:50:25 +01:00
|
|
|
this.navigateToError();
|
2019-11-05 15:59:06 +01:00
|
|
|
} else {
|
|
|
|
this.title.setTitle(stakeholder.index_name);
|
2019-11-08 16:47:47 +01:00
|
|
|
this.categoryIndex = 0;
|
|
|
|
this.selectedCategoryIndex = 0;
|
|
|
|
this.subCategoryIndex = 0;
|
|
|
|
this.toggle = true;
|
2019-11-04 12:50:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnDestroy() {
|
|
|
|
}
|
|
|
|
|
2019-11-05 15:59:06 +01:00
|
|
|
public hide(element) {
|
|
|
|
UIkit.drop(element).hide();
|
2019-11-04 12:50:25 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 15:59:06 +01:00
|
|
|
public show(element) {
|
|
|
|
UIkit.drop(element).show();
|
2019-11-04 12:50:25 +01:00
|
|
|
}
|
|
|
|
|
2019-11-13 11:26:50 +01:00
|
|
|
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() {
|
|
|
|
let path: string[] = [
|
|
|
|
this.stakeholder.alias,
|
|
|
|
this.stakeholder.topics[this.topicIndex].alias
|
|
|
|
];
|
|
|
|
this.delete('Topic has been successfully be deleted', path, true);
|
|
|
|
}
|
|
|
|
|
2019-11-05 15:59:06 +01:00
|
|
|
public toggleCategory(index: number) {
|
2019-11-08 16:47:47 +01:00
|
|
|
if(this.selectedCategoryIndex !== index) {
|
|
|
|
this.selectedCategoryIndex = index;
|
2019-11-05 15:59:06 +01:00
|
|
|
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 {
|
2019-11-11 14:28:36 +01:00
|
|
|
this.copyCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex].categories[index]);
|
2019-11-05 15:59:06 +01:00
|
|
|
this.show(element);
|
|
|
|
this.valid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public saveCategory(element, index = -1) {
|
|
|
|
if(this.copyCategory.name && this.copyCategory.name !== '') {
|
2019-11-12 16:55:37 +01:00
|
|
|
if(!this.copyCategory.alias) {
|
|
|
|
this.copyCategory.alias = this.copyCategory.name.toLowerCase();
|
|
|
|
}
|
2019-11-05 15:59:06 +01:00
|
|
|
if(index === -1) {
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories.push(this.copyCategory);
|
2019-11-12 16:55:37 +01:00
|
|
|
this.save('Category has been successfully created', element);
|
2019-11-05 15:59:06 +01:00
|
|
|
} else {
|
2019-11-11 14:28:36 +01:00
|
|
|
this.stakeholder.topics[this.topicIndex].categories[index] = HelperFunctions.copy(this.copyCategory);
|
2019-11-12 16:55:37 +01:00
|
|
|
this.save('Category has been successfully saved', element);
|
2019-11-05 15:59:06 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 16:55:37 +01:00
|
|
|
public deleteCategoryOpen(element, index) {
|
|
|
|
this.deleteOpen('category', this.deleteCategoryModal, element, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
public deleteCategory() {
|
2019-11-13 11:26:50 +01:00
|
|
|
let path: string[] = [
|
|
|
|
this.stakeholder.alias,
|
|
|
|
this.stakeholder.topics[this.topicIndex].alias,
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.index].alias
|
|
|
|
];
|
|
|
|
this.delete('Category has been successfully be deleted', path);
|
2019-11-12 16:55:37 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 15:59:06 +01:00
|
|
|
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 {
|
2019-11-11 14:28:36 +01:00
|
|
|
this.copySubCategory = HelperFunctions.copy(this.stakeholder.topics[this.topicIndex].
|
2019-11-05 15:59:06 +01:00
|
|
|
categories[this.categoryIndex].subCategories[index]);
|
|
|
|
this.show(element);
|
|
|
|
this.valid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public saveSubCategory(element, index = -1) {
|
|
|
|
if(this.copySubCategory.name && this.copySubCategory.name !== '') {
|
2019-11-12 16:55:37 +01:00
|
|
|
if(!this.copySubCategory.alias) {
|
|
|
|
this.copySubCategory.alias = this.copySubCategory.name.toLowerCase();
|
|
|
|
}
|
2019-11-05 15:59:06 +01:00
|
|
|
if(index === -1) {
|
2019-11-08 16:47:47 +01:00
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].
|
2019-11-05 15:59:06 +01:00
|
|
|
subCategories.push(this.copySubCategory);
|
2019-11-12 16:55:37 +01:00
|
|
|
this.save('Subcategory has been successfully created', element);
|
2019-11-05 15:59:06 +01:00
|
|
|
} else {
|
2019-11-08 16:47:47 +01:00
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].
|
2019-11-11 14:28:36 +01:00
|
|
|
subCategories[index] = HelperFunctions.copy(this.copySubCategory);
|
2019-11-12 16:55:37 +01:00
|
|
|
this.save('Subcategory has been successfully saved', element);
|
2019-11-05 15:59:06 +01:00
|
|
|
}
|
|
|
|
this.hide(element);
|
|
|
|
} else {
|
|
|
|
this.valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 16:55:37 +01:00
|
|
|
public deleteSubcategoryOpen(element, index) {
|
|
|
|
this.deleteOpen('subcategory', this.deleteSubcategoryModal, element, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
public deleteSubcategory() {
|
2019-11-13 11:26:50 +01:00
|
|
|
let path: string[] = [
|
|
|
|
this.stakeholder.alias,
|
|
|
|
this.stakeholder.topics[this.topicIndex].alias,
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].alias,
|
|
|
|
this.stakeholder.topics[this.topicIndex].categories[this.selectedCategoryIndex].subCategories[this.index].alias
|
|
|
|
];
|
|
|
|
this.delete('Subcategory has been successfully be deleted', path);
|
2019-11-05 15:59:06 +01:00
|
|
|
}
|
|
|
|
|
2019-11-04 12:50:25 +01:00
|
|
|
private navigateToError() {
|
|
|
|
this.router.navigate(['/error'], {queryParams: {'page': this.router.url}});
|
|
|
|
}
|
2019-11-05 15:59:06 +01:00
|
|
|
|
2019-11-12 16:55:37 +01:00
|
|
|
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);
|
|
|
|
});
|
2019-11-08 16:47:47 +01:00
|
|
|
}
|
|
|
|
|
2019-11-13 11:26:50 +01:00
|
|
|
private delete(message: string, path: string[], redirect = false) {
|
|
|
|
this.stakeholderService.deleteElement(this.properties.monitorServiceAPIURL, path).subscribe(stakeholder => {
|
|
|
|
this.stakeholderService.setStakeholder(stakeholder);
|
|
|
|
UIkit.notification(message, {
|
|
|
|
status: 'success',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
if(redirect) {
|
|
|
|
this.back();
|
|
|
|
}
|
|
|
|
this.hide(this.element);
|
|
|
|
}, error => {
|
|
|
|
UIkit.notification(error.error.message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 3000,
|
|
|
|
pos: 'top-left'
|
|
|
|
});
|
|
|
|
this.hide(this.element);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-05 15:59:06 +01:00
|
|
|
back() {
|
|
|
|
this.router.navigate(['../'], {
|
|
|
|
relativeTo: this.route
|
|
|
|
});
|
|
|
|
}
|
2019-11-08 16:47:47 +01:00
|
|
|
|
|
|
|
chooseSubcategory(categoryIndex: number, subcategoryIndex: number) {
|
|
|
|
this.categoryIndex = categoryIndex;
|
|
|
|
this.subCategoryIndex = subcategoryIndex;
|
|
|
|
}
|
2019-11-04 12:50:25 +01:00
|
|
|
}
|